You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:16:47 UTC

[sling-org-apache-sling-testing-caconfig-mock-plugin] 02/13: SLING-6360 suppor registering of configuration annotation classes

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

rombert pushed a commit to annotated tag org.apache.sling.testing.caconfig-mock-plugin-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-caconfig-mock-plugin.git

commit 3b17db3fbec5b7acf0e2b9ba497ffd1d861353bd
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Dec 6 08:53:09 2016 +0000

    SLING-6360 suppor registering of configuration annotation classes
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/contextaware-config/testing/mocks/caconfig-mock-plugin@1772846 13f79535-47bb-0310-9956-ffa450edef68
---
 .../mock/caconfig/ConfigurationMetadataUtil.java   | 269 +++++++++++++++++++++
 .../testing/mock/caconfig/ContextPlugins.java      |   7 +
 .../mock/caconfig/MockContextAwareConfig.java      |  52 ++++
 .../testing/mock/caconfig/ContextPluginsTest.java  |  33 ++-
 .../mock/caconfig/example/SimpleConfig.java        |  32 +++
 5 files changed, 384 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/caconfig/ConfigurationMetadataUtil.java b/src/main/java/org/apache/sling/testing/mock/caconfig/ConfigurationMetadataUtil.java
new file mode 100644
index 0000000..a14bf81
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/mock/caconfig/ConfigurationMetadataUtil.java
@@ -0,0 +1,269 @@
+/*
+ * 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.caconfig;
+
+import static org.apache.sling.caconfig.impl.ConfigurationNameConstants.CONFIGURATION_CLASSES_HEADER;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.security.cert.X509Certificate;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.testing.mock.osgi.ManifestScanner;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+
+/**
+ * Helper methods for registering Configuration annotation classes from the classpath.
+ */
+final class ConfigurationMetadataUtil {
+    
+    private static final String[] CONFIGURATION_CLASSES_FROM_MANIFEST;
+    
+    static {
+        // scan classpath for configuration classes bundle header entries only once
+        CONFIGURATION_CLASSES_FROM_MANIFEST = toArray(ManifestScanner.getValues(CONFIGURATION_CLASSES_HEADER));
+    }
+    
+    private ConfigurationMetadataUtil() {
+        // static methods only
+    }
+
+    private static String[] toArray(Collection<String> values) {
+        return values.toArray(new String[values.size()]);
+    }
+
+    /**
+     * Search classpath for given class names to scan for and register all classes with @Configuration annotation.
+     * @param bundleContext Bundle context
+     * @param classNames Java class names
+     */
+    public static void registerAnnotationClasses(BundleContext bundleContext, String... classNames) {
+        Bundle bundle = new RegisterConfigurationMetadataBundle(bundleContext, Bundle.ACTIVE, classNames);
+        BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle);
+        MockOsgi.sendBundleEvent(bundleContext, event);
+    }
+
+    /**
+     * Search classpath for given class names to scan for and register all classes with @Configuration annotation.
+     * @param bundleContext Bundle context
+     * @param classNames Java class names
+     */
+    public static void registerAnnotationClasses(BundleContext bundleContext, Class... classes) {
+        String[] classNames = new String[classes.length];
+        for (int i = 0; i < classes.length; i++) {
+            classNames[i] = classes[i].getName();
+        }
+        registerAnnotationClasses(bundleContext, classNames);
+    }
+    
+    /**
+     * Scan MANIFEST.MF in the classpath and automatically register all Configuration annotation classes found.
+     * @param bundleContext Bundle context
+     */
+    public static void addAnnotationClassesForManifestEntries(BundleContext bundleContext) {
+        if (CONFIGURATION_CLASSES_FROM_MANIFEST.length > 0) {
+            registerAnnotationClasses(bundleContext, CONFIGURATION_CLASSES_FROM_MANIFEST);
+        }
+    }
+    
+
+    private static class RegisterConfigurationMetadataBundle implements Bundle {
+        
+        private final BundleContext bundleContext;
+        private final int state;
+        private final String classNames;
+
+        public RegisterConfigurationMetadataBundle(BundleContext bundleContext, int state, String[] classNames) {
+            this.bundleContext = bundleContext;
+            this.state = state;
+            this.classNames = normalizeValueList(classNames);
+        }
+        
+        private String normalizeValueList(String[] values) {
+            if (values == null || values.length == 0) {
+                return null;
+            }
+            return StringUtils.join(values, ",");
+        }
+
+        @Override
+        public int getState() {
+            return this.state;
+        }
+
+        @Override
+        public Dictionary<String,String> getHeaders() {
+            Dictionary<String, String> headers = new Hashtable<String, String>();
+            headers.put(CONFIGURATION_CLASSES_HEADER, classNames);
+            return headers;
+        }
+
+        @Override
+        public Enumeration<URL> findEntries(String path, String filePattern, boolean recurse) {
+            return new Vector<URL>().elements();
+        }
+        
+        @Override
+        public Class<?> loadClass(String name) throws ClassNotFoundException {
+            return getClass().getClassLoader().loadClass(name);
+        }
+
+        @Override
+        public BundleContext getBundleContext() {
+            return bundleContext;
+        }
+
+        @Override
+        public void start(int options) throws BundleException {
+            // do nothing
+        }
+
+        @Override
+        public void start() throws BundleException {
+            // do nothing
+        }
+
+        @Override
+        public void stop(int options) throws BundleException {
+            // do nothing
+        }
+
+        @Override
+        public void stop() throws BundleException {
+            // do nothing
+        }
+
+        @Override
+        public void update(InputStream input) throws BundleException {
+            // do nothing
+        }
+
+        @Override
+        public void update() throws BundleException {
+            // do nothing
+        }
+
+        @Override
+        public void uninstall() throws BundleException {
+            // do nothing
+        }
+
+        @Override
+        public long getBundleId() {
+            return 0;
+        }
+
+        @Override
+        public String getLocation() {
+            return null;
+        }
+
+        @Override
+        public ServiceReference<?>[] getRegisteredServices() { // NOPMD
+            return null;
+        }
+
+        @Override
+        public ServiceReference<?>[] getServicesInUse() { // NOPMD
+            return null;
+        }
+
+        @Override
+        public boolean hasPermission(Object permission) {
+            return false;
+        }
+
+        @Override
+        public URL getResource(String name) {
+            return null;
+        }
+
+        @Override
+        public Dictionary<String,String> getHeaders(String locale) {
+            return null;
+        }
+
+        @Override
+        public String getSymbolicName() {
+            return null;
+        }
+
+        @Override
+        public Enumeration<URL> getResources(String name) throws IOException {
+            return null;
+        }
+
+        @Override
+        public Enumeration<String> getEntryPaths(String path) {
+            return null;
+        }
+
+        @Override
+        public URL getEntry(String path) {
+            return null;
+        }
+
+        @Override
+        public long getLastModified() {
+            return 0;
+        }
+
+        @Override
+        public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(int signersType) {
+            return null;
+        }
+
+        @Override
+        public Version getVersion() {
+            return null;
+        }
+
+        @Override
+        public int compareTo(Bundle o) {
+            return 0;
+        }
+
+        @Override
+        public <A> A adapt(Class<A> type) {
+            return null;
+        }
+
+        @Override
+        public File getDataFile(String filename) {
+            return null;
+        }
+        
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/mock/caconfig/ContextPlugins.java b/src/main/java/org/apache/sling/testing/mock/caconfig/ContextPlugins.java
index a9a74b3..d2b908d 100644
--- a/src/main/java/org/apache/sling/testing/mock/caconfig/ContextPlugins.java
+++ b/src/main/java/org/apache/sling/testing/mock/caconfig/ContextPlugins.java
@@ -23,6 +23,7 @@ import org.apache.sling.caconfig.impl.ConfigurationInheritanceStrategyMultiplexe
 import org.apache.sling.caconfig.impl.ConfigurationResolverImpl;
 import org.apache.sling.caconfig.impl.def.DefaultConfigurationInheritanceStrategy;
 import org.apache.sling.caconfig.impl.def.DefaultConfigurationPersistenceStrategy;
+import org.apache.sling.caconfig.impl.metadata.AnnotationClassConfigurationMetadataProvider;
 import org.apache.sling.caconfig.impl.metadata.ConfigurationMetadataProviderMultiplexer;
 import org.apache.sling.caconfig.impl.override.ConfigurationOverrideManager;
 import org.apache.sling.caconfig.management.impl.ConfigurationManagerImpl;
@@ -35,10 +36,12 @@ import org.apache.sling.caconfig.resource.impl.def.DefaultContextPathStrategy;
 import org.apache.sling.testing.mock.osgi.context.AbstractContextPlugin;
 import org.apache.sling.testing.mock.osgi.context.ContextPlugin;
 import org.apache.sling.testing.mock.sling.context.SlingContextImpl;
+import org.osgi.annotation.versioning.ProviderType;
 
 /**
  * Mock context plugins.
  */
+@ProviderType
 public final class ContextPlugins {
 
   private ContextPlugins() {
@@ -56,6 +59,9 @@ public final class ContextPlugins {
         registerConfigurationManagement(context);
         registerConfigurationResourceResolverDefaultImpl(context);
         registerConfigurationResolverDefaultImpl(context);
+        
+        // Scan MANIFEST.MF in the classpath and automatically register all Configuration annotation classes found.
+        ConfigurationMetadataUtil.addAnnotationClassesForManifestEntries(context.bundleContext());
     }
   };
 
@@ -115,6 +121,7 @@ public final class ContextPlugins {
   private static void registerConfigurationManagement(SlingContextImpl context) {
       context.registerInjectActivateService(new ConfigurationMetadataProviderMultiplexer());
       context.registerInjectActivateService(new ConfigurationManagerImpl());
+      context.registerInjectActivateService(new AnnotationClassConfigurationMetadataProvider());
   }
   
 }
diff --git a/src/main/java/org/apache/sling/testing/mock/caconfig/MockContextAwareConfig.java b/src/main/java/org/apache/sling/testing/mock/caconfig/MockContextAwareConfig.java
new file mode 100644
index 0000000..02acb13
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/mock/caconfig/MockContextAwareConfig.java
@@ -0,0 +1,52 @@
+/*
+ * 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.caconfig;
+
+import org.osgi.annotation.versioning.ProviderType;
+import org.osgi.framework.BundleContext;
+
+/**
+ * Helps setting up a mock environment for Context-Aware Configuration.
+ */
+@ProviderType
+public final class MockContextAwareConfig {
+    
+    private MockContextAwareConfig() {
+        // static methods only
+    }
+
+    /**
+     * Search classpath for given class names to scan for and register all classes with @Configuration annotation.
+     * @param bundleContext Bundle context
+     * @param classNames Java class names
+     */
+    public static void registerAnnotationClasses(BundleContext bundleContext, String... classNames) {
+        ConfigurationMetadataUtil.registerAnnotationClasses(bundleContext, classNames);
+    }
+
+    /**
+     * Search classpath for given class names to scan for and register all classes with @Configuration annotation.
+     * @param bundleContext Bundle context
+     * @param classNames Java class names
+     */
+    public static void registerAnnotationClasses(BundleContext bundleContext, Class... classes) {
+        ConfigurationMetadataUtil.registerAnnotationClasses(bundleContext, classes);
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/testing/mock/caconfig/ContextPluginsTest.java b/src/test/java/org/apache/sling/testing/mock/caconfig/ContextPluginsTest.java
index f172a85..2a69ebd 100644
--- a/src/test/java/org/apache/sling/testing/mock/caconfig/ContextPluginsTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/caconfig/ContextPluginsTest.java
@@ -20,12 +20,14 @@ package org.apache.sling.testing.mock.caconfig;
 
 import static org.apache.sling.testing.mock.caconfig.ContextPlugins.CACONFIG;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.caconfig.ConfigurationBuilder;
 import org.apache.sling.caconfig.management.ConfigurationManager;
 import org.apache.sling.caconfig.spi.ConfigurationPersistData;
+import org.apache.sling.testing.mock.caconfig.example.SimpleConfig;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.apache.sling.testing.mock.sling.junit.SlingContextBuilder;
 import org.junit.Before;
@@ -47,22 +49,35 @@ public class ContextPluginsTest {
     public void setUp() {
         context.create().resource("/content/site1", "sling:configRef", "/conf/site1");
         contextResource = context.create().resource("/content/site1/page1");
-    }
-    
-    @Test
-    public void testPlugin() {
         
+        // register configuration annotation class
+        MockContextAwareConfig.registerAnnotationClasses(context.bundleContext(), SimpleConfig.class);
+
         // write config
         ConfigurationManager configManager = context.getService(ConfigurationManager.class);
         configManager.persistConfiguration(contextResource, CONFIG_NAME, 
                 new ConfigurationPersistData(ImmutableMap.<String, Object>of(
-                        "prop1", "value1",
-                        "prop2", 123)));
-        
+                        "stringParam", "value1",
+                        "intParam", 123,
+                        "boolParam", true)));
+    }
+    
+    @Test
+    public void testValueMap() {
         // read config
         ValueMap props = contextResource.adaptTo(ConfigurationBuilder.class).name(CONFIG_NAME).asValueMap();
-        assertEquals("value1", props.get("prop1", String.class));
-        assertEquals((Integer)123, props.get("prop2", Integer.class));
+        assertEquals("value1", props.get("stringParam", String.class));
+        assertEquals((Integer)123, props.get("intParam", Integer.class));
+        assertTrue(props.get("boolParam", Boolean.class));
+    }
+
+    @Test
+    public void testAnnotationClass() {
+        // read config
+        SimpleConfig config = contextResource.adaptTo(ConfigurationBuilder.class).as(SimpleConfig.class);
+        assertEquals("value1", config.stringParam());
+        assertEquals(123, config.intParam());
+        assertTrue(config.boolParam());
     }
 
 }
diff --git a/src/test/java/org/apache/sling/testing/mock/caconfig/example/SimpleConfig.java b/src/test/java/org/apache/sling/testing/mock/caconfig/example/SimpleConfig.java
new file mode 100644
index 0000000..821bd9e
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/caconfig/example/SimpleConfig.java
@@ -0,0 +1,32 @@
+/*
+ * 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.caconfig.example;
+
+import org.apache.sling.caconfig.annotation.Configuration;
+
+@Configuration(name = "testConfig")
+public @interface SimpleConfig {
+
+    String stringParam();
+    
+    int intParam() default 5;
+    
+    boolean boolParam();
+    
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.