XBEAN-282 trying to avoid suplication in ClassLoaders.findUrls()
git-svn-id: https://svn.apache.org/repos/asf/geronimo/xbean/trunk@1677945 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java b/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
index 8f05968..4468b8e 100644
--- a/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
+++ b/xbean-finder/src/main/java/org/apache/xbean/finder/ClassLoaders.java
@@ -58,7 +58,14 @@
// 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;
diff --git a/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java b/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
index 6c340f1..46d50e2 100644
--- a/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
+++ b/xbean-finder/src/test/java/org/apache/xbean/finder/ClassLoadersTest.java
@@ -18,11 +18,20 @@
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 @@
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());
+ }
}