Author: rombert
Date: Tue Jun  9 15:30:24 2015
New Revision: 1684449

URL: http://svn.apache.org/r1684449
Log:
SLING-4781 - Implement MockBundle.getEntryPaths

MockBundle.getEntryPaths() now looks up resources in the classpath using
getClass().getResource(), much like MockBundle.getEntry().

MockBundle.getEntry() was adjusted to make sure that it works with the
results returned from MockBundle.getEntryPaths().

Added:
    sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/
    sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/
    
sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/first.txt
    
sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/second.txt
Modified:
    
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
    
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java

Modified: 
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java?rev=1684449&r1=1684448&r2=1684449&view=diff
==============================================================================
--- 
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
 (original)
+++ 
sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
 Tue Jun  9 15:30:24 2015
@@ -20,12 +20,14 @@ package org.apache.sling.testing.mock.os
 
 import java.io.File;
 import java.io.InputStream;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.security.cert.X509Certificate;
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
+import java.util.Vector;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -67,8 +69,21 @@ public final class MockBundle implements
 
     @Override
     public URL getEntry(final String name) {
+        
+        // the original implementation of this method performed 
getClass().getResource()
+        // however, this means that the it does not work out-of-the-box with 
paths
+        // returned from getEntryPaths(), which are by definition relative
+        
+        // as a fallback we make sure the resource is absolute if the relative 
one does
+        // not get a result, but perhaps we should enforce a relative lookup 
at all times
+        
         // try to load resource from classpath
-        return getClass().getResource(name);
+        URL resource = getClass().getResource(name);
+        
+        if ( resource == null || ! name.startsWith("/")) {
+            resource = getClass().getResource("/" + name);
+        }
+        return resource;
     }
 
     @Override
@@ -112,7 +127,7 @@ public final class MockBundle implements
     public long getLastModified() {
         return lastModified;
     }
