SLING-6427 Move Sling Context-Aware Config out of contrib
- move in svn
- update all SCM URLs
- update jenkins build jobs
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1775601 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..1875af9
--- /dev/null
+++ b/README.txt
@@ -0,0 +1,6 @@
+Apache Sling Context-Aware Configuration Mock Plugin
+
+Mock Context Plugin for Apache Sling Context-Aware Configuration
+
+Documentation:
+http://sling.apache.org/documentation/bundles/context-aware-configuration/context-aware-configuration.html
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..879ff2f
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,120 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling</artifactId>
+ <version>29</version>
+ <relativePath />
+ </parent>
+
+ <artifactId>org.apache.sling.testing.caconfig-mock-plugin</artifactId>
+ <packaging>bundle</packaging>
+ <version>1.0.1-SNAPSHOT</version>
+ <name>Apache Sling Context-Aware Configuration Mock Plugin</name>
+ <description>Mock Context Plugin for Apache Sling Context-Aware Configuration</description>
+
+ <scm>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/testing/mocks/caconfig-mock-plugin</connection>
+ <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/caconfig-mock-plugin</developerConnection>
+ <url>http://svn.apache.org/viewvc/sling/trunk/testing/mocks/caconfig-mock-plugin</url>
+ </scm>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.caconfig.api</artifactId>
+ <version>1.1.0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.caconfig.spi</artifactId>
+ <version>1.2.0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.caconfig.impl</artifactId>
+ <version>1.2.0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.api</artifactId>
+ <version>2.9.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+ <version>2.2.0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+ <version>1.9.0</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+ <profiles>
+
+ <!-- Test with Sling CAConfig Impl/SPI 1.1 -->
+ <profile>
+ <id>caconfig-1.1</id>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.caconfig.api</artifactId>
+ <version>1.0.0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.caconfig.spi</artifactId>
+ <version>1.1.0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.caconfig.impl</artifactId>
+ <version>1.1.0</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+ </profile>
+
+ </profiles>
+
+</project>
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
new file mode 100644
index 0000000..dc06a59
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/mock/caconfig/ContextPlugins.java
@@ -0,0 +1,153 @@
+/*
+ * 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.apache.sling.caconfig.impl.ConfigurationBuilderAdapterFactory;
+import org.apache.sling.caconfig.impl.ConfigurationResolverImpl;
+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.management.impl.ConfigurationManagerImpl;
+import org.apache.sling.caconfig.management.impl.ConfigurationPersistenceStrategyMultiplexer;
+import org.apache.sling.caconfig.resource.impl.ConfigurationResourceResolverImpl;
+import org.apache.sling.caconfig.resource.impl.ConfigurationResourceResolvingStrategyMultiplexer;
+import org.apache.sling.caconfig.resource.impl.def.DefaultConfigurationResourceResolvingStrategy;
+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.
+ * The plugin supports both 1.0/1.1 and 1.2+ version of the Impl/SPI.
+ */
+@ProviderType
+public final class ContextPlugins {
+
+ private ContextPlugins() {
+ // constants only
+ }
+
+ /**
+ * Context plugin for Sling Context-Aware Configuration.
+ */
+ public static final ContextPlugin<? extends SlingContextImpl> CACONFIG = new AbstractContextPlugin<SlingContextImpl>() {
+ @Override
+ public void afterSetUp(SlingContextImpl context) throws Exception {
+ registerConfigurationResourceResolver(context);
+ registerConfigurationResolver(context);
+ registerConfigurationManagement(context);
+ registerConfigurationResourceResolverDefaultImpl(context);
+ registerConfigurationResolverDefaultImpl(context);
+
+ // Scan MANIFEST.MF in the classpath and automatically register all Configuration annotation classes found.
+ ConfigurationMetadataUtil.addAnnotationClassesForManifestEntries(context.bundleContext());
+ }
+ };
+
+ /**
+ * Context plugin for Sling Context-Aware Configuration (without the default implementations).
+ */
+ public static final ContextPlugin<? extends SlingContextImpl> CACONFIG_NODEF = new AbstractContextPlugin<SlingContextImpl>() {
+ @Override
+ public void afterSetUp(SlingContextImpl context) throws Exception {
+ registerConfigurationResourceResolver(context);
+ registerConfigurationResolver(context);
+ registerConfigurationManagement(context);
+ }
+ };
+
+ /**
+ * Register all services for ConfigurationResourceResolver (without the default implementations).
+ * @param context Sling context
+ */
+ private static void registerConfigurationResourceResolver(SlingContextImpl context) {
+
+ if (!registerByClassName(context, "org.apache.sling.caconfig.management.impl.ContextPathStrategyMultiplexerImpl")) {
+ // fallback to impl 1.1
+ registerByClassName(context, "org.apache.sling.caconfig.resource.impl.ContextPathStrategyMultiplexer");
+ }
+
+ context.registerInjectActivateService(new ConfigurationResourceResolvingStrategyMultiplexer());
+ context.registerInjectActivateService(new ConfigurationResourceResolverImpl());
+ }
+
+ /**
+ * Register default implementations for for ConfigurationResourceResolver.
+ * @param context Sling context
+ */
+ private static void registerConfigurationResourceResolverDefaultImpl(SlingContextImpl context) {
+ context.registerInjectActivateService(new DefaultContextPathStrategy());
+ context.registerInjectActivateService(new DefaultConfigurationResourceResolvingStrategy());
+ }
+
+ /**
+ * Register all services for ConfigurationResolver (without the default implementations).
+ * @param context Sling context
+ */
+ private static void registerConfigurationResolver(SlingContextImpl context) {
+ context.registerInjectActivateService(new ConfigurationPersistenceStrategyMultiplexer());
+ context.registerInjectActivateService(new ConfigurationMetadataProviderMultiplexer());
+
+ // only required for impl 1.2+
+ registerByClassName(context, "org.apache.sling.caconfig.impl.ConfigurationInheritanceStrategyMultiplexer");
+
+ // only required for impl 1.2+
+ registerByClassName(context, "org.apache.sling.caconfig.impl.override.ConfigurationOverrideManager");
+
+ context.registerInjectActivateService(new ConfigurationResolverImpl());
+ context.registerInjectActivateService(new ConfigurationBuilderAdapterFactory());
+ }
+
+ /**
+ * Register default implementations for for ConfigurationResolver.
+ * @param context Sling context
+ */
+ private static void registerConfigurationResolverDefaultImpl(SlingContextImpl context) {
+ context.registerInjectActivateService(new DefaultConfigurationPersistenceStrategy());
+
+ // only required for impl 1.2+
+ registerByClassName(context,"org.apache.sling.caconfig.impl.def.DefaultConfigurationInheritanceStrategy");
+ }
+
+ private static void registerConfigurationManagement(SlingContextImpl context) {
+ context.registerInjectActivateService(new ConfigurationManagerImpl());
+ context.registerInjectActivateService(new AnnotationClassConfigurationMetadataProvider());
+ }
+
+ private static boolean registerByClassName(SlingContextImpl context, String className) {
+ try {
+ Class<?> clazz = Class.forName(className);
+ context.registerInjectActivateService(clazz.newInstance());
+ return true;
+ }
+ catch (ClassNotFoundException ex) {
+ return false;
+ }
+ catch (InstantiationException ex) {
+ throw new RuntimeException(ex);
+ }
+ catch (IllegalAccessException ex) {
+ throw new RuntimeException(ex);
+ }
+
+ }
+
+}
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..03b0212
--- /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.apache.sling.testing.mock.sling.context.SlingContextImpl;
+import org.osgi.annotation.versioning.ProviderType;
+
+/**
+ * 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 context Sling context
+ * @param classNames Java class names
+ */
+ public static void registerAnnotationClasses(SlingContextImpl context, String... classNames) {
+ ConfigurationMetadataUtil.registerAnnotationClasses(context.bundleContext(), classNames);
+ }
+
+ /**
+ * Search classpath for given class names to scan for and register all classes with @Configuration annotation.
+ * @param context Sling context
+ * @param classes Java classes
+ */
+ public static void registerAnnotationClasses(SlingContextImpl context, Class... classes) {
+ ConfigurationMetadataUtil.registerAnnotationClasses(context.bundleContext(), classes);
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/mock/caconfig/package-info.java b/src/main/java/org/apache/sling/testing/mock/caconfig/package-info.java
new file mode 100644
index 0000000..de6d18e
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/mock/caconfig/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+/**
+ * Apache Sling Context-Aware Configuration Mock Plugin
+ */
+@org.osgi.annotation.versioning.Version("1.0.0")
+package org.apache.sling.testing.mock.caconfig;
diff --git a/src/test/java/org/apache/sling/testing/mock/caconfig/CompatibilityUtil.java b/src/test/java/org/apache/sling/testing/mock/caconfig/CompatibilityUtil.java
new file mode 100644
index 0000000..56a40bf
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/caconfig/CompatibilityUtil.java
@@ -0,0 +1,68 @@
+/*
+ * 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 java.lang.reflect.Method;
+import java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.caconfig.management.ConfigurationManager;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+
+/**
+ * Helper methods interacting with Context-Aware context supporting both version 1.1 and 2.0 of
+ * the Management API via reflection.
+ */
+public class CompatibilityUtil {
+
+ /**
+ * Write configuration for impl 1.2
+ */
+ public static void writeConfig(SlingContext context, Resource contextResource, String configName, Map<String,Object> props) {
+ try {
+ Class<?> configurationPersistDataClass;
+ try {
+ configurationPersistDataClass = Class.forName("org.apache.sling.caconfig.spi.ConfigurationPersistData");
+ }
+ catch (ClassNotFoundException e) {
+ // fallback to caconfig impl 1.1
+ writeConfigImpl11(context, contextResource, configName, props);
+ return;
+ }
+
+ Object persistData = configurationPersistDataClass.getConstructor(Map.class).newInstance(props);
+ ConfigurationManager configManager = context.getService(ConfigurationManager.class);
+ Method persistMethod = ConfigurationManager.class.getMethod("persistConfiguration", Resource.class, String.class, configurationPersistDataClass);
+ persistMethod.invoke(configManager, contextResource, configName, persistData);
+ }
+ catch (Exception ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ /**
+ * Fallback: Write configuration for impl 1.1
+ */
+ public static void writeConfigImpl11(SlingContext context, Resource contextResource, String configName, Map<String,Object> props) throws Exception {
+ ConfigurationManager configManager = context.getService(ConfigurationManager.class);
+ Method persistMethod = ConfigurationManager.class.getMethod("persist", Resource.class, String.class, Map.class);
+ persistMethod.invoke(configManager, contextResource, configName, props);
+ }
+
+}
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
new file mode 100644
index 0000000..bacb27d
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/caconfig/ContextPluginsTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.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.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;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+public class ContextPluginsTest {
+
+ private static final String CONFIG_NAME = "testConfig";
+
+ @Rule
+ public SlingContext context = new SlingContextBuilder().plugin(CACONFIG).build();
+
+ private Resource contextResource;
+
+ @Before
+ public void setUp() {
+ context.create().resource("/content/site1", "sling:configRef", "/conf/site1");
+ contextResource = context.create().resource("/content/site1/page1");
+
+ // register configuration annotation class
+ MockContextAwareConfig.registerAnnotationClasses(context, SimpleConfig.class);
+
+ // write config
+ CompatibilityUtil.writeConfig(context, contextResource, CONFIG_NAME, ImmutableMap.<String, Object>of(
+ "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("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();
+
+}