Author: ningjiang Date: Wed Mar 27 05:30:55 2013 New Revision: 1461419 URL: http://svn.apache.org/r1461419 Log: CAMEL-6217 Camel-test-blueprint supports to create bundle context per class
Modified: camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java camel/trunk/tests/camel-blueprint-cxf-test/src/test/java/org/apache/camel/test/cxf/blueprint/CxfConsumerSoap12Test.java Modified: camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java?rev=1461419&r1=1461418&r2=1461419&view=diff ============================================================================== --- camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java (original) +++ camel/trunk/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java Wed Mar 27 05:30:55 2013 @@ -26,6 +26,7 @@ import org.apache.camel.component.proper import org.apache.camel.model.ModelCamelContext; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.osgi.framework.BundleContext; import org.osgi.service.blueprint.container.BlueprintContainer; @@ -36,20 +37,18 @@ import org.osgi.service.cm.Configuration * Base class for OSGi Blueprint unit tests with Camel. */ public abstract class CamelBlueprintTestSupport extends CamelTestSupport { - - private BundleContext bundleContext; - - @Before - @Override - public void setUp() throws Exception { + private static ThreadLocal<BundleContext> threadLocalBundleContext = new ThreadLocal<BundleContext>(); + private volatile BundleContext bundleContext; + + protected BundleContext createBundleContext() throws Exception { String symbolicName = getClass().getSimpleName(); - this.bundleContext = CamelBlueprintHelper.createBundleContext(symbolicName, getBlueprintDescriptor(), + BundleContext answer = CamelBlueprintHelper.createBundleContext(symbolicName, getBlueprintDescriptor(), true, getBundleFilter(), getBundleVersion(), getBundleDirectives()); // must register override properties early in OSGi containers Properties extra = useOverridePropertiesWithPropertiesComponent(); if (extra != null) { - bundleContext.registerService(PropertiesComponent.OVERRIDE_PROPERTIES, extra, null); + answer.registerService(PropertiesComponent.OVERRIDE_PROPERTIES, extra, null); } // must reuse props as we can do both load from .cfg file and override afterwards @@ -76,7 +75,7 @@ public abstract class CamelBlueprintTest props.put(key, value); } - ConfigurationAdmin configAdmin = getOsgiService(ConfigurationAdmin.class); + ConfigurationAdmin configAdmin = CamelBlueprintHelper.getOsgiService(answer, ConfigurationAdmin.class); if (configAdmin != null) { // ensure we update Configuration config = configAdmin.getConfiguration(pid); @@ -88,7 +87,7 @@ public abstract class CamelBlueprintTest // allow end user to override properties String pid = useOverridePropertiesWithConfigAdmin(props); if (pid != null) { - ConfigurationAdmin configAdmin = getOsgiService(ConfigurationAdmin.class); + ConfigurationAdmin configAdmin = CamelBlueprintHelper.getOsgiService(answer, ConfigurationAdmin.class); Configuration config = configAdmin.getConfiguration(pid); if (config == null) { throw new IllegalArgumentException("Cannot find configuration with pid " + pid + " in OSGi ConfigurationAdmin service."); @@ -96,9 +95,25 @@ public abstract class CamelBlueprintTest log.info("Updating ConfigAdmin {} by overriding properties {}", config, props); config.update(props); } + return answer; + } - super.setUp(); + @Before + @Override + public void setUp() throws Exception { + String symbolicName = getClass().getSimpleName(); + if (isCreateCamelContextPerClass()) { + // test is per class, so only setup once (the first time) + boolean first = threadLocalBundleContext.get() == null; + if (first) { + threadLocalBundleContext.set(createBundleContext()); + } + bundleContext = threadLocalBundleContext.get(); + } else { + bundleContext = createBundleContext(); + } + super.setUp(); // must wait for blueprint container to be published then the namespace parser is complete and we are ready for testing log.debug("Waiting for BlueprintContainer to be published with symbolicName: {}", symbolicName); getOsgiService(BlueprintContainer.class, "(osgi.blueprint.container.symbolicname=" + symbolicName + ")"); @@ -128,8 +143,21 @@ public abstract class CamelBlueprintTest @Override public void tearDown() throws Exception { super.tearDown(); + if (isCreateCamelContextPerClass()) { + // we tear down in after class + return; + } CamelBlueprintHelper.disposeBundleContext(bundleContext); } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + if (threadLocalBundleContext.get() != null) { + CamelBlueprintHelper.disposeBundleContext(threadLocalBundleContext.get()); + threadLocalBundleContext.remove(); + } + CamelTestSupport.tearDownAfterClass(); + } /** * Return the system bundle context @@ -188,6 +216,7 @@ public abstract class CamelBlueprintTest context = (ModelCamelContext) answer; return answer; } + protected <T> T getOsgiService(Class<T> type) { return CamelBlueprintHelper.getOsgiService(bundleContext, type); Modified: camel/trunk/tests/camel-blueprint-cxf-test/src/test/java/org/apache/camel/test/cxf/blueprint/CxfConsumerSoap12Test.java URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-blueprint-cxf-test/src/test/java/org/apache/camel/test/cxf/blueprint/CxfConsumerSoap12Test.java?rev=1461419&r1=1461418&r2=1461419&view=diff ============================================================================== --- camel/trunk/tests/camel-blueprint-cxf-test/src/test/java/org/apache/camel/test/cxf/blueprint/CxfConsumerSoap12Test.java (original) +++ camel/trunk/tests/camel-blueprint-cxf-test/src/test/java/org/apache/camel/test/cxf/blueprint/CxfConsumerSoap12Test.java Wed Mar 27 05:30:55 2013 @@ -49,13 +49,11 @@ public class CxfConsumerSoap12Test exten return "org/apache/camel/test/cxf/blueprint/CxfConsumerSoap12Beans.xml"; } - @Test - public void testBeanDefinitionParserAndInvokeGreeter() throws Exception { - // the execution order of the test could cause the test failed - testInvokeGreeter(); - testCxfEndpointBeanDefinitionParser(); + public boolean isCreateCamelContextPerClass() { + return true; } + @Test public void testCxfEndpointBeanDefinitionParser() { CxfEndpoint routerEndpoint = context.getEndpoint("routerEndpoint", CxfEndpoint.class); assertEquals("Got the wrong endpoint address", routerEndpoint.getAddress(), @@ -70,6 +68,7 @@ public class CxfConsumerSoap12Test exten assertTrue("Mtom not enabled", ((SoapBindingConfiguration)binding).isMtomEnabled()); } + @Test public void testInvokeGreeter() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1);