incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1606131 - in /sling/trunk/testing: junit/core/ junit/core/src/main/java/org/apache/sling/junit/ junit/core/src/main/java/org/apache/sling/junit/impl/ junit/core/src/main/java/org/apache/sling/junit/impl/servlet/ samples/integration-tests/ ...
Date Fri, 27 Jun 2014 15:38:22 GMT
Author: bdelacretaz
Date: Fri Jun 27 15:38:22 2014
New Revision: 1606131

URL: http://svn.apache.org/r1606131
Log:
SLING-3708 - Let server-side JUnit tests provide optional output metadata

Added:
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContext.java
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContextProvider.java
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java
    sling/trunk/testing/samples/sample-tests/src/main/java/org/apache/sling/testing/samples/sampletests/OutputMetadataTest.java
Modified:
    sling/trunk/testing/junit/core/pom.xml
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
    sling/trunk/testing/samples/integration-tests/pom.xml
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/sling/SlingServerSideTest.java
    sling/trunk/testing/samples/sample-tests/pom.xml

Modified: sling/trunk/testing/junit/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/pom.xml?rev=1606131&r1=1606130&r2=1606131&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/pom.xml (original)
+++ sling/trunk/testing/junit/core/pom.xml Fri Jun 27 15:38:22 2014
@@ -59,7 +59,7 @@
                     <instructions>
                         <Bundle-Activator>org.apache.sling.junit.Activator</Bundle-Activator>
                         <Export-Package>
-                            org.apache.sling.junit;version=1.1.0,
+                            org.apache.sling.junit;version=1.2.0,
                             org.apache.sling.junit.annotations;version=1.0.8,
                         </Export-Package>
                         <_exportcontents>

Added: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContext.java?rev=1606131&view=auto
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContext.java
(added)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContext.java
Fri Jun 27 15:38:22 2014
@@ -0,0 +1,41 @@
+/*
+ * 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.junit;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/** Provide test parameters (the "input map") and allow 
+ *  tests to provide additional metadata (in an "output
+ *  map") about their results.
+ *  
+ *  Meant to be used to implement performance tests
+ *  that run inside Sling instances - we'll expand
+ *  the junit.core module to optionally use this.  
+ */
+public class SlingTestContext {
+    private final Map<String, Object> inputMap = new HashMap<String, Object>();
+    private final Map<String, Object> outputMap = new HashMap<String, Object>();
+    
+    public Map<String, Object> input() {
+        return inputMap;
+    }
+    
+    public Map<String, Object> output() {
+        return outputMap;
+    }
+}

Added: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContextProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContextProvider.java?rev=1606131&view=auto
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContextProvider.java
(added)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/SlingTestContextProvider.java
Fri Jun 27 15:38:22 2014
@@ -0,0 +1,46 @@
+/*
+ * 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.junit;
+
+
+/** Provider of {@link SlingTestContext} that uses
+ *  thread locals to provide per-testing thread contexts.   
+ */
+public class SlingTestContextProvider {
+    private static final ThreadLocal<SlingTestContext> threadLocal = new ThreadLocal<SlingTestContext>();
+    
+    public static SlingTestContext getContext() {
+        final SlingTestContext result = threadLocal.get();
+        if(result == null) {
+            throw new IllegalStateException("Null context, createContext() not called?");
+        }
+        return result;
+    }
+    
+    public static SlingTestContext createContext() {
+        threadLocal.set(new SlingTestContext());
+        return getContext();
+    }
+    
+    public static void deleteContext() {
+        threadLocal.set(null);
+    }
+    
+    public static boolean hasContext() {
+        return threadLocal.get() != null;
+    }
+}

