Work on a new camel-itest-osgi that uses camel-test-karaf for testing.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/fb35ca3d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fb35ca3d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fb35ca3d Branch: refs/heads/master Commit: fb35ca3d8eba21af0d31d5702963c9e7b955203a Parents: 857f4b9 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Apr 18 13:29:29 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Apr 18 13:39:42 2016 +0200 ---------------------------------------------------------------------- .../camel/test/karaf/AbstractFeatureTest.java | 30 ++++++++++++++++++++ .../camel/test/karaf/CamelKarafTestSupport.java | 13 --------- .../org/apache/camel/itest/CamelCoreTest.java | 25 ++++++++++------ .../org/apache/camel/itest/CamelCoreTest.xml | 2 +- 4 files changed, 47 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/fb35ca3d/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/AbstractFeatureTest.java ---------------------------------------------------------------------- diff --git a/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/AbstractFeatureTest.java b/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/AbstractFeatureTest.java index e510dc5..6e5cddf 100644 --- a/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/AbstractFeatureTest.java +++ b/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/AbstractFeatureTest.java @@ -19,6 +19,7 @@ package org.apache.camel.test.karaf; import java.io.File; import java.io.InputStream; import java.lang.reflect.Field; +import java.net.URL; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; @@ -27,6 +28,7 @@ import java.util.Dictionary; import java.util.EnumSet; import java.util.Enumeration; import java.util.List; +import java.util.Locale; import java.util.Properties; import javax.inject.Inject; @@ -46,7 +48,11 @@ import org.ops4j.pax.exam.TestProbeBuilder; import org.ops4j.pax.exam.karaf.options.KarafDistributionOption; import org.ops4j.pax.exam.karaf.options.LogLevelOption; import org.ops4j.pax.exam.options.UrlReference; +import org.ops4j.pax.tinybundles.core.TinyBundle; +import org.ops4j.pax.tinybundles.core.TinyBundles; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; import org.osgi.framework.Constants; import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; @@ -69,6 +75,7 @@ import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel; public abstract class AbstractFeatureTest { + static final Long SERVICE_TIMEOUT = 30000L; protected static final Logger LOG = LoggerFactory.getLogger(AbstractFeatureTest.class); @Inject @@ -97,6 +104,21 @@ public abstract class AbstractFeatureTest { LOG.info("tearDown()"); } + protected Bundle installBlueprintAsBundle(String name, URL url, boolean start) throws BundleException { + TinyBundle bundle = TinyBundles.bundle(); + bundle.add("OSGI-INF/blueprint/blueprint-" + name.toLowerCase(Locale.ENGLISH) + ".xml", url); + bundle.set("Manifest-Version", "2") + .set("Bundle-ManifestVersion", "2") + .set("Bundle-SymbolicName", name) + .set("Bundle-Version", "1.0.0"); + Bundle answer = bundleContext.installBundle(name, bundle.build()); + + if (start) { + answer.start(); + } + return answer; + } + protected void installCamelFeature(String mainFeature) throws Exception { if (!mainFeature.startsWith("camel-")) { mainFeature = "camel-" + mainFeature; @@ -274,6 +296,14 @@ public abstract class AbstractFeatureTest { return options; } + protected <T> T getOsgiService(BundleContext bundleContext, Class<T> type) { + return getOsgiService(bundleContext, type, null, SERVICE_TIMEOUT); + } + + protected <T> T getOsgiService(BundleContext bundleContext, Class<T> type, long timeout) { + return getOsgiService(bundleContext, type, null, timeout); + } + @SuppressWarnings("unchecked") public static <T> T getOsgiService(BundleContext bundleContext, Class<T> type, String filter, long timeout) { ServiceTracker tracker; http://git-wip-us.apache.org/repos/asf/camel/blob/fb35ca3d/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/CamelKarafTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/CamelKarafTestSupport.java b/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/CamelKarafTestSupport.java index ad1ccb5..3f28bc5 100644 --- a/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/CamelKarafTestSupport.java +++ b/components/camel-test-karaf/src/main/java/org/apache/camel/test/karaf/CamelKarafTestSupport.java @@ -49,10 +49,7 @@ import org.apache.karaf.features.FeaturesService; import org.ops4j.pax.exam.Option; import org.ops4j.pax.exam.ProbeBuilder; import org.ops4j.pax.exam.TestProbeBuilder; -import org.ops4j.pax.tinybundles.core.TinyBundle; -import org.ops4j.pax.tinybundles.core.TinyBundles; import org.osgi.framework.BundleContext; -import org.osgi.framework.BundleException; import org.osgi.framework.Constants; import org.osgi.framework.Filter; import org.osgi.framework.FrameworkUtil; @@ -79,16 +76,6 @@ public class CamelKarafTestSupport extends CamelTestSupport { return probe; } - public void installBlueprintAsBundle(String name, URL url) throws BundleException { - TinyBundle bundle = TinyBundles.bundle(); - bundle.add("OSGI-INF/blueprint/blueprint-" + name, url); - bundle.set("Manifest-Version", "2") - .set("Bundle-ManifestVersion", "2") - .set("Bundle-SymbolicName", name) - .set("Bundle-Version", "1.0.0"); - bundleContext.installBundle(name, bundle.build()); - } - public File getConfigFile(String path) { URL res = this.getClass().getResource(path); if (res == null) { http://git-wip-us.apache.org/repos/asf/camel/blob/fb35ca3d/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/CamelCoreTest.java ---------------------------------------------------------------------- diff --git a/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/CamelCoreTest.java b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/CamelCoreTest.java index 45e508f..73fa62d 100644 --- a/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/CamelCoreTest.java +++ b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/CamelCoreTest.java @@ -5,9 +5,9 @@ * 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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> + * + * 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. @@ -19,6 +19,8 @@ package org.apache.camel.itest; import java.net.URL; import org.apache.camel.CamelContext; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.karaf.AbstractFeatureTest; import org.apache.camel.test.karaf.CamelKarafTestSupport; import org.apache.camel.util.ObjectHelper; import org.junit.Test; @@ -28,18 +30,23 @@ import org.ops4j.pax.exam.Option; import org.ops4j.pax.exam.junit.PaxExam; @RunWith(PaxExam.class) -public class CamelCoreTest extends CamelKarafTestSupport { +public class CamelCoreTest extends AbstractFeatureTest { @Test public void testCamelCore() throws Exception { URL url = ObjectHelper.loadResourceAsURL("org/apache/camel/itest/CamelCoreTest.xml", CamelCoreTest.class.getClassLoader()); - System.out.println(">>>> " + url); - installBlueprintAsBundle("CamelCoreTest", url); + installBlueprintAsBundle("CamelCoreTest", url, true); - // wait for Camel to be ready -// CamelContext camel = getOsgiService(CamelContext.class); + // lookup Camel from OSGi + CamelContext camel = getOsgiService(bundleContext, CamelContext.class); -// System.out.println(">>> " + camel); + // test camel + MockEndpoint mock = camel.getEndpoint("mock:result", MockEndpoint.class); + mock.expectedBodiesReceived("Hello World"); + + camel.createProducerTemplate().sendBody("direct:start", "World"); + + mock.assertIsSatisfied(); } @Configuration http://git-wip-us.apache.org/repos/asf/camel/blob/fb35ca3d/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/CamelCoreTest.xml ---------------------------------------------------------------------- diff --git a/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/CamelCoreTest.xml b/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/CamelCoreTest.xml index fbff918..d5f3bee 100644 --- a/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/CamelCoreTest.xml +++ b/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/CamelCoreTest.xml @@ -20,7 +20,7 @@ xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> - <camelContext xmlns="http://camel.apache.org/schema/blueprint"> + <camelContext id="myCamel" xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="direct:start"/>