geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1677945 - in /geronimo/xbean/trunk/xbean-finder/src: main/java/org/apache/xbean/finder/ClassLoaders.java test/java/org/apache/xbean/finder/ClassLoadersTest.java
Date Wed, 06 May 2015 08:10:08 GMT
Author: rmannibucau
Date: Wed May  6 08:10:08 2015
New Revision: 1677945

URL: http://svn.apache.org/r1677945
Log:
XBEAN-282 trying to avoid suplication in ClassLoaders.findUrls()

Modified:
    geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
    geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java

Modified: geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java?rev=1677945&r1=1677944&r2=1677945&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
Wed May  6 08:10:08 2015
@@ -58,7 +58,14 @@ public final class ClassLoaders {
         // DONT_USE_GET_URLS || java -jar xxx.jar and use MANIFEST.MF Class-Path?
         // here perf is not an issue since we would either miss all the classpath or we have
a single jar
         if (urls.size() <= 1) {
-            urls.addAll(findUrlFromResources(classLoader));
+            final Set<URL> urlFromResources = findUrlFromResources(classLoader);
+            if (!urls.isEmpty()) {
+                final URL theUrl = urls.iterator().next();
+                if ("file".equals(theUrl.getProtocol())) {  // theUrl can be file:xxxx but
it is the same entry actually
+                    urlFromResources.remove(new URL("jar:" + theUrl.toExternalForm() + "!/"));
+                }
+            }
+            urls.addAll(urlFromResources);
         }
 
         return urls;

Modified: geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java?rev=1677945&r1=1677944&r2=1677945&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
(original)
+++ geronimo/xbean/trunk/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
Wed May  6 08:10:08 2015
@@ -18,11 +18,20 @@ package org.apache.xbean.finder;
 
 import org.junit.Test;
 
+import java.io.File;
+import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Enumeration;
 
+import static java.util.Collections.emptyEnumeration;
+import static java.util.Collections.enumeration;
+import static java.util.Collections.singleton;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class ClassLoadersTest {
     @Test
@@ -38,4 +47,42 @@ public class ClassLoadersTest {
         assertTrue(ClassLoaders.isNative(new URL("jar:" + base + "!/")));
         assertFalse(ClassLoaders.isNative(new URL("jar:" + base + ".jar!/")));
     }
+
+    @Test
+    public void singleUrl() throws IOException {
+        final File metaInf = new File("target/ClassLoadersTest/singleUrl/META-INF");
+        metaInf.getParentFile().mkdirs();
+
+        final URL url = metaInf.getParentFile().toURI().toURL();
+        final ClassLoader loader = new URLClassLoader(new URL[] {url}, new ClassLoader()
{
+            @Override
+            protected Class<?> loadClass(final String name, final boolean resolve)
throws ClassNotFoundException {
+                throw new ClassNotFoundException();
+            }
+
+            @Override
+            public Enumeration<URL> getResources(final String name) throws IOException
{
+                return emptyEnumeration();
+            }
+        }) {
+            @Override
+            public Enumeration<URL> getResources(final String name) throws IOException
{
+                if ("META-INF".equals(name)) {
+                    return emptyEnumeration();
+                }
+                return enumeration(singleton(new URL("jar:file:/tmp/app.jar!/")));
+            }
+
+            @Override
+            public URL[] getURLs() {
+                try {
+                    return new URL[] { new URL("file:/tmp/app.jar") };
+                } catch (final MalformedURLException e) {
+                    fail();
+                    throw new IllegalStateException(e);
+                }
+            }
+        };
+        assertEquals(1, ClassLoaders.findUrls(loader).size());
+    }
 }



Mime
View raw message