1 17 18 package org.apache.tools.ant; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.Enumeration ; 27 import java.util.List ; 28 import junit.framework.TestCase; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.types.Path; 32 import org.apache.tools.ant.util.FileUtils; 33 34 38 public class AntClassLoaderDelegationTest extends TestCase { 39 40 private Project p; 41 42 public AntClassLoaderDelegationTest(String name) { 43 super(name); 44 } 45 46 public void setUp() { 47 p = new Project(); 48 p.init(); 49 } 50 51 52 private static final String TEST_RESOURCE = "org/apache/tools/ant/IncludeTest.class"; 53 54 public void testFindResources() throws Exception { 55 String buildTestcases = System.getProperty("build.tests"); 59 assertNotNull("defined ${build.tests}", buildTestcases); 60 assertTrue("have a dir " + buildTestcases, new File (buildTestcases).isDirectory()); 61 Path path = new Path(p, buildTestcases); 62 ClassLoader parent = new ParentLoader(); 64 ClassLoader acl = new AntClassLoader(parent, p, path, true); 66 URL urlFromPath = new URL (FileUtils.newFileUtils().toURI(buildTestcases) + TEST_RESOURCE); 68 URL urlFromParent = new URL ("http://ant.apache.org/" + TEST_RESOURCE); 69 assertEquals("correct resources (regular delegation order)", 70 Arrays.asList(new URL [] {urlFromParent, urlFromPath}), 71 enum2List(acl.getResources(TEST_RESOURCE))); 72 acl = new AntClassLoader(parent, p, path, false); 73 assertEquals("correct resources (reverse delegation order)", 74 Arrays.asList(new URL [] {urlFromPath, urlFromParent}), 75 enum2List(acl.getResources(TEST_RESOURCE))); 76 } 77 78 private static List enum2List(Enumeration e) { 79 List l = new ArrayList (); 81 while (e.hasMoreElements()) { 82 l.add(e.nextElement()); 83 } 84 return l; 85 } 86 87 88 private static final class ParentLoader extends ClassLoader { 89 90 public ParentLoader() {} 91 92 protected Enumeration findResources(String name) throws IOException { 93 if (name.equals(TEST_RESOURCE)) { 94 return Collections.enumeration(Collections.singleton(new URL ("http://ant.apache.org/" + name))); 95 } else { 96 return Collections.enumeration(Collections.EMPTY_SET); 97 } 98 } 99 100 } 101 102 } 103 | Popular Tags |