This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.serviceusermapper-1.2.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-serviceusermapper.git
commit d3e3149870c5d2475e95fe6874c87c475ead25fa Author: Marius Petria <[email protected]> AuthorDate: Fri Feb 27 11:48:32 2015 +0000 SLING-4312: Register an osgi service for each available service user (+ change tests to mockito) git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/serviceusermapper@1662665 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 23 ++- .../serviceusermapping/ServiceUserMapping.java | 29 ++++ .../sling/serviceusermapping/impl/Mapping.java | 50 ++++++- .../impl/ServiceUserMapperImpl.java | 68 ++++++++- .../impl/ServiceUserMappingBundleFilter.java | 94 ++++++++++++ .../sling/serviceusermapping/package-info.java | 2 +- .../impl/ServiceUserMapperImplTest.java | 164 +++++++++++++++++---- .../impl/ServiceUserMappingBundleFilterTest.java | 116 +++++++++++++++ 8 files changed, 500 insertions(+), 46 deletions(-) diff --git a/pom.xml b/pom.xml index a543e21..8a67990 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,8 @@ <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> + <version>4.3.0</version> + <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> @@ -123,19 +125,7 @@ <artifactId>junit</artifactId> <scope>test</scope> </dependency> - <dependency> - <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.commons.testing</artifactId> - <version>2.0.6</version> - <scope>test</scope> - <exclusions> - <!-- slf4j simple implementation logs INFO + higher to stdout (we don't want that behaviour) --> - <exclusion> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-simple</artifactId> - </exclusion> - </exclusions> - </dependency> + <!-- using log4j under slf4j to allow fine-grained logging config (see src/test/resources/log4j.properties) --> <dependency> <groupId>org.slf4j</groupId> @@ -149,6 +139,13 @@ <version>1.2.13</version> <scope>test</scope> </dependency> + + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>1.9.5</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapping.java b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapping.java new file mode 100644 index 0000000..93fe6eb --- /dev/null +++ b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapping.java @@ -0,0 +1,29 @@ +package org.apache.sling.serviceusermapping; + +import aQute.bnd.annotation.ProviderType; + +/** + * The <code>ServiceUserMapping</code> service can be used to retrieve an already registered service user mapping. + * A service reference targeting a service user mapping will be satisfied only when <code>ServiceUserMapper.getServiceUserID</code> + * will return the registered user ID in that bundle. + * For example setting the reference target to "(subServiceName=mySubService)" ensures that your component only starts when the subService is available. + * The subServiceName will not be set for mappings that do not have one, and those can be referenced with a negating target "(!(subServiceName=*))". + * Trying to reference a sub service from a bundle for which it was not registered for will not work. + */ +@ProviderType +public interface ServiceUserMapping { + + + /** + * The name of the osgi property holding the sub service name. + */ + static String SUBSERVICENAME = "subServiceName"; + + + /** + * Returns the sub service name for this mapping. + * + * @return The sub service name for this mapping. This can be {@code null} if no sub service name is configured for this mapping. + */ + String getSubServiceName(); +} diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java b/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java index 07ca263..cdbe4dc 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java +++ b/src/main/java/org/apache/sling/serviceusermapping/impl/Mapping.java @@ -18,11 +18,19 @@ */ package org.apache.sling.serviceusermapping.impl; +import org.apache.sling.serviceusermapping.ServiceUserMapping; + /** * The <code>Mapping</code> class defines the mapping of a service's name and * optional service information to a user name. */ -class Mapping { +class Mapping implements ServiceUserMapping, Comparable<Mapping> { + + + /** + * The name of the osgi property holding the service name. + */ + static String SERVICENAME = "serviceName"; private final String serviceName; @@ -93,4 +101,44 @@ class Mapping { return "Mapping [serviceName=" + serviceName + ", subServiceName=" + subServiceName + ", userName=" + userName + "]"; } + + public String getServiceName() { + return serviceName; + } + + public String getSubServiceName() { + return subServiceName; + } + + + public int compareTo(Mapping o) { + if (o == null) { + return -1; + } + + int result = compare(this.serviceName, o.serviceName); + if (result == 0) { + result = compare(this.subServiceName, o.subServiceName); + if (result == 0) { + result = compare(this.userName, o.userName); + } + } + return result; + } + + private int compare(String str1, String str2) { + if (str1 == str2) { + return 0; + } + + if (str1 == null) { + return -1; + } + + if (str2 == null) { + return 1; + } + + return str1.hashCode() - str2.hashCode(); + } } diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java index 914a642..9cbe000 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java +++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java @@ -19,13 +19,22 @@ package org.apache.sling.serviceusermapping.impl; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; +import java.util.Dictionary; import java.util.HashMap; +import java.util.Hashtable; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Vector; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Modified; import org.apache.felix.scr.annotations.Property; @@ -38,8 +47,11 @@ import org.apache.felix.scr.annotations.Service; import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.serviceusermapping.ServiceUserMapper; import org.apache.sling.serviceusermapping.ServiceUserValidator; +import org.apache.sling.serviceusermapping.ServiceUserMapping; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; +import org.osgi.framework.ServiceRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -95,9 +107,13 @@ public class ServiceUserMapperImpl implements ServiceUserMapper { private Vector <ServiceUserValidator> validators = new Vector<ServiceUserValidator>(); + private SortedMap<Mapping, ServiceRegistration> activeMappingRegistrations = new TreeMap<Mapping, ServiceRegistration>(); + + private BundleContext bundleContext; + @Activate @Modified - void configure(final Map<String, Object> config) { + void configure(BundleContext bundleContext, final Map<String, Object> config) { final String[] props = PropertiesUtil.toStringArray(config.get(PROP_SERVICE2USER_MAPPING), PROP_SERVICE2USER_MAPPING_DEFAULT); @@ -116,9 +132,18 @@ public class ServiceUserMapperImpl implements ServiceUserMapper { this.globalServiceUserMappings = mappings.toArray(new Mapping[mappings.size()]); this.defaultUser = PropertiesUtil.toString(config.get(PROP_DEFAULT_USER), PROP_DEFAULT_USER_DEFAULT); synchronized ( this.amendments ) { + this.bundleContext = bundleContext; this.updateMappings(); } } + + @Deactivate + void deactivate() { + synchronized ( this.amendments) { + updateServiceMappings(new ArrayList<Mapping>()); + bundleContext = null; + } + } /** * bind the serviceUserValidator @@ -185,7 +210,48 @@ public class ServiceUserMapperImpl implements ServiceUserMapper { mappings.add(m); } } + + activeMappings = mappings.toArray(new Mapping[mappings.size()]); + + updateServiceMappings(mappings); + + } + + + void updateServiceMappings(List<Mapping> newMappings) { + + // do not do anything if not activated + if (bundleContext == null) { + return; + } + + SortedSet<Mapping> orderedActiveMappings = new TreeSet<Mapping>(newMappings); + + + Iterator<Map.Entry<Mapping, ServiceRegistration>> it = activeMappingRegistrations.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry<Mapping, ServiceRegistration> registrationEntry = it.next(); + + if (!orderedActiveMappings.contains(registrationEntry.getKey())) { + registrationEntry.getValue().unregister(); + it.remove(); + } + } + + + for (Mapping mapping: orderedActiveMappings) { + if (!activeMappingRegistrations.containsKey(mapping)) { + Dictionary<String, Object> properties = new Hashtable<String, Object>(); + if (mapping.getSubServiceName() != null) { + properties.put(ServiceUserMapping.SUBSERVICENAME, mapping.getSubServiceName()); + } + + properties.put(Mapping.SERVICENAME, mapping.getServiceName()); + ServiceRegistration registration = bundleContext.registerService(ServiceUserMapping.class.getName(), mapping, properties); + activeMappingRegistrations.put(mapping, registration); + } + } } private String internalGetUserId(String serviceName, String subServiceName) { diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappingBundleFilter.java b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappingBundleFilter.java new file mode 100644 index 0000000..81bf8e6 --- /dev/null +++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappingBundleFilter.java @@ -0,0 +1,94 @@ +/* + * 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.serviceusermapping.impl; + +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Service; +import org.apache.sling.serviceusermapping.ServiceUserMapping; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.hooks.service.EventListenerHook; +import org.osgi.framework.hooks.service.FindHook; +import org.osgi.framework.hooks.service.ListenerHook; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; + +@Component +@Service(value = {EventListenerHook.class, FindHook.class} ) +/** + * The <code>ServiceUserMappingBundleFilter</code> only allows the bundle for which the service mapping is available to see it. + */ +public class ServiceUserMappingBundleFilter implements EventListenerHook, FindHook { + + public void event(ServiceEvent serviceEvent, Map map) { + + ServiceReference serviceReference = serviceEvent.getServiceReference(); + if (isServiceMappingReference(serviceReference)) { + Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME); + + if (serviceName != null && serviceName instanceof String) { + Iterator<Map.Entry<BundleContext, Collection<ListenerHook.ListenerInfo>>> it = map.entrySet().iterator(); + while (it.hasNext()) { + BundleContext ctx = it.next().getKey(); + + String bundleName = ctx.getBundle().getSymbolicName(); + if (!serviceName.equals(bundleName)) { + it.remove(); + } + } + } + } + } + + public void find(BundleContext bundleContext, String name, String filter, boolean allServices, + Collection references) { + String bundleName = bundleContext.getBundle().getSymbolicName(); + + Iterator<ServiceReference> it = references.iterator(); + while (it.hasNext()) { + ServiceReference serviceReference = it.next(); + if (isServiceMappingReference(serviceReference)) { + Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME); + + if (serviceName != null && !serviceName.equals(bundleName)) { + it.remove(); + } + } + } + } + + private static boolean isServiceMappingReference(ServiceReference serviceReference) { + Object objectClass = serviceReference.getProperty(Constants.OBJECTCLASS); + for (Object o : (Object[]) objectClass) { + if (ServiceUserMapping.class.getName().equals(o)) { + return true; + } + } + return false; + } + + + +} diff --git a/src/main/java/org/apache/sling/serviceusermapping/package-info.java b/src/main/java/org/apache/sling/serviceusermapping/package-info.java index c05c7ae..8eadc1a 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/package-info.java +++ b/src/main/java/org/apache/sling/serviceusermapping/package-info.java @@ -17,7 +17,7 @@ * under the License. */ -@Version("1.1") +@Version("1.2") @Export(optional = "provide:=true") package org.apache.sling.serviceusermapping; diff --git a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java index ca5ba7f..fcae724 100644 --- a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java +++ b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java @@ -18,17 +18,26 @@ */ package org.apache.sling.serviceusermapping.impl; +import java.util.Dictionary; import java.util.HashMap; -import java.util.Hashtable; import java.util.Map; import junit.framework.TestCase; -import org.apache.sling.commons.testing.osgi.MockBundle; +import org.apache.sling.serviceusermapping.ServiceUserMapping; import org.apache.sling.serviceusermapping.ServiceUserValidator; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ServiceUserMapperImplTest { private static final String BUNDLE_SYMBOLIC1 = "bundle1"; @@ -47,34 +56,21 @@ public class ServiceUserMapperImplTest { private static final String ANOTHER_SUB = "another_sub"; - private static final Bundle BUNDLE1 = new MockBundle(10) { - @Override - public String getSymbolicName() { - return BUNDLE_SYMBOLIC1; - }; + private static final Bundle BUNDLE1; - @Override - public java.util.Dictionary<?, ?> getHeaders() { - return new Hashtable<String, Object>(); - }; + private static final Bundle BUNDLE2; + + + static { + BUNDLE1 = mock(Bundle.class); + when(BUNDLE1.getSymbolicName()).thenReturn(BUNDLE_SYMBOLIC1); + + BUNDLE2 = mock(Bundle.class); + when(BUNDLE2.getSymbolicName()).thenReturn(BUNDLE_SYMBOLIC2); + } - @Override - public java.util.Dictionary<?, ?> getHeaders(String locale) { - return getHeaders(); - }; - }; - private static final Bundle BUNDLE2 = new MockBundle(10) { - @Override - public String getSymbolicName() { - return BUNDLE_SYMBOLIC2; - }; - @Override - public java.util.Dictionary<?, ?> getHeaders(String locale) { - return getHeaders(); - }; - }; @Test public void test_getServiceUserID() { @@ -92,7 +88,7 @@ public class ServiceUserMapperImplTest { }; final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); - sum.configure(config); + sum.configure(null, config); TestCase.assertEquals(SAMPLE, sum.getServiceUserID(BUNDLE1, null)); TestCase.assertEquals(ANOTHER, sum.getServiceUserID(BUNDLE2, null)); @@ -118,7 +114,7 @@ public class ServiceUserMapperImplTest { }; final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); - sum.configure(config); + sum.configure(null, config); ServiceUserValidator serviceUserValidator = new ServiceUserValidator() { public boolean isValid(String serviceUserId, String serviceName, @@ -153,7 +149,7 @@ public class ServiceUserMapperImplTest { }; final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); - sum.configure(config); + sum.configure(null, config); final MappingConfigAmendment mca1 = new MappingConfigAmendment(); @SuppressWarnings("serial") final Map<String, Object> mca1Config = new HashMap<String, Object>() { @@ -195,7 +191,7 @@ public class ServiceUserMapperImplTest { }; final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); - sum.configure(config); + sum.configure(null, config); final MappingConfigAmendment mca1 = new MappingConfigAmendment(); @SuppressWarnings("serial") @@ -221,4 +217,112 @@ public class ServiceUserMapperImplTest { TestCase.assertEquals(ANOTHER_SUB, sum.getServiceUserID(BUNDLE2, "")); } + + + + @Test + public void test_amendmentServiceUserMapping() { + @SuppressWarnings("serial") + Map<String, Object> config = new HashMap<String, Object>() { + { + put("user.mapping", new String[] { + BUNDLE_SYMBOLIC1 + "=" + SAMPLE, // + BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, // + }); + put("user.default", NONE); + } + }; + + final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); + final ServiceRegistrationContextHelper context = new ServiceRegistrationContextHelper(); + sum.configure(context.getBundleContext(), config); + + TestCase.assertEquals(2, context.getRegistrations(ServiceUserMapping.class.getName()).size()); + + final MappingConfigAmendment mca1 = new MappingConfigAmendment(); + @SuppressWarnings("serial") + final Map<String, Object> mca1Config = new HashMap<String, Object>() { + { + put("user.mapping", new String [] {BUNDLE_SYMBOLIC2 + "=" + ANOTHER}); + put(Constants.SERVICE_ID, 1L); + put(Constants.SERVICE_RANKING, 100); + } + }; + mca1.configure(mca1Config); + sum.bindAmendment(mca1, mca1Config); + + TestCase.assertEquals(3, context.getRegistrations(ServiceUserMapping.class.getName()).size()); + + final MappingConfigAmendment mca2 = new MappingConfigAmendment(); + @SuppressWarnings("serial") + final Map<String, Object> mca2Config = new HashMap<String, Object>() { + { + put("user.mapping", new String [] {BUNDLE_SYMBOLIC2 + ":" + SUB + "=" + ANOTHER_SUB}); + put(Constants.SERVICE_ID, 2L); + put(Constants.SERVICE_RANKING, 200); + } + }; + mca2.configure(mca2Config); + sum.bindAmendment(mca2, mca2Config); + + TestCase.assertEquals(4, context.getRegistrations(ServiceUserMapping.class.getName()).size()); + + sum.unbindAmendment(mca1, mca1Config); + + TestCase.assertEquals(3, context.getRegistrations(ServiceUserMapping.class.getName()).size()); + } + + + private class ServiceRegistrationContextHelper { + + + final BundleContext bundleContext = mock(BundleContext.class); + final Map<String, Map<Object, Dictionary>> registrations = new HashMap<String, Map<Object, Dictionary>>(); + + public ServiceRegistrationContextHelper() { + when(bundleContext.registerService(any(String.class), any(Object.class), any(Dictionary.class))) + .then(new Answer<ServiceRegistration>() { + public ServiceRegistration answer(InvocationOnMock invocationOnMock) throws Throwable { + + Object[] arguments = invocationOnMock.getArguments(); + return registerService((String) arguments[0], arguments[1], (Dictionary) arguments[2]); + } + }); + } + + private ServiceRegistration registerService(String string, Object o, Dictionary dictionary) { + if (!registrations.containsKey(string)) { + registrations.put(string, new HashMap<Object, Dictionary>()); + } + final Map<Object, Dictionary> serviceRegistrations = registrations.get(string); + serviceRegistrations.put(o, dictionary); + + final Object registeredObject = o; + + + return new ServiceRegistration() { + public ServiceReference getReference() { + return null; + } + + public void setProperties(Dictionary dictionary) { + + } + + public void unregister() { + serviceRegistrations.remove(registeredObject); + } + }; + } + + public Map<Object, Dictionary> getRegistrations(String name) { + return registrations.get(name); + } + + public BundleContext getBundleContext() { + return bundleContext; + } + + } + } diff --git a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappingBundleFilterTest.java b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappingBundleFilterTest.java new file mode 100644 index 0000000..5e3d6ae --- /dev/null +++ b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappingBundleFilterTest.java @@ -0,0 +1,116 @@ +/* + * 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.serviceusermapping.impl; + + +import junit.framework.TestCase; +import org.apache.sling.serviceusermapping.ServiceUserMapping; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceReference; +import org.osgi.framework.hooks.service.EventListenerHook; +import org.osgi.framework.hooks.service.FindHook; +import org.osgi.framework.hooks.service.ListenerHook; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Test reference and bundle filtering based on <code>Mapping.SERVICENAME</code> + */ +public class ServiceUserMappingBundleFilterTest { + + final static String BUNDLE1 = "bundle1"; + final static String BUNDLE2 = "bundle2"; + + + final static BundleContext bundleContext1; + final static BundleContext bundleContext2; + + static { + bundleContext1 = mock(BundleContext.class); + Bundle bundle1 = mock(Bundle.class); + when(bundleContext1.getBundle()).thenReturn(bundle1); + when(bundle1.getSymbolicName()).thenReturn(BUNDLE1); + + + bundleContext2 = mock(BundleContext.class); + Bundle bundle2 = mock(Bundle.class); + when(bundleContext2.getBundle()).thenReturn(bundle2); + when(bundle2.getSymbolicName()).thenReturn(BUNDLE2); + + } + + + + + @Test + public void testEvent() { + Map<BundleContext, Collection<ListenerHook.ListenerInfo>> map = new HashMap<BundleContext, Collection<ListenerHook.ListenerInfo>>(); + + map.put(bundleContext1, new ArrayList<ListenerHook.ListenerInfo>()); + map.put(bundleContext2, new ArrayList<ListenerHook.ListenerInfo>()); + + ServiceEvent serviceEvent = mock(ServiceEvent.class); + ServiceReference serviceReference = mock(ServiceReference.class); + when(serviceEvent.getServiceReference()).thenReturn(serviceReference); + when(serviceReference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[]{ServiceUserMapping.class.getName()}); + when(serviceReference.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE1); + + + EventListenerHook eventListenerHook = new ServiceUserMappingBundleFilter(); + eventListenerHook.event(serviceEvent, map); + + TestCase.assertEquals(1, map.size()); + TestCase.assertTrue(map.containsKey(bundleContext1)); + + } + + @Test + public void testFind() { + List collection = new ArrayList<ServiceReference>(); + + ServiceReference serviceReference1 = mock(ServiceReference.class); + ServiceReference serviceReference2 = mock(ServiceReference.class); + collection.add(serviceReference1); + collection.add(serviceReference2); + + when(serviceReference1.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE1); + when(serviceReference1.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[]{ServiceUserMapping.class.getName()}); + + when(serviceReference2.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE2); + when(serviceReference2.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[]{ServiceUserMapping.class.getName()}); + + FindHook findHook = new ServiceUserMappingBundleFilter(); + findHook.find(bundleContext1, null, null, false, collection); + + TestCase.assertEquals(1, collection.size()); + TestCase.assertTrue(collection.contains(serviceReference1)); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