-
+    
     /**
      * Set the last modified value for the mock bundle 
      * @param lastModified last modified
@@ -120,15 +135,58 @@ public final class MockBundle implements
     public void setLastModified(long lastModified) {
         this.lastModified = lastModified;
     }
-
-    // --- unsupported operations ---
+    
     @Override
-    public Enumeration<URL> findEntries(final String path, final String 
filePattern, final boolean recurse) {
-        throw new UnsupportedOperationException();
+    public Enumeration<String> getEntryPaths(final String path) {
+        
+        String queryPath = path.startsWith("/") ? path : "/" + path;
+        
+        URL res = getClass().getResource(queryPath);
+        if ( res == null ) {
+            return null;
+        }
+        
+        Vector<String> matching = new Vector<String>();
+        
+        try {
+            File file = new File(res.toURI());
+            if ( file.isDirectory()) {
+                for ( File entry : file.listFiles() ) {
+                    String name = entry.isDirectory() ? entry.getName() + "/" 
: entry.getName();
+                    
matching.add(relativeWithTrailingSlash(queryPath.substring(1, 
queryPath.length())) + name);
+                }
+            }
+        } catch (URISyntaxException e) {
+            throw new RuntimeException("Failed opening file from " + res , e);
+        } catch ( RuntimeException e) {
+            throw new RuntimeException("Failed opening file from " + res , e);
+        }
+        
+        if ( matching.isEmpty() ) {
+            return null;
+        }
+        
+        return matching.elements();
+    }
+
+    private String relativeWithTrailingSlash(String queryPath) {
+        
+        // make relative
+        if ( queryPath.startsWith("/")) {
+            queryPath = queryPath.substring(1, queryPath.length());
+        }
+        
+        // remove trailing slash
+        if ( !queryPath.isEmpty() && !queryPath.endsWith("/") ) {
+            queryPath = queryPath +"/";
+        }
+        
+        return queryPath;
     }
-
+    
+    // --- unsupported operations ---
     @Override
-    public Enumeration<String> getEntryPaths(final String path) {
+    public Enumeration<URL> findEntries(final String path, final String 
filePattern, final boolean recurse) {
         throw new UnsupportedOperationException();
     }
 

Modified: 
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java?rev=1684449&r1=1684448&r2=1684449&view=diff
==============================================================================
--- 
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java
 (original)
+++ 
sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java
 Tue Jun  9 15:30:24 2015
@@ -21,8 +21,14 @@ package org.apache.sling.testing.mock.os
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+
+import org.hamcrest.CoreMatchers;
 import org.junit.Before;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
@@ -77,4 +83,78 @@ public class MockBundleTest {
         bundle.setLastModified(42);
         assertEquals(42, bundle.getLastModified());
     }
+    
+    @Test
+    public void getEntryPaths_noMatches() {
+        assertNull(bundle.getEntryPaths("resources"));
+    }    
+    
+    @Test
+    public void getEntryPaths() {
+        
+        Enumeration<String> entryPaths = bundle.getEntryPaths("bundleData");
+        
+        List<String> paths = Collections.list(entryPaths);
+        
+        assertThat(paths.size(), CoreMatchers.is(1));
+        assertTrue(paths.contains("bundleData/nested/"));
+    }
+
+    @Test
+    public void getEntryPaths_leadingSlash() {
+        
+        Enumeration<String> entryPaths = bundle.getEntryPaths("bundleData");
+        
+        List<String> paths = Collections.list(entryPaths);
+        
+        assertThat(paths.size(), CoreMatchers.is(1));
+        assertTrue(paths.contains("bundleData/nested/"));
+    }
+
+    @Test
+    public void getEntryPaths_slash() {
+        
+        Enumeration<String> entryPaths = bundle.getEntryPaths("/");
+        
+        List<String> paths = Collections.list(entryPaths);
+        
+        // intentionally less precise as we don't want to be broken when e.g. 
test resources change 
+        assertTrue(paths.size() >= 3);
+        assertTrue(paths.contains("bundleData/"));
+        assertTrue(paths.contains("OSGI-INF/"));
+        assertTrue(paths.contains("META-INF/"));
+    }
+
+    @Test
+    public void getEntryPaths_empty() {
+        
+        Enumeration<String> entryPaths = bundle.getEntryPaths("/");
+        
+        List<String> paths = Collections.list(entryPaths);
+        
+        // intentionally less precise as we don't want to be broken when e.g. 
test resources change 
+        assertTrue(paths.size() >= 3);
+        assertTrue(paths.contains("bundleData/"));
+        assertTrue(paths.contains("OSGI-INF/"));
+        assertTrue(paths.contains("META-INF/"));
+    }
+    
+    @Test
+    public void getEntryPaths_noMatch() {
+        
+        assertNull(bundle.getEntryPaths("/EMPTY"));
+        assertNull(bundle.getEntryPaths("EMPTY"));
+    }
+
+    @Test
+    public void getEntryPaths_Nested() {
+
+        Enumeration<String> entryPaths = 
bundle.getEntryPaths("bundleData/nested");
+        
+        List<String> paths = Collections.list(entryPaths);
+        
+        assertThat(paths.size(), CoreMatchers.is(2));
+        assertTrue(paths.contains("bundleData/nested/first.txt"));
+        assertTrue(paths.contains("bundleData/nested/second.txt"));
+    }
 }

Added: 
sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/first.txt
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/first.txt?rev=1684449&view=auto
==============================================================================
--- 
sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/first.txt
 (added)
+++ 
sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/first.txt
 Tue Jun  9 15:30:24 2015
@@ -0,0 +1 @@
+first
\ No newline at end of file

Added: 
sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/second.txt
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/second.txt?rev=1684449&view=auto
==============================================================================
--- 
sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/second.txt
 (added)
+++ 
sling/trunk/testing/mocks/osgi-mock/src/test/resources/bundleData/nested/second.txt
 Tue Jun  9 15:30:24 2015
@@ -0,0 +1 @@
+second
\ No newline at end of file


Reply via email to