You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:19:24 UTC

[sling-org-apache-sling-testing-osgi-mock] 13/17: SLING-4781 - Implement MockBundle.getEntryPaths

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-1.4.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 9248ced42f250feca53ee66e23c435f8e25ea6d7
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Tue Jun 9 15:30:24 2015 +0000

    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().
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1684449 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/testing/mock/osgi/MockBundle.java | 74 +++++++++++++++++---
 .../sling/testing/mock/osgi/MockBundleTest.java    | 80 ++++++++++++++++++++++
 src/test/resources/bundleData/nested/first.txt     |  1 +
 src/test/resources/bundleData/nested/second.txt    |  1 +
 4 files changed, 148 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
index 523a12e..ffeecf2 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
@@ -20,12 +20,14 @@ package org.apache.sling.testing.mock.osgi;
 
 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 Bundle {
 
     @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 Bundle {
     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 Bundle {
     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();
     }
 
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java
index e699b5c..fefcd93 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleTest.java
@@ -21,8 +21,14 @@ package org.apache.sling.testing.mock.osgi;
 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"));
+    }
 }
diff --git a/src/test/resources/bundleData/nested/first.txt b/src/test/resources/bundleData/nested/first.txt
new file mode 100644
index 0000000..fe4f02a
--- /dev/null
+++ b/src/test/resources/bundleData/nested/first.txt
@@ -0,0 +1 @@
+first
\ No newline at end of file
diff --git a/src/test/resources/bundleData/nested/second.txt b/src/test/resources/bundleData/nested/second.txt
new file mode 100644
index 0000000..2147e41
--- /dev/null
+++ b/src/test/resources/bundleData/nested/second.txt
@@ -0,0 +1 @@
+second
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.