Updated Branches: refs/heads/camel-2.11.x febef4488 -> e2c1a4041 refs/heads/camel-2.12.x 77079a34c -> 20b60b702 refs/heads/master d3265079a -> bb9e3d1ec
CAMEL-6697: camel-test-blueprint. Make it essy to add services on startup before Camel starts so they can be used during testing. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4b9979e3 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4b9979e3 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4b9979e3 Branch: refs/heads/camel-2.12.x Commit: 4b9979e35157e3c9b69dcad48f815f6576e858e1 Parents: 77079a3 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Sep 2 12:19:01 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Sep 2 12:19:20 2013 +0200 ---------------------------------------------------------------------- .../blueprint/CamelBlueprintTestSupport.java | 55 ++++++++++++++++++++ .../test/blueprint/BlueprintAddServiceTest.java | 54 +++++++++++++++++++ .../apache/camel/test/blueprint/MyService.java | 24 +++++++++ .../test/blueprint/BlueprintAddServiceTest.xml | 33 ++++++++++++ 4 files changed, 166 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/4b9979e3/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 bbd0b7d..1a52d08 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 @@ -17,16 +17,22 @@ package org.apache.camel.test.blueprint; import java.util.Dictionary; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.Map; import java.util.Properties; +import java.util.Set; import org.apache.camel.CamelContext; import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.model.ModelCamelContext; import org.apache.camel.test.junit4.CamelTestSupport; +import org.apache.camel.util.KeyValueHolder; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; import org.osgi.service.blueprint.container.BlueprintContainer; import org.osgi.service.cm.Configuration; import org.osgi.service.cm.ConfigurationAdmin; @@ -37,6 +43,7 @@ import org.osgi.service.cm.ConfigurationAdmin; public abstract class CamelBlueprintTestSupport extends CamelTestSupport { private static ThreadLocal<BundleContext> threadLocalBundleContext = new ThreadLocal<BundleContext>(); private volatile BundleContext bundleContext; + private final Set<ServiceRegistration<?>> services = new LinkedHashSet<ServiceRegistration<?>>(); @SuppressWarnings({"rawtypes", "unchecked"}) protected BundleContext createBundleContext() throws Exception { @@ -50,6 +57,19 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport { answer.registerService(PropertiesComponent.OVERRIDE_PROPERTIES, extra, null); } + Map<String, KeyValueHolder<Object, Dictionary>> map = new LinkedHashMap<String, KeyValueHolder<Object, Dictionary>>(); + addServicesOnStartup(map); + for (Map.Entry<String, KeyValueHolder<Object, Dictionary>> entry : map.entrySet()) { + String clazz = entry.getKey(); + Object service = entry.getValue().getKey(); + Dictionary dict = entry.getValue().getValue(); + log.debug("Registering service {} -> {}", clazz, service); + ServiceRegistration<?> reg = answer.registerService(clazz, service, dict); + if (reg != null) { + services.add(reg); + } + } + // must reuse props as we can do both load from .cfg file and override afterwards Dictionary props = new Properties(); @@ -107,6 +127,34 @@ 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 #asService(Object, java.util.Dictionary)}, {@link #asService(Object, String, String)} + * to make it easy to add the services to the map. + */ + protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { + // noop + } + + /** + * Creates a holder for the given service, which make it easier to use {@link #addServicesOnStartup(java.util.Map)} + */ + 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.Map)} + */ + KeyValueHolder<Object, Dictionary> asService(Object service, String key, String value) { + Properties prop = new Properties(); + if (key != null && value != null) { + prop.put(key, value); + } + return new KeyValueHolder<Object, Dictionary>(service, prop); + } + + /** * Override this method to override config admin properties. * * @param props properties where you add the properties to override @@ -136,6 +184,13 @@ public abstract class CamelBlueprintTestSupport extends CamelTestSupport { // we tear down in after class return; } + + // unregister services + if (bundleContext != null) { + for (ServiceRegistration<?> reg : services) { + bundleContext.ungetService(reg.getReference()); + } + } CamelBlueprintHelper.disposeBundleContext(bundleContext); } http://git-wip-us.apache.org/repos/asf/camel/blob/4b9979e3/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintAddServiceTest.java ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintAddServiceTest.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintAddServiceTest.java new file mode 100644 index 0000000..5327a5a --- /dev/null +++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/BlueprintAddServiceTest.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.test.blueprint; + +import java.util.Dictionary; +import java.util.Map; + +import org.apache.camel.util.KeyValueHolder; +import org.junit.Test; +import org.osgi.framework.ServiceReference; + +public class BlueprintAddServiceTest extends CamelBlueprintTestSupport { + + private MyService myService = new MyService(); + + @Override + protected String getBlueprintDescriptor() { + return "org/apache/camel/test/blueprint/BlueprintAddServiceTest.xml"; + } + + @Override + protected void addServicesOnStartup(Map<String, KeyValueHolder<Object, Dictionary>> services) { + services.put("myService", asService(myService, "beer", "Carlsberg")); + } + + @Test + public void testAddService() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("CamelCamel"); + + template.sendBody("direct:start", "Camel"); + + assertMockEndpointsSatisfied(); + + ServiceReference<?> ref = getBundleContext().getServiceReference("myService"); + assertEquals("Carlsberg", ref.getProperty("beer")); + Object service = getBundleContext().getService(ref); + assertSame(myService, service); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/4b9979e3/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyService.java ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyService.java b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyService.java new file mode 100644 index 0000000..460c0b0 --- /dev/null +++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MyService.java @@ -0,0 +1,24 @@ +/** + * 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; + +public class MyService { + + public String echo(String hello) { + return hello + hello; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4b9979e3/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintAddServiceTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintAddServiceTest.xml b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintAddServiceTest.xml new file mode 100644 index 0000000..732ee3b --- /dev/null +++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/BlueprintAddServiceTest.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="bean:myService"/> + <to uri="mock:result"/> + </route> + + </camelContext> + +</blueprint>