Updated Branches: refs/heads/camel-2.11.x 31960541c -> 37d4307f7 refs/heads/camel-2.12.x df9d866da -> 16230cbb2 refs/heads/master 99b40bd77 -> bc6e7908d
CAMEL-6760 supporting to configure Camel Context creation timeoutin camel-test-blueprint with thanks to Eugene Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1f4adc2d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1f4adc2d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1f4adc2d Branch: refs/heads/master Commit: 1f4adc2d57b42d63b8c81eec0873368cbe20114b Parents: 99b40bd Author: Willem Jiang <ningji...@apache.org> Authored: Tue Sep 17 10:58:10 2013 +0800 Committer: Willem Jiang <ningji...@apache.org> Committed: Tue Sep 17 10:58:51 2013 +0800 ---------------------------------------------------------------------- .../blueprint/CamelBlueprintTestSupport.java | 42 +++++- .../blueprint/ContextCreationTimeoutTest.java | 144 +++++++++++++++++++ 2 files changed, 184 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1f4adc2d/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java index 1a52d08..cce7a8e 100644 --- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java +++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintTestSupport.java @@ -41,10 +41,14 @@ import org.osgi.service.cm.ConfigurationAdmin; * Base class for OSGi Blueprint unit tests with Camel. */ public abstract class CamelBlueprintTestSupport extends CamelTestSupport { + /** Name of a system property that sets camel context creation timeout. */ + public static final String SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT = "org.apache.camel.test.blueprint.camelContextCreationTimeout"; + private static ThreadLocal<BundleContext> threadLocalBundleContext = new ThreadLocal<BundleContext>(); private volatile BundleContext bundleContext; private final Set<ServiceRegistration<?>> services = new LinkedHashSet<ServiceRegistration<?>>(); - + + @SuppressWarnings({"rawtypes", "unchecked"}) protected BundleContext createBundleContext() throws Exception { String symbolicName = getClass().getSimpleName(); @@ -253,9 +257,43 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport { return null; } + /** + * Returns how long to wait for Camel Context + * to be created. + * + * @return timeout in milliseconds. + */ + protected Long getCamelContextCreationTimeout() { + String tm = System.getProperty(SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT); + if (tm == null) { + return null; + } + try { + Long val = Long.valueOf(tm); + if (val < 0) { + throw new IllegalArgumentException("Value of " + + SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT + + " cannot be negative."); + } + return val; + } catch (NumberFormatException e) { + throw new IllegalArgumentException("Value of " + + SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT + + " has wrong format.", e); + } + } + @Override protected CamelContext createCamelContext() throws Exception { - CamelContext answer = CamelBlueprintHelper.getOsgiService(bundleContext, CamelContext.class); + CamelContext answer = null; + Long timeout = getCamelContextCreationTimeout(); + if (timeout == null) { + answer = CamelBlueprintHelper.getOsgiService(bundleContext, CamelContext.class); + } else if (timeout >= 0) { + answer = CamelBlueprintHelper.getOsgiService(bundleContext, CamelContext.class, timeout); + } else { + throw new IllegalArgumentException("getCamelContextCreationTimeout cannot return a negative value."); + } // must override context so we use the correct one in testing context = (ModelCamelContext) answer; return answer; http://git-wip-us.apache.org/repos/asf/camel/blob/1f4adc2d/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ContextCreationTimeoutTest.java ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ContextCreationTimeoutTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ContextCreationTimeoutTest.java new file mode 100644 index 0000000..4c6bc0a --- /dev/null +++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ContextCreationTimeoutTest.java @@ -0,0 +1,144 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.test.blueprint; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; + +public class ContextCreationTimeoutTest extends Assert { + + @After + public void cleanup() { + System.clearProperty(CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT); + } + + @Test + public void testDefault() { + System.clearProperty(CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT); + CamelBlueprintTestSupport ts = new DefaultTestSupport(); + assertNull(ts.getCamelContextCreationTimeout()); + } + + @Test + public void testSystemPropertyNormal() { + final Long someValue = 60000L; + System.setProperty( + CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT, + someValue.toString()); + CamelBlueprintTestSupport ts = new DefaultTestSupport(); + assertEquals(someValue, ts.getCamelContextCreationTimeout()); + } + + @Test + public void testSystemPropertyMaxVal() { + final Long someValue = Long.MAX_VALUE; + System.setProperty( + CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT, + someValue.toString()); + CamelBlueprintTestSupport ts = new DefaultTestSupport(); + assertEquals(someValue, ts.getCamelContextCreationTimeout()); + } + + @Test + public void testSystemPropertyZero() { + final Long zeroValue = 0L; + System.setProperty( + CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT, + zeroValue.toString()); + CamelBlueprintTestSupport ts = new DefaultTestSupport(); + assertEquals(zeroValue, ts.getCamelContextCreationTimeout()); + } + + @Test + public void testSystemPropertyNegative() { + System.setProperty( + CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT, + "-100"); + CamelBlueprintTestSupport ts = new DefaultTestSupport(); + try { + ts.getCamelContextCreationTimeout(); + fail(); + } catch (IllegalArgumentException e) { + assertNull(e.getCause()); + } + } + + @Test + public void testSystemPropertyWrongFormat() { + System.setProperty( + CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT, + "NaN"); + CamelBlueprintTestSupport ts = new DefaultTestSupport(); + try { + ts.getCamelContextCreationTimeout(); + fail(); + } catch (IllegalArgumentException e) { + assertTrue(e.getCause() != null + && e.getCause() instanceof NumberFormatException); + } + } + + @Test + public void testOverrideNormal() { + final Long someValue = 60000L; + System.clearProperty(CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT); + CamelBlueprintTestSupport ts = new OverridingTestSupport(someValue); + assertEquals(someValue, ts.getCamelContextCreationTimeout()); + } + + @Test + public void testOverrideSystemPropNormal() { + final Long someValue = 60000L; + final Long syspropValue = someValue + 60000L; + System.setProperty( + CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT, + syspropValue.toString()); + CamelBlueprintTestSupport ts = new OverridingTestSupport(someValue); + assertEquals(someValue, ts.getCamelContextCreationTimeout()); + } + + @Test + public void testOverrideSystemPropNegative() { + final Long someValue = 60000L; + final Long syspropValue = (Math.abs(someValue) + 10) * -1; + System.setProperty( + CamelBlueprintTestSupport.SPROP_CAMEL_CONTEXT_CREATION_TIMEOUT, + syspropValue.toString()); + CamelBlueprintTestSupport ts = new OverridingTestSupport(someValue); + assertEquals(someValue, ts.getCamelContextCreationTimeout()); + } + + private static class DefaultTestSupport extends CamelBlueprintTestSupport { + + } + + private static class OverridingTestSupport extends + CamelBlueprintTestSupport { + + private final Long timeout; + + public OverridingTestSupport(Long timeout) { + this.timeout = timeout; + } + + @Override + protected Long getCamelContextCreationTimeout() { + return timeout; + } + } +}