Added: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java?rev=1606131&view=auto
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java
(added)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestContextRunListenerWrapper.java
Fri Jun 27 15:38:22 2014
@@ -0,0 +1,71 @@
+/*
+ * 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.junit.impl;
+
+import org.apache.sling.junit.SlingTestContextProvider;
+import org.junit.runner.Description;
+import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunListener;
+
+public class TestContextRunListenerWrapper extends RunListener {
+    private final RunListener wrapped;
+    private long testStartTime;
+    
+    TestContextRunListenerWrapper(RunListener toWrap) {
+        wrapped = toWrap;
+    }
+
+    @Override
+    public void testAssumptionFailure(Failure failure) {
+        wrapped.testAssumptionFailure(failure);
+    }
+
+    @Override
+    public void testFailure(Failure failure) throws Exception {
+        wrapped.testFailure(failure);
+    }
+
+    @Override
+    public void testFinished(Description description) throws Exception {
+        if(SlingTestContextProvider.hasContext()) {
+            SlingTestContextProvider.getContext().output().put("test_execution_time_msec",
System.currentTimeMillis() - testStartTime);
+        }
+        wrapped.testFinished(description);
+    }
+
+    @Override
+    public void testIgnored(Description description) throws Exception {
+        wrapped.testIgnored(description);
+    }
+
+    @Override
+    public void testRunFinished(Result result) throws Exception {
+        wrapped.testRunFinished(result);
+    }
+
+    @Override
+    public void testRunStarted(Description description) throws Exception {
+        wrapped.testRunStarted(description);
+    }
+
+    @Override
+    public void testStarted(Description description) throws Exception {
+        testStartTime = System.currentTimeMillis();
+        wrapped.testStarted(description);
+    }
+}

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java?rev=1606131&r1=1606130&r2=1606131&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java
(original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/TestsManagerImpl.java
Fri Jun 27 15:38:22 2014
@@ -27,6 +27,8 @@ import java.util.concurrent.ConcurrentHa
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.junit.Renderer;
+import org.apache.sling.junit.SlingTestContext;
+import org.apache.sling.junit.SlingTestContextProvider;
 import org.apache.sling.junit.TestSelector;
 import org.apache.sling.junit.TestsManager;
 import org.apache.sling.junit.TestsProvider;
@@ -174,16 +176,35 @@ public class TestsManagerImpl implements
     public void executeTests(Collection<String> testNames, Renderer renderer, TestSelector
selector) throws Exception {
         renderer.title(2, "Running tests");
         final JUnitCore junit = new JUnitCore();
-        junit.addListener(renderer.getRunListener());
-        for(String className : testNames) {
-            renderer.title(3, className);
-            final String testMethodName = selector == null ? null : selector.getSelectedTestMethodName();
-            if(testMethodName != null && testMethodName.length() > 0) {
-                log.debug("Running test method {} from test class {}", testMethodName, className);
-                junit.run(Request.method(getTestClass(className), testMethodName));
-            } else {
-                log.debug("Running test class {}", className);
-                junit.run(getTestClass(className));
+        
+        // Create a test context if we don't have one yet
+        final boolean createContext =  !SlingTestContextProvider.hasContext();
+        if(createContext) {
+            SlingTestContextProvider.createContext();
+        }
+        
+        try {
+            junit.addListener(new TestContextRunListenerWrapper(renderer.getRunListener()));
+            for(String className : testNames) {
+                renderer.title(3, className);
+                
+                // If we have a test context, clear its output metadata
+                if(SlingTestContextProvider.hasContext()) {
+                    SlingTestContextProvider.getContext().output().clear();
+                }
+                
+                final String testMethodName = selector == null ? null : selector.getSelectedTestMethodName();
+                if(testMethodName != null && testMethodName.length() > 0) {
+                    log.debug("Running test method {} from test class {}", testMethodName,
className);
+                    junit.run(Request.method(getTestClass(className), testMethodName));
+                } else {
+                    log.debug("Running test class {}", className);
+                    junit.run(getTestClass(className));
+                }
+            }
+        } finally {
+            if(createContext) {
+                SlingTestContextProvider.deleteContext();
             }
         }
     }

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java?rev=1606131&r1=1606130&r2=1606131&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
(original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JsonRenderer.java
Fri Jun 27 15:38:22 2014
@@ -19,6 +19,7 @@ package org.apache.sling.junit.impl.serv
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.util.Collection;
+import java.util.Map;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -27,6 +28,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.io.JSONWriter;
 import org.apache.sling.junit.Renderer;
+import org.apache.sling.junit.SlingTestContextProvider;
 import org.apache.sling.junit.TestSelector;
 import org.junit.runner.Description;
 import org.junit.runner.Result;
@@ -43,9 +45,9 @@ public class JsonRenderer extends RunLis
     public static final String EXTENSION = "json";
     public static final String INFO_TYPE_KEY = "INFO_TYPE";
     public static final String INFO_SUBTYPE_KEY = "INFO_SUBTYPE";
+    public static final String TEST_METADATA = "test_metadata";
     private final Logger log = LoggerFactory.getLogger(getClass());
     private JSONWriter writer;
-    private int counter;
     
     /** @inheritDoc */
     public boolean appliesTo(TestSelector selector) {
@@ -144,6 +146,9 @@ public class JsonRenderer extends RunLis
     @Override
     public void testFinished(Description description) throws Exception {
         super.testFinished(description);
+        if(SlingTestContextProvider.hasContext()) {
+            outputContextMap(SlingTestContextProvider.getContext().output());
+        }
         endItem();
     }
 
@@ -159,7 +164,6 @@ public class JsonRenderer extends RunLis
     }
     
     void startItem(String name) throws JSONException {
-        ++counter;
         writer.object();
         writer.key(INFO_TYPE_KEY).value(name);
     }
