You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2018/07/31 11:47:00 UTC
[sling-org-apache-sling-testing-sling-mock] branch master updated:
SLING-7712 sling-mock: make register Sling Models from classpath optional
This is an automated email from the ASF dual-hosted git repository.
sseifert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
The following commit(s) were added to refs/heads/master by this push:
new 8415c24 SLING-7712 sling-mock: make register Sling Models from classpath optional
8415c24 is described below
commit 8415c24609e6c40b59d2227038b9a8e5766f7a21
Author: sseifert <ss...@pro-vision.de>
AuthorDate: Tue Jul 31 13:46:42 2018 +0200
SLING-7712 sling-mock: make register Sling Models from classpath optional
---
.../mock/sling/context/SlingContextImpl.java | 9 ++++-
.../testing/mock/sling/context/package-info.java | 2 +-
.../testing/mock/sling/junit/SlingContext.java | 15 ++++++++
.../mock/sling/junit/SlingContextBuilder.java | 14 ++++++-
.../testing/mock/sling/junit/package-info.java | 2 +-
.../context/AbstractSlingContextImplTest.java | 8 ++++
.../context/NoSlingModelsRegistrationTest.java | 44 ++++++++++++++++++++++
.../modelsautoreg/ClasspathRegisteredModel.java} | 19 ++++++++--
src/test/resources/META-INF/MANIFEST.MF | 1 +
9 files changed, 107 insertions(+), 7 deletions(-)
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java b/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
index f379178..247fe9a 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
@@ -84,6 +84,7 @@ public class SlingContextImpl extends OsgiContextImpl {
protected UniqueRoot uniqueRoot;
private Map<String, Object> resourceResolverFactoryActivatorProps;
+ private boolean registerSlingModelsFromClassPath = true;
/**
* @param resourceResolverType Resource resolver type
@@ -96,6 +97,10 @@ public class SlingContextImpl extends OsgiContextImpl {
this.resourceResolverFactoryActivatorProps = props;
}
+ protected void setRegisterSlingModelsFromClassPath(boolean registerSlingModelsFromClassPath) {
+ this.registerSlingModelsFromClassPath = registerSlingModelsFromClassPath;
+ }
+
/**
* Setup actions before test method execution
*/
@@ -165,7 +170,9 @@ public class SlingContextImpl extends OsgiContextImpl {
registerInjectActivateService(new ResourceBuilderFactoryService());
// scan for models defined via bundle headers in classpath
- ModelAdapterFactoryUtil.addModelsForManifestEntries(this.bundleContext());
+ if (registerSlingModelsFromClassPath) {
+ ModelAdapterFactoryUtil.addModelsForManifestEntries(this.bundleContext());
+ }
}
private void registerInjectActivateServiceByClassName(String... classNames) {
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java b/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
index 36a2859..aef0cd0 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
@@ -19,5 +19,5 @@
/**
* Sling context implementation for unit tests.
*/
-@org.osgi.annotation.versioning.Version("3.5")
+@org.osgi.annotation.versioning.Version("3.6")
package org.apache.sling.testing.mock.sling.context;
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java
index bcd5da5..3239c5f 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java
@@ -112,9 +112,24 @@ public final class SlingContext extends SlingContextImpl implements TestRule {
SlingContext(final ContextPlugins contextPlugins,
final Map<String, Object> resourceResolverFactoryActivatorProps,
final ResourceResolverType resourceResolverType) {
+ this(contextPlugins, resourceResolverFactoryActivatorProps, resourceResolverType, true);
+ }
+
+ /**
+ * Initialize Sling context with resource resolver type.
+ * @param contextPlugins Context plugins
+ * @param resourceResolverFactoryActivatorProps Allows to override OSGi configuration parameters for the Resource Resolver Factory Activator service.
+ * @param resourceResolverType Resource resolver type.
+ * @param registerSlingModelsFromClassPath Automatic registering of all Sling Models found in the classpath on startup.
+ */
+ SlingContext(final ContextPlugins contextPlugins,
+ final Map<String, Object> resourceResolverFactoryActivatorProps,
+ final ResourceResolverType resourceResolverType,
+ final boolean registerSlingModelsFromClassPath) {
this.plugins = contextPlugins;
setResourceResolverFactoryActivatorProps(resourceResolverFactoryActivatorProps);
+ setRegisterSlingModelsFromClassPath(registerSlingModelsFromClassPath);
// set resource resolver type in parent context
setResourceResolverType(resourceResolverType);
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java
index b9ca1c4..0a2ccc7 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java
@@ -36,6 +36,7 @@ public final class SlingContextBuilder {
private final ContextPlugins plugins = new ContextPlugins();
private ResourceResolverType resourceResolverType;
private Map<String, Object> resourceResolverFactoryActivatorProps;
+ private boolean registerSlingModelsFromClassPath = true;
/**
* Create builder with default resource resolver type.
@@ -125,12 +126,23 @@ public final class SlingContextBuilder {
}
/**
+ * Automatic registering of all Sling Models found in the classpath on startup (active by default).
+ * @param registerSlingModelsFromClassPath If set to false Sling Models are not registered automatically from the classpath on startup.
+ * @return this
+ */
+ public SlingContextBuilder registerSlingModelsFromClassPath(boolean registerSlingModelsFromClassPath) {
+ this.registerSlingModelsFromClassPath = registerSlingModelsFromClassPath;
+ return this;
+ }
+
+ /**
* @return Build {@link SlingContext} instance.
*/
public SlingContext build() {
return new SlingContext(this.plugins,
this.resourceResolverFactoryActivatorProps,
- this.resourceResolverType);
+ this.resourceResolverType,
+ this.registerSlingModelsFromClassPath);
}
}
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java b/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
index a998c8f..ae9cd05 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
@@ -19,5 +19,5 @@
/**
* Rule for providing easy access to Sling context in JUnit tests.
*/
-@org.osgi.annotation.versioning.Version("4.1")
+@org.osgi.annotation.versioning.Version("4.2")
package org.apache.sling.testing.mock.sling.junit;
diff --git a/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java b/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java
index 4c2d13b..5f7de84 100644
--- a/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java
@@ -34,6 +34,7 @@ import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.context.models.OsgiServiceModel;
import org.apache.sling.testing.mock.sling.context.models.RequestAttributeModel;
import org.apache.sling.testing.mock.sling.context.models.ServiceInterface;
+import org.apache.sling.testing.mock.sling.context.modelsautoreg.ClasspathRegisteredModel;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.apache.sling.testing.mock.sling.loader.ContentLoader;
import org.apache.sling.testing.mock.sling.services.MockMimeTypeService;
@@ -122,6 +123,13 @@ public abstract class AbstractSlingContextImplTest {
}
@Test
+ public void testSlnigModelClasspathRegistered() {
+ context.request().setAttribute("prop1", "myValue");
+ ClasspathRegisteredModel model = context.request().adaptTo(ClasspathRegisteredModel.class);
+ assertEquals("myValue", model.getProp1());
+ }
+
+ @Test
public void testAdaptToInterface() {
context.request().setAttribute("prop1", "myValue");
ServiceInterface model = context.request().adaptTo(ServiceInterface.class);
diff --git a/src/test/java/org/apache/sling/testing/mock/sling/context/NoSlingModelsRegistrationTest.java b/src/test/java/org/apache/sling/testing/mock/sling/context/NoSlingModelsRegistrationTest.java
new file mode 100644
index 0000000..a034c51
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/sling/context/NoSlingModelsRegistrationTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.sling.context;
+
+import static org.junit.Assert.assertNull;
+
+import org.apache.sling.testing.mock.sling.context.modelsautoreg.ClasspathRegisteredModel;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.apache.sling.testing.mock.sling.junit.SlingContextBuilder;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class NoSlingModelsRegistrationTest {
+
+ @Rule
+ public SlingContext context = new SlingContextBuilder()
+ .registerSlingModelsFromClassPath(false)
+ .build();
+
+ @Test
+ public void testSlnigModelClasspathRegistered() {
+ context.request().setAttribute("prop1", "myValue");
+ ClasspathRegisteredModel model = context.request().adaptTo(ClasspathRegisteredModel.class);
+ // expect null because ClasspathRegisteredModel should not be registered automatically from classpath
+ assertNull(model);
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java b/src/test/java/org/apache/sling/testing/mock/sling/context/modelsautoreg/ClasspathRegisteredModel.java
similarity index 63%
copy from src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
copy to src/test/java/org/apache/sling/testing/mock/sling/context/modelsautoreg/ClasspathRegisteredModel.java
index a998c8f..b5c7f81 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java
+++ b/src/test/java/org/apache/sling/testing/mock/sling/context/modelsautoreg/ClasspathRegisteredModel.java
@@ -16,8 +16,21 @@
* specific language governing permissions and limitations
* under the License.
*/
+package org.apache.sling.testing.mock.sling.context.modelsautoreg;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.models.annotations.Model;
+
/**
- * Rule for providing easy access to Sling context in JUnit tests.
+ * For testing Sling Models support.
+ * This model is registered automatically via OSGi metadata (MANIFEST) in the classpath.
*/
-@org.osgi.annotation.versioning.Version("4.1")
-package org.apache.sling.testing.mock.sling.junit;
+@Model(adaptables = SlingHttpServletRequest.class)
+public interface ClasspathRegisteredModel {
+
+ @Inject
+ String getProp1();
+
+}
diff --git a/src/test/resources/META-INF/MANIFEST.MF b/src/test/resources/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..5423d41
--- /dev/null
+++ b/src/test/resources/META-INF/MANIFEST.MF
@@ -0,0 +1 @@
+Sling-Model-Packages: org.apache.sling.testing.mock.sling.context.modelsautoreg