You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dk...@apache.org on 2011/09/20 00:43:37 UTC

svn commit: r1172870 - in /camel/branches/camel-2.8.x: ./ components/camel-test/src/test/java/org/apache/camel/test/patterns/ components/camel-test/src/test/resources/org/ components/camel-test/src/test/resources/org/apache/ components/camel-test/src/t...

Author: dkulp
Date: Mon Sep 19 22:43:36 2011
New Revision: 1172870

URL: http://svn.apache.org/viewvc?rev=1172870&view=rev
Log:
Merged revisions 1161373 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r1161373 | ningjiang | 2011-08-24 23:36:05 -0400 (Wed, 24 Aug 2011) | 1 line
  
  CAMEL-4374 Fixed the issue that The debugger doesn't work in the CamelSpringTestSupport with route defined in XML
........

Added:
    camel/branches/camel-2.8.x/components/camel-test/src/test/java/org/apache/camel/test/patterns/DebugSpringCamelContextTest.java
      - copied unchanged from r1161373, camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/DebugSpringCamelContextTest.java
    camel/branches/camel-2.8.x/components/camel-test/src/test/resources/org/
      - copied from r1161373, camel/trunk/components/camel-test/src/test/resources/org/
    camel/branches/camel-2.8.x/components/camel-test/src/test/resources/org/apache/
      - copied from r1161373, camel/trunk/components/camel-test/src/test/resources/org/apache/
    camel/branches/camel-2.8.x/components/camel-test/src/test/resources/org/apache/camel/
      - copied from r1161373, camel/trunk/components/camel-test/src/test/resources/org/apache/camel/
    camel/branches/camel-2.8.x/components/camel-test/src/test/resources/org/apache/camel/test/
      - copied from r1161373, camel/trunk/components/camel-test/src/test/resources/org/apache/camel/test/
    camel/branches/camel-2.8.x/components/camel-test/src/test/resources/org/apache/camel/test/patterns/
      - copied from r1161373, camel/trunk/components/camel-test/src/test/resources/org/apache/camel/test/patterns/
    camel/branches/camel-2.8.x/components/camel-test/src/test/resources/org/apache/camel/test/patterns/applicationContext.xml
      - copied unchanged from r1161373, camel/trunk/components/camel-test/src/test/resources/org/apache/camel/test/patterns/applicationContext.xml
    camel/branches/camel-2.8.x/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringCamelContextTest.java
      - copied unchanged from r1161373, camel/trunk/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringCamelContextTest.java
    camel/branches/camel-2.8.x/components/camel-testng/src/test/resources/org/
      - copied from r1161373, camel/trunk/components/camel-testng/src/test/resources/org/
    camel/branches/camel-2.8.x/components/camel-testng/src/test/resources/org/apache/
      - copied from r1161373, camel/trunk/components/camel-testng/src/test/resources/org/apache/
    camel/branches/camel-2.8.x/components/camel-testng/src/test/resources/org/apache/camel/
      - copied from r1161373, camel/trunk/components/camel-testng/src/test/resources/org/apache/camel/
    camel/branches/camel-2.8.x/components/camel-testng/src/test/resources/org/apache/camel/testng/
      - copied from r1161373, camel/trunk/components/camel-testng/src/test/resources/org/apache/camel/testng/
    camel/branches/camel-2.8.x/components/camel-testng/src/test/resources/org/apache/camel/testng/patterns/
      - copied from r1161373, camel/trunk/components/camel-testng/src/test/resources/org/apache/camel/testng/patterns/
    camel/branches/camel-2.8.x/components/camel-testng/src/test/resources/org/apache/camel/testng/patterns/applicationContext.xml
      - copied unchanged from r1161373, camel/trunk/components/camel-testng/src/test/resources/org/apache/camel/testng/patterns/applicationContext.xml
