Navigation Menu

Skip to content

Commit

Permalink
CAMEL-8762: Ability to add multiple services with same interface for …
Browse files Browse the repository at this point in the history
…blueprint testing
  • Loading branch information
sabre1041 authored and davsclaus committed May 10, 2015
1 parent b684be1 commit 9f8d3cd
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 3 deletions.
Expand Up @@ -19,6 +19,8 @@
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;
Expand Down Expand Up @@ -80,10 +82,18 @@ protected BundleContext createBundleContext() throws Exception {

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) {
Expand Down Expand Up @@ -201,13 +211,31 @@ protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionar
// noop
}

/**
* 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) {
return new KeyValueHolder<Object, Dictionary>(service, dict);
}

/**
* 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)}
*/
Expand Down
@@ -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();

}

}
@@ -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>

0 comments on commit 9f8d3cd

Please sign in to comment.