You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/02/17 09:51:44 UTC
[camel] branch camel-2.x updated: CAMEL-13210: Testing Spring Boot
with camel-test-spring should support @ExcludeRoutes annotation also.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.x by this push:
new ba3459c CAMEL-13210: Testing Spring Boot with camel-test-spring should support @ExcludeRoutes annotation also.
ba3459c is described below
commit ba3459c1bf502de49e89912c55255fbdefeaa39a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Feb 17 10:50:30 2019 +0100
CAMEL-13210: Testing Spring Boot with camel-test-spring should support @ExcludeRoutes annotation also.
---
.../apache/camel/spring/boot/RoutesCollector.java | 18 ++++++++++++
...est.java => FooExcludeRouteAnnotationTest.java} | 7 +++--
.../camel/spring/boot/routefilter/FooTest.java | 2 +-
.../camel/test/spring/CamelAnnotationsHandler.java | 32 ++++++++++++++++++++--
.../spring/CamelSpringBootExecutionListener.java | 4 +++
5 files changed, 57 insertions(+), 6 deletions(-)
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
index ecb0668..e0064f5 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
@@ -114,6 +114,24 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
}
}
}
+ // special support for testing with @ExcludeRoutes annotation with camel-test-spring
+ exclude = System.getProperty("CamelTestSpringExcludeRoutes");
+ // exclude take precedence over include
+ if (match && ObjectHelper.isNotEmpty(exclude)) {
+ // this property is a comma separated list of FQN class names, so we need to make
+ // name as path so we can use ant patch matcher
+ exclude = exclude.replace('.', '/');
+ // there may be multiple separated by comma
+ String[] parts = exclude.split(",");
+ for (String part : parts) {
+ // must negate when excluding, and hence !
+ match = !matcher.match(part, name);
+ LOG.trace("Java RoutesBuilder: {} exclude filter: {} -> {}", name, part, match);
+ if (!match) {
+ break;
+ }
+ }
+ }
if (match && ObjectHelper.isNotEmpty(include)) {
// there may be multiple separated by comma
String[] parts = include.split(",");
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooExcludeRouteAnnotationTest.java
similarity index 90%
copy from components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java
copy to components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooExcludeRouteAnnotationTest.java
index ccc6640..f9d158e 100644
--- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooExcludeRouteAnnotationTest.java
@@ -20,6 +20,7 @@ import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.test.spring.CamelSpringBootRunner;
+import org.apache.camel.test.spring.ExcludeRoutes;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,9 +29,9 @@ import org.springframework.boot.test.context.SpringBootTest;
@RunWith(CamelSpringBootRunner.class)
@SpringBootApplication()
-@SpringBootTest(classes = BarTest.class,
- properties = {"camel.springboot.java-routes-exclude-pattern=**/Bar*,**/Drink*"})
-public class FooTest {
+@SpringBootTest(classes = FooTest.class)
+@ExcludeRoutes({BarRoute.class, DrinkRoute.class})
+public class FooExcludeRouteAnnotationTest {
@Autowired
ProducerTemplate producerTemplate;
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java
index ccc6640..a77c583 100644
--- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/routefilter/FooTest.java
@@ -28,7 +28,7 @@ import org.springframework.boot.test.context.SpringBootTest;
@RunWith(CamelSpringBootRunner.class)
@SpringBootApplication()
-@SpringBootTest(classes = BarTest.class,
+@SpringBootTest(classes = FooTest.class,
properties = {"camel.springboot.java-routes-exclude-pattern=**/Bar*,**/Drink*"})
public class FooTest {
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
index ba74630..76cd37a 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
@@ -35,6 +35,7 @@ import org.apache.camel.spi.Debugger;
import org.apache.camel.spi.EventNotifier;
import org.apache.camel.spring.SpringCamelContext;
import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.util.CollectionStringBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
@@ -50,6 +51,33 @@ public final class CamelAnnotationsHandler {
}
/**
+ * Handles @ExcludeRoutes to make it easier to exclude other routes when testing with Spring Boot.
+ *
+ * @param context the initialized Spring context
+ * @param testClass the test class being executed
+ */
+ public static void handleExcludeRoutesForSpringBoot(Class<?> testClass) {
+ if (testClass.isAnnotationPresent(ExcludeRoutes.class)) {
+ Class[] routes = testClass.getAnnotation(ExcludeRoutes.class).value();
+ // need to setup this as a JVM system property
+ CollectionStringBuffer csb = new CollectionStringBuffer(",");
+ for (Class clazz : routes) {
+ csb.append(clazz.getName());
+ }
+ String key = "CamelTestSpringExcludeRoutes";
+ String value = csb.toString();
+
+ String exists = System.getProperty(key);
+ if (exists != null) {
+ LOGGER.warn("Cannot use @ExcludeRoutes as JVM property " + key + " has already been set.");
+ } else {
+ LOGGER.info("@ExcludeRoutes annotation found. Setting up JVM property {}={}", key, value);
+ System.setProperty(key, value);
+ }
+ }
+ }
+
+ /**
* Handles disabling of JMX on Camel contexts based on {@link DisableJmx}.
*
* @param context the initialized Spring context
@@ -115,7 +143,7 @@ public final class CamelAnnotationsHandler {
}
// turn off dumping one more time by removing the event listener (which would dump as well when Camel is stopping)
- // but this method was explict invoked to dump such as from afterTest callbacks from JUnit.
+ // but this method was explicit invoked to dump such as from afterTest callbacks from JUnit.
RouteCoverageEventNotifier eventNotifier = camelContext.hasService(RouteCoverageEventNotifier.class);
if (eventNotifier != null) {
camelContext.getManagementStrategy().removeEventNotifier(eventNotifier);
@@ -238,7 +266,7 @@ public final class CamelAnnotationsHandler {
public void execute(String contextName, SpringCamelContext camelContext)
throws Exception {
- // resovle the property place holders of the mockEndpoints
+ // resolve the property place holders of the mockEndpoints
String mockEndpointsValue = camelContext.resolvePropertyPlaceholders(mockEndpoints);
LOGGER.info("Enabling auto mocking and skipping of endpoints matching pattern [{}] on CamelContext with name [{}].", mockEndpointsValue, contextName);
camelContext.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(mockEndpointsValue, true));
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java
index ce0eb5c..3ab8df6 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java
@@ -34,6 +34,10 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe
LOG.info("@RunWith(CamelSpringBootRunner.class) preparing: {}", testContext.getTestClass());
Class<?> testClass = testContext.getTestClass();
+
+ // need to prepare this before we load spring application context
+ CamelAnnotationsHandler.handleExcludeRoutesForSpringBoot(testClass);
+
// we are customizing the Camel context with
// CamelAnnotationsHandler so we do not want to start it
// automatically, which would happen when SpringCamelContext