Modified:
    camel/branches/camel-2.8.x/   (props changed)
    camel/branches/camel-2.8.x/components/camel-testng/pom.xml
    camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java
    camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
    camel/branches/camel-2.8.x/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringTest.java

Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-2.8.x/components/camel-testng/pom.xml
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-testng/pom.xml?rev=1172870&r1=1172869&r2=1172870&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-testng/pom.xml (original)
+++ camel/branches/camel-2.8.x/components/camel-testng/pom.xml Mon Sep 19 22:43:36 2011
@@ -59,23 +59,4 @@
 
     </dependencies>
 
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-surefire-plugin</artifactId>
-                <configuration>
-                    <forkMode>pertest</forkMode>
-                    <childDelegation>false</childDelegation>
-                    <useFile>true</useFile>
-                    <failIfNoTests>false</failIfNoTests>
-                    <runOrder>alphabetical</runOrder>
-                    <includes>
-                        <include>**/*Test.java</include>
-                    </includes>
-                </configuration>
-            </plugin>
-        </plugins>
-    </build>
-
 </project>

Modified: camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java?rev=1172870&r1=1172869&r2=1172870&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java (original)
+++ camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelSpringTestSupport.java Mon Sep 19 22:43:36 2011
@@ -30,35 +30,56 @@ import org.apache.camel.impl.scan.Invert
 import org.apache.camel.spring.SpringCamelContext;
 import org.apache.camel.util.CastUtils;
 import org.apache.camel.util.ObjectHelper;
-import org.junit.AfterClass;
 import org.springframework.beans.factory.support.RootBeanDefinition;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.AfterTest;
 
+
 /**
  * @version $Revision$
  */
 public abstract class CamelSpringTestSupport extends CamelTestSupport {
+    protected static ThreadLocal<AbstractApplicationContext> threadAppContext
+        = new ThreadLocal<AbstractApplicationContext>();
+    protected static Object lock = new Object();
     protected static AbstractApplicationContext applicationContext;
     protected abstract AbstractApplicationContext createApplicationContext();
 
+    public void postProcessTest() throws Exception {
+        super.postProcessTest();
+        if (isCreateCamelContextPerClass()) {
+            applicationContext = threadAppContext.get();
+        }
+    }
+
     @Override
-    public void doSetUp() throws Exception { 
+    public void doPreSetup() throws Exception {
         if (!"true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"))) {
             // tell camel-spring it should not trigger starting CamelContext, since we do that later
             // after we are finished setting up the unit test
-            System.setProperty("maybeStartCamelContext", "false");
-            applicationContext = createApplicationContext();
-            assertNotNull(applicationContext, "Should have created a valid spring context");
-            System.clearProperty("maybeStartCamelContext");
+            synchronized (lock) {
+                SpringCamelContext.setNoStart(true);
+                if (isCreateCamelContextPerClass()) {
+                    applicationContext = threadAppContext.get();
+                    if (applicationContext == null) {
+                        applicationContext = createApplicationContext();
+                        threadAppContext.set(applicationContext);
+                    }
+                } else {
+                    applicationContext = createApplicationContext();
+                }
+                assertNotNull(applicationContext, "Should have created a valid spring context");
+                SpringCamelContext.setNoStart(false);
+            }
         } else {
             log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
         }
-        super.doSetUp();
     }
 
