This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-1.2.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit e6baa2cfef3edcbb95b1e9b2ad419dd400648c0b Author: Stefan Seifert <[email protected]> AuthorDate: Thu Feb 19 17:15:13 2015 +0000 SLING-4439 implement dynamic service registration git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1660943 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/testing/mock/osgi/MockBundleContext.java | 8 +- .../sling/testing/mock/osgi/OsgiServiceUtil.java | 10 +- .../MockBundleContextDynamicReferncesTest.java | 169 +++++++++++++++++++++ .../testing/mock/osgi/OsgiMetadataUtilTest.java | 4 +- .../testing/mock/osgi/OsgiServiceUtilTest.java | 18 +++ ...sling.testing.mock.osgi.OsgiServiceUtilTest.xml | 3 +- 6 files changed, 200 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java index 9ae85bc..fddb3c2 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java @@ -104,8 +104,8 @@ class MockBundleContext implements BundleContext { case MANDATORY_MULTIPLE: case OPTIONAL_MULTIPLE: case OPTIONAL_UNARY: - OsgiServiceUtil.invokeBindMethod(reference, registration.getService(), - new ServiceInfo(referenceInfo.getServiceRegistration())); + OsgiServiceUtil.invokeBindMethod(reference, referenceInfo.getServiceRegistration().getService(), + new ServiceInfo(registration)); break; default: throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality()); @@ -137,8 +137,8 @@ class MockBundleContext implements BundleContext { case OPTIONAL_MULTIPLE: case OPTIONAL_UNARY: // it is currently not checked if for a MANDATORY_MULTIPLE reference the last reference is removed - OsgiServiceUtil.invokeUnbindMethod(reference, registration.getService(), - new ServiceInfo(referenceInfo.getServiceRegistration())); + OsgiServiceUtil.invokeUnbindMethod(reference, referenceInfo.getServiceRegistration().getService(), + new ServiceInfo(registration)); break; default: throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality()); diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java index 152379f..1960d0d 100644 --- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java +++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java @@ -242,13 +242,13 @@ final class OsgiServiceUtil { method.setAccessible(true); method.invoke(target, args); } catch (IllegalAccessException ex) { - throw new RuntimeException("Unable to invoke activate/deactivate method for class " + throw new RuntimeException("Unable to invoke method '" + method.getName() + "' for class " + target.getClass().getName(), ex); } catch (IllegalArgumentException ex) { - throw new RuntimeException("Unable to invoke activate/deactivate method for class " + throw new RuntimeException("Unable to invoke method '" + method.getName() + "' for class " + target.getClass().getName(), ex); } catch (InvocationTargetException ex) { - throw new RuntimeException("Unable to invoke activate/deactivate method for class " + throw new RuntimeException("Unable to invoke method '" + method.getName() + "' for class " + target.getClass().getName(), ex.getCause()); } } @@ -351,8 +351,8 @@ final class OsgiServiceUtil { } } - throw new RuntimeException((bind ? "Bind" : "Unbind") + "method with name " + methodName + " not found " - + "for reference '" + reference.getName() + "' for class {}" + targetClass.getName()); + throw new RuntimeException((bind ? "Bind" : "Unbind") + " method with name " + methodName + " not found " + + "for reference '" + reference.getName() + "' for class " + targetClass.getName()); } /** diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesTest.java new file mode 100644 index 0000000..9900f01 --- /dev/null +++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesTest.java @@ -0,0 +1,169 @@ +/* + * 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.sling.testing.mock.osgi; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.Service3; +import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface1; +import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface1Optional; +import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface2; +import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface3; +import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceSuperInterface3; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + +import com.google.common.collect.ImmutableSet; + +@RunWith(MockitoJUnitRunner.class) +public class MockBundleContextDynamicReferncesTest { + + private BundleContext bundleContext; + private Service3 service; + private ServiceRegistration reg1a; + private ServiceRegistration reg2a; + + @Mock + private ServiceInterface1 dependency1a; + @Mock + private ServiceInterface1 dependency1b; + @Mock + private ServiceInterface1Optional dependency1aOptional; + @Mock + private ServiceInterface1Optional dependency1bOptional; + @Mock + private ServiceInterface2 dependency2a; + @Mock + private ServiceInterface2 dependency2b; + @Mock + private ServiceSuperInterface3 dependency3a; + @Mock + private ServiceSuperInterface3 dependency3b; + + @Before + public void setUp() { + bundleContext = MockOsgi.newBundleContext(); + + // setup service instance with only minimum mandatory references + reg1a = bundleContext.registerService(ServiceInterface1.class.getName(), dependency1a, null); + reg2a = bundleContext.registerService(ServiceInterface2.class.getName(), dependency2a, null); + + service = new Service3(); + MockOsgi.injectServices(service, bundleContext); + MockOsgi.activate(service); + bundleContext.registerService(Service3.class.getName(), service, null); + + assertDependency1(dependency1a); + assertDependency1Optional(null); + assertDependencies2(dependency2a); + assertDependencies3(); + } + + @Test + public void testAddRemoveOptionalUnaryService() { + ServiceRegistration reg1aOptional = bundleContext.registerService(ServiceInterface1Optional.class.getName(), dependency1aOptional, null); + assertDependency1Optional(dependency1aOptional); + + reg1aOptional.unregister(); + assertDependency1Optional(null); + } + + public void testAddOptionalUnaryService_TooMany() { + bundleContext.registerService(ServiceInterface1Optional.class.getName(), dependency1aOptional, null); + assertDependency1Optional(dependency1aOptional); + + // in real OSGi this should fail - but this is not covered by the current implementation. so test the real implementation here. + bundleContext.registerService(ServiceInterface1Optional.class.getName(), dependency1bOptional, null); + assertDependency1Optional(dependency1bOptional); + } + + @Test(expected = ReferenceViolationException.class) + public void testAddMandatoryUnaryService_TooMany() { + bundleContext.registerService(ServiceInterface1.class.getName(), dependency1b, null); + } + + @Test(expected = ReferenceViolationException.class) + public void testRemoveMandatoryUnaryService_TooMany() { + reg1a.unregister(); + } + + @Test + public void testAddRemoveOptionalMultipleService() { + ServiceRegistration reg3a = bundleContext.registerService(ServiceInterface3.class.getName(), dependency3a, null); + assertDependencies3(dependency3a); + + ServiceRegistration reg3b = bundleContext.registerService(ServiceInterface3.class.getName(), dependency3b, null); + assertDependencies3(dependency3a, dependency3b); + + reg3a.unregister(); + assertDependencies3(dependency3b); + + reg3b.unregister(); + assertDependencies3(); + } + + @Test + public void testAddRemoveMandatoryMultipleService() { + ServiceRegistration reg2b = bundleContext.registerService(ServiceInterface2.class.getName(), dependency2b, null); + assertDependencies2(dependency2a, dependency2b); + + reg2b.unregister(); + assertDependencies2(dependency2a); + + // in real OSGi this should fail - but this is not covered by the current implementation. so test the real implementation here. + reg2a.unregister(); + assertDependencies2(); + } + + private void assertDependency1(ServiceInterface1 instance) { + if (instance == null) { + assertNull(service.getReference1()); + } + else { + assertSame(instance, service.getReference1()); + } + } + + private void assertDependency1Optional(ServiceInterface1Optional instance) { + if (instance == null) { + assertNull(service.getReference1Optional()); + } + else { + assertSame(instance, service.getReference1Optional()); + } + } + + private void assertDependencies2(ServiceInterface2... instances) { + assertEquals(ImmutableSet.<ServiceInterface2>copyOf(instances), + ImmutableSet.<ServiceInterface2>copyOf(service.getReferences2())); + } + + private void assertDependencies3(ServiceSuperInterface3... instances) { + assertEquals(ImmutableSet.<ServiceSuperInterface3>copyOf(instances), + ImmutableSet.<ServiceSuperInterface3>copyOf(service.getReferences3())); + } + +} diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtilTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtilTest.java index 9db1197..4611d25 100644 --- a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtilTest.java +++ b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtilTest.java @@ -65,9 +65,9 @@ public class OsgiMetadataUtilTest { public void testReferences() { OsgiMetadata metadata = OsgiMetadataUtil.getMetadata(OsgiServiceUtilTest.Service3.class); List<Reference> references = metadata.getReferences(); - assertEquals(3, references.size()); + assertEquals(4, references.size()); - Reference ref1 = references.get(0); + Reference ref1 = references.get(2); assertEquals("reference2", ref1.getName()); assertEquals("org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface2", ref1.getInterfaceType()); assertEquals(ReferenceCardinality.MANDATORY_MULTIPLE, ref1.getCardinality()); diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java index f71ed38..abed04a 100644 --- a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java +++ b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java @@ -147,6 +147,10 @@ public class OsgiServiceUtilTest { // no methods } + public interface ServiceInterface1Optional { + // no methods + } + public interface ServiceInterface2 { // no methods } @@ -179,6 +183,8 @@ public class OsgiServiceUtilTest { @Reference private ServiceInterface1 reference1; + @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY) + private ServiceInterface1Optional reference1Optional; private List<ServiceReference> references2 = new ArrayList<ServiceReference>(); @@ -210,6 +216,10 @@ public class OsgiServiceUtilTest { return this.reference1; } + public ServiceInterface1Optional getReference1Optional() { + return this.reference1Optional; + } + public List<ServiceInterface2> getReferences2() { List<ServiceInterface2> services = new ArrayList<ServiceInterface2>(); for (ServiceReference serviceReference : references2) { @@ -234,6 +244,14 @@ public class OsgiServiceUtilTest { return config; } + protected void bindReference1Optional(ServiceInterface1Optional service) { + reference1Optional = service; + } + + protected void unbindReference1Optional(ServiceInterface1Optional service) { + reference1Optional = null; + } + protected void bindReference1(ServiceInterface1 service) { reference1 = service; } diff --git a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml index d678f77..ab82616 100644 --- a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml +++ b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml @@ -20,8 +20,9 @@ <scr:component name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3" activate="activate" deactivate="deactivate" modified="modified"> <implementation class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3"/> <property name="service.pid" value="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3"/> - <reference name="reference2" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface2" cardinality="1..n" policy="static" bind="bindReference2" unbind="unbindReference2"/> <reference name="reference1" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface1" cardinality="1..1" policy="static" bind="bindReference1" unbind="unbindReference1"/> + <reference name="reference1Optional" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface1Optional" cardinality="0..1" policy="static" bind="bindReference1Optional" unbind="unbindReference1Optional"/> + <reference name="reference2" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface2" cardinality="1..n" policy="static" bind="bindReference2" unbind="unbindReference2"/> <reference name="reference3" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface3" cardinality="0..n" policy="static" bind="bindReference3" unbind="unbindReference3"/> </scr:component> <scr:component name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service4"> -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
