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 2016/12/03 13:14:35 UTC
svn commit: r1772466 [2/2] - in /sling:
branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/context/
branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/junit/
branches/testing/mocks/o...
Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java Sat Dec 3 13:14:34 2016
@@ -18,65 +18,72 @@
*/
package org.apache.sling.testing.mock.osgi.junit;
+import org.apache.sling.testing.mock.osgi.context.ContextCallback;
+import org.apache.sling.testing.mock.osgi.context.ContextPlugins;
import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
+import org.osgi.annotation.versioning.ProviderType;
/**
* JUnit rule for setting up and tearing down OSGi context for unit tests.
*/
+@ProviderType
public final class OsgiContext extends OsgiContextImpl implements TestRule {
- private final CallbackParams callbackParams;
+ private final ContextPlugins plugins;
private final TestRule delegate;
/**
* Initialize OSGi context.
*/
public OsgiContext() {
- this(new CallbackParams());
+ this(new ContextPlugins());
}
/**
* Initialize OSGi context.
+ * @param <T> context type
* @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
*/
- public OsgiContext(final ContextCallback afterSetUpCallback) {
- this(new CallbackParams(afterSetUpCallback));
+ public <T extends OsgiContextImpl> OsgiContext(final ContextCallback<T> afterSetUpCallback) {
+ this(new ContextPlugins(afterSetUpCallback));
}
/**
* Initialize OSGi context.
+ * @param <U> context type
+ * @param <V> context type
* @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
* @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed.
*/
- public OsgiContext(final ContextCallback afterSetUpCallback, final ContextCallback beforeTearDownCallback) {
- this(new CallbackParams(afterSetUpCallback, beforeTearDownCallback));
+ public <U extends OsgiContextImpl, V extends OsgiContextImpl> OsgiContext(final ContextCallback<U> afterSetUpCallback, final ContextCallback<V> beforeTearDownCallback) {
+ this(new ContextPlugins(afterSetUpCallback, beforeTearDownCallback));
}
/**
* Initialize OSGi context with resource resolver type.
- * @param callbackParams Callback parameters
+ * @param contextPlugins Context plugins
*/
- OsgiContext(final CallbackParams callbackParams) {
- this.callbackParams = callbackParams;
+ OsgiContext(final ContextPlugins contextPlugins) {
+ this.plugins = contextPlugins;
// wrap {@link ExternalResource} rule executes each test method once
this.delegate = new ExternalResource() {
@Override
protected void before() {
- OsgiContext.this.executeBeforeSetUpCallback();
+ plugins.executeBeforeSetUpCallback(OsgiContext.this);
OsgiContext.this.setUp();
- OsgiContext.this.executeAfterSetUpCallback();
+ plugins.executeAfterSetUpCallback(OsgiContext.this);
}
@Override
protected void after() {
- OsgiContext.this.executeBeforeTearDownCallback();
+ plugins.executeBeforeTearDownCallback(OsgiContext.this);
OsgiContext.this.tearDown();
- OsgiContext.this.executeAfterTearDownCallback();
+ plugins.executeAfterTearDownCallback(OsgiContext.this);
}
};
}
@@ -86,56 +93,4 @@ public final class OsgiContext extends O
return this.delegate.apply(base, description);
}
- @SuppressWarnings("unchecked")
- private void executeBeforeSetUpCallback() {
- if (callbackParams.beforeSetUpCallback != null) {
- try {
- for (ContextCallback callback : callbackParams.beforeSetUpCallback) {
- callback.execute(this);
- }
- } catch (Throwable ex) {
- throw new RuntimeException("Before setup failed: " + ex.getMessage(), ex);
- }
- }
- }
-
- @SuppressWarnings("unchecked")
- private void executeAfterSetUpCallback() {
- if (callbackParams.afterSetUpCallback != null) {
- try {
- for (ContextCallback callback : callbackParams.afterSetUpCallback) {
- callback.execute(this);
- }
- } catch (Throwable ex) {
- throw new RuntimeException("After setup failed: " + ex.getMessage(), ex);
- }
- }
- }
-
- @SuppressWarnings("unchecked")
- private void executeBeforeTearDownCallback() {
- if (callbackParams.beforeTearDownCallback != null) {
- try {
- for (ContextCallback callback : callbackParams.beforeTearDownCallback) {
- callback.execute(this);
- }
- } catch (Throwable ex) {
- throw new RuntimeException("Before teardown failed: " + ex.getMessage(), ex);
- }
- }
- }
-
- @SuppressWarnings("unchecked")
- private void executeAfterTearDownCallback() {
- if (callbackParams.afterTearDownCallback != null) {
- try {
- for (ContextCallback callback : callbackParams.afterTearDownCallback) {
- callback.execute(this);
- }
- } catch (Throwable ex) {
- throw new RuntimeException("After teardown failed: " + ex.getMessage(), ex);
- }
- }
- }
-
}
Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextBuilder.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextBuilder.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextBuilder.java Sat Dec 3 13:14:34 2016
@@ -18,12 +18,19 @@
*/
package org.apache.sling.testing.mock.osgi.junit;
+import org.apache.sling.testing.mock.osgi.context.ContextPlugins;
+import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl;
+import org.apache.sling.testing.mock.osgi.context.ContextCallback;
+import org.apache.sling.testing.mock.osgi.context.ContextPlugin;
+import org.osgi.annotation.versioning.ProviderType;
+
/**
* Builder class for creating {@link OsgiContext} instances with different sets of parameters.
*/
+@ProviderType
public final class OsgiContextBuilder {
- private final CallbackParams callbackParams = new CallbackParams();
+ private final ContextPlugins plugins = new ContextPlugins();
/**
* Create builder with default resource resolver type.
@@ -31,54 +38,57 @@ public final class OsgiContextBuilder {
public OsgiContextBuilder() {}
/**
- * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
+ * @param <T> context type
+ * @param plugin Context plugin which listens to context lifecycle events.
* @return this
*/
- public OsgiContextBuilder setUp(ContextCallback... afterSetUpCallback) {
- return afterSetUp(afterSetUpCallback);
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> OsgiContextBuilder plugin(ContextPlugin<T>... plugin) {
+ plugins.addPlugin(plugin);
+ return this;
}
/**
+ * @param <T> context type
* @param beforeSetUpCallback Allows the application to register an own callback function that is called before the built-in setup rules are executed.
* @return this
*/
- public OsgiContextBuilder beforeSetUp(ContextCallback... beforeSetUpCallback) {
- callbackParams.beforeSetUpCallback = beforeSetUpCallback;
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> OsgiContextBuilder beforeSetUp(ContextCallback<T>... beforeSetUpCallback) {
+ plugins.addBeforeSetUpCallback(beforeSetUpCallback);
return this;
}
/**
+ * @param <T> context type
* @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
* @return this
*/
- public OsgiContextBuilder afterSetUp(ContextCallback... afterSetUpCallback) {
- callbackParams.afterSetUpCallback = afterSetUpCallback;
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> OsgiContextBuilder afterSetUp(ContextCallback<T>... afterSetUpCallback) {
+ plugins.addAfterSetUpCallback(afterSetUpCallback);
return this;
}
/**
+ * @param <T> context type
* @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed.
* @return this
*/
- public OsgiContextBuilder tearDown(ContextCallback... beforeTearDownCallback) {
- return beforeTearDown(beforeTearDownCallback);
- }
-
- /**
- * @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed.
- * @return this
- */
- public OsgiContextBuilder beforeTearDown(ContextCallback... beforeTearDownCallback) {
- callbackParams.beforeTearDownCallback = beforeTearDownCallback;
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> OsgiContextBuilder beforeTearDown(ContextCallback<T>... beforeTearDownCallback) {
+ plugins.addBeforeTearDownCallback(beforeTearDownCallback);
return this;
}
/**
+ * @param <T> context type
* @param afterTearDownCallback Allows the application to register an own callback function that is after before the built-in teardown rules are executed.
* @return this
*/
- public OsgiContextBuilder afterTearDown(ContextCallback... afterTearDownCallback) {
- callbackParams.afterTearDownCallback = afterTearDownCallback;
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> OsgiContextBuilder afterTearDown(ContextCallback<T>... afterTearDownCallback) {
+ plugins.addAfterTearDownCallback(afterTearDownCallback);
return this;
}
@@ -86,7 +96,7 @@ public final class OsgiContextBuilder {
* @return Build {@link OsgiContext} instance.
*/
public OsgiContext build() {
- return new OsgiContext(callbackParams);
+ return new OsgiContext(plugins);
}
}
Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java Sat Dec 3 13:14:34 2016
@@ -18,10 +18,14 @@
*/
package org.apache.sling.testing.mock.osgi.junit;
+import org.apache.sling.testing.mock.osgi.context.ContextCallback;
+import org.osgi.annotation.versioning.ConsumerType;
+
/**
- * Callback-interface for application-specific setup and teardown operations to
+ * Callback interface for application-specific setup and teardown operations to
* customize the {@link OsgiContext} JUnit rule.
*/
+@ConsumerType
public interface OsgiContextCallback extends ContextCallback<OsgiContext> {
// specialized version of ContextCallback
Added: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/ContextPluginsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/ContextPluginsTest.java?rev=1772466&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/ContextPluginsTest.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/ContextPluginsTest.java Sat Dec 3 13:14:34 2016
@@ -0,0 +1,175 @@
+/*
+ * 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.osgi.context;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+@SuppressWarnings("unchecked")
+public class ContextPluginsTest {
+
+ private OsgiContext context = new OsgiContext();
+
+ @Mock
+ private ContextPlugin plugin1;
+ @Mock
+ private ContextPlugin plugin2;
+ @Mock
+ private ContextCallback callback1;
+ @Mock
+ private ContextCallback callback2;
+
+ @Test
+ public void testConstructorSetUp() throws Exception {
+ ContextPlugins underTest = new ContextPlugins(callback1);
+
+ assertEquals(1, underTest.getPlugins().size());
+
+ underTest.executeAfterSetUpCallback(context);
+
+ verify(callback1, times(1)).execute(context);
+ }
+
+ @Test
+ public void testConstructorSetUpTearDown() throws Exception {
+ ContextPlugins underTest = new ContextPlugins(callback1, callback2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeAfterSetUpCallback(context);
+ underTest.executeBeforeTearDownCallback(context);
+
+ verify(callback1, times(1)).execute(context);
+ verify(callback2, times(1)).execute(context);
+ }
+
+ @Test
+ public void testExecuteBeforeSetUpCallback() throws Exception {
+ ContextPlugins underTest = new ContextPlugins();
+ underTest.addPlugin(plugin1, plugin2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeBeforeSetUpCallback(context);
+ verify(plugin1, times(1)).beforeSetUp(context);
+ verify(plugin2, times(1)).beforeSetUp(context);
+ verifyNoMoreInteractions(plugin1);
+ verifyNoMoreInteractions(plugin2);
+ }
+
+ @Test
+ public void testExecuteAfterSetUpCallback() throws Exception {
+ ContextPlugins underTest = new ContextPlugins();
+ underTest.addPlugin(plugin1, plugin2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeAfterSetUpCallback(context);
+ verify(plugin1, times(1)).afterSetUp(context);
+ verify(plugin2, times(1)).afterSetUp(context);
+ verifyNoMoreInteractions(plugin1);
+ verifyNoMoreInteractions(plugin2);
+ }
+
+ @Test
+ public void testExecuteBeforeTearDownCallback() throws Exception {
+ ContextPlugins underTest = new ContextPlugins();
+ underTest.addPlugin(plugin1, plugin2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeBeforeTearDownCallback(context);
+ verify(plugin1, times(1)).beforeTearDown(context);
+ verify(plugin2, times(1)).beforeTearDown(context);
+ verifyNoMoreInteractions(plugin1);
+ verifyNoMoreInteractions(plugin2);
+ }
+
+ @Test
+ public void testExecuteAfterTearDownCallback() throws Exception {
+ ContextPlugins underTest = new ContextPlugins();
+ underTest.addPlugin(plugin1, plugin2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeAfterTearDownCallback(context);
+ verify(plugin1, times(1)).afterTearDown(context);
+ verify(plugin2, times(1)).afterTearDown(context);
+ verifyNoMoreInteractions(plugin1);
+ verifyNoMoreInteractions(plugin2);
+ }
+
+ @Test
+ public void testAddBeforeSetUpCallback() throws Exception {
+ ContextPlugins underTest = new ContextPlugins();
+ underTest.addBeforeSetUpCallback(callback1, callback2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeBeforeSetUpCallback(context);
+ verify(callback1, times(1)).execute(context);
+ verify(callback2, times(1)).execute(context);
+ }
+
+ @Test
+ public void testAddAfterSetUpCallback() throws Exception {
+ ContextPlugins underTest = new ContextPlugins();
+ underTest.addAfterSetUpCallback(callback1, callback2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeAfterSetUpCallback(context);
+ verify(callback1, times(1)).execute(context);
+ verify(callback2, times(1)).execute(context);
+ }
+
+ @Test
+ public void testAddBeforeTearDownCallback() throws Exception {
+ ContextPlugins underTest = new ContextPlugins();
+ underTest.addBeforeTearDownCallback(callback1, callback2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeBeforeTearDownCallback(context);
+ verify(callback1, times(1)).execute(context);
+ verify(callback2, times(1)).execute(context);
+ }
+
+ @Test
+ public void testAddAfterTearDownCallback() throws Exception {
+ ContextPlugins underTest = new ContextPlugins();
+ underTest.addAfterTearDownCallback(callback1, callback2);
+
+ assertEquals(2, underTest.getPlugins().size());
+
+ underTest.executeAfterTearDownCallback(context);
+ verify(callback1, times(1)).execute(context);
+ verify(callback2, times(1)).execute(context);
+ }
+
+}
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/ContextPluginsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/ContextPluginsTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sat Dec 3 13:14:34 2016
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author
Propchange: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/context/ContextPluginsTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java Sat Dec 3 13:14:34 2016
@@ -24,11 +24,13 @@ import org.apache.sling.api.resource.Log
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.testing.mock.sling.MockSling;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.osgi.annotation.versioning.ProviderType;
import org.osgi.framework.BundleContext;
/**
* Create resolve resolver instance and initialize it depending on it's type.
*/
+@ProviderType
final class ContextResourceResolverFactory {
private ContextResourceResolverFactory() {
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java Sat Dec 3 13:14:34 2016
@@ -34,6 +34,7 @@ import java.util.Vector;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.osgi.annotation.versioning.ProviderType;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
@@ -45,6 +46,7 @@ import org.reflections.Reflections;
/**
* Helper methos for simulating sling models bundle events.
*/
+@ProviderType
final class ModelAdapterFactoryUtil {
static {
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java Sat Dec 3 13:14:34 2016
@@ -26,6 +26,7 @@ import org.apache.jackrabbit.JcrConstant
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
+import org.osgi.annotation.versioning.ConsumerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,6 +38,7 @@ import com.google.common.collect.Immutab
* where the repository is not cleaned for each test run. This class provides
* unique root paths for each run, and cleans them up when done.
*/
+@ConsumerType
public class UniqueRoot {
private final SlingContextImpl context;
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java Sat Dec 3 13:14:34 2016
@@ -19,5 +19,5 @@
/**
* Sling context implementation for unit tests.
*/
-@org.osgi.annotation.versioning.Version("3.3")
+@org.osgi.annotation.versioning.Version("3.4")
package org.apache.sling.testing.mock.sling.context;
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java Sat Dec 3 13:14:34 2016
@@ -20,22 +20,26 @@ package org.apache.sling.testing.mock.sl
import java.util.Map;
-import org.apache.sling.testing.mock.osgi.junit.ContextCallback;
+import org.apache.sling.testing.mock.osgi.context.ContextCallback;
+import org.apache.sling.testing.mock.osgi.context.ContextPlugins;
+import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.context.SlingContextImpl;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
+import org.osgi.annotation.versioning.ProviderType;
/**
* JUnit rule for setting up and tearing down Sling context objects for unit tests.
* You can use {@link SlingContextBuilder} alternatively to the constructors on this class - it offers
* more options and fine-grained control about setting up the test context.
*/
+@ProviderType
public final class SlingContext extends SlingContextImpl implements TestRule {
- private final CallbackParams callbackParams;
+ private final ContextPlugins plugins;
private final TestRule delegate;
/**
@@ -43,7 +47,7 @@ public final class SlingContext extends
* {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}.
*/
public SlingContext() {
- this(new CallbackParams(), null, null);
+ this(new ContextPlugins(), null, null);
}
/**
@@ -51,59 +55,65 @@ public final class SlingContext extends
* @param resourceResolverType Resource resolver type.
*/
public SlingContext(final ResourceResolverType resourceResolverType) {
- this(new CallbackParams(), null, resourceResolverType);
+ this(new ContextPlugins(), null, resourceResolverType);
}
/**
* Initialize Sling context with default resource resolver type:
* {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}.
+ * @param <T> context type
* @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
*/
- public SlingContext(final ContextCallback<?> afterSetUpCallback) {
- this(new CallbackParams(afterSetUpCallback), null, null);
+ public <T extends OsgiContextImpl> SlingContext(final ContextCallback<T> afterSetUpCallback) {
+ this(new ContextPlugins(afterSetUpCallback), null, null);
}
/**
* Initialize Sling context with resource resolver type.
+ * @param <T> context type
* @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
* @param resourceResolverType Resource resolver type.
*/
- public SlingContext(final ContextCallback<?> afterSetUpCallback, final ResourceResolverType resourceResolverType) {
- this(new CallbackParams(afterSetUpCallback), null, resourceResolverType);
+ public <T extends OsgiContextImpl> SlingContext(final ContextCallback<T> afterSetUpCallback, final ResourceResolverType resourceResolverType) {
+ this(new ContextPlugins(afterSetUpCallback), null, resourceResolverType);
}
/**
* Initialize Sling context with default resource resolver type:
* {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}.
+ * @param <U> context type
+ * @param <V> context type
* @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
* @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed.
*/
- public SlingContext(final ContextCallback<?> afterSetUpCallback, final ContextCallback<?> beforeTearDownCallback) {
- this(new CallbackParams(afterSetUpCallback, beforeTearDownCallback), null, null);
+ public <U extends OsgiContextImpl, V extends OsgiContextImpl> SlingContext(final ContextCallback<U> afterSetUpCallback, final ContextCallback<V> beforeTearDownCallback) {
+ this(new ContextPlugins(afterSetUpCallback, beforeTearDownCallback), null, null);
}
/**
* Initialize Sling context with resource resolver type.
+ * @param <U> context type
+ * @param <V> context type
* @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
* @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed.
* @param resourceResolverType Resource resolver type.
*/
- public SlingContext(final ContextCallback<?> afterSetUpCallback, final ContextCallback<?> beforeTearDownCallback,
+ public <U extends OsgiContextImpl, V extends OsgiContextImpl> SlingContext(final ContextCallback<U> afterSetUpCallback, final ContextCallback<V> beforeTearDownCallback,
final ResourceResolverType resourceResolverType) {
- this(new CallbackParams(afterSetUpCallback, beforeTearDownCallback), null, resourceResolverType);
+ this(new ContextPlugins(afterSetUpCallback, beforeTearDownCallback), null, resourceResolverType);
}
/**
* Initialize Sling context with resource resolver type.
- * @param callbackParams Callback parameters
+ * @param contextPlugins Context plugins
* @param resourceResolverFactoryActivatorProps Allows to override OSGi configuration parameters for the Resource Resolver Factory Activator service.
* @param resourceResolverType Resource resolver type.
*/
- SlingContext(final CallbackParams callbackParams,
+ SlingContext(final ContextPlugins contextPlugins,
final Map<String, Object> resourceResolverFactoryActivatorProps,
final ResourceResolverType resourceResolverType) {
- this.callbackParams = callbackParams;
+ this.plugins = contextPlugins;
setResourceResolverFactoryActivatorProps(resourceResolverFactoryActivatorProps);
// set resource resolver type in parent context
@@ -113,16 +123,16 @@ public final class SlingContext extends
this.delegate = new ExternalResource() {
@Override
protected void before() {
- SlingContext.this.executeBeforeSetUpCallback();
+ plugins.executeBeforeSetUpCallback(SlingContext.this);
SlingContext.this.setUp();
- SlingContext.this.executeAfterSetUpCallback();
+ plugins.executeAfterSetUpCallback(SlingContext.this);
}
@Override
protected void after() {
- SlingContext.this.executeBeforeTearDownCallback();
+ plugins.executeBeforeTearDownCallback(SlingContext.this);
SlingContext.this.tearDown();
- SlingContext.this.executeAfterTearDownCallback();
+ plugins.executeAfterTearDownCallback(SlingContext.this);
}
};
}
@@ -132,56 +142,4 @@ public final class SlingContext extends
return this.delegate.apply(base, description);
}
- @SuppressWarnings("unchecked")
- private void executeBeforeSetUpCallback() {
- if (callbackParams.beforeSetUpCallback != null) {
- try {
- for (ContextCallback callback : callbackParams.beforeSetUpCallback) {
- callback.execute(this);
- }
- } catch (Throwable ex) {
- throw new RuntimeException("Before setup failed: " + ex.getMessage(), ex);
- }
- }
- }
-
- @SuppressWarnings("unchecked")
- private void executeAfterSetUpCallback() {
- if (callbackParams.afterSetUpCallback != null) {
- try {
- for (ContextCallback callback : callbackParams.afterSetUpCallback) {
- callback.execute(this);
- }
- } catch (Throwable ex) {
- throw new RuntimeException("After setup failed: " + ex.getMessage(), ex);
- }
- }
- }
-
- @SuppressWarnings("unchecked")
- private void executeBeforeTearDownCallback() {
- if (callbackParams.beforeTearDownCallback != null) {
- try {
- for (ContextCallback callback : callbackParams.beforeTearDownCallback) {
- callback.execute(this);
- }
- } catch (Throwable ex) {
- throw new RuntimeException("Before teardown failed: " + ex.getMessage(), ex);
- }
- }
- }
-
- @SuppressWarnings("unchecked")
- private void executeAfterTearDownCallback() {
- if (callbackParams.afterTearDownCallback != null) {
- try {
- for (ContextCallback callback : callbackParams.afterTearDownCallback) {
- callback.execute(this);
- }
- } catch (Throwable ex) {
- throw new RuntimeException("After teardown failed: " + ex.getMessage(), ex);
- }
- }
- }
-
}
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java Sat Dec 3 13:14:34 2016
@@ -20,15 +20,20 @@ package org.apache.sling.testing.mock.sl
import java.util.Map;
-import org.apache.sling.testing.mock.osgi.junit.ContextCallback;
+import org.apache.sling.testing.mock.osgi.context.ContextCallback;
+import org.apache.sling.testing.mock.osgi.context.ContextPlugin;
+import org.apache.sling.testing.mock.osgi.context.ContextPlugins;
+import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.osgi.annotation.versioning.ProviderType;
/**
* Builder class for creating {@link SlingContext} instances with different sets of parameters.
*/
+@ProviderType
public final class SlingContextBuilder {
- private final CallbackParams callbackParams = new CallbackParams();
+ private final ContextPlugins plugins = new ContextPlugins();
private ResourceResolverType resourceResolverType;
private Map<String, Object> resourceResolverFactoryActivatorProps;
@@ -55,54 +60,57 @@ public final class SlingContextBuilder {
}
/**
- * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
+ * @param <T> context type
+ * @param plugin Context plugin which listens to context lifecycle events.
* @return this
*/
- public SlingContextBuilder setUp(ContextCallback... afterSetUpCallback) {
- return afterSetUp(afterSetUpCallback);
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> SlingContextBuilder plugin(ContextPlugin<T>... plugin) {
+ plugins.addPlugin(plugin);
+ return this;
}
/**
+ * @param <T> context type
* @param beforeSetUpCallback Allows the application to register an own callback function that is called before the built-in setup rules are executed.
* @return this
*/
- public SlingContextBuilder beforeSetUp(ContextCallback... beforeSetUpCallback) {
- callbackParams.beforeSetUpCallback = beforeSetUpCallback;
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> SlingContextBuilder beforeSetUp(ContextCallback<T>... beforeSetUpCallback) {
+ plugins.addBeforeSetUpCallback(beforeSetUpCallback);
return this;
}
/**
+ * @param <T> context type
* @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed.
* @return this
*/
- public SlingContextBuilder afterSetUp(ContextCallback... afterSetUpCallback) {
- callbackParams.afterSetUpCallback = afterSetUpCallback;
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> SlingContextBuilder afterSetUp(ContextCallback<T>... afterSetUpCallback) {
+ plugins.addAfterSetUpCallback(afterSetUpCallback);
return this;
}
/**
+ * @param <T> context type
* @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed.
* @return this
*/
- public SlingContextBuilder tearDown(ContextCallback... beforeTearDownCallback) {
- return beforeTearDown(beforeTearDownCallback);
- }
-
- /**
- * @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed.
- * @return this
- */
- public SlingContextBuilder beforeTearDown(ContextCallback... beforeTearDownCallback) {
- callbackParams.beforeTearDownCallback = beforeTearDownCallback;
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> SlingContextBuilder beforeTearDown(ContextCallback<T>... beforeTearDownCallback) {
+ plugins.addBeforeTearDownCallback(beforeTearDownCallback);
return this;
}
/**
+ * @param <T> context type
* @param afterTearDownCallback Allows the application to register an own callback function that is after before the built-in teardown rules are executed.
* @return this
*/
- public SlingContextBuilder afterTearDown(ContextCallback... afterTearDownCallback) {
- callbackParams.afterTearDownCallback = afterTearDownCallback;
+ @SafeVarargs
+ public final <T extends OsgiContextImpl> SlingContextBuilder afterTearDown(ContextCallback<T>... afterTearDownCallback) {
+ plugins.addAfterTearDownCallback(afterTearDownCallback);
return this;
}
@@ -120,7 +128,7 @@ public final class SlingContextBuilder {
* @return Build {@link SlingContext} instance.
*/
public SlingContext build() {
- return new SlingContext(this.callbackParams,
+ return new SlingContext(this.plugins,
this.resourceResolverFactoryActivatorProps,
this.resourceResolverType);
}
Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java?rev=1772466&r1=1772465&r2=1772466&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java Sat Dec 3 13:14:34 2016
@@ -18,12 +18,14 @@
*/
package org.apache.sling.testing.mock.sling.junit;
-import org.apache.sling.testing.mock.osgi.junit.ContextCallback;
+import org.apache.sling.testing.mock.osgi.context.ContextCallback;
+import org.osgi.annotation.versioning.ConsumerType;
/**
- * Callback-interface for application-specific setup and teardown operations to
+ * Callback interface for application-specific setup and teardown operations to
* customize the {@link SlingContext} JUnit rule.
*/
+@ConsumerType
public interface SlingContextCallback extends ContextCallback<SlingContext> {
// specialized version of ContextCallback