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

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

commit 3f1b5cf92a12f834007657dcc7577585647115c0
Author: Stefan Seifert <[email protected]>
AuthorDate: Sat Dec 3 10:01:00 2016 +0000

    SLING-6359 osgi-mock, sling-mock: Make ContextCallback and ContextBuilder 
more flexible
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1772443 
13f79535-47bb-0310-9956-ffa450edef68
---
 ...sgiContextCallback.java => CallbackParams.java} | 33 ++++----
 ...giContextCallback.java => ContextCallback.java} | 12 +--
 .../sling/testing/mock/osgi/junit/OsgiContext.java | 87 +++++++++++++-------
 .../mock/osgi/junit/OsgiContextBuilder.java        | 92 ++++++++++++++++++++++
 .../mock/osgi/junit/OsgiContextCallback.java       | 13 +--
 .../testing/mock/osgi/junit/package-info.java      |  2 +-
 .../testing/mock/osgi/junit/OsgiContextTest.java   | 20 +++--
 7 files changed, 194 insertions(+), 65 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
 b/src/main/java/org/apache/sling/testing/mock/osgi/junit/CallbackParams.java
similarity index 55%
copy from 
src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
copy to 
src/main/java/org/apache/sling/testing/mock/osgi/junit/CallbackParams.java
index a283f38..a7d69a1 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/junit/CallbackParams.java
@@ -18,19 +18,24 @@
  */
 package org.apache.sling.testing.mock.osgi.junit;
 