+
     @Override
     @AfterTest
     public void tearDown() throws Exception {
@@ -74,9 +95,9 @@ public abstract class CamelSpringTestSup
 
     @AfterClass
     public static void tearSpringDownAfterClass() throws Exception {
-        if (applicationContext != null) {
-            applicationContext.destroy();
-            applicationContext = null;
+        if (threadAppContext.get() != null) {
+            threadAppContext.get().destroy();
+            threadAppContext.remove();
         }
     }
 

Modified: camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java?rev=1172870&r1=1172869&r2=1172870&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java (original)
+++ camel/branches/camel-2.8.x/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java Mon Sep 19 22:43:36 2011
@@ -48,9 +48,9 @@ import org.apache.camel.spi.Language;
 import org.apache.camel.spring.CamelBeanPostProcessor;
 import org.apache.camel.util.StopWatch;
 import org.apache.camel.util.TimeUtils;
-import org.junit.AfterClass;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.testng.annotations.AfterClass;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 
@@ -61,18 +61,30 @@ import org.testng.annotations.BeforeMeth
  * @version $Revision$
  */
 public abstract class CamelTestSupport extends TestSupport {
-
-    protected static volatile CamelContext context;
-    protected static volatile ProducerTemplate template;
-    protected static volatile ConsumerTemplate consumer;
-    protected static volatile Service camelContextService;
     private static final Logger LOG = LoggerFactory.getLogger(TestSupport.class);
-    private static final AtomicBoolean INIT = new AtomicBoolean();
+    private static final ThreadLocal<Boolean> INIT = new ThreadLocal<Boolean>();
+
+
+    private static ThreadLocal<CamelContext> threadCamelContext
+        = new ThreadLocal<CamelContext>();
+    private static ThreadLocal<ProducerTemplate> threadTemplate
+        = new ThreadLocal<ProducerTemplate>();
+    private static ThreadLocal<ConsumerTemplate> threadConsumer
+        = new ThreadLocal<ConsumerTemplate>();
+    private static ThreadLocal<Service> threadService
+        = new ThreadLocal<Service>();
+
+    protected volatile CamelContext context;
+    protected volatile ProducerTemplate template;
+    protected volatile ConsumerTemplate consumer;
+    protected volatile Service camelContextService;
+
+
     private boolean useRouteBuilder = true;
     private final DebugBreakpoint breakpoint = new DebugBreakpoint();
     private final StopWatch watch = new StopWatch();
 
-    /**
+     /**
      * Use the RouteBuilder or not
      * @return <tt>true</tt> then {@link CamelContext} will be auto started,
      *        <tt>false</tt> then {@link CamelContext} will <b>not</b> be auto started (you will have to start it manually)
@@ -93,6 +105,9 @@ public abstract class CamelTestSupport e
      * <p/>
      * <b>Important:</b> Use this with care as the {@link CamelContext} will carry over state
      * from previous tests, such as endpoints, components etc. So you cannot use this in all your tests.
+     * <p/>
+     * Setting up {@link CamelContext} uses the {@link #doPreSetup()}, {@link #doSetUp()}, and {@link #doPostSetup()}
+     * methods in that given order.
      *
      * @return <tt>true</tt> per class, <tt>false</tt> per test.
      */
@@ -115,6 +130,22 @@ public abstract class CamelTestSupport e
         return camelContextService;
     }
 
+    public Service camelContextService() {
+        return camelContextService;
+    }
+
+    public CamelContext context() {
+        return context;
+    }
+
+    public ProducerTemplate template() {
+        return template;
+    }
+
+    public ConsumerTemplate consumer() {
+        return consumer;
+    }
+
     /**
      * Allows a service to be registered a separate lifecycle service to start
      * and stop the context; such as for Spring when the ApplicationContext is
@@ -122,6 +153,7 @@ public abstract class CamelTestSupport e
      */
     public void setCamelContextService(Service service) {
         camelContextService = service;
+        threadService.set(camelContextService);
     }
 
     @BeforeMethod
@@ -130,11 +162,13 @@ public abstract class CamelTestSupport e
         log.info("Testing: " + getTestMethodName() + "(" + getClass().getName() + ")");
         log.info("********************************************************************************");
 
-        boolean first = INIT.compareAndSet(false, true);
         if (isCreateCamelContextPerClass()) {
             // test is per class, so only setup once (the first time)
+            boolean first = INIT.get() == null;
             if (first) {
+                doPreSetup();
                 doSetUp();
+                doPostSetup();
             } else {
                 // and in between tests we must do IoC and reset mocks
                 postProcessTest();
@@ -142,14 +176,30 @@ public abstract class CamelTestSupport e
             }
         } else {
             // test is per test so always setup
+            doPreSetup();
             doSetUp();
+            doPostSetup();
         }
 
         // only start timing after all the setup
         watch.restart();
     }
 
-    protected void doSetUp() throws Exception {
+    /**
+     * Strategy to perform any pre setup, before {@link CamelContext} is created
+     */
+    protected void doPreSetup() throws Exception {
+        // noop
+    }
+
+    /**
+     * Strategy to perform any post setup after {@link CamelContext} is createt.
+     */
+    protected void doPostSetup() throws Exception {
+        // noop
+    }
+
+    private void doSetUp() throws Exception {
         log.debug("setUp test");
         if (!useJmx()) {
             disableJMX();
@@ -158,6 +208,8 @@ public abstract class CamelTestSupport e
         }
 
         context = createCamelContext();
+        threadCamelContext.set(context);
+
         assertNotNull(context, "No context found!");
 
         // reduce default shutdown timeout to avoid waiting for 300 seconds
@@ -173,6 +225,9 @@ public abstract class CamelTestSupport e
         consumer = context.createConsumerTemplate();
         consumer.start();
 
+        threadTemplate.set(template);
+        threadConsumer.set(consumer);
+
         // enable auto mocking if enabled
         String pattern = isMockEndpoints();
         if (pattern != null) {
@@ -216,17 +271,17 @@ public abstract class CamelTestSupport e
             return;
         }
 
-        log.debug("tearDown test");
-        doStopTemplates();
-        stopCamelContext();
+        LOG.debug("tearDown test");
+        doStopTemplates(consumer, template);
+        doStopCamelContext(context, camelContextService);
     }
 
     @AfterClass
     public static void tearDownAfterClass() throws Exception {
-        INIT.set(false);
+        INIT.remove();
         LOG.debug("tearDownAfterClass test");
-        doStopTemplates();
-        doStopCamelContext();
+        doStopTemplates(threadConsumer.get(), threadTemplate.get());
+        doStopCamelContext(threadCamelContext.get(), threadService.get());
     }
 
     /**
@@ -265,53 +320,56 @@ public abstract class CamelTestSupport e
      * Note that using Spring Test or Guice is a more powerful approach.
      */
     protected void postProcessTest() throws Exception {
+        context = threadCamelContext.get();
+        template = threadTemplate.get();
+        consumer = threadConsumer.get();
+        camelContextService = threadService.get();
+
         CamelBeanPostProcessor processor = new CamelBeanPostProcessor();
         processor.setCamelContext(context);
         processor.postProcessBeforeInitialization(this, "this");
     }
 
     protected void stopCamelContext() throws Exception {
-        doStopCamelContext();
+        doStopCamelContext(context, camelContextService);
     }
 
-    private static void doStopCamelContext() throws Exception {
+    private static void doStopCamelContext(CamelContext context,
+                                           Service camelContextService) throws Exception {
         if (camelContextService != null) {
+            if (camelContextService == threadService.get()) {
+                threadService.remove();
+            }
             camelContextService.stop();
             camelContextService = null;
         } else {
             if (context != null) {
+                if (context == threadCamelContext.get()) {
+                    threadCamelContext.remove();
+                }
                 context.stop();
                 context = null;
             }
         }
     }
 
-    private static void doStopTemplates() throws Exception {
+    private static void doStopTemplates(ConsumerTemplate consumer,
+                                        ProducerTemplate template) throws Exception {
         if (consumer != null) {
+            if (consumer == threadConsumer.get()) {
+                threadConsumer.remove();
+            }
             consumer.stop();
             consumer = null;
         }
         if (template != null) {
+            if (template == threadTemplate.get()) {
+                threadTemplate.remove();
+            }
             template.stop();
             template = null;
         }
     }
-    
-    public Service camelContextService() {
-        return camelContextService;
-    }
-    
-    public CamelContext context() {
-        return context;
-    }
-    
-    public ProducerTemplate template() {
-        return template;
-    }
-    
-    public ConsumerTemplate consumer() {
-        return consumer;
-    }
 
     protected void startCamelContext() throws Exception {
         if (camelContextService != null) {

Modified: camel/branches/camel-2.8.x/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringTest.java?rev=1172870&r1=1172869&r2=1172870&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringTest.java (original)
+++ camel/branches/camel-2.8.x/components/camel-testng/src/test/java/org/apache/camel/testng/patterns/DebugSpringTest.java Mon Sep 19 22:43:36 2011
@@ -21,10 +21,13 @@ import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.testng.CamelSpringTestSupport;
+
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
 import org.testng.annotations.Test;
 
+import org.testng.annotations.Test;
+
 public class DebugSpringTest extends CamelSpringTestSupport {
     private boolean debugged;