@@ -167,4 +171,17 @@ public class JsonRenderer extends RunLis
     void endItem() throws JSONException {
         writer.endObject();
     }
-}
+    
+    void outputContextMap(Map<String, Object> data) throws JSONException {
+        writer.key(TEST_METADATA);
+        writer.object();
+        try {
+            for(Map.Entry<String, Object> e : data.entrySet()) {
+                writer.key(e.getKey());
+                writer.value(e.getValue());
+            }
+        } finally {
+            writer.endObject();
+        }
+    }
+}
\ No newline at end of file

Modified: sling/trunk/testing/samples/integration-tests/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/pom.xml?rev=1606131&r1=1606130&r2=1606131&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/pom.xml (original)
+++ sling/trunk/testing/samples/integration-tests/pom.xml Fri Jun 27 15:38:22 2014
@@ -250,7 +250,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.junit.core</artifactId>
-            <version>1.0.8</version>
+            <version>1.0.9-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java?rev=1606131&r1=1606130&r2=1606131&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java
Fri Jun 27 15:38:22 2014
@@ -32,7 +32,7 @@ public class ServerSideSampleTest extend
 implements SlingRemoteTestParameters, SlingTestsCountChecker, RequestCustomizer {
     
     public static final String TEST_SELECTOR = "org.apache.sling.testing.samples.sampletests";
-    public static final int TESTS_AT_THIS_PATH = 8;
+    public static final int TESTS_AT_THIS_PATH = 9;
     private int customizeCalled;
     
     public void checkNumberOfTests(int numberOfTestsExecuted) {

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/sling/SlingServerSideTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/sling/SlingServerSideTest.java?rev=1606131&r1=1606130&r2=1606131&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/sling/SlingServerSideTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/sling/SlingServerSideTest.java
Fri Jun 27 15:38:22 2014
@@ -29,7 +29,7 @@ public class SlingServerSideTest extends
 implements SlingRemoteTestParameters, SlingTestsCountChecker {
     
     public static final String TEST_SELECTOR = "org.apache.sling.testing.samples.sampletests";
-    public static final int TESTS_AT_THIS_PATH = 8;
+    public static final int TESTS_AT_THIS_PATH = 9;
     
     public void checkNumberOfTests(int numberOfTestsExecuted) {
         assertEquals(TESTS_AT_THIS_PATH, numberOfTestsExecuted);

Modified: sling/trunk/testing/samples/sample-tests/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/sample-tests/pom.xml?rev=1606131&r1=1606130&r2=1606131&view=diff
==============================================================================
--- sling/trunk/testing/samples/sample-tests/pom.xml (original)
+++ sling/trunk/testing/samples/sample-tests/pom.xml Fri Jun 27 15:38:22 2014
@@ -91,10 +91,10 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
-            <!-- TODO create separate API package, this is just to get the special Sling
test runner -->
+            <!-- TODO create separate API package, this is just to get the special Sling
test runner and SlingTestContext -->
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.junit.core</artifactId>
-            <version>1.0.8</version>
+            <version>1.0.9-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         

Added: sling/trunk/testing/samples/sample-tests/src/main/java/org/apache/sling/testing/samples/sampletests/OutputMetadataTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/sample-tests/src/main/java/org/apache/sling/testing/samples/sampletests/OutputMetadataTest.java?rev=1606131&view=auto
==============================================================================
--- sling/trunk/testing/samples/sample-tests/src/main/java/org/apache/sling/testing/samples/sampletests/OutputMetadataTest.java
(added)
+++ sling/trunk/testing/samples/sample-tests/src/main/java/org/apache/sling/testing/samples/sampletests/OutputMetadataTest.java
Fri Jun 27 15:38:22 2014
@@ -0,0 +1,35 @@
+/*
+ * 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.samples.sampletests;
+
+import static org.junit.Assert.assertNotNull;
+import org.apache.sling.junit.SlingTestContext;
+import org.apache.sling.junit.SlingTestContextProvider;
+import org.junit.Test;
+
+/** Test that adds metadata to the SlingTestContext */
+public class OutputMetadataTest {
+    
+    @Test
+    public void addMetadata() {
+        final SlingTestContext c = SlingTestContextProvider.getContext();
+        assertNotNull("Expecting a SlingTestContext", c);
+        
+        c.output().put("the_quick", "brown fox!");
+        c.output().put("the_answer", 42);
+    }
+}



Mime
View raw message