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 2013/09/19 14:42:13 UTC

svn commit: r1524717 - in /sling/trunk/testing/junit: core/ core/src/main/java/org/apache/sling/junit/ core/src/main/java/org/apache/sling/junit/impl/ core/src/main/java/org/apache/sling/junit/impl/servlet/ remote/src/main/java/org/apache/sling/junit/r...

Author: bdelacretaz
Date: Thu Sep 19 12:42:13 2013
New Revision: 1524717

URL: http://svn.apache.org/r1524717
Log:
SLING-3084 - add forceReload option to JUnitServlet

Modified:
    sling/trunk/testing/junit/core/pom.xml
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TestsManager.java
    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/JUnitServlet.java
    sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java

Modified: sling/trunk/testing/junit/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/pom.xml?rev=1524717&r1=1524716&r2=1524717&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/pom.xml (original)
+++ sling/trunk/testing/junit/core/pom.xml Thu Sep 19 12:42:13 2013
@@ -59,8 +59,8 @@
                     <instructions>
                         <Bundle-Activator>org.apache.sling.junit.Activator</Bundle-Activator>
                         <Export-Package>
-                            org.apache.sling.junit;version=${project.version},
-                            org.apache.sling.junit.annotations;version=${project.version}
+                            org.apache.sling.junit;version=1.1.0,
+                            org.apache.sling.junit.annotations;version=1.0.8,
                         </Export-Package>
                         <_exportcontents>
                             junit.framework;version=${junit.version},

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TestsManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TestsManager.java?rev=1524717&r1=1524716&r2=1524717&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TestsManager.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TestsManager.java Thu Sep 19 12:42:13 2013
@@ -25,6 +25,12 @@ public interface TestsManager {
      */
     public Collection<String> getTestNames(TestSelector selector);
     
+    /** Clear our internal caches. Useful in automated testing, to make
+     *  sure changes introduced by recent uploads or configuration or bundles
+     *  changes are taken into account immediately. 
+     */
+    public void clearCaches();
+    
     /** Instantiate test class for specified test */
     public Class<?> getTestClass(String testName) throws ClassNotFoundException;
     

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=1524717&r1=1524716&r2=1524717&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 Thu Sep 19 12:42:13 2013
@@ -22,6 +22,7 @@ import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Service;
@@ -50,7 +51,7 @@ public class TestsManagerImpl implements
     private List<TestsProvider> providers = new ArrayList<TestsProvider>();
     
     // Map of test names to their provider's PID
-    private Map<String, String> tests = new HashMap<String, String>();
+    private Map<String, String> tests = new ConcurrentHashMap<String, String>();
     
     // Last-modified values for each provider
     private Map<String, Long> lastModified = new HashMap<String, Long>();
@@ -69,7 +70,14 @@ public class TestsManagerImpl implements
         bundleContext = null;
     }
     
-    /** inheritDoc */
+    /** @inheritDoc */
+    public void clearCaches() {
+        log.debug("Clearing internal caches");
+        lastModified.clear();
+        lastTrackingCount = -1;
+    }
+    
+    /** @inheritDoc */
     public Class<?> getTestClass(String testName) throws ClassNotFoundException {
         maybeUpdateProviders();
 

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java?rev=1524717&r1=1524716&r2=1524717&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/servlet/JUnitServlet.java Thu Sep 19 12:42:13 2013
@@ -51,6 +51,7 @@ public class JUnitServlet extends HttpSe
     private final Logger log = LoggerFactory.getLogger(getClass());
     
     public static final String CSS = "junit.css";
+    public static final String FORCE_RELOAD_PARAM = "forceReload";
     
     @Property(value="/system/sling/junit")
     static final String SERVLET_PATH_NAME = "servlet.path";
@@ -100,8 +101,11 @@ public class JUnitServlet extends HttpSe
     /** Return sorted list of available tests
      * @param prefix optionally select only names that match this prefix
      */
-    private List<String> getTestNames(TestSelector selector) {
+    private List<String> getTestNames(TestSelector selector, boolean forceReload) {
         final List<String> result = new LinkedList<String>();
+        if(forceReload) {
+            log.debug("{} is true, clearing TestsManager caches", FORCE_RELOAD_PARAM);
+        }
         result.addAll(testsManager.getTestNames(selector));
         Collections.sort(result);
         return result;
@@ -123,10 +127,17 @@ public class JUnitServlet extends HttpSe
         }
     }
     
+    private boolean getForceReloadOption(HttpServletRequest request) {
+        final boolean forceReload = "true".equalsIgnoreCase(request.getParameter(FORCE_RELOAD_PARAM));
+        log.debug("{} option is set to {}", FORCE_RELOAD_PARAM, forceReload);
+        return forceReload;
+    }
+    
     /** GET request lists available tests */
     @Override
     protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException {
+        final boolean forceReload = getForceReloadOption(request);
         
         // Redirect to / if called without it, and serve CSS if requested 
         {
@@ -150,7 +161,7 @@ public class JUnitServlet extends HttpSe
         renderer.info("info", "Test selector: " + selector); 
         
         // Any test classes?
-        final List<String> testNames = getTestNames(selector); 
+        final List<String> testNames = getTestNames(selector, forceReload); 
         if(testNames.isEmpty()) {
             renderer.info(
                     "warning",
@@ -175,7 +186,9 @@ public class JUnitServlet extends HttpSe
     protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException {
         final TestSelector selector = getTestSelector(request);
-        log.info("POST request, executing tests: {}", selector);
+        final boolean forceReload = getForceReloadOption(request);
+        log.info("POST request, executing tests: {}, {}={}", 
+                new Object[] { selector, FORCE_RELOAD_PARAM, forceReload});
         
         final Renderer renderer = rendererSelector.getRenderer(selector);
         if(renderer == null) {
@@ -183,7 +196,7 @@ public class JUnitServlet extends HttpSe
         }
         renderer.setup(response, getClass().getSimpleName());
         
-        final List<String> testNames = getTestNames(selector);
+        final List<String> testNames = getTestNames(selector, forceReload);
         if(testNames.isEmpty()) {
             response.sendError(
                     HttpServletResponse.SC_NOT_FOUND, 

Modified: sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java?rev=1524717&r1=1524716&r2=1524717&view=diff
==============================================================================
--- sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java (original)
+++ sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java Thu Sep 19 12:42:13 2013
@@ -17,10 +17,16 @@
 package org.apache.sling.junit.remote.httpclient;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
 
+import org.apache.http.NameValuePair;
 import org.apache.http.ParseException;
 import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicNameValuePair;
 import org.apache.sling.testing.tools.http.Request;
 import org.apache.sling.testing.tools.http.RequestBuilder;
 import org.apache.sling.testing.tools.http.RequestCustomizer;
@@ -70,7 +76,12 @@ public class RemoteTestHttpClient {
         requestCustomizer = c;
     }
     
-    public RequestExecutor runTests(String testClassesSelector, String testMethodSelector, String extension) 
+    public RequestExecutor runTests(String testClassesSelector, String testMethodSelector, String extension)
+    throws ClientProtocolException, IOException {
+        return runTests(testClassesSelector, testMethodSelector, extension, null);
+    }
+    
+    public RequestExecutor runTests(String testClassesSelector, String testMethodSelector, String extension, Map<String, String> requestOptions) 
     throws ClientProtocolException, IOException {
         final RequestBuilder builder = new RequestBuilder(junitServletUrl);
 
@@ -104,13 +115,22 @@ public class RemoteTestHttpClient {
             subpath.append(DOT);
         }
         subpath.append(extension);
+
+        // Request options if any
+        final List<NameValuePair> opt = new ArrayList<NameValuePair>();
+        if(requestOptions != null) {
+            for(Map.Entry<String, String> e : requestOptions.entrySet()) {
+                opt.add(new BasicNameValuePair(e.getKey(), e.getValue()));
+            }
+        }
         
         log.info("Executing test remotely, path={} JUnit servlet URL={}",
                 subpath, junitServletUrl);
         final Request r = builder
         .buildPostRequest(subpath.toString())
         .withCredentials(username, password)
-        .withCustomizer(requestCustomizer);
+        .withCustomizer(requestCustomizer)
+        .withEntity(new UrlEncodedFormEntity(opt));
         executor.execute(r).assertStatus(200);
 
         return executor;