-import java.io.IOException;
-
-/**
- * Callback-interface for application-specific setup and teardown operations to
- * customize the {@link OsgiContext} JUnit rule.
- */
-public interface OsgiContextCallback {
-
-    /**
-     * Execute callback action
-     * @param context OSGi context
-     * @throws IOException I/O exception
-     */
-    void execute(OsgiContext context) throws IOException;
+final class CallbackParams {
 
+    ContextCallback[] beforeSetUpCallback;
+    ContextCallback[] afterSetUpCallback;
+    ContextCallback[] beforeTearDownCallback;
+    ContextCallback[] afterTearDownCallback;
+    
+    CallbackParams() {
+        // no callbacks
+    }
+    
+    CallbackParams(ContextCallback afterSetUpCallback) {
+        this.afterSetUpCallback = new ContextCallback[] { afterSetUpCallback 
}; 
+    }
+    
+    CallbackParams(ContextCallback afterSetUpCallback, ContextCallback 
beforeTearDownCallback) {
+        this.afterSetUpCallback = new ContextCallback[] { afterSetUpCallback 
}; 
+        this.beforeTearDownCallback = new ContextCallback[] { 
beforeTearDownCallback }; 
+    }
+    
 }
diff --git 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
 b/src/main/java/org/apache/sling/testing/mock/osgi/junit/ContextCallback.java
similarity index 77%
copy from 
src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
copy to 
src/main/java/org/apache/sling/testing/mock/osgi/junit/ContextCallback.java
index a283f38..12f0c29 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
+++ 
b/src/main/java/org/apache/sling/testing/mock/osgi/junit/ContextCallback.java
@@ -18,19 +18,19 @@
  */
 package org.apache.sling.testing.mock.osgi.junit;
 
-import java.io.IOException;
+import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl;
 
 /**
  * Callback-interface for application-specific setup and teardown operations to
- * customize the {@link OsgiContext} JUnit rule.
+ * customize the {@link SlingContext} JUnit rule.
  */
-public interface OsgiContextCallback {
+public interface ContextCallback<T extends OsgiContextImpl> {
 
     /**
      * Execute callback action
-     * @param context OSGi context
-     * @throws IOException I/O exception
+     * @param context Sling context
+     * @throws Exception exception
      */
-    void execute(OsgiContext context) throws IOException;
+    void execute(T context) throws Exception;
 
 }
diff --git 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java 
b/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java
index cd527b1..d64876d 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContext.java
@@ -29,53 +29,54 @@ import org.junit.runners.model.Statement;
  */
 public final class OsgiContext extends OsgiContextImpl implements TestRule {
 
-    private final OsgiContextCallback setUpCallback;
-    private final OsgiContextCallback tearDownCallback;
+    private final CallbackParams callbackParams;
     private final TestRule delegate;
 
     /**
      * Initialize OSGi context.
      */
     public OsgiContext() {
-        this(null, null);
+        this(new CallbackParams());
     }
 
     /**
      * Initialize OSGi context.
-     * @param setUpCallback Allows the application to register an own callback
-     *            function that is called after the built-in setup rules are
-     *            executed.
+     * @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 OsgiContextCallback setUpCallback) {
-        this(setUpCallback, null);
+    public OsgiContext(final ContextCallback afterSetUpCallback) {
+        this(new CallbackParams(afterSetUpCallback));
     }
 
     /**
      * Initialize OSGi context.
-     * @param setUpCallback Allows the application to register an own callback
-     *            function that is called after the built-in setup rules are
-     *            executed.
-     * @param tearDownCallback Allows the application to register an own
-     *            callback function that is called before the built-in teardown
-     *            rules are executed.
+     * @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 OsgiContextCallback setUpCallback, final 
OsgiContextCallback tearDownCallback) {
+    public OsgiContext(final ContextCallback afterSetUpCallback, final 
ContextCallback beforeTearDownCallback) {
+        this(new CallbackParams(afterSetUpCallback, beforeTearDownCallback));
+    }
 
-        this.setUpCallback = setUpCallback;
-        this.tearDownCallback = tearDownCallback;
+    /**
+     * Initialize OSGi context with resource resolver type.
+     * @param callbackParams Callback parameters
+     */
+    OsgiContext(final CallbackParams callbackParams) {
+        this.callbackParams = callbackParams;
 
         // wrap {@link ExternalResource} rule executes each test method once
         this.delegate = new ExternalResource() {
             @Override
             protected void before() {
+                OsgiContext.this.executeBeforeSetUpCallback();
                 OsgiContext.this.setUp();
-                OsgiContext.this.executeSetUpCallback();
+                OsgiContext.this.executeAfterSetUpCallback();
             }
 
             @Override
             protected void after() {
-                OsgiContext.this.executeTearDownCallback();
+                OsgiContext.this.executeBeforeTearDownCallback();
                 OsgiContext.this.tearDown();
+                OsgiContext.this.executeAfterTearDownCallback();
             }
         };
     }
@@ -85,22 +86,54 @@ public final class OsgiContext extends OsgiContextImpl 
implements TestRule {
         return this.delegate.apply(base, description);
     }
 
-    private void executeSetUpCallback() {
-        if (this.setUpCallback != null) {
+    @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 {
-                this.setUpCallback.execute(this);
+                for (ContextCallback callback : 
callbackParams.beforeTearDownCallback) {
+                    callback.execute(this);
+                }
             } catch (Throwable ex) {
-                throw new RuntimeException("Setup failed: " + ex.getMessage(), 
ex);
+                throw new RuntimeException("Before teardown failed: " + 
ex.getMessage(), ex);
             }
         }
     }
 
-    private void executeTearDownCallback() {
-        if (this.tearDownCallback != null) {
+    @SuppressWarnings("unchecked")
+    private void executeAfterTearDownCallback() {
+        if (callbackParams.afterTearDownCallback != null) {
             try {
-                this.tearDownCallback.execute(this);
+                for (ContextCallback callback : 
callbackParams.afterTearDownCallback) {
+                    callback.execute(this);
+                }
             } catch (Throwable ex) {
-                throw new RuntimeException("Teardown failed: " + 
ex.getMessage(), ex);
+                throw new RuntimeException("After teardown failed: " + 
ex.getMessage(), ex);
             }
         }
     }
diff --git 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextBuilder.java
 
b/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextBuilder.java
new file mode 100644
index 0000000..f6d6305
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextBuilder.java
@@ -0,0 +1,92 @@
+/*
+ * 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.junit;
+
+/**
+ * Builder class for creating {@link OsgiContext} instances with different 
sets of parameters.
+ */
+public final class OsgiContextBuilder {
+    
+    private final CallbackParams callbackParams = new CallbackParams();
+    
+    /**
+     * Create builder with default resource resolver type.
+     */
+    public OsgiContextBuilder() {}
+    
+    /**
+     * @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 setUp(ContextCallback... afterSetUpCallback) {
+        return afterSetUp(afterSetUpCallback);
+    }
+
+    /**
+     * @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;
+        return this;
+    }
+
+    /**
+     * @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;
+        return this;
+    }
+
+    /**
+     * @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;
+        return this;
+    }
+
+    /**
+     * @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;
+        return this;
+    }
+
+    /**
+     * @return Build {@link OsgiContext} instance.
+     */
+    public OsgiContext build() {
+        return new OsgiContext(callbackParams);
+    }
+    
+}
diff --git 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
 
b/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
index a283f38..779e86f 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
+++ 
b/src/main/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextCallback.java
@@ -18,19 +18,12 @@
  */
 package org.apache.sling.testing.mock.osgi.junit;
 
-import java.io.IOException;
-
 /**
  * Callback-interface for application-specific setup and teardown operations to
  * customize the {@link OsgiContext} JUnit rule.
  */
-public interface OsgiContextCallback {
-
-    /**
-     * Execute callback action
-     * @param context OSGi context
-     * @throws IOException I/O exception
-     */
-    void execute(OsgiContext context) throws IOException;
+public interface OsgiContextCallback extends ContextCallback<OsgiContext> {
 
+    // specialized version of ContextCallback
+    
 }
diff --git 
a/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java 
b/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java
index eeb2980..2295cbc 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/junit/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Rule for providing easy access to OSGi context in JUnit tests.
  */
[email protected]("1.1")
[email protected]("2.0")
 package org.apache.sling.testing.mock.osgi.junit;
diff --git 
a/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java 
b/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java
index 0de149b..840e951 100644
--- 
a/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java
+++ 
b/src/test/java/org/apache/sling/testing/mock/osgi/junit/OsgiContextTest.java
@@ -22,8 +22,6 @@ import static org.junit.Assert.assertNotNull;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
-import java.io.IOException;
-
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -33,16 +31,24 @@ import org.mockito.runners.MockitoJUnitRunner;
 @RunWith(MockitoJUnitRunner.class)
 public class OsgiContextTest {
 
-    private final OsgiContextCallback contextSetup = 
mock(OsgiContextCallback.class);
-    private final OsgiContextCallback contextTeardown = 
mock(OsgiContextCallback.class);
+    private final OsgiContextCallback contextBeforeSetup = 
mock(OsgiContextCallback.class);
+    private final OsgiContextCallback contextAfterSetup = 
mock(OsgiContextCallback.class);
+    private final OsgiContextCallback contextBeforeTeardown = 
mock(OsgiContextCallback.class);
+    private final OsgiContextCallback contextAfterTeardown = 
mock(OsgiContextCallback.class);
 
     // Run all unit tests for each resource resolver types listed here
     @Rule
-    public OsgiContext context = new OsgiContext(contextSetup, 
contextTeardown);
+    public OsgiContext context = new OsgiContextBuilder()
+        .beforeSetUp(contextBeforeSetup)
+        .afterSetUp(contextAfterSetup)
+        .beforeTearDown(contextBeforeTeardown)
+        .afterTearDown(contextAfterTeardown)
+        .build();
 
     @Before
-    public void setUp() throws IOException {
-        verify(contextSetup).execute(context);
+    public void setUp() throws Exception {
+        verify(contextBeforeSetup).execute(context);
+        verify(contextAfterSetup).execute(context);
     }
 
     @Test

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to