Repository: camel
Updated Branches:
  refs/heads/master b684be16f -> 9f8d3cdae


CAMEL-8762: Ability to add multiple services with same interface for blueprint 
testing


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

Branch: refs/heads/master
Commit: 9f8d3cdae0273d5a0e9bdc64b4886aa077c6e19a
Parents: b684be1
Author: Andrew Block <andy.bl...@gmail.com>
Authored: Sat May 9 11:03:15 2015 -0500
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun May 10 09:18:32 2015 +0200

----------------------------------------------------------------------
 .../blueprint/CamelBlueprintTestSupport.java    | 34 +++++++-
 .../blueprint/BlueprintMultipleServiceTest.java | 82 ++++++++++++++++++++
 .../blueprint/BlueprintMultipleServiceTest.xml  | 33 ++++++++
 3 files changed, 146 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9f8d3cda/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 fc88f56..a6b861c 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
@@ -19,6 +19,8 @@ package org.apache.camel.test.blueprint;
 import java.util.Dictionary;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
@@ -80,10 +82,18 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
 
         Map<String, KeyValueHolder<Object, Dictionary>> map = new 
LinkedHashMap<String, KeyValueHolder<Object, Dictionary>>();
         addServicesOnStartup(map);
+
+        List<KeyValueHolder<String, KeyValueHolder<Object, Dictionary>>> 
servicesList = new LinkedList<KeyValueHolder<String, KeyValueHolder<Object, 
Dictionary>>>();
         for (Map.Entry<String, KeyValueHolder<Object, Dictionary>> entry : 
map.entrySet()) {
-            String clazz = entry.getKey();
-            Object service = entry.getValue().getKey();
-            Dictionary dict = entry.getValue().getValue();
+            servicesList.add(asKeyValueService(entry.getKey(), 
entry.getValue().getKey(), entry.getValue().getValue()));
+        }
+
+        addServicesOnStartup(servicesList);
+
+        for (KeyValueHolder<String, KeyValueHolder<Object, Dictionary>> item : 
servicesList) {
+            String clazz = item.getKey();
+            Object service = item.getValue().getKey();
+            Dictionary dict = item.getValue().getValue();
             log.debug("Registering service {} -> {}", clazz, service);
             ServiceRegistration<?> reg = answer.registerService(clazz, 
service, dict);
             if (reg != null) {
@@ -202,6 +212,16 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
     }
 
     /**
+     * Override this method to add services to be registered on startup.
+     * <p/>
+     * You can use the builder methods {@link #asKeyValueService(String, 
Object, Dictionary)}
+     * to make it easy to add the services to the List.
+     */
+    protected void addServicesOnStartup(List<KeyValueHolder<String, 
KeyValueHolder<Object, Dictionary>>> services) {
+        // noop
+    }
+
+    /**
      * Creates a holder for the given service, which make it easier to use 
{@link #addServicesOnStartup(java.util.Map)}
      */
     protected KeyValueHolder<Object, Dictionary> asService(Object service, 
Dictionary dict) {
@@ -209,6 +229,14 @@ public abstract class CamelBlueprintTestSupport extends 
CamelTestSupport {
     }
 
     /**
+     * Creates a holder for the given service, which make it easier to use 
{@link #addServicesOnStartup(java.util.List)}
+     */
+    protected KeyValueHolder<String, KeyValueHolder<Object, Dictionary>> 
asKeyValueService(String name, Object service, Dictionary dict) {
+        return new KeyValueHolder<String, KeyValueHolder<Object, 
Dictionary>>(name, new KeyValueHolder<Object, Dictionary>(service, dict));
+    }
+
+
+    /**
      * Creates a holder for the given service, which make it easier to use 
{@link #addServicesOnStartup(java.util.Map)}
      */
     protected KeyValueHolder<Object, Dictionary> asService(Object service, 
String key, String value) {

http://git-wip-us.apache.org/repos/asf/camel/blob/9f8d3cda/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.java
 
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.java
new file mode 100644
index 0000000..f22af51
--- /dev/null
+++ 
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.java
@@ -0,0 +1,82 @@
+/**
+ * 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 java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.component.mock.MockComponent;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.ComponentResolver;
+import org.apache.camel.util.KeyValueHolder;
+import org.junit.Test;
+
+public class BlueprintMultipleServiceTest extends CamelBlueprintTestSupport {
+
+    @EndpointInject(uri = "fakeservice1:mock")
+    private MockEndpoint fakeServiceOneMock;
+
+    @EndpointInject(uri = "fakeservice2:mock")
+    private MockEndpoint fakeServiceTwoMock;
+
+    private MockComponent mockComponentOne = new MockComponent();
+    private MockComponent mockComponentTwo = new MockComponent();
+
+    @Override
+    protected String getBlueprintDescriptor() {
+        return 
"org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.xml";
+    }
+
+    @Override
+    @SuppressWarnings("rawtypes")
+    protected void addServicesOnStartup(List<KeyValueHolder<String, 
KeyValueHolder<Object, Dictionary>>> services) {
+        Dictionary<String, String> dict1 = new Hashtable<String, String>();
+        dict1.put("component", "fakeservice1");
+
+        Dictionary<String, String> dict2 = new Hashtable<String, String>();
+        dict2.put("component", "fakeservice2");
+
+        services.add(asKeyValueService(ComponentResolver.class.getName(), 
mockComponentOne, dict1));
+        services.add(asKeyValueService(ComponentResolver.class.getName(), 
mockComponentTwo, dict2));
+
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.addComponent("fakeservice1", mockComponentOne);
+        context.addComponent("fakeservice2", mockComponentTwo);
+
+        return context;
+    }
+
+    @Test
+    public void testMultipleService() throws Exception {
+
+        template.sendBody("direct:start", "Camel");
+
+        fakeServiceOneMock.expectedMessageCount(1);
+        fakeServiceTwoMock.expectedMessageCount(1);
+
+        assertMockEndpointsSatisfied();
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/9f8d3cda/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.xml
----------------------------------------------------------------------
diff --git 
a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.xml
 
b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.xml
new file mode 100644
index 0000000..2de39e7
--- /dev/null
+++ 
b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintMultipleServiceTest.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0";
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+           xsi:schemaLocation="
+             http://www.osgi.org/xmlns/blueprint/v1.0.0 
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd";>
+
+  <camelContext xmlns="http://camel.apache.org/schema/blueprint";>
+
+    <route>
+      <from uri="direct:start"/>
+      <to uri="fakeservice1:mock"/>
+      <to uri="fakeservice2:mock"/>
+    </route>
+
+  </camelContext>
+
+</blueprint>

Reply via email to