You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/09/27 16:38:47 UTC

camel git commit: CAMEL-9166 for 2.15.x

Repository: camel
Updated Branches:
  refs/heads/camel-2.15.x 2dd6f6880 -> bf6d1b7c4


CAMEL-9166 for 2.15.x


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/bf6d1b7c
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bf6d1b7c
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bf6d1b7c

Branch: refs/heads/camel-2.15.x
Commit: bf6d1b7c495a46788529e797873fbe25a950082d
Parents: 2dd6f68
Author: Jyrki Ruuskanen <yu...@kotikone.fi>
Authored: Sat Sep 26 13:14:21 2015 +0300
Committer: Jyrki Ruuskanen <yu...@kotikone.fi>
Committed: Sat Sep 26 13:14:21 2015 +0300

----------------------------------------------------------------------
 .../apache/camel/scr/AbstractCamelRunner.java   | 28 +++++-----
 .../camel/scr/AbstractCamelRunnerTest.java      | 18 +++----
 .../apache/camel/scr/ConcreteCamelRunner.java   |  3 +-
 .../camel/scr/internal/ScrHelperTest.java       | 55 ++++++++++++++++++++
 .../resources/componentDefinitionExample.xml    | 13 +++++
 5 files changed, 94 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/bf6d1b7c/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 892c885..c4be591 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
@@ -42,6 +42,7 @@ import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.impl.ExplicitCamelContextNameStrategy;
 import org.apache.camel.impl.SimpleRegistry;
+import org.apache.camel.model.ModelCamelContext;
 import org.apache.camel.util.ReflectionHelper;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -54,9 +55,12 @@ public abstract class AbstractCamelRunner implements Runnable {
     public static final String PROPERTY_PREFIX = "camel.scr.properties.prefix";
     
     protected Logger log = LoggerFactory.getLogger(getClass());
-    protected CamelContext context;
+    protected ModelCamelContext 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,11 +86,11 @@ 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) {
-        if (null != bundleContext) {
+        if (bundleContext != null) {
             context = new OsgiDefaultCamelContext(bundleContext, registry);
             // Setup the application context classloader with the bundle classloader
             context.setApplicationContextClassLoader(new BundleDelegatingClassLoader(bundleContext.getBundle()));
@@ -96,18 +100,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 +114,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/bf6d1b7c/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/bf6d1b7c/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/bf6d1b7c/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java
----------------------------------------------------------------------
diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java b/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java
new file mode 100644
index 0000000..d97cf71
--- /dev/null
+++ b/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java
@@ -0,0 +1,55 @@
+/**
+ * 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.internal;
+
+import java.util.Map;
+
+import org.apache.camel.scr.internal.ScrHelper;
+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/bf6d1b7c/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>