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/10/22 12:12:44 UTC

svn commit: r1534587 - in /sling/trunk/launchpad/base/src: main/java/org/apache/sling/launchpad/base/impl/ test/java/org/apache/sling/launchpad/base/impl/

Author: bdelacretaz
Date: Tue Oct 22 10:12:43 2013
New Revision: 1534587

URL: http://svn.apache.org/r1534587
Log:
SLING-3196 and SLING-3022 - back to the code of revision 1487419, with improved test coverage

Modified:
    sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProvider.java
    sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderChildrenTest.java
    sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderTest.java

Modified: sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProvider.java?rev=1534587&r1=1534586&r2=1534587&view=diff
==============================================================================
--- sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProvider.java (original)
+++ sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProvider.java Tue Oct 22 10:12:43 2013
@@ -44,17 +44,19 @@ public class ClassLoaderResourceProvider
                 ? classLoader
                 : this.getClass().getClassLoader();
     }
-    
-    static Pattern getResourcePathPattern(String forPath) {
-        return Pattern.compile("^" + forPath + "(\\.[^/]+)?/[^/]+/?$");
-    }
 
     public Iterator<String> getChildren(String path) {
         List<String> children;
 
+        // Guard against extra trailing slashes
+        if(path.endsWith("/") && path.length() > 1) {
+            path = path.substring(0, path.length()-1);
+        }
+        
         URL url = this.classLoader.getResource(path);
         if (url != null) {
-            final Pattern pathPattern = getResourcePathPattern(path);
+            Pattern pathPattern = Pattern.compile("^" + path + "/[^/]+/?$");
+
             children = new ArrayList<String>();
             try {
                 URLConnection conn = url.openConnection();

Modified: sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderChildrenTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderChildrenTest.java?rev=1534587&r1=1534586&r2=1534587&view=diff
==============================================================================
--- sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderChildrenTest.java (original)
+++ sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderChildrenTest.java Tue Oct 22 10:12:43 2013
@@ -19,6 +19,7 @@
 package org.apache.sling.launchpad.base.impl;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.when;
 
@@ -36,16 +37,15 @@ import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.mockito.Matchers;
 import org.mockito.Mockito;
 
-@Ignore("SLING-3196")
 public class ClassLoaderResourceProviderChildrenTest {
     
     private ClassLoader classLoader;
     private ClassLoaderResourceProvider provider;
+    private boolean throwExceptionOnOpenConnection = false;
     
     private static final String [] TEST_PATHS = {
         "resources/install",
@@ -64,6 +64,9 @@ public class ClassLoaderResourceProvider
         final URLStreamHandler handler = new URLStreamHandler() {
             @Override
             protected URLConnection openConnection(final URL url) throws IOException {
+                if(throwExceptionOnOpenConnection) {
+                    throw new IOException("Throwing up for testing that");
+                }
                 return conn;
             }
         };
@@ -75,7 +78,7 @@ public class ClassLoaderResourceProvider
             entries.add(new JarEntry(path));
         }
         
-        when(cl.getResource(Matchers.any(String.class))).thenReturn(url);
+        when(cl.getResource(Matchers.contains("install"))).thenReturn(url);
         when(conn.getJarFile()).thenReturn(f);
         when(f.entries()).thenReturn(entries.elements());
         
@@ -142,4 +145,17 @@ public class ClassLoaderResourceProvider
                 "resources/install.oak",
                 "resources/install.oak/four.jar");
     }
+    
+    @Test
+    public void testNoResults() {
+        final Iterator<String> it = provider.getChildren("FOO");
+        assertFalse("Expecting no children", it.hasNext());
+    }
+    
+    @Test
+    public void testException() {
+        throwExceptionOnOpenConnection = true;
+        final Iterator<String> it = provider.getChildren("resources/install");
+        assertFalse("Expecting no results with ignored IOException", it.hasNext());
+    }
 }

Modified: sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderTest.java?rev=1534587&r1=1534586&r2=1534587&view=diff
==============================================================================
--- sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderTest.java (original)
+++ sling/trunk/launchpad/base/src/test/java/org/apache/sling/launchpad/base/impl/ClassLoaderResourceProviderTest.java Tue Oct 22 10:12:43 2013
@@ -18,45 +18,61 @@
  */
 package org.apache.sling.launchpad.base.impl;
 
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 
-import java.util.regex.Pattern;
+import java.io.IOException;
+import java.io.InputStream;
 
+import org.junit.After;
 import org.junit.Test;
 
 public class ClassLoaderResourceProviderTest {
     
-    private final Pattern pattern = ClassLoaderResourceProvider.getResourcePathPattern("resources/bundles");
-    
-    private void assertMatch(String path, boolean expectMatch) {
-        if(expectMatch != pattern.matcher(path).matches()) {
-            fail("Expected match=" + expectMatch + " for path '" + path + "', got " + !expectMatch);
+    public static final String TEST_RESOURCE_PATH = "holaworld-invalid.jar"; 
+    private ClassLoaderResourceProvider provider = new ClassLoaderResourceProvider(getClass().getClassLoader());
+    private InputStream toClose;
+
+    @After
+    public void cleanup() throws IOException {
+        if(toClose != null) {
+            toClose.close();
         }
     }
     
     @Test
-    public void testResourcePathPatterns() {
-        assertMatch("resources/bundles/", false);
-        
-        assertMatch("resources/bundles/0/", true);
-        assertMatch("resources/bundles/0", true);
-        
-        assertMatch("resources/bundles/1234/", true);
-        assertMatch("resources/bundles/1234", true);
-        
-        assertMatch("resources/bundles/12/42", false);
-        assertMatch("resources/bundles/12/42", false);
-        
-        assertMatch("something/else/0/", false);
-        assertMatch("something/else/0", false);
-        
-        assertMatch("resources/bundles.someRunMode/", false);
-        assertMatch("resources/bundles.someRunMode/14/", true);
-        assertMatch("resources/bundles.someRunMode/15", true);
-        
-        assertMatch("resources/bundles.runModeA.runModeB/", false);
-        assertMatch("resources/bundles.runModeA.runModeB/14/", true);
-        assertMatch("resources/bundles.runModeA.runModeB/14", true);
-        assertMatch("resources/bundles.runModeA.runModeB/15/16", false);
+    public void testGetResourceFound() {
+        assertNotNull(provider.getResource(TEST_RESOURCE_PATH));
+    }
+    
+    @Test
+    public void testGetResourceLeadingSlash() {
+        assertNotNull(provider.getResource("/" + TEST_RESOURCE_PATH));
+    }
+    
+    @Test
+    public void testGetResourceNotFound() {
+        assertNull(provider.getResource("NONEXISTENT"));
+    }
+    
+    @Test
+    public void testGetResourceNull() {
+        assertNull(provider.getResource(null));
     }
+    
+    @Test
+    public void testGetResourceStreamFound() throws IOException {
+        assertNotNull(toClose = provider.getResourceAsStream(TEST_RESOURCE_PATH));
+    }
+    
+    @Test
+    public void testGetResourceStreamLeadingSlash() throws IOException {
+        assertNotNull(toClose = provider.getResourceAsStream("/" + TEST_RESOURCE_PATH));
+    }
+    
+    @Test
+    public void testGetResourceStreamNotFound() throws IOException {
+        assertNull(provider.getResourceAsStream("NONEXISTENT"));
+    }
+    
 }