You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2017/11/08 17:30:25 UTC
[GitHub] stefanseifert closed pull request #1: SLING-7233: MockConfigurationAdmin ignores configuration PID
stefanseifert closed pull request #1: SLING-7233: MockConfigurationAdmin ignores configuration PID
URL: https://github.com/apache/sling-org-apache-sling-testing-osgi-mock/pull/1
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MapMergeUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/MapMergeUtil.java
index d690752..d2f71d5 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MapMergeUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MapMergeUtil.java
@@ -78,14 +78,15 @@ private MapMergeUtil() {
// merge with configuration from config admin
if (configAdmin != null) {
- Object pid = metadata.getPID();
- if (pid != null) {
- try {
- Configuration config = configAdmin.getConfiguration(pid.toString());
- mergedProperties.putAll(toMap(config.getProperties()));
- }
- catch (IOException ex) {
- throw new RuntimeException("Unable to read config for pid " + pid, ex);
+ for (String pid : metadata.getConfigurationPID()) {
+ if (pid != null) {
+ try {
+ Configuration config = configAdmin.getConfiguration(pid);
+ mergedProperties.putAll(toMap(config.getProperties()));
+ }
+ catch (IOException ex) {
+ throw new RuntimeException("Unable to read config for pid " + pid, ex);
+ }
}
}
}
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
index a4c5a4b..1fca7a5 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
@@ -263,7 +263,20 @@ private static String getComponentName(Class clazz, Document metadata) {
if (nodes != null && nodes.getLength() > 0) {
return getAttributeValue(nodes.item(0), "name");
}
- return null;
+ return clazz.getName();
+ }
+
+ private static String[] getConfigurationPID(Class clazz, Document metadata) {
+ String value = null;
+ String query = getComponentXPathQuery(clazz);
+ NodeList nodes = queryNodes(metadata, query);
+ if (nodes != null && nodes.getLength() > 0) {
+ value = getAttributeValue(nodes.item(0), "configuration-pid");
+ }
+ if (value == null) {
+ value = getComponentName(clazz, metadata);
+ }
+ return StringUtils.split(value);
}
private static Set<String> getServiceInterfaces(Class clazz, Document metadata) {
@@ -377,6 +390,7 @@ private static String getAttributeValue(Node node, String attributeName) {
private final Class<?> clazz;
private final String name;
+ private final String[] configurationPID;
private final Set<String> serviceInterfaces;
private final Map<String, Object> properties;
private final List<Reference> references;
@@ -387,6 +401,7 @@ private static String getAttributeValue(Node node, String attributeName) {
private OsgiMetadata(Class<?> clazz, Document metadataDocument) {
this.clazz = clazz;
this.name = OsgiMetadataUtil.getComponentName(clazz, metadataDocument);
+ this.configurationPID = OsgiMetadataUtil.getConfigurationPID(clazz, metadataDocument);
this.serviceInterfaces = OsgiMetadataUtil.getServiceInterfaces(clazz, metadataDocument);
this.properties = OsgiMetadataUtil.getProperties(clazz, metadataDocument);
this.references = OsgiMetadataUtil.getReferences(clazz, metadataDocument);
@@ -398,6 +413,7 @@ private OsgiMetadata(Class<?> clazz, Document metadataDocument) {
private OsgiMetadata() {
this.clazz = null;
this.name = null;
+ this.configurationPID = null;
this.serviceInterfaces = null;
this.properties = null;
this.references = null;
@@ -422,6 +438,10 @@ public String getPID() {
return StringUtils.defaultString(pid, name);
}
+ public String[] getConfigurationPID() {
+ return configurationPID;
+ }
+
public Set<String> getServiceInterfaces() {
return serviceInterfaces;
}
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockConfigurationAdminTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockConfigurationAdminTest.java
index 857ebb7..187efbd 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/MockConfigurationAdminTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockConfigurationAdminTest.java
@@ -69,4 +69,39 @@ public void testGetConfigurationString() throws IOException {
assertEquals("org.apache.sling.testing.mock.osgi.OsgiMetadataUtilTest$ServiceWithMetadata", reference.getProperty(Constants.SERVICE_PID));
}
+ @Test
+ public void testConfigurationPID() throws IOException {
+ MockOsgi.setConfigForPid(context.bundleContext(), ServiceWithConfigurationPID.class.getSimpleName(),
+ "prop1", 1);
+
+ context.registerInjectActivateService(new ServiceWithConfigurationPID(), ImmutableMap.<String, Object>builder()
+ .put("prop2", 2)
+ .build());
+
+ ServiceReference reference = context.bundleContext().getServiceReference(Comparable.class.getName());
+
+ assertEquals(1, reference.getProperty("prop1"));
+ assertEquals(2, reference.getProperty("prop2"));
+ }
+
+ @Test
+ public void testMultipleConfigurationPID() throws IOException {
+ MockOsgi.setConfigForPid(context.bundleContext(), "Configuration1",
+ "prop1", 1);
+ MockOsgi.setConfigForPid(context.bundleContext(), "Configuration2",
+ "prop1", 2);
+
+ context.registerInjectActivateService(new ServiceWithMultipleConfigurationPID(), ImmutableMap.<String, Object>builder()
+ .put("prop2", 2)
+ .build());
+
+ ServiceReference reference = context.bundleContext().getServiceReference(Comparable.class.getName());
+
+ assertEquals(2, reference.getProperty("prop1"));
+ assertEquals(2, reference.getProperty("prop2"));
+ }
+
+ static class ServiceWithConfigurationPID {}
+
+ static class ServiceWithMultipleConfigurationPID {}
}
diff --git a/src/test/resources/OSGI-INF/serviceComponents.xml b/src/test/resources/OSGI-INF/serviceComponents.xml
index f173b6b..db1c94a 100644
--- a/src/test/resources/OSGI-INF/serviceComponents.xml
+++ b/src/test/resources/OSGI-INF/serviceComponents.xml
@@ -18,10 +18,10 @@
under the License.
-->
<!-- This file follows the old SCR convention using a fixed name "serviceComponents.xml" -->
-<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
+<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0">
<scr:component name="org.apache.sling.testing.mock.osgi.OsgiMetadataUtilTest$ServiceWithMetadata">
<implementation class="org.apache.sling.testing.mock.osgi.OsgiMetadataUtilTest$ServiceWithMetadata"/>
- <service servicefactory="false">
+ <service>
<provide interface="org.apache.sling.models.spi.Injector"/>
<provide interface="org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory"/>
<provide interface="java.lang.Comparable"/>
@@ -33,4 +33,18 @@
org.apache.sling.api.resource.ResourceResolver
</property>
</scr:component>
+ <scr:component name="org.apache.sling.testing.mock.osgi.MockConfigurationAdminTest$ServiceWithConfigurationPID"
+ configuration-pid="ServiceWithConfigurationPID">
+ <implementation class="org.apache.sling.testing.mock.osgi.MockConfigurationAdminTest$ServiceWithConfigurationPID"/>
+ <service>
+ <provide interface="java.lang.Comparable"/>
+ </service>
+ </scr:component>
+ <scr:component name="org.apache.sling.testing.mock.osgi.MockConfigurationAdminTest$ServiceWithMultipleConfigurationPID"
+ configuration-pid="Configuration1 Configuration2">
+ <implementation class="org.apache.sling.testing.mock.osgi.MockConfigurationAdminTest$ServiceWithMultipleConfigurationPID"/>
+ <service>
+ <provide interface="java.lang.Comparable"/>
+ </service>
+ </scr:component>
</components>
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services