@@ -19,16 +19,21 @@
package org.apache.tools.ant;
package org.apache.tools.ant;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintStream;
import java.net.URL;
import java.net.URL;
import java.util.Enumeration;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.CollectionUtils;
import org.junit.After;
import org.junit.After;
import org.junit.Before;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Rule;
@@ -40,8 +45,8 @@ import org.junit.Test;
*/
*/
public class AntClassLoaderTest {
public class AntClassLoaderTest {
@Rule
public BuildFileRule buildRule = new BuildFileRule();
@Rule
public BuildFileRule buildRule = new BuildFileRule();
private AntClassLoader loader;
private AntClassLoader loader;
@@ -199,6 +204,44 @@ public class AntClassLoaderTest {
}
}
}
}
/**
* Asserts that getResources won't return resources that cannot be
* seen by AntClassLoader but by ClassLoader.this.parent.
*
* @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=46752">
* https://issues.apache.org/bugzilla/show_bug.cgi?id=46752</a>
*/
@Test
public void testGetResources() throws IOException {
AntClassLoader acl = new AntClassLoader(new EmptyLoader(), null,
new Path(null), true);
assertNull(acl.getResource("META-INF/MANIFEST.MF"));
assertFalse(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements());
// double check using system classloader as parent
acl = new AntClassLoader(null, null, new Path(null), true);
assertNotNull(acl.getResource("META-INF/MANIFEST.MF"));
assertTrue(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements());
}
@Test
public void testGetResourcesUsingFactory() throws IOException {
AntClassLoader acl =
AntClassLoader.newAntClassLoader(new EmptyLoader(), null,
new Path(null), true);
assertNull(acl.getResource("META-INF/MANIFEST.MF"));
assertFalse(acl.getResources("META-INF/MANIFEST.MF").hasMoreElements());
}
private static class EmptyLoader extends ClassLoader {
public URL getResource(String n) {
return null;
}
public Enumeration getResources(String n) {
return new CollectionUtils.EmptyEnumeration();
}
}
private static class GetPackageWrapper extends ClassLoader {
private static class GetPackageWrapper extends ClassLoader {
GetPackageWrapper(ClassLoader parent) {
GetPackageWrapper(ClassLoader parent) {
super(parent);
super(parent);