You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2014/04/16 12:37:06 UTC

svn commit: r1587856 - in /sling/trunk/testing: samples/integration-tests/ samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/http/ tools/ tools/src/main/java/org/apache/sling/testing/tools/sling/ tools/src/test/j...

Author: bdelacretaz
Date: Wed Apr 16 10:37:06 2014
New Revision: 1587856

URL: http://svn.apache.org/r1587856
Log:
SLING-3478 - provide a JUnit Rule to run tests on multiple instances

Added:
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstancesRule.java
    sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/sling/SlingInstancesRuleTest.java
Modified:
    sling/trunk/testing/samples/integration-tests/pom.xml
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/http/MultipleOsgiConsoleTest.java
    sling/trunk/testing/tools/pom.xml
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstanceManager.java

Modified: sling/trunk/testing/samples/integration-tests/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/pom.xml?rev=1587856&r1=1587855&r2=1587856&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/pom.xml (original)
+++ sling/trunk/testing/samples/integration-tests/pom.xml Wed Apr 16 10:37:06 2014
@@ -335,7 +335,7 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>4.8.2</version>
+            <version>4.11</version>
             <scope>test</scope>
         </dependency>
     </dependencies>

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/http/MultipleOsgiConsoleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/http/MultipleOsgiConsoleTest.java?rev=1587856&r1=1587855&r2=1587856&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/http/MultipleOsgiConsoleTest.java (original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/http/MultipleOsgiConsoleTest.java Wed Apr 16 10:37:06 2014
@@ -17,8 +17,12 @@
 
 package org.apache.sling.testing.samples.integrationtests.http;
 
+import static org.junit.Assert.assertEquals;
 import org.apache.sling.testing.tools.sling.SlingInstance;
-import org.apache.sling.testing.tools.sling.SlingInstanceManager;
+import org.apache.sling.testing.tools.sling.SlingInstancesRule;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -27,18 +31,34 @@ import org.slf4j.LoggerFactory;
 public class MultipleOsgiConsoleTest {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
+
+    /** Instance names must be consistent with the system properties that we get */
+    final static String [] INSTANCE_NAMES = { "instance1", "instance2" };
+    
+    @Rule
+    /** This causes our tests to be executed once for each specified instance */
+    public final SlingInstancesRule rule = new SlingInstancesRule(INSTANCE_NAMES);
+    
+    // Verify that our SlingInstancesRule works properly
+    // (not needed for regular tests - here we also test the testing framework)
+    private static int numberOfTestsExecuted;
     
-    private static final SlingInstanceManager manager = new SlingInstanceManager("instance1", "instance2");
+    @BeforeClass
+    public static void beforeClass() {
+        numberOfTestsExecuted = 0;
+    }
+    
+    @AfterClass
+    public static void afterClass() {
+        assertEquals("Expecting all instances to be tested", 
+                INSTANCE_NAMES.length * 2, numberOfTestsExecuted);
+    }
 
     @Test
     public void testSomeConsolePaths() throws Exception {
-        for (SlingInstance slingInstance : manager.getInstances()) {
-            testSomeConsolePaths(slingInstance);
-        }
-    }
-
-    public void testSomeConsolePaths(SlingInstance slingInstance) throws Exception {
-        log.info("Running {} on {}", getClass().getSimpleName(), slingInstance.getServerBaseUrl());
+        numberOfTestsExecuted++;
+        final SlingInstance instance = rule.getSlingInstance();
+        log.info("Running testSomeConsolePaths {} on {}", getClass().getSimpleName(), instance.getServerBaseUrl());
 
         final String [] subpaths = {
                 "bundles",
@@ -53,10 +73,23 @@ public class MultipleOsgiConsoleTest {
 
         for(String subpath : subpaths) {
             final String path = "/system/console/" + subpath;
-            slingInstance.getRequestExecutor().execute(
-                    slingInstance.getRequestBuilder().buildGetRequest(path)
-                            .withCredentials(slingInstance.getServerUsername(), slingInstance.getServerPassword())
+            instance.getRequestExecutor().execute(
+                    instance.getRequestBuilder().buildGetRequest(path)
+                            .withCredentials(instance.getServerUsername(), instance.getServerPassword())
             ).assertStatus(200);
         }
     }
+    
+    @Test
+    public void checkRuleWithMoreThanOneTest() throws Exception {
+        numberOfTestsExecuted++;
+        final SlingInstance instance = rule.getSlingInstance();
+        log.info("Running checkRuleWithMoreThanOneTest {} on {}", getClass().getSimpleName(), instance.getServerBaseUrl());
+
+        final String path = "/system/console/bundles";
+        instance.getRequestExecutor().execute(
+                instance.getRequestBuilder().buildGetRequest(path)
+                        .withCredentials(instance.getServerUsername(), instance.getServerPassword())
+        ).assertStatus(200);
+    }
 }
\ No newline at end of file

Modified: sling/trunk/testing/tools/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/pom.xml?rev=1587856&r1=1587855&r2=1587856&view=diff
==============================================================================
--- sling/trunk/testing/tools/pom.xml (original)
+++ sling/trunk/testing/tools/pom.xml Wed Apr 16 10:37:06 2014
@@ -121,7 +121,7 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>4.10</version>
+            <version>4.11</version>
             <scope>compile</scope>
         </dependency>
     </dependencies>

Modified: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstanceManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstanceManager.java?rev=1587856&r1=1587855&r2=1587856&view=diff
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstanceManager.java (original)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstanceManager.java Wed Apr 16 10:37:06 2014
@@ -16,7 +16,7 @@
  */
 package org.apache.sling.testing.tools.sling;
 
-import java.util.Collection;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
@@ -25,7 +25,7 @@ import java.util.concurrent.ConcurrentHa
  *  Helper class for running tests against multiple Sling instances,
  *  takes care of starting the Sling instances and waiting for them to be ready.
  */
-public class SlingInstanceManager {
+public class SlingInstanceManager implements Iterable<SlingInstance > {
     private final Map<String, SlingInstance> slingTestInstances = new ConcurrentHashMap<String, SlingInstance>();
 
     public SlingInstanceManager(String... instanceNames) {
@@ -81,7 +81,7 @@ public class SlingInstanceManager {
         return slingTestInstances.get(instanceName);
     }
 
-    public Collection<SlingInstance> getInstances() {
-        return slingTestInstances.values();
+    public Iterator<SlingInstance> iterator() {
+        return slingTestInstances.values().iterator();
     }
 }
\ No newline at end of file

Added: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstancesRule.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstancesRule.java?rev=1587856&view=auto
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstancesRule.java (added)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingInstancesRule.java Wed Apr 16 10:37:06 2014
@@ -0,0 +1,58 @@
+/*
+ * 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.tools.sling;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ *  JUnit Rule that executes tests for multiple Sling instances.
+ */
+public class SlingInstancesRule implements TestRule {
+
+    private SlingInstance currentInstance;
+    private final Iterable<SlingInstance> instances;
+    
+    public SlingInstancesRule(String ... instanceNames) {
+        this(new SlingInstanceManager(instanceNames));
+    }
+    
+    public SlingInstancesRule(Iterable<SlingInstance> it) {
+        instances = it;
+    }
+
+    /** Evaluate our base statement once for every instance.
+     *  Tests can use our getInstance() method to access the current one.
+     */
+    public Statement apply(final Statement base, Description dest) {
+        return new Statement() {
+            @Override
+            public void evaluate() throws Throwable {
+                for(SlingInstance instance : instances) {
+                    currentInstance = instance;
+                    base.evaluate();
+               }
+                currentInstance = null;
+            }
+        };
+    }
+    
+    public SlingInstance getSlingInstance() {
+        return currentInstance;
+    }
+}
\ No newline at end of file

Added: sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/sling/SlingInstancesRuleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/sling/SlingInstancesRuleTest.java?rev=1587856&view=auto
==============================================================================
--- sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/sling/SlingInstancesRuleTest.java (added)
+++ sling/trunk/testing/tools/src/test/java/org/apache/sling/testing/tools/sling/SlingInstancesRuleTest.java Wed Apr 16 10:37:06 2014
@@ -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.tools.sling;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.testing.tools.http.RequestBuilder;
+import org.apache.sling.testing.tools.http.RequestExecutor;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class SlingInstancesRuleTest {
+    private static final List<SlingInstance> instances = new ArrayList<SlingInstance>();
+    
+    private static void addInstance(final String url) {
+        instances.add(new SlingInstance() {
+            public RequestBuilder getRequestBuilder() {
+                return null;
+            }
+
+            public String getServerBaseUrl() {
+                return url;
+            }
+
+            public String getServerUsername() {
+                return null;
+            }
+
+            public String getServerPassword() {
+                return null;
+            }
+
+            public RequestExecutor getRequestExecutor() {
+                return null;
+            }
+                
+        });
+    }
+    
+    @Rule
+    public final SlingInstancesRule rule = new SlingInstancesRule(instances);
+    
+    private static String result = "";
+    
+    @BeforeClass
+    public static void setup() {
+        addInstance("it does ");
+        addInstance("work, cool!");
+    }
+    
+    @AfterClass
+    public static void verifyResult() {
+        assertEquals("it does work, cool!", result);
+    }
+    
+    @Test
+    public void testInstanceName() {
+        result = result + rule.getSlingInstance().getServerBaseUrl();
+    }
+}