Skip to content

Commit

Permalink
CAMEL-18201 Enhance CamelTestSupport to turn off context stopping
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Jun 17, 2022
1 parent 3096762 commit bd510e5
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ protected void doSpringBootCheck() {
}
}

private void doSetUp() throws Exception {
protected void doSetUp() throws Exception {
LOG.debug("setUp test");
// jmx is enabled if we have configured to use it, or if dump route
// coverage is enabled (it requires JMX)
Expand Down Expand Up @@ -706,7 +706,7 @@ protected void stopCamelContext() throws Exception {
doStopCamelContext(context, camelContextService);
}

private static void doStopCamelContext(CamelContext context, Service camelContextService) {
protected void doStopCamelContext(CamelContext context, Service camelContextService) {
if (camelContextService != null) {
if (camelContextService == threadService.get()) {
threadService.remove();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.test.junit5;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.Service;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.Registry;
import org.apache.camel.support.DefaultRegistry;
import org.apache.camel.test.junit5.patterns.CreateCamelContextPerTestTrueTest;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CamelTestSupporOneContextForAllTest extends CamelTestSupport {

private static final Logger LOG = LoggerFactory.getLogger(CreateCamelContextPerTestTrueTest.class);

private static final CamelContext CUSTOM_CONTEXT;

static {
CUSTOM_CONTEXT = new MockContext();
}

@EndpointInject("mock:result")
protected MockEndpoint resultEndpoint;

@Produce("direct:start")
protected ProducerTemplate template;

@Override
protected CamelContext createCamelContext() throws Exception {
return CUSTOM_CONTEXT;
}

@Override
protected void doStopCamelContext(CamelContext context, Service camelContextService) {
//don't stop
}

@Override
protected void doSetUp() throws Exception {
if(context == null) {
super.doSetUp();
}
}

@Test
@Order(1)
public void initContextTest() throws Exception {
String expectedBody = "<matched/>";

resultEndpoint.expectedBodiesReceived(expectedBody);

template.sendBodyAndHeader(expectedBody, "foo", "bar");

resultEndpoint.assertIsSatisfied();
resultEndpoint.reset();

}

@Test
@Order(2)
public void stopNotEnabledTest() throws Exception {
String expectedBody = "<matched/>";

resultEndpoint.expectedBodiesReceived(expectedBody);

template.sendBodyAndHeader(expectedBody, "foo", "bar");

resultEndpoint.assertIsSatisfied();
resultEndpoint.reset();

}

@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
}
};
}

private static class MockContext extends DefaultCamelContext {

boolean initialized;

@Override
protected Registry createRegistry() {
if (initialized) {
throw new UnsupportedOperationException();
}
if (!initialized) {
initialized = true;
}
return new DefaultRegistry();
}

@Override
public void addRoutes(RoutesBuilder builder) throws Exception {
//if routes are already added, do not add them again
if (getRoutes().isEmpty()) {
super.addRoutes(builder);
}
}
}
}

0 comments on commit bd510e5

Please sign in to comment.