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.6 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-serviceusermapper.git
commit dfbde948459f5c88247463d438895144aa134275 Author: Carsten Ziegeler <[email protected]> AuthorDate: Thu Feb 23 12:29:06 2017 +0000 SLING-6555 : Migrate to R6 annotations, clean up dependencies git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/serviceusermapper@1784117 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 69 ++----- .../serviceusermapping/ServiceUserMapped.java | 2 +- .../serviceusermapping/ServiceUserMapper.java | 3 +- .../serviceusermapping/ServiceUserValidator.java | 2 +- .../impl/MappingConfigAmendment.java | 67 ++++--- .../impl/MappingInventoryPrinter.java | 31 ++-- .../impl/ServiceUserMappedBundleFilter.java | 7 +- .../impl/ServiceUserMapperImpl.java | 73 +++----- .../sling/serviceusermapping/package-info.java | 6 +- .../impl/ServiceUserMapperImplTest.java | 205 ++++++++++----------- 10 files changed, 190 insertions(+), 275 deletions(-) diff --git a/pom.xml b/pom.xml index d3a6b4a..59f901b 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ <parent> <groupId>org.apache.sling</groupId> <artifactId>sling</artifactId> - <version>26</version> + <version>29</version> <relativePath /> </parent> @@ -58,14 +58,16 @@ <configuration> <instructions> <Import-Package> - org.apache.sling.commons.json, org.apache.felix.inventory;resolution:=optional, * </Import-Package> - <DynamicImport-Package> + <DynamicImport-Package> org.apache.felix.inventory </DynamicImport-Package> - </instructions> + <Embed-Dependency> + org.apache.felix.utils;inline=org/apache/felix/utils/json/JSONWriter** + </Embed-Dependency> + </instructions> </configuration> </plugin> </plugins> @@ -78,18 +80,12 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.commons.osgi</artifactId> - <version>2.1.0</version> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.utils</artifactId> + <version>1.9.0</version> <scope>provided</scope> </dependency> <dependency> - <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.commons.json</artifactId> - <version>2.0.6</version> - <scope>provided</scope> - </dependency> - <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.inventory</artifactId> <version>1.0.0</version> @@ -97,69 +93,34 @@ </dependency> <dependency> - <groupId>javax.jcr</groupId> - <artifactId>jcr</artifactId> - <version>2.0</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.apache.jackrabbit</groupId> - <artifactId>jackrabbit-api</artifactId> - <version>2.4.0</version> - <scope>provided</scope> - </dependency> - - <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.osgi</groupId> - <artifactId>org.osgi.core</artifactId> - <version>4.3.0</version> - <scope>provided</scope> + <artifactId>osgi.core</artifactId> </dependency> <dependency> <groupId>org.osgi</groupId> - <artifactId>org.osgi.compendium</artifactId> + <artifactId>org.osgi.service.event</artifactId> + <version>1.3.1</version> + <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> - <artifactId>servlet-api</artifactId> - </dependency> - - <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.scr.annotations</artifactId> - </dependency> - <dependency> - <groupId>biz.aQute</groupId> - <artifactId>bndlib</artifactId> - <scope>provided</scope> + <artifactId>javax.servlet-api</artifactId> </dependency> <!-- Testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <scope>test</scope> </dependency> - - <!-- using log4j under slf4j to allow fine-grained logging config (see src/test/resources/log4j.properties) --> <dependency> <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - <version>1.5.0</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.13</version> - <scope>test</scope> + <artifactId>slf4j-simple</artifactId> </dependency> - <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> diff --git a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java index 949a1bf..921dffb 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java +++ b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapped.java @@ -18,7 +18,7 @@ */ package org.apache.sling.serviceusermapping; -import aQute.bnd.annotation.ProviderType; +import org.osgi.annotation.versioning.ProviderType; /** * The <code>ServiceUserMapped</code> is a marker service that can be used to ensure that there is an already registered mapping for a certain service/subService. diff --git a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java index 21e801d..d8e6701 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java +++ b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserMapper.java @@ -18,10 +18,9 @@ */ package org.apache.sling.serviceusermapping; +import org.osgi.annotation.versioning.ProviderType; import org.osgi.framework.Bundle; -import aQute.bnd.annotation.ProviderType; - /** * The <code>ServiceUserMapper</code> service can be used to map a service * provided by a bundle to the ID of a user account used to access the diff --git a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserValidator.java b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserValidator.java index 7930582..eaa77f7 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/ServiceUserValidator.java +++ b/src/main/java/org/apache/sling/serviceusermapping/ServiceUserValidator.java @@ -16,7 +16,7 @@ */ package org.apache.sling.serviceusermapping; -import aQute.bnd.annotation.ConsumerType; +import org.osgi.annotation.versioning.ConsumerType; /** * The {@code ServiceUserValidator} allows to implement validation of configured diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/MappingConfigAmendment.java b/src/main/java/org/apache/sling/serviceusermapping/impl/MappingConfigAmendment.java index 9b981e9..1d10c38 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/impl/MappingConfigAmendment.java +++ b/src/main/java/org/apache/sling/serviceusermapping/impl/MappingConfigAmendment.java @@ -19,47 +19,42 @@ package org.apache.sling.serviceusermapping.impl; import java.util.ArrayList; -import java.util.Map; -import org.apache.felix.scr.annotations.Activate; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.ConfigurationPolicy; -import org.apache.felix.scr.annotations.Modified; -import org.apache.felix.scr.annotations.Properties; -import org.apache.felix.scr.annotations.Property; -import org.apache.felix.scr.annotations.PropertyUnbounded; -import org.apache.felix.scr.annotations.Service; -import org.apache.sling.commons.osgi.PropertiesUtil; -import org.osgi.framework.Constants; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.ConfigurationPolicy; +import org.osgi.service.component.annotations.Modified; +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.Designate; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@Component(metatype=true, - name="org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended", - label="Apache Sling Service User Mapper Service Amendment", - description="An amendment mapping for the user mapping service.", - configurationFactory=true, - policy=ConfigurationPolicy.REQUIRE) -@Service(value={MappingConfigAmendment.class}) -@Properties({ - @Property(name=Constants.SERVICE_RANKING, intValue=0, propertyPrivate=false, - label="Ranking", - description="Amendments are processed in order of their ranking, an amendment with a higher ranking has" + - " precedence over a mapping with a lower ranking."), - @Property(name="webconsole.configurationFactory.nameHint", value="Mapping: {user.mapping}") -}) +@Designate(factory=true, ocd=MappingConfigAmendment.Config.class) +@Component(name = "org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended", + configurationPolicy=ConfigurationPolicy.REQUIRE, + service={MappingConfigAmendment.class}, + property= { + "webconsole.configurationFactory.nameHint=Mapping: {user.mapping}", + }) public class MappingConfigAmendment implements Comparable<MappingConfigAmendment> { - @Property( - label = "Service Mappings", + @ObjectClassDefinition(name ="Apache Sling Service User Mapper Service Amendment", + description="An amendment mapping for the user mapping service.") + public @interface Config { + + @AttributeDefinition(name = "Ranking", + description="Amendments are processed in order of their ranking, an amendment with a higher ranking has" + + " precedence over a mapping with a lower ranking.") + int service_ranking() default 0; + + @AttributeDefinition(name = "Service Mappings", description = "Provides mappings from service name to user names. " + "Each entry is of the form 'bundleId [ \":\" subServiceName ] \"=\" userName' " + "where bundleId and subServiceName identify the service and userName " - + "defines the name of the user to provide to the service. Invalid entries are logged and ignored.", - unbounded = PropertyUnbounded.ARRAY) - private static final String PROP_SERVICE2USER_MAPPING = "user.mapping"; - - private static final String[] PROP_SERVICE2USER_MAPPING_DEFAULT = {}; + + "defines the name of the user to provide to the service. Invalid entries are logged and ignored.") + String[] user_mapping() default {}; + } /** default logger */ private final Logger logger = LoggerFactory.getLogger(getClass()); @@ -70,9 +65,8 @@ public class MappingConfigAmendment implements Comparable<MappingConfigAmendment @Activate @Modified - void configure(final Map<String, Object> config) { - final String[] props = PropertiesUtil.toStringArray(config.get(PROP_SERVICE2USER_MAPPING), - PROP_SERVICE2USER_MAPPING_DEFAULT); + void configure(final Config config) { + final String[] props = config.user_mapping(); final ArrayList<Mapping> mappings = new ArrayList<Mapping>(props.length); for (final String prop : props) { @@ -87,13 +81,14 @@ public class MappingConfigAmendment implements Comparable<MappingConfigAmendment } this.serviceUserMappings = mappings.toArray(new Mapping[mappings.size()]); - this.serviceRanking = PropertiesUtil.toInteger(config.get(Constants.SERVICE_RANKING), 0); + this.serviceRanking = config.service_ranking(); } public Mapping[] getServiceUserMappings() { return this.serviceUserMappings; } + @Override public int compareTo(final MappingConfigAmendment o) { // Sort by rank in descending order. if ( this.serviceRanking > o.serviceRanking ) { diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java b/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java index b431b19..e3488f5 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java +++ b/src/main/java/org/apache/sling/serviceusermapping/impl/MappingInventoryPrinter.java @@ -18,6 +18,7 @@ */ package org.apache.sling.serviceusermapping.impl; +import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; @@ -27,23 +28,18 @@ import java.util.TreeMap; import org.apache.felix.inventory.Format; import org.apache.felix.inventory.InventoryPrinter; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Properties; -import org.apache.felix.scr.annotations.Property; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; -import org.apache.sling.commons.json.JSONException; -import org.apache.sling.commons.json.io.JSONWriter; +import org.apache.felix.utils.json.JSONWriter; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; /** InventoryPrinter for service user mappings */ -@Component -@Service(value = InventoryPrinter.class) -@Properties({ - @Property(name = InventoryPrinter.FORMAT, value = { "JSON", "TEXT" }), - @Property(name = InventoryPrinter.NAME, value = "slingserviceusers"), - @Property(name = InventoryPrinter.TITLE, value = "Sling Service User Mappings"), - @Property(name = InventoryPrinter.WEBCONSOLE, boolValue = true) -}) +@Component(service = InventoryPrinter.class, + property = { + InventoryPrinter.FORMAT + "=JSON", + InventoryPrinter.FORMAT + "=TEXT", + InventoryPrinter.TITLE + "=Sling Service User Mappings", + InventoryPrinter.WEBCONSOLE + ":Boolean=true" + }) public class MappingInventoryPrinter implements InventoryPrinter { @Reference @@ -80,7 +76,7 @@ public class MappingInventoryPrinter implements InventoryPrinter { return result; } - private void asJSON(JSONWriter w, Mapping m) throws JSONException { + private void asJSON(JSONWriter w, Mapping m) throws IOException { w.object(); w.key("serviceName").value(m.getServiceName()); w.key("subServiceName").value(m.getSubServiceName()); @@ -88,12 +84,11 @@ public class MappingInventoryPrinter implements InventoryPrinter { w.endObject(); } - private void renderJson(PrintWriter out) throws JSONException { + private void renderJson(PrintWriter out) throws IOException { final List<Mapping> data = mapper.getActiveMappings(); final Map<String, List<Mapping>> byUser = getMappingsByUser(data); final JSONWriter w = new JSONWriter(out); - w.setTidy(true); w.object(); w.key("title").value("Service User Mappings"); w.key("mappingsCount").value(data.size()); diff --git a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java index c2179d2..a8b0719 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java +++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMappedBundleFilter.java @@ -23,8 +23,6 @@ import java.util.Collection; import java.util.Iterator; import java.util.Map; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Service; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.ServiceEvent; @@ -32,12 +30,13 @@ 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 org.osgi.service.component.annotations.Component; -@Component(immediate=true) // framework gets/ungets hooks each time -@Service(value = {EventListenerHook.class, FindHook.class} ) /** * The <code>ServiceUserMappingBundleFilter</code> only allows the bundle for which the service mapping is available to see it. */ +@Component(immediate=true, // framework gets/ungets hooks each time + service = {EventListenerHook.class, FindHook.class} ) public class ServiceUserMappedBundleFilter implements EventListenerHook, FindHook { @Override 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 b2f980e..314046b 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java +++ b/src/main/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImpl.java @@ -35,18 +35,6 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import org.apache.felix.scr.annotations.Activate; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Deactivate; -import org.apache.felix.scr.annotations.Modified; -import org.apache.felix.scr.annotations.Property; -import org.apache.felix.scr.annotations.PropertyUnbounded; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.ReferenceCardinality; -import org.apache.felix.scr.annotations.ReferencePolicy; -import org.apache.felix.scr.annotations.References; -import org.apache.felix.scr.annotations.Service; -import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.serviceusermapping.ServiceUserMapped; import org.apache.sling.serviceusermapping.ServiceUserMapper; import org.apache.sling.serviceusermapping.ServiceUserValidator; @@ -54,47 +42,39 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Modified; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicy; +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.Designate; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@Component( - metatype = true, - label = "Apache Sling Service User Mapper Service", - description = "Configuration for the service mapping service names to names of users.") -@Service(value={ServiceUserMapper.class, ServiceUserMapperImpl.class}) -@References( { - @Reference(name="amendment", - referenceInterface=MappingConfigAmendment.class, - cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, - policy=ReferencePolicy.DYNAMIC, - updated="updateAmendment"), - @Reference(name = "serviceUserValidator ", referenceInterface = ServiceUserValidator.class, - bind = "bindServiceUserValidator", unbind = "unbindServiceUserValidator", - cardinality= ReferenceCardinality.OPTIONAL_MULTIPLE, policy= ReferencePolicy.DYNAMIC) - -}) - +@Designate(ocd = ServiceUserMapperImpl.Config.class) +@Component(service = {ServiceUserMapper.class, ServiceUserMapperImpl.class}) public class ServiceUserMapperImpl implements ServiceUserMapper { - @Property( - label = "Service Mappings", + @ObjectClassDefinition(name = "Apache Sling Service User Mapper Service", + description = "Configuration for the service mapping service names to names of users.") + public @interface Config { + + @AttributeDefinition(name = "Service Mappings", description = "Provides mappings from service name to user names. " + "Each entry is of the form 'bundleId [ \":\" subServiceName ] \"=\" userName' " + "where bundleId and subServiceName identify the service and userName " - + "defines the name of the user to provide to the service. Invalid entries are logged and ignored.", - unbounded = PropertyUnbounded.ARRAY) - private static final String PROP_SERVICE2USER_MAPPING = "user.mapping"; + + "defines the name of the user to provide to the service. Invalid entries are logged and ignored.") + String[] user_mapping() default {}; - private static final String[] PROP_SERVICE2USER_MAPPING_DEFAULT = {}; - - private static final String PROP_DEFAULT_USER = "user.default"; - - @Property( - name = PROP_DEFAULT_USER, - label = "Default User", + @AttributeDefinition(name = "Default User", description = "The name of the user to use as the default if no service mapping" + "applies. If this property is missing or empty no default user is defined.") - private static final String PROP_DEFAULT_USER_DEFAULT = null; + String user_default(); + } /** default log */ private final Logger log = LoggerFactory.getLogger(getClass()); @@ -119,13 +99,12 @@ public class ServiceUserMapperImpl implements ServiceUserMapper { @Activate @Modified - synchronized void configure(BundleContext bundleContext, final Map<String, Object> config) { + synchronized void configure(BundleContext bundleContext, final Config config) { if (registerAsync && executorService == null) { executorService = Executors.newSingleThreadExecutor(); } - final String[] props = PropertiesUtil.toStringArray(config.get(PROP_SERVICE2USER_MAPPING), - PROP_SERVICE2USER_MAPPING_DEFAULT); + final String[] props = config.user_mapping(); final ArrayList<Mapping> mappings = new ArrayList<Mapping>(props.length); for (final String prop : props) { @@ -140,7 +119,7 @@ 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); + this.defaultUser = config.user_default(); RegistrationSet registrationSet = null; this.bundleContext = bundleContext; @@ -172,6 +151,7 @@ public class ServiceUserMapperImpl implements ServiceUserMapper { * bind the serviceUserValidator * @param serviceUserValidator */ + @Reference(cardinality=ReferenceCardinality.MULTIPLE, policy= ReferencePolicy.DYNAMIC) protected synchronized void bindServiceUserValidator(final ServiceUserValidator serviceUserValidator) { validators.add(serviceUserValidator); restartAllActiveServiceUserMappedServices(); @@ -201,6 +181,7 @@ public class ServiceUserMapperImpl implements ServiceUserMapper { return result; } + @Reference(cardinality=ReferenceCardinality.MULTIPLE,policy=ReferencePolicy.DYNAMIC,updated="updateAmendment") protected synchronized void bindAmendment(final MappingConfigAmendment amendment, final Map<String, Object> props) { final Long key = (Long) props.get(Constants.SERVICE_ID); RegistrationSet registrationSet = null; 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 8eadc1a..69a2cdd 100644 --- a/src/main/java/org/apache/sling/serviceusermapping/package-info.java +++ b/src/main/java/org/apache/sling/serviceusermapping/package-info.java @@ -17,10 +17,6 @@ * under the License. */ -@Version("1.2") -@Export(optional = "provide:=true") [email protected]("1.2.1") package org.apache.sling.serviceusermapping; -import aQute.bnd.annotation.Export; -import aQute.bnd.annotation.Version; - 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 553f312..199f6f7 100644 --- a/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java +++ b/src/test/java/org/apache/sling/serviceusermapping/impl/ServiceUserMapperImplTest.java @@ -26,18 +26,17 @@ import java.util.Dictionary; import java.util.HashMap; import java.util.Map; -import junit.framework.TestCase; - 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 junit.framework.TestCase; + public class ServiceUserMapperImplTest { private static final String BUNDLE_SYMBOLIC1 = "bundle1"; @@ -68,23 +67,16 @@ public class ServiceUserMapperImplTest { when(BUNDLE2.getSymbolicName()).thenReturn(BUNDLE_SYMBOLIC2); } - - - @Test public void test_getServiceUserID() { - @SuppressWarnings("serial") - Map<String, Object> config = new HashMap<String, Object>() { - { - put("user.mapping", new String[] { - BUNDLE_SYMBOLIC1 + "=" + SAMPLE, // - BUNDLE_SYMBOLIC2 + "=" + ANOTHER, // - BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, // - BUNDLE_SYMBOLIC2 + ":" + SUB + "=" + ANOTHER_SUB // - }); - put("user.default", NONE); - } - }; + ServiceUserMapperImpl.Config config = mock(ServiceUserMapperImpl.Config.class); + when(config.user_mapping()).thenReturn(new String[] { + BUNDLE_SYMBOLIC1 + "=" + SAMPLE, // + BUNDLE_SYMBOLIC2 + "=" + ANOTHER, // + BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, // + BUNDLE_SYMBOLIC2 + ":" + SUB + "=" + ANOTHER_SUB // + }); + when(config.user_default()).thenReturn(NONE); final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); sum.configure(null, config); @@ -99,23 +91,20 @@ public class ServiceUserMapperImplTest { @Test public void test_getServiceUserID_WithServiceUserValidator() { - @SuppressWarnings("serial") - Map<String, Object> config = new HashMap<String, Object>() { - { - put("user.mapping", new String[] { - BUNDLE_SYMBOLIC1 + "=" + SAMPLE, // - BUNDLE_SYMBOLIC2 + "=" + ANOTHER, // - BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, // - BUNDLE_SYMBOLIC2 + ":" + SUB + "=" + ANOTHER_SUB // - }); - put("user.default", NONE); - } - }; + ServiceUserMapperImpl.Config config = mock(ServiceUserMapperImpl.Config.class); + when(config.user_mapping()).thenReturn(new String[] { + BUNDLE_SYMBOLIC1 + "=" + SAMPLE, // + BUNDLE_SYMBOLIC2 + "=" + ANOTHER, // + BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, // + BUNDLE_SYMBOLIC2 + ":" + SUB + "=" + ANOTHER_SUB // + }); + when(config.user_default()).thenReturn(NONE); final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); sum.configure(null, config); ServiceUserValidator serviceUserValidator = new ServiceUserValidator() { + @Override public boolean isValid(String serviceUserId, String serviceName, String subServiceName) { if (SAMPLE.equals(serviceUserId)) { @@ -136,41 +125,39 @@ public class ServiceUserMapperImplTest { @Test public void test_amendment() { - @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); - } - }; + ServiceUserMapperImpl.Config config = mock(ServiceUserMapperImpl.Config.class); + when(config.user_mapping()).thenReturn(new String[] { + BUNDLE_SYMBOLIC1 + "=" + SAMPLE, // + BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, // + }); + when(config.user_default()).thenReturn(NONE); final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); sum.configure(null, config); 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); - } - }; + + MappingConfigAmendment.Config mca1Config = mock(MappingConfigAmendment.Config.class); + when(mca1Config.user_mapping()).thenReturn(new String[] {BUNDLE_SYMBOLIC2 + "=" + ANOTHER}); + when(mca1Config.service_ranking()).thenReturn(100); + Map<String, Object> mca1ConfigMap = new HashMap<>(); + mca1ConfigMap.put("user.mapping", mca1Config.user_mapping()); + mca1ConfigMap.put("service.ranking", mca1Config.service_ranking()); + mca1ConfigMap.put("service.id", 1L); + mca1.configure(mca1Config); - sum.bindAmendment(mca1, mca1Config); + sum.bindAmendment(mca1, mca1ConfigMap); 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); - } - }; + + MappingConfigAmendment.Config mca2Config = mock(MappingConfigAmendment.Config.class); + when(mca2Config.user_mapping()).thenReturn(new String[] {BUNDLE_SYMBOLIC2 + ":" + SUB + "=" + ANOTHER_SUB}); + when(mca2Config.service_ranking()).thenReturn(200); + Map<String, Object> mca2ConfigMap = new HashMap<>(); + mca2ConfigMap.put("user.mapping", mca2Config.user_mapping()); + mca2ConfigMap.put("service.ranking", mca2Config.service_ranking()); + mca2ConfigMap.put("service.id", 2L); + mca2.configure(mca2Config); - sum.bindAmendment(mca2, mca2Config); + sum.bindAmendment(mca2, mca2ConfigMap); TestCase.assertEquals(SAMPLE, sum.getServiceUserID(BUNDLE1, null)); TestCase.assertEquals(ANOTHER, sum.getServiceUserID(BUNDLE2, null)); @@ -182,37 +169,36 @@ public class ServiceUserMapperImplTest { @Test public void test_amendmentOverlap() { - @SuppressWarnings("serial") - final Map<String, Object> config = new HashMap<String, Object>() { - { - put("user.default", NONE); - } - }; + ServiceUserMapperImpl.Config config = mock(ServiceUserMapperImpl.Config.class); + when(config.user_mapping()).thenReturn(new String[] {}); + when(config.user_default()).thenReturn(NONE); final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); sum.configure(null, config); 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_RANKING, 100); - } - }; + + MappingConfigAmendment.Config mca1Config = mock(MappingConfigAmendment.Config.class); + when(mca1Config.user_mapping()).thenReturn(new String[] {BUNDLE_SYMBOLIC2 + "=" + ANOTHER}); + when(mca1Config.service_ranking()).thenReturn(100); + Map<String, Object> mca1ConfigMap = new HashMap<>(); + mca1ConfigMap.put("user.mapping", mca1Config.user_mapping()); + mca1ConfigMap.put("service.ranking", mca1Config.service_ranking()); + mca1.configure(mca1Config); final MappingConfigAmendment mca2 = new MappingConfigAmendment(); - @SuppressWarnings("serial") - final Map<String, Object> mca2Config = new HashMap<String, Object>() { - { - put("user.mapping", new String [] {BUNDLE_SYMBOLIC2 + "=" + ANOTHER_SUB}); - put(Constants.SERVICE_RANKING, 200); - } - }; + + MappingConfigAmendment.Config mca2Config = mock(MappingConfigAmendment.Config.class); + when(mca2Config.user_mapping()).thenReturn(new String[] {BUNDLE_SYMBOLIC2 + "=" + ANOTHER_SUB}); + when(mca2Config.service_ranking()).thenReturn(200); + Map<String, Object> mca2ConfigMap = new HashMap<>(); + mca2ConfigMap.put("user.mapping", mca2Config.user_mapping()); + mca2ConfigMap.put("service.ranking", mca2Config.service_ranking()); + mca2.configure(mca2Config); - sum.bindAmendment(mca1, mca1Config); - sum.bindAmendment(mca2, mca2Config); + sum.bindAmendment(mca1, mca1ConfigMap); + sum.bindAmendment(mca2, mca2ConfigMap); TestCase.assertEquals(ANOTHER_SUB, sum.getServiceUserID(BUNDLE2, "")); } @@ -221,16 +207,13 @@ public class ServiceUserMapperImplTest { @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, // + + ServiceUserMapperImpl.Config config = mock(ServiceUserMapperImpl.Config.class); + when(config.user_mapping()).thenReturn(new String[] { + BUNDLE_SYMBOLIC1 + "=" + SAMPLE, // + BUNDLE_SYMBOLIC1 + ":" + SUB + "=" + SAMPLE_SUB, // }); - put("user.default", NONE); - } - }; + when(config.user_default()).thenReturn(NONE); final ServiceUserMapperImpl sum = new ServiceUserMapperImpl(); sum.registerAsync = false; @@ -240,34 +223,36 @@ public class ServiceUserMapperImplTest { TestCase.assertEquals(2, context.getRegistrations(ServiceUserMappedImpl.SERVICEUSERMAPPED).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); - } - }; + + MappingConfigAmendment.Config mca1Config = mock(MappingConfigAmendment.Config.class); + when(mca1Config.user_mapping()).thenReturn(new String[] {BUNDLE_SYMBOLIC2 + "=" + ANOTHER}); + when(mca1Config.service_ranking()).thenReturn(100); + Map<String, Object> mca1ConfigMap = new HashMap<>(); + mca1ConfigMap.put("user.mapping", mca1Config.user_mapping()); + mca1ConfigMap.put("service.ranking", mca1Config.service_ranking()); + mca1ConfigMap.put("service.id", 1L); + mca1.configure(mca1Config); - sum.bindAmendment(mca1, mca1Config); + sum.bindAmendment(mca1, mca1ConfigMap); TestCase.assertEquals(3, context.getRegistrations(ServiceUserMappedImpl.SERVICEUSERMAPPED).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); - } - }; + + MappingConfigAmendment.Config mca2Config = mock(MappingConfigAmendment.Config.class); + when(mca2Config.user_mapping()).thenReturn(new String[] {BUNDLE_SYMBOLIC2 + ":" + SUB + "=" + ANOTHER_SUB}); + when(mca2Config.service_ranking()).thenReturn(200); + Map<String, Object> mca2ConfigMap = new HashMap<>(); + mca2ConfigMap.put("user.mapping", mca2Config.user_mapping()); + mca2ConfigMap.put("service.ranking", mca2Config.service_ranking()); + mca2ConfigMap.put("service.id", 2L); + mca2.configure(mca2Config); - sum.bindAmendment(mca2, mca2Config); + sum.bindAmendment(mca2, mca2ConfigMap); TestCase.assertEquals(4, context.getRegistrations(ServiceUserMappedImpl.SERVICEUSERMAPPED).size()); - sum.unbindAmendment(mca1, mca1Config); + sum.unbindAmendment(mca1, mca1ConfigMap); TestCase.assertEquals(3, context.getRegistrations(ServiceUserMappedImpl.SERVICEUSERMAPPED).size()); } @@ -282,6 +267,7 @@ public class ServiceUserMapperImplTest { public ServiceRegistrationContextHelper() { when(bundleContext.registerService(any(String.class), any(Object.class), any(Dictionary.class))) .then(new Answer<ServiceRegistration>() { + @Override public ServiceRegistration answer(InvocationOnMock invocationOnMock) throws Throwable { Object[] arguments = invocationOnMock.getArguments(); @@ -301,14 +287,17 @@ public class ServiceUserMapperImplTest { return new ServiceRegistration() { + @Override public ServiceReference getReference() { return null; } + @Override public void setProperties(Dictionary dictionary) { } + @Override public void unregister() { serviceRegistrations.remove(registeredObject); } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
