Repository: camel Updated Branches: refs/heads/master 2b6feb622 -> 651b7dda0
Camel-9166 Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/745d4c88 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/745d4c88 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/745d4c88 Branch: refs/heads/master Commit: 745d4c881e8736391c959714797659bf6b39988a Parents: 2b6feb6 Author: Jyrki Ruuskanen <yur...@kotikone.fi> Authored: Fri Sep 25 13:27:05 2015 +0300 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Sep 25 16:40:12 2015 +0200 ---------------------------------------------------------------------- components/camel-scr/pom.xml | 1 + .../apache/camel/scr/AbstractCamelRunner.java | 23 +++++---- .../camel/scr/AbstractCamelRunnerTest.java | 18 +++---- .../apache/camel/scr/ConcreteCamelRunner.java | 3 +- .../org/apache/camel/scr/ScrHelperTest.java | 54 ++++++++++++++++++++ .../resources/componentDefinitionExample.xml | 13 +++++ 6 files changed, 91 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/745d4c88/components/camel-scr/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-scr/pom.xml b/components/camel-scr/pom.xml index 64e0b77..1e47652 100644 --- a/components/camel-scr/pom.xml +++ b/components/camel-scr/pom.xml @@ -31,6 +31,7 @@ <properties> <camel.osgi.export.pkg>org.apache.camel.scr</camel.osgi.export.pkg> + <camel.osgi.import.camel.version>version="[2.12,$(version;=+;${camel.osgi.version.clean}))"</camel.osgi.import.camel.version> </properties> <dependencies> http://git-wip-us.apache.org/repos/asf/camel/blob/745d4c88/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java b/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java index 32bfe81..e9647ab 100644 --- a/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java +++ b/components/camel-scr/src/main/java/org/apache/camel/scr/AbstractCamelRunner.java @@ -56,7 +56,10 @@ public abstract class AbstractCamelRunner implements Runnable { protected Logger log = LoggerFactory.getLogger(getClass()); protected CamelContext context; protected SimpleRegistry registry = new SimpleRegistry(); - protected boolean active; + + // Configured fields + private String camelContextId; + private boolean active; private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); private ScheduledFuture starter; @@ -82,7 +85,7 @@ public abstract class AbstractCamelRunner implements Runnable { // Configure fields from properties configure(context, this, log, true); - setupCamelContext(bundleContext); + setupCamelContext(bundleContext, camelContextId); } protected void createCamelContext(final BundleContext bundleContext, final Map<String, String> props) { @@ -96,18 +99,13 @@ public abstract class AbstractCamelRunner implements Runnable { context = new DefaultCamelContext(registry); } setupPropertiesComponent(context, props, log); - - String name = props.remove("camelContextId"); - if (name != null) { - context.setNameStrategy(new ExplicitCamelContextNameStrategy(name)); - } - - // ensure we publish this CamelContext to the OSGi service registry - context.getManagementStrategy().addEventNotifier(new OsgiCamelContextPublisher(bundleContext)); } - protected void setupCamelContext(final BundleContext bundleContext) throws Exception { + protected void setupCamelContext(final BundleContext bundleContext, final String camelContextId) throws Exception { // Set up CamelContext + if (camelContextId != null) { + context.setNameStrategy(new ExplicitCamelContextNameStrategy(camelContextId)); + } context.setUseMDCLogging(true); context.setUseBreadcrumb(true); @@ -115,6 +113,9 @@ public abstract class AbstractCamelRunner implements Runnable { for (RoutesBuilder route : getRouteBuilders()) { context.addRoutes(configure(context, route, log)); } + + // ensure we publish this CamelContext to the OSGi service registry + context.getManagementStrategy().addEventNotifier(new OsgiCamelContextPublisher(bundleContext)); } public static void setupPropertiesComponent(final CamelContext context, final Map<String, String> props, Logger log) { http://git-wip-us.apache.org/repos/asf/camel/blob/745d4c88/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java b/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java index 5b10b15..f8f64c7 100644 --- a/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java +++ b/components/camel-scr/src/test/java/org/apache/camel/scr/AbstractCamelRunnerTest.java @@ -38,25 +38,29 @@ public class AbstractCamelRunnerTest { public TestName testName = new TestName(); private Logger log = LoggerFactory.getLogger(getClass()); + private ConcreteCamelRunner integration; @Before public void setUp() throws Exception { log.info("*******************************************************************"); log.info("Test: " + testName.getMethodName()); log.info("*******************************************************************"); + + // Set property prefix for unit testing + System.setProperty(AbstractCamelRunner.PROPERTY_PREFIX, "unit"); + + // Prepare the integration + integration = new ConcreteCamelRunner(); } @Test - public void testDeepConfigure() throws Exception { - ConcreteCamelRunner integration = new ConcreteCamelRunner(); - + public void testConfigure() throws Exception { integration.activate(null, integration.getDefaultProperties()); - assertEquals("Overriding camelContextId failed (deep configure)", integration.getDefaultProperties().get("camelContextId"), integration.getContext().getName()); + assertEquals("Configuring camelContextId with prefix failed", integration.getDefaultProperties().get("unit.camelContextId"), integration.getContext().getName()); } @Test public void testActivateDeactivate() { - ConcreteCamelRunner integration = new ConcreteCamelRunner(); try { integration.activate(null, integration.getDefaultProperties()); Thread.sleep(AbstractCamelRunner.START_DELAY + 1000); @@ -72,7 +76,6 @@ public class AbstractCamelRunnerTest { @Test public void testPrepareRunStop() { - ConcreteCamelRunner integration = new ConcreteCamelRunner(); try { integration.prepare(null, integration.getDefaultProperties()); integration.run(); @@ -91,7 +94,6 @@ public class AbstractCamelRunnerTest { @Test public void testDelayedStart() { - ConcreteCamelRunner integration = new ConcreteCamelRunner(); try { integration.activate(null, integration.getDefaultProperties()); Thread.sleep(2000); @@ -111,8 +113,6 @@ public class AbstractCamelRunnerTest { @Test public void testDelayedStartCancel() { - ConcreteCamelRunner integration = new ConcreteCamelRunner(); - Map<String, String> properties = integration.getDefaultProperties(); properties.put("from", "notfound:something"); properties.put("camelroute.id", "test/notfound-mock"); http://git-wip-us.apache.org/repos/asf/camel/blob/745d4c88/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java b/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java index 0ad1123..b1a7090 100644 --- a/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java +++ b/components/camel-scr/src/test/java/org/apache/camel/scr/ConcreteCamelRunner.java @@ -45,7 +45,8 @@ public class ConcreteCamelRunner extends AbstractCamelRunner implements Lifecycl public Map<String, String> getDefaultProperties() { // Set default properties Map<String, String> defaultProps = new HashMap<String, String>(); - defaultProps.put("camelContextId", "camel-runner-test"); + defaultProps.put("camelContextId", "camel-runner"); + defaultProps.put("unit.camelContextId", "camel-runner-unitTest"); defaultProps.put("camelRouteId", "test/direct-mock"); defaultProps.put("active", "true"); defaultProps.put("from", "direct:start"); http://git-wip-us.apache.org/repos/asf/camel/blob/745d4c88/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java b/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java new file mode 100644 index 0000000..e80e208 --- /dev/null +++ b/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java @@ -0,0 +1,54 @@ +/** + * 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.scr; + +import java.util.Map; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(JUnit4.class) +public class ScrHelperTest { + + @Rule + public TestName testName = new TestName(); + + private Logger log = LoggerFactory.getLogger(getClass()); + + @Before + public void setUp() throws Exception { + log.info("*******************************************************************"); + log.info("Test: " + testName.getMethodName()); + log.info("*******************************************************************"); + } + + @Test + public void scrHelperTest() throws Exception { + Map<String, String> properties = ScrHelper.getScrProperties("src/test/resources/componentDefinitionExample.xml", "my.example.Component"); + assertEquals("exampleContext", properties.get("camelContextId")); + assertTrue(properties.size() == 6); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/745d4c88/components/camel-scr/src/test/resources/componentDefinitionExample.xml ---------------------------------------------------------------------- diff --git a/components/camel-scr/src/test/resources/componentDefinitionExample.xml b/components/camel-scr/src/test/resources/componentDefinitionExample.xml new file mode 100644 index 0000000..162ca9c --- /dev/null +++ b/components/camel-scr/src/test/resources/componentDefinitionExample.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0"> + <scr:component immediate="true" name="my.example.Component"> + <implementation class="my.example.Component"/> + <property name="camelContextId" value="exampleContext"/> + <property name="unit.camelContextId" value="exampleContextForUnitTest"/> + <property name="camelRouteId" value="{{camelContextId}}/timer-log"/> + <property name="active" value="true"/> + <property name="master" value="master:{{camelContextId}}"/> + <property name="service.pid" value="my.example.Component"/> + <reference name="camelComponent" interface="org.apache.camel.spi.ComponentResolver" cardinality="1..n" policy="dynamic" bind="gotCamelComponent" unbind="lostCamelComponent" policy-option="greedy"/> + </scr:component> +</components>