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 f66a873  CAMEL-12389: Log a WARN when extending 
CamelTestSupport/CamelSpringTestSupport when doing Spring Boot testing as they 
are not intended/supporting this.
f66a873 is described below

commit f66a87380a99f766806ed5b6d295cbfaa193fff8
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed Mar 21 16:31:41 2018 +0100

    CAMEL-12389: Log a WARN when extending 
CamelTestSupport/CamelSpringTestSupport when doing Spring Boot testing as they 
are not intended/supporting this.
---
 .../spring/boot/issues/StreamCachingTest.java      | 16 +-------
 .../camel/test/spring/CamelSpringTestSupport.java  |  3 +-
 .../apache/camel/test/junit4/CamelTestSupport.java | 44 +++++++++++++++++++---
 3 files changed, 41 insertions(+), 22 deletions(-)

diff --git 
a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/issues/StreamCachingTest.java
 
b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/issues/StreamCachingTest.java
index d0a5fea..2d73101 100644
--- 
a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/issues/StreamCachingTest.java
+++ 
b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/issues/StreamCachingTest.java
@@ -29,14 +29,11 @@ import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.spring.boot.SpringTypeConverter;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
-import 
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.annotation.Bean;
 import org.springframework.core.convert.ConversionService;
-import org.springframework.core.convert.converter.Converter;
 import org.springframework.core.convert.support.DefaultConversionService;
 
 public class StreamCachingTest extends CamelTestSupport {
+    // this is not a spring boot test as its standalone Camel testing by 
extending CamelTestSupport
 
     public static final String URI_END_OF_ROUTE = "mock:end_of_route";
 
@@ -76,21 +73,10 @@ public class StreamCachingTest extends CamelTestSupport {
     /**
      * Copied from org.apache.camel.spring.boot.TypeConversionConfiguration 
(they are package protected)
      **/
-    @Bean
     SpringTypeConverter springTypeConverter(CamelContext camelContext, 
ConversionService[] conversionServices) {
         SpringTypeConverter springTypeConverter = new 
SpringTypeConverter(asList(conversionServices));
         
camelContext.getTypeConverterRegistry().addFallbackTypeConverter(springTypeConverter,
 true);
         return springTypeConverter;
     }
 
-    @ConditionalOnMissingBean
-    @Bean
-    ConversionService defaultCamelConversionService(ApplicationContext 
applicationContext) {
-        DefaultConversionService service = new DefaultConversionService();
-        for (Converter converter : 
applicationContext.getBeansOfType(Converter.class).values()) {
-            service.addConverter(converter);
-        }
-        return service;
-    }
-
 }
\ No newline at end of file
diff --git 
a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
 
b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
index b855b40..d69ebf1 100644
--- 
a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
+++ 
b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestSupport.java
@@ -35,7 +35,8 @@ import 
org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
 
 /**
- * @version 
+ * Base test-class for classic Spring application such as standalone, web 
applications.
+ * Do <tt>not</tt> use this class for Spring Boot testing, instead use 
<code>@RunWith(CamelSpringBootRunner.class)</code>.
  */
 public abstract class CamelSpringTestSupport extends CamelTestSupport {
     protected static ThreadLocal<AbstractApplicationContext> threadAppContext 
= new ThreadLocal<AbstractApplicationContext>();
diff --git 
a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
 
b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
index 9157d55..85e9a92 100644
--- 
a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
+++ 
b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
@@ -21,6 +21,7 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.annotation.Annotation;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
@@ -87,8 +88,7 @@ import org.slf4j.LoggerFactory;
 /**
  * A useful base class which creates a {@link org.apache.camel.CamelContext} 
with some routes
  * along with a {@link org.apache.camel.ProducerTemplate} for use in the test 
case
- *
- * @version
+ * Do <tt>not</tt> use this class for Spring Boot testing, instead use 
<code>@RunWith(CamelSpringBootRunner.class)</code>.
  */
 public abstract class CamelTestSupport extends TestSupport {
 
@@ -256,6 +256,7 @@ public abstract class CamelTestSupport extends TestSupport {
             // test is per class, so only setup once (the first time)
             boolean first = INIT.get() == null;
             if (first) {
+                doSpringBootWarning();
                 doPreSetup();
                 doSetUp();
                 doPostSetup();
@@ -266,6 +267,7 @@ public abstract class CamelTestSupport extends TestSupport {
             }
         } else {
             // test is per test so always setup
+            doSpringBootWarning();
             doPreSetup();
             doSetUp();
             doPostSetup();
@@ -289,6 +291,18 @@ public abstract class CamelTestSupport extends TestSupport 
{
         // noop
     }
 
+    /**
+     * Detects if this is a Spring-Boot test and reports a warning as these 
base classes is not intended
+     * for testing Camel on Spring Boot.
+     */
+    protected void doSpringBootWarning() {
+        boolean springBoot = 
hasClassAnnotation("org.springframework.boot.test.context.SpringBootTest");
+        if (springBoot) {
+            log.warn("Spring Boot detected: The 
CamelTestSupport/CamelSpringTestSupport class is not intended for Camel testing 
with Spring Boot."
+                + " Prefer to not extend this class, but use 
@RunWith(CamelSpringBootRunner.class) instead.");
+        }
+    }
+
     private 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)
@@ -632,10 +646,28 @@ public abstract class CamelTestSupport extends 
TestSupport {
      * we would just use CDI to inject this
      */
     protected void applyCamelPostProcessor() throws Exception {
-        // use the default bean post processor from camel-core
-        DefaultCamelBeanPostProcessor processor = new 
DefaultCamelBeanPostProcessor(context);
-        processor.postProcessBeforeInitialization(this, getClass().getName());
-        processor.postProcessAfterInitialization(this, getClass().getName());
+        // use the default bean post processor from camel-core if the test 
class is not dependency injected already by Spring
+        boolean spring = 
hasClassAnnotation("org.springframework.boot.test.context.SpringBootTest", 
"org.springframework.context.annotation.ComponentScan");
+        if (!spring) {
+            DefaultCamelBeanPostProcessor processor = new 
DefaultCamelBeanPostProcessor(context);
+            processor.postProcessBeforeInitialization(this, 
getClass().getName());
+            processor.postProcessAfterInitialization(this, 
getClass().getName());
+        }
+    }
+
+    /**
+     * Does this test class have any of the following annotations on the 
class-level.
+     */
+    protected boolean hasClassAnnotation(String... names) {
+        for (String name : names) {
+            for (Annotation ann : getClass().getAnnotations()) {
+                String annName = ann.annotationType().getName();
+                if (annName.equals(name)) {
+                    return true;
+                }
+            }
+        }
+        return false;
     }
 
     protected void stopCamelContext() throws Exception {

-- 
To stop receiving notification emails like this one, please contact
davscl...@apache.org.

Reply via email to