geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1482820 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/ResourceFinder.java test/java/org/apache/xbean/finder/ResourceFinderFromWithWeirdUrlTest.java
Date Wed, 15 May 2013 13:26:35 GMT
Author: rmannibucau
Date: Wed May 15 13:26:35 2013
New Revision: 1482820

URL: http://svn.apache.org/r1482820
Log:
XBEAN-246 resource finder expects some kind of url for type file which is not always the case
in particular with AppClassLoader

Added:
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderFromWithWeirdUrlTest.java
      - copied, changed from r1475766, geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java
Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java?rev=1482820&r1=1482819&r2=1482820&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ResourceFinder.java
Wed May 15 13:26:35 2013
@@ -80,12 +80,13 @@ public class ResourceFinder {
 
         for (int i = 0; urls != null && i < urls.length; i++) {
             URL url = urls[i];
-            if (url == null || isDirectory(url) || url.getProtocol().equals("jar")) {
+            if (url == null || "jar".equals(url.getProtocol()) || isDirectory(url)) { //
test directory last since it is the longer in time
                 continue;
             }
             try {
                 urls[i] = new URL("jar", "", -1, url.toString() + "!/");
             } catch (MalformedURLException e) {
+                // no-op
             }
         }
         this.urls = (urls == null || urls.length == 0)? null : urls;
@@ -93,7 +94,7 @@ public class ResourceFinder {
 
     private static boolean isDirectory(URL url) {
         String file = url.getFile();
-        return (file.length() > 0 && file.charAt(file.length() - 1) == '/');
+        return (file.length() > 0 && file.charAt(file.length() - 1) == '/') ||
new File(file).isDirectory(); // with surefire first test can easily fail
     }
 
     /**
@@ -1020,8 +1021,12 @@ public class ResourceFinder {
                     if (hostLength > 0) {
                         buf.append("//").append(host);
                     }
-                    // baseFile always ends with '/'
+                    // baseFile should always ends with '/'
                     buf.append(baseFile);
+                    if (!baseFile.endsWith("/")) {
+                        buf.append("/");
+                    }
+
                     String fixedResName = resourceName;
                     // Do not create a UNC path, i.e. \\host
                     while (fixedResName.startsWith("/") || fixedResName.startsWith("\\"))
{
@@ -1065,11 +1070,14 @@ public class ResourceFinder {
     }
 
     private URL targetURL(URL base, String name) throws MalformedURLException {
-        StringBuffer sb = new StringBuffer(base.getFile().length() + name.length());
-        sb.append(base.getFile());
+        final String baseFile = base.getFile();
+        final StringBuffer sb = new StringBuffer(baseFile.length() + name.length());
+        sb.append(baseFile);
+        if (!baseFile.endsWith("/")) {
+            sb.append("/");
+        }
         sb.append(name);
-        String file = sb.toString();
-        return new URL(base.getProtocol(), base.getHost(), base.getPort(), file, null);
+        return new URL(base.getProtocol(), base.getHost(), base.getPort(), sb.toString(),
null);
     }
 
     public static String decode(String fileName) {

Copied: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderFromWithWeirdUrlTest.java
(from r1475766, geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java)
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderFromWithWeirdUrlTest.java?p2=geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderFromWithWeirdUrlTest.java&p1=geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java&r1=1475766&r2=1482820&rev=1482820&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderTest.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ResourceFinderFromWithWeirdUrlTest.java
Wed May 15 13:26:35 2013
@@ -16,455 +16,26 @@
  */
 package org.apache.xbean.finder;
 
-/**
- * @version $Rev$ $Date$
- */
+import org.junit.Test;
 
 import java.io.File;
+import java.io.FileWriter;
 import java.io.IOException;
-import java.net.JarURLConnection;
 import java.net.URL;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
-
-import junit.framework.TestCase;
-import org.acme.BarUrlHandler;
-import org.acme.FooUrlHandler;
-import org.acme.One;
-import org.acme.Three;
-import org.acme.Two;
-import org.acme.javaURLContextFactory;
-import org.acme.kernelURLContextFactory;
-import org.acme.ldapURLContextFactory;
-import org.apache.xbean.finder.archive.Archives;
-
-public class ResourceFinderTest extends TestCase {
-    ResourceFinder resourceFinder = new ResourceFinder("META-INF/");
-
-    public void testGetResourcesMap1() throws Exception {
-        Map<String, URL> resourcesMap = resourceFinder.getResourcesMap("");
-        Set<Map.Entry<String, URL>> entries = resourcesMap.entrySet();
-        for (Map.Entry<String, URL> entry : entries) {
-            String key = entry.getKey();
-            URL value = entry.getValue();
-
-            assertTrue("key not a directory", !key.contains("/"));
-            assertTrue("contains META-INF/", value.getPath().contains("META-INF/"));
-            assertTrue("ends with META-INF/" + key, value.getPath().endsWith("META-INF/"
+ key));
-            assertTrue("value not a directory", !value.getPath().endsWith("/"));
-        }
-    }
-
-    public void testGetResourcesMap2() throws Exception {
-        String token = "tvshows";
-        Map<String, URL> resourcesMap = resourceFinder.getResourcesMap(token);
-        Set<Map.Entry<String, URL>> entries = resourcesMap.entrySet();
-        for (Map.Entry<String, URL> entry : entries) {
-            String key = entry.getKey();
-            URL value = entry.getValue();
-
-            assertTrue("key not a directory", !key.contains("/"));
-            assertTrue("contains META-INF/", value.getPath().contains("META-INF/"));
-            assertTrue("ends with META-INF/" + token + "/" + key, value.getPath().endsWith("META-INF/"
+ token + "/" + key));
-            assertTrue("value not a directory", !value.getPath().endsWith("/"));
-        }
-
-        assertTrue("map contains simpsons.properties", resourcesMap.containsKey("simpsons.properties"));
-        assertTrue("map contains familyguy.properties", resourcesMap.containsKey("familyguy.properties"));
-    }
-
-    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-    //
-    //   Find String
-    //
-    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
-    public void testFindString() throws Exception {
-        String expected = One.class.getName();
-        String actual = resourceFinder.findString("java.io.Serializable");
-        assertEquals(expected, actual);
-    }
-
-    public void testFindAllStrings() throws Exception {
-        List<String> manifests;
-        try {
-            manifests = resourceFinder.findAllStrings("MANIFEST.MF");
-        } catch (Exception thisIsLegal) {
-            return;
-        }
-
-        assertTrue("manifests found", manifests.size() > 1);
-        for (String manifest : manifests) {
-            assertTrue("starts with 'Manifest-Version'", manifest.startsWith("Manifest-Version"));
-        }
-    }
-
-    public void testFindAvailableStrings() throws Exception {
-        List<String> manifests = resourceFinder.findAvailableStrings("MANIFEST.MF");
-
-        assertTrue("manifests found", manifests.size() > 1);
-        for (String manifest : manifests) {
-            assertTrue("starts with 'Manifest-Version'", manifest.startsWith("Manifest-Version"));
-        }
-    }
-
-    public void testMapAllStrings() throws Exception {
-        Map<String, String> resourcesMap = resourceFinder.mapAllStrings("serializables");
-
-        assertEquals("map size", 3, resourcesMap.size());
-        assertTrue("map contains key 'one'", resourcesMap.containsKey("one"));
-        assertEquals(One.class.getName(), resourcesMap.get("one"));
-
-        assertTrue("map contains key 'two'", resourcesMap.containsKey("two"));
-        assertEquals(Two.class.getName(), resourcesMap.get("two"));
-
-        assertTrue("map contains key 'three'", resourcesMap.containsKey("three"));
-        assertEquals(Three.class.getName(), resourcesMap.get("three"));
-    }
-
-    public void testMapAvailableStrings() throws Exception {
-        Map<String, String> resourcesMap = resourceFinder.mapAvailableStrings("serializables");
-
-        assertEquals("map size", 3, resourcesMap.size());
-        assertTrue("map contains key 'one'", resourcesMap.containsKey("one"));
-        assertEquals(One.class.getName(), resourcesMap.get("one"));
-
-        assertTrue("map contains key 'two'", resourcesMap.containsKey("two"));
-        assertEquals(Two.class.getName(), resourcesMap.get("two"));
-
-        assertTrue("map contains key 'three'", resourcesMap.containsKey("three"));
-        assertEquals(Three.class.getName(), resourcesMap.get("three"));
-    }
-
-    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-    //
-    //   Find Class
-    //
-    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
-    public void testFindClass() throws Exception {
-        Class actual = resourceFinder.findClass("java.io.Serializable");
-        assertEquals(One.class, actual);
-
-        try {
-            resourceFinder.findClass("java.io.OutputStream");
-            fail("ClassNotFoundException should be thrown");
-        } catch (ClassNotFoundException success) {
-            // pass
-        } catch (Exception e) {
-            fail("Wrong exception type was thrown: " + e.getClass().getName());
-        }
-    }
-
-    public void testFindAllClasses() throws Exception {
-        List<Class<?>> classes = resourceFinder.findAllClasses("java.io.Serializable");
-        assertEquals("size", 1, classes.size());
-        assertEquals(One.class, classes.get(0));
-
-        try {
-            resourceFinder.findAllClasses("java.io.OutputStream");
-            fail("ClassNotFoundException should be thrown");
-        } catch (ClassNotFoundException success) {
-            // pass
-        } catch (Exception e) {
-            fail("Wrong exception type was thrown: " + e.getClass().getName());
-        }
-    }
-
-    public void testFindAvailableClasses() throws Exception {
-        List<Class<?>> classes = resourceFinder.findAvailableClasses("java.io.Serializable");
-        assertEquals("size", 1, classes.size());
-        assertEquals(One.class, classes.get(0));
-
-        classes = resourceFinder.findAvailableClasses("java.io.OutputStream");
-        assertEquals("size", 0, classes.size());
-    }
-
-    public void testMapAllClasses() throws Exception {
-        Map<String, Class<?>> resourcesMap = resourceFinder.mapAllClasses("serializables");
-
-        assertEquals("map size", 3, resourcesMap.size());
-        assertTrue("map contains key 'one'", resourcesMap.containsKey("one"));
-        assertEquals(One.class, resourcesMap.get("one"));
-
-        assertTrue("map contains key 'two'", resourcesMap.containsKey("two"));
-        assertEquals(Two.class, resourcesMap.get("two"));
 
-        assertTrue("map contains key 'three'", resourcesMap.containsKey("three"));
-        assertEquals(Three.class, resourcesMap.get("three"));
+import static org.junit.Assert.assertEquals;
 
-        try {
-            resourceFinder.mapAllClasses("externalizables");
-            fail("ClassNotFoundException should be thrown");
-        } catch (ClassNotFoundException success) {
-            // pass
-        } catch (Exception e) {
-            fail("Wrong exception type was thrown: " + e.getClass().getName());
-        }
-    }
-
-    public void testMapAvailableClasses() throws Exception {
-        Map<String, Class<?>> resourcesMap = resourceFinder.mapAvailableClasses("externalizables");
-
-        assertEquals("map size", 2, resourcesMap.size());
-        assertTrue("map contains key 'one'", resourcesMap.containsKey("one"));
-        assertEquals(One.class, resourcesMap.get("one"));
-
-        assertTrue("map contains key 'two'", resourcesMap.containsKey("two"));
-        assertEquals(Two.class, resourcesMap.get("two"));
-    }
-
-    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-    //
-    //   Find Implementation
-    //
-    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
-    public void testFindImplementation() throws Exception {
-        Class expected = One.class;
-        Class actual = resourceFinder.findImplementation(java.io.Serializable.class);
-        assertEquals(expected, actual);
+public class ResourceFinderFromWithWeirdUrlTest {
+    @Test
+    public void folderUrlCorrectButNotCommon() throws IOException {
+        final File file = new File("ResourceFinderFromWithWeirdUrlTest/META-INF/foo.txt");
+        file.getParentFile().mkdirs();
+        final FileWriter w = new FileWriter(file);
+        w.write("just some content");
+        w.close();
 
-        try {
-            resourceFinder.findImplementation(java.io.InputStream.class);
-            fail("ClassCastException should be thrown");
-        } catch (ClassCastException success) {
-        } catch (Exception e) {
-            fail("Wrong exception type was thrown: " + e.getClass().getName());
-        }
-    }
-
-    public void testFindAllImplementations() throws Exception {
-        List<Class<? extends java.io.Serializable>> classes = resourceFinder.findAllImplementations(java.io.Serializable.class);
-        assertEquals("size", 1, classes.size());
-        assertEquals(One.class, classes.get(0));
 
-        try {
-            resourceFinder.findAllImplementations(java.io.InputStream.class);
-            fail("ClassNotFoundException should be thrown");
-        } catch (ClassCastException success) {
-        } catch (Exception e) {
-            fail("Wrong exception type was thrown: " + e.getClass().getName());
-        }
+        final ResourceFinder r = new ResourceFinder("", new URL("file:ResourceFinderFromWithWeirdUrlTest"));
+        assertEquals(1, r.getResourcesMap("META-INF/").size());
     }
-
-    public void testMapAllImplementations() throws Exception {
-        Map<String, Class<? extends javax.naming.spi.ObjectFactory>> resourcesMap
= resourceFinder.mapAllImplementations(javax.naming.spi.ObjectFactory.class);
-
-        assertEquals("map size", 3, resourcesMap.size());
-        assertTrue("map contains key 'java'", resourcesMap.containsKey("java"));
-        assertEquals(javaURLContextFactory.class, resourcesMap.get("java"));
-
-        assertTrue("map contains key 'kernel'", resourcesMap.containsKey("kernel"));
-        assertEquals(kernelURLContextFactory.class, resourcesMap.get("kernel"));
-
-        assertTrue("map contains key 'ldap'", resourcesMap.containsKey("ldap"));
-        assertEquals(ldapURLContextFactory.class, resourcesMap.get("ldap"));
-
-        try {
-            resourceFinder.mapAllImplementations(java.net.URLStreamHandler.class);
-            fail("ClassNotFoundException should be thrown");
-        } catch (ClassCastException success) {
-            // pass
-        } catch (Exception e) {
-            fail("Wrong exception type was thrown: " + e.getClass().getName());
-        }
-    }
-
-    public void testMapAvailableImplementations() throws Exception {
-        Map<String, Class<? extends java.net.URLStreamHandler>> resourcesMap
= resourceFinder.mapAvailableImplementations(java.net.URLStreamHandler.class);
-
-        assertEquals("map size", 2, resourcesMap.size());
-        assertTrue("map contains key 'bar'", resourcesMap.containsKey("bar"));
-        assertEquals(BarUrlHandler.class, resourcesMap.get("bar"));
-
-        assertTrue("map contains key 'foo'", resourcesMap.containsKey("foo"));
-        assertEquals(FooUrlHandler.class, resourcesMap.get("foo"));
-    }
-
-    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-    //
-    //   Find Properties
-    //
-    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
-    public void testFindProperties() throws Exception {
-        Properties properties = resourceFinder.findProperties("tvshows/familyguy.properties");
-        assertNotNull("properties", properties);
-        validateFamilyGuy(properties);
-
-        properties = resourceFinder.findProperties("tvshows/simpsons.properties");
-        assertNotNull("properties", properties);
-        validateSimpsons(properties);
-    }
-
-    public void testFindAllProperties() throws Exception {
-        List<Properties> propertiesLists = resourceFinder.findAllProperties("tvshows/familyguy.properties");
-        assertNotNull("properties", propertiesLists);
-        assertEquals("list size", 1, propertiesLists.size());
-
-        Properties properties = propertiesLists.get(0);
-        validateFamilyGuy(properties);
-    }
-
-    public void testFindAvailableProperties() throws Exception {
-        List<Properties> propertiesLists = resourceFinder.findAvailableProperties("tvshows/familyguy.properties");
-        assertNotNull("properties", propertiesLists);
-        assertEquals("list size", 1, propertiesLists.size());
-
-        Properties properties = propertiesLists.get(0);
-        validateFamilyGuy(properties);
-    }
-
-    public void testMapAllProperties() throws Exception {
-        Map<String, Properties> propertiesMap = resourceFinder.mapAllProperties("tvshows");
-        assertNotNull("properties", propertiesMap);
-        assertEquals("map size", 2, propertiesMap.size());
-
-        assertTrue("contains 'familyguy.properties'", propertiesMap.containsKey("familyguy.properties"));
-        validateFamilyGuy(propertiesMap.get("familyguy.properties"));
-
-        assertTrue("contains 'simpsons.properties'", propertiesMap.containsKey("simpsons.properties"));
-        validateSimpsons(propertiesMap.get("simpsons.properties"));
-
-        try {
-            resourceFinder.mapAllProperties("movies");
-        } catch (Exception success) {
-        }
-
-    }
-
-    public void testMapAvailableProperties() throws Exception {
-        Map<String, Properties> propertiesMap = resourceFinder.mapAvailableProperties("movies");
-        assertNotNull("properties", propertiesMap);
-        assertEquals("map size", 2, propertiesMap.size());
-
-        assertTrue("contains 'serenity.properties'", propertiesMap.containsKey("serentity.properties"));
-        Properties properties = propertiesMap.get("serentity.properties");
-        assertEquals("director", "Joss Whedon", properties.getProperty("director"));
-        assertEquals("star", "Nathan Fillion", properties.getProperty("star"));
-        assertEquals("year", "2005", properties.getProperty("year"));
-
-        assertTrue("contains 'kingkong.properties'", propertiesMap.containsKey("kingkong.properties"));
-        properties = propertiesMap.get("kingkong.properties");
-        assertEquals("director", "Peter Jackson", properties.getProperty("director"));
-        assertEquals("star", "Naomi Watts", properties.getProperty("star"));
-        assertEquals("year", "2005", properties.getProperty("year"));
-    }
-
-
-    public void testWebinfJar() throws Exception {
-
-        Map<String, String> map = new HashMap<String, String>();
-        map.put("WEB-INF/beans.xml", "<beans/>");
-
-        final File jarFile = Archives.jarArchive(map);
-
-        final URL jarFileUrl = jarFile.toURI().toURL();
-        final ResourceFinder finder = new ResourceFinder(jarFileUrl);
-
-        final URL beansXmlUrl = finder.find("WEB-INF/beans.xml");
-
-        assertNotNull(beansXmlUrl);
-    }
-
-
-    private static void readJarEntries(URL location, String basePath, Map<String, URL>
resources) throws IOException {
-        JarURLConnection conn = (JarURLConnection) location.openConnection();
-        JarFile jarfile = null;
-        jarfile = conn.getJarFile();
-
-        Enumeration<JarEntry> entries = jarfile.entries();
-        while (entries != null && entries.hasMoreElements()) {
-            JarEntry entry = entries.nextElement();
-            String name = entry.getName();
-
-            if (entry.isDirectory() || !name.startsWith(basePath) || name.length() == basePath.length())
{
-                continue;
-            }
-
-            name = name.substring(basePath.length());
-
-            if (name.contains("/")) {
-                continue;
-            }
-
-            URL resource = new URL(location, name);
-            resources.put(name, resource);
-        }
-    }
-
-
-
-    private void validateSimpsons(Properties properties) {
-        assertEquals("props size", 6, properties.size());
-        assertEquals("creator", "Matt Groening", properties.getProperty("creator"));
-        assertEquals("father", "Homer", properties.getProperty("father"));
-        assertEquals("mother", "Marge", properties.getProperty("mother"));
-        assertEquals("son", "Bart", properties.getProperty("son"));
-        assertEquals("daughter", "Lisa", properties.getProperty("daughter"));
-        assertEquals("baby", "Maggie", properties.getProperty("baby"));
-    }
-
-    private void validateFamilyGuy(Properties properties) {
-        assertEquals("props size", 6, properties.size());
-        assertEquals("creator", "Seth MacFarlane", properties.getProperty("creator"));
-        assertEquals("father", "Peter", properties.getProperty("father"));
-        assertEquals("mother", "Lois", properties.getProperty("mother"));
-        assertEquals("son", "Chris", properties.getProperty("son"));
-        assertEquals("daughter", "Meg", properties.getProperty("daughter"));
-        assertEquals("baby", "Stewie", properties.getProperty("baby"));
-    }
-
-
-    /*
-     * Disable test because it's failing its purpose:
-     *   - when running in maven in a clean build, no urls are found
-     *       so the test runs with the ResourceFinder using the classloader
-     *       instead of urls
-     *   - when running on a non clean build in maven, one url is found,
-     *       but the test fails
-
-    public void testUrlConstructor() throws Exception {
-        List<URL> all = resourceFinder.findAll("MANIFEST.MF");
-
-        List<URL> urls = new ArrayList();
-        for (URL url : all) {
-            if (url.getPath().contains("xbean-finder")){
-                urls.add(url);
-            }
-        }
-
-        resourceFinder = new ResourceFinder("META-INF/", urls.toArray(new URL[]{}));
-        testGetResourcesMap1();
-        testGetResourcesMap2();
-        testFindString();
-        testFindAllStrings();
-        testFindAvailableStrings();
-        testMapAllStrings();
-        testMapAvailableStrings();
-        testFindClass();
-        testFindAllClasses();
-        testFindAvailableClasses();
-        testMapAllClasses();
-        testMapAvailableClasses();
-        testFindImplementation();
-        testFindAllImplementations();
-        testMapAllImplementations();
-        testMapAvailableImplementations();
-        testFindProperties();
-        testFindAllProperties();
-        testFindAvailableProperties();
-        testMapAllProperties();
-        testMapAvailableProperties();
-    }
-    */
-
-
 }



Mime
View raw message