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:50:58 UTC

[camel] branch master 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 master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new ef3195b  CAMEL-13210: Testing Spring Boot with camel-test-spring should support @ExcludeRoutes annotation also.
ef3195b is described below

commit ef3195b2b4722350311c0c79b87617e9dd0d2eee
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 0dc2f01..1226ffc 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
@@ -115,6 +115,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 c9074c3..21789ab 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
@@ -36,6 +36,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;
@@ -51,6 +52,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
@@ -116,7 +144,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);
@@ -239,7 +267,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