Author: dkulp
Date: Thu Jul 28 23:44:42 2011
New Revision: 1152039

URL: http://svn.apache.org/viewvc?rev=1152039&view=rev
Log:
Update the junit4 test support classes to be parallel=classes safe

Modified:
    
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java
    
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java

Modified: 
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java?rev=1152039&r1=1152038&r2=1152039&view=diff
==============================================================================
--- 
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java
 (original)
+++ 
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelSpringTestSupport.java
 Thu Jul 28 23:44:42 2011
@@ -21,6 +21,7 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.locks.Lock;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Route;
@@ -42,18 +43,42 @@ import org.springframework.context.suppo
  * @version 
  */
 public abstract class CamelSpringTestSupport extends CamelTestSupport {
-    protected static AbstractApplicationContext applicationContext;
+    protected static ThreadLocal<AbstractApplicationContext> threadAppContext 
+        = new ThreadLocal<AbstractApplicationContext>();
+    protected static Object lock = new Object();
+    
+    protected AbstractApplicationContext applicationContext;
+    
     protected abstract AbstractApplicationContext createApplicationContext();
 
     @Override
+    public void postProcessTest() throws Exception {
+        super.postProcessTest();
+        if (isCreateCamelContextPerClass()) {
+            applicationContext = threadAppContext.get();
+        }
+    }
+
+    
+    @Override
     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("Should have created a valid spring context", 
applicationContext);
-            System.clearProperty("maybeStartCamelContext");
+            synchronized (lock) {
+                System.setProperty("maybeStartCamelContext", "false");
+                if (isCreateCamelContextPerClass()) {
+                    applicationContext = threadAppContext.get();
+                    if (applicationContext == null) {
+                        applicationContext = createApplicationContext();
+                        threadAppContext.set(applicationContext);
+                    }
+                } else {
+                    applicationContext = createApplicationContext();
+                }
+                assertNotNull("Should have created a valid spring context", 
applicationContext);
+                System.clearProperty("maybeStartCamelContext");
+            }
         } else {
             log.info("Skipping starting CamelContext as system property 
skipStartingCamelContext is set to be true.");
         }
@@ -74,9 +99,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/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java?rev=1152039&r1=1152038&r2=1152039&view=diff
==============================================================================
--- 
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
 (original)
+++ 
camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
 Thu Jul 28 23:44:42 2011
@@ -61,13 +61,25 @@ import org.slf4j.LoggerFactory;
  * @version 
  */
 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();
@@ -141,6 +153,7 @@ public abstract class CamelTestSupport e
      */
     public void setCamelContextService(Service service) {
         camelContextService = service;
+        threadService.set(camelContextService);
     }
 
     @Before
@@ -149,9 +162,9 @@ 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();
@@ -195,6 +208,8 @@ public abstract class CamelTestSupport e
         }
 
         context = createCamelContext();
+        threadCamelContext.set(context);
+
         assertNotNull("No context found!", context);
 
         // reduce default shutdown timeout to avoid waiting for 300 seconds
@@ -209,6 +224,9 @@ public abstract class CamelTestSupport e
         template.start();
         consumer = context.createConsumerTemplate();
         consumer.start();
+        
+        threadTemplate.set(template);
+        threadConsumer.set(consumer);
 
         // enable auto mocking if enabled
         String pattern = isMockEndpoints();
@@ -254,16 +272,16 @@ public abstract class CamelTestSupport e
         }
 
         LOG.debug("tearDown test");
-        doStopTemplates();
-        stopCamelContext();
+        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());
     }
 
     /**
@@ -302,33 +320,52 @@ 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;
         }


Reply via email to