sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1067171 - in /sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable: ExplainTests.java ScriptableTestsProvider.java
Date Fri, 04 Feb 2011 12:49:31 GMT
Author: bdelacretaz
Date: Fri Feb  4 12:49:30 2011
New Revision: 1067171

URL: http://svn.apache.org/viewvc?rev=1067171&view=rev
Log:
SLING-1963 - ScriptableTestsProvider only considers test nodes under /apps or /libs

Modified:
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java

Modified: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java?rev=1067171&r1=1067170&r2=1067171&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java
(original)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java
Fri Feb  4 12:49:30 2011
@@ -30,9 +30,10 @@ public class ExplainTests {
         fail(
                 "No scriptable tests found."
                 + " To create scriptable tests, create nodes with the sling:Test"
-                + " mixin, and setup Sling so that requesting them with .test.txt generates"
+                + " mixin under /apps or /libs (*), and setup Sling so that requesting them
with .test.txt generates"
                 + " a text response containing just TEST_PASSED if the test is successful."
                 + " Empty lines and lines starting with # are ignored in the test output."
+                + " (*) depends on the JCR resource resolver configuration."
                 );
     }
 }

Modified: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java?rev=1067171&r1=1067170&r2=1067171&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java
(original)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java
Fri Feb  4 12:49:30 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.junit.scriptable;
 
+import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -45,10 +46,19 @@ import org.slf4j.LoggerFactory;
 public class ScriptableTestsProvider implements TestsProvider {
     private final Logger log = LoggerFactory.getLogger(getClass());
     private String pid;
-    private static List<String> testPaths = new LinkedList<String>();
     private Session session;
     private ResourceResolver resolver;
     
+    /** List of resource paths that point to tests */
+    private static List<String> testPaths = new LinkedList<String>();
+    
+    /** We only consider test resources under the search path
+     *  of the JCR resource resolver. These paths are supposed 
+     *  to be secured, as they contain other admin stuff anyway, 
+     *  so non-admin users are prevented from creating test nodes. 
+     */
+    private String[] allowedRoots;
+    
     @Reference
     private SlingRepository repository;
     
@@ -62,6 +72,16 @@ public class ScriptableTestsProvider imp
         pid = (String)ctx.getProperties().get(Constants.SERVICE_PID);
         session = repository.loginAdministrative(repository.getDefaultWorkspace());
         resolver = resolverFactory.getResourceResolver(session);
+        
+        // Copy resource resolver paths and make sure they end with a /
+        final String [] paths = resolver.getSearchPath();
+        allowedRoots = Arrays.copyOf(paths, paths.length);
+        for(int i=0; i < allowedRoots.length; i++) {
+            if(!allowedRoots[i].endsWith("/")) {
+                allowedRoots[i] += "/";
+            }
+        }
+        log.info("Activated, will look for test resources under {}", Arrays.asList(allowedRoots));
     }
     
     protected void deactivate(ComponentContext ctx) throws RepositoryException {
@@ -78,8 +98,6 @@ public class ScriptableTestsProvider imp
         if(testPaths.size() == 0) {
             return ExplainTests.class;
         } else {
-            // TODO this would cause a mess if TestAllPaths is executed concurrently
-            // and these values change
             TestAllPaths.testPaths = testPaths;
             TestAllPaths.requestProcessor = requestProcessor;
             TestAllPaths.resolver = resolver;
@@ -105,11 +123,19 @@ public class ScriptableTestsProvider imp
         
         // TODO do we want to cache results, use observation, etc.
         try {
-            final Query q = session.getWorkspace().getQueryManager().createQuery("//element(*,
sling:Test)", Query.XPATH);
-            final NodeIterator it = q.execute().getNodes();
-            while(it.hasNext()) {
-                result.add(it.nextNode().getPath());
+            for(String root : allowedRoots) {
+                final String statement = "/jcr:root" + root + "/element(*, sling:Test)";
+                log.debug("Querying for test nodes: {}", statement);
+                final Query q = session.getWorkspace().getQueryManager().createQuery(statement,
Query.XPATH);
+                final NodeIterator it = q.execute().getNodes();
+                while(it.hasNext()) {
+                    final String path = it.nextNode().getPath();
+                    result.add(path);
+                    log.debug("Test resource found: {}", path);
+                }
             }
+            log.info("List of test resources updated, {} resource(s) found under {}", 
+                    result.size(), Arrays.asList(allowedRoots));
         } catch(RepositoryException re) {
             log.warn("RepositoryException in getTestNames()", re);
         }



Mime
View raw message