This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-apiregions.git


The following commit(s) were added to refs/heads/master by this push:
     new 00b7f98  SLING-9575 Existing API Regions with factory Pids are not 
found
     new 8d3c874  Merge pull request #12 from bosschaert/SLING-9575
00b7f98 is described below

commit 00b7f98a60dea3c045de2da1133398e7e754ce96
Author: David Bosschaert <[email protected]>
AuthorDate: Wed Jul 8 18:45:36 2020 +0100

    SLING-9575 Existing API Regions with factory Pids are not found
---
 .../sling/feature/apiregions/impl/Activator.java   |  2 +-
 .../feature/apiregions/impl/ActivatorTest.java     | 67 ++++++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/apache/sling/feature/apiregions/impl/Activator.java 
b/src/main/java/org/apache/sling/feature/apiregions/impl/Activator.java
index b3a6701..7082979 100644
--- a/src/main/java/org/apache/sling/feature/apiregions/impl/Activator.java
+++ b/src/main/java/org/apache/sling/feature/apiregions/impl/Activator.java
@@ -278,7 +278,7 @@ public class Activator implements BundleActivator, 
FrameworkListener {
             });
             final ServiceRegistration<?> reg = 
bundleContext.registerService(CFG_LISTENER_CLASS_NAME, msf, null);
             // get existing configurations
-            final Object result = caListConfigcMethod.invoke(cfgAdmin, 
"(service.factoryPid= " + FACTORY_PID + ")");
+            final Object result = caListConfigcMethod.invoke(cfgAdmin, 
"(service.factoryPid=" + FACTORY_PID + ")");
             if ( result != null ) {
                 for(int i=0; i<Array.getLength(result); i++) {
                     final Object cfg = Array.get(result, i);
diff --git 
a/src/test/java/org/apache/sling/feature/apiregions/impl/ActivatorTest.java 
b/src/test/java/org/apache/sling/feature/apiregions/impl/ActivatorTest.java
index 6b0a984..c52b0ce 100644
--- a/src/test/java/org/apache/sling/feature/apiregions/impl/ActivatorTest.java
+++ b/src/test/java/org/apache/sling/feature/apiregions/impl/ActivatorTest.java
@@ -28,12 +28,14 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 
 import java.io.File;
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
 import org.junit.After;
@@ -45,6 +47,7 @@ import org.mockito.stubbing.Answer;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.framework.hooks.resolver.ResolverHookFactory;
 import org.osgi.framework.namespace.PackageNamespace;
@@ -53,7 +56,11 @@ import org.osgi.framework.wiring.BundleRevision;
 import org.osgi.framework.wiring.BundleWiring;
 import org.osgi.framework.wiring.FrameworkWiring;
 import org.osgi.resource.Requirement;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.cm.ManagedService;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 public class ActivatorTest {
     private Properties savedProps;
@@ -276,4 +283,64 @@ public class ActivatorTest {
         assertEquals(0, req.getAttributes().size());
         assertNull(req.getResource());
     }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testConfigAdminListener() throws Exception {
+        String i = 
getClass().getResource("/idbsnver1.properties").toURI().toString();
+        String b = 
getClass().getResource("/bundles1.properties").toURI().toString();
+        String f = 
getClass().getResource("/features1.properties").toURI().toString();
+        String r = 
getClass().getResource("/regions1.properties").toURI().toString();
+
+        BundleContext bc = Mockito.mock(BundleContext.class);
+        Mockito.when(bc.getBundle()).thenReturn(Mockito.mock(Bundle.class));
+        
Mockito.when(bc.getProperty(Activator.REGIONS_PROPERTY_NAME)).thenReturn("*");
+        Mockito.when(bc.getProperty(PROPERTIES_RESOURCE_PREFIX + 
IDBSNVER_FILENAME)).
+            thenReturn(i);
+        Mockito.when(bc.getProperty(PROPERTIES_RESOURCE_PREFIX + 
BUNDLE_FEATURE_FILENAME)).
+            thenReturn(b);
+        Mockito.when(bc.getProperty(PROPERTIES_RESOURCE_PREFIX + 
FEATURE_REGION_FILENAME)).
+            thenReturn(f);
+        Mockito.when(bc.getProperty(PROPERTIES_RESOURCE_PREFIX + 
REGION_PACKAGE_FILENAME)).
+            thenReturn(r);
+
+        ConfigurationAdmin cm = Mockito.mock(ConfigurationAdmin.class);
+        
Mockito.when(cm.listConfigurations("(service.factoryPid=org.apache.sling.feature.apiregions.factory)")).
+            thenAnswer(new Answer<Configuration[]>() {
+                @Override
+                public Configuration[] answer(InvocationOnMock invocation) 
throws Throwable {
+                    Dictionary<String, Object> props = new Hashtable<>();
+                    props.put("foo", "bar");
+
+                    Configuration cfg = Mockito.mock(Configuration.class);
+                    
Mockito.when(cfg.getPid()).thenReturn("org.apache.sling.feature.apiregions.factory~123");
+                    Mockito.when(cfg.getProperties()).thenReturn(props);
+
+                    return new Configuration[] {cfg};
+                }
+            });
+
+        ServiceReference<Object> caRef = Mockito.mock(ServiceReference.class);
+        Mockito.when(bc.getService(caRef)).thenReturn(cm);
+
+
+        Activator a = new Activator();
+        a.start(bc);
+        ServiceTracker<Object, Object> st = a.configAdminTracker;
+        Field custField = st.getClass().getDeclaredField("customizer");
+        custField.setAccessible(true);
+
+        @SuppressWarnings("rawtypes")
+        ServiceTrackerCustomizer customizer = (ServiceTrackerCustomizer) 
custField.get(st);
+
+        Field fcf = 
a.configuration.getClass().getDeclaredField("factoryConfigs");
+        fcf.setAccessible(true);
+        @SuppressWarnings("rawtypes")
+        Map<String, Dictionary<String, Object>> factoryConfigs = (Map) 
fcf.get(a.configuration);
+        assertEquals("Precondition", 0, factoryConfigs.size());
+
+        customizer.addingService(caRef);
+        Dictionary<String, Object> dict = 
factoryConfigs.get("org.apache.sling.feature.apiregions.factory~123");
+        assertEquals("bar", dict.get("foo"));
+    }
 }

Reply via email to