KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > AntClassLoaderDelegationTest


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.List JavaDoc;
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 /**
35  * Test case for ant class loader
36  *
37  */

38 public class AntClassLoaderDelegationTest extends TestCase {
39
40     private Project p;
41
42     public AntClassLoaderDelegationTest(String JavaDoc name) {
43         super(name);
44     }
45
46     public void setUp() {
47         p = new Project();
48         p.init();
49     }
50
51     /** Sample resource present in build/testcases/ */
52     private static final String JavaDoc TEST_RESOURCE = "org/apache/tools/ant/IncludeTest.class";
53     
54     public void testFindResources() throws Exception JavaDoc {
55         //System.err.println("loading from: " + AntClassLoader.class.getProtectionDomain().getCodeSource().getLocation());
56
// See bug #30161.
57
// This path should contain the class files for these testcases:
58
String JavaDoc buildTestcases = System.getProperty("build.tests");
59         assertNotNull("defined ${build.tests}", buildTestcases);
60         assertTrue("have a dir " + buildTestcases, new File JavaDoc(buildTestcases).isDirectory());
61         Path path = new Path(p, buildTestcases);
62         // A special parent loader which is not the system class loader:
63
ClassLoader JavaDoc parent = new ParentLoader();
64         // An AntClassLoader which is supposed to delegate to the parent and then to the disk path:
65
ClassLoader JavaDoc acl = new AntClassLoader(parent, p, path, true);
66         // The intended result URLs:
67
URL JavaDoc urlFromPath = new URL JavaDoc(FileUtils.newFileUtils().toURI(buildTestcases) + TEST_RESOURCE);
68         URL JavaDoc urlFromParent = new URL JavaDoc("http://ant.apache.org/" + TEST_RESOURCE);
69         assertEquals("correct resources (regular delegation order)",
70             Arrays.asList(new URL JavaDoc[] {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 JavaDoc[] {urlFromPath, urlFromParent}),
75             enum2List(acl.getResources(TEST_RESOURCE)));
76     }
77     
78     private static List JavaDoc enum2List(Enumeration JavaDoc e) {
79         // JDK 1.4: return Collections.list(e);
80
List JavaDoc l = new ArrayList JavaDoc();
81         while (e.hasMoreElements()) {
82             l.add(e.nextElement());
83         }
84         return l;
85     }
86     
87     /** Special loader that just knows how to find TEST_RESOURCE. */
88     private static final class ParentLoader extends ClassLoader JavaDoc {
89         
90         public ParentLoader() {}
91         
92         protected Enumeration JavaDoc findResources(String JavaDoc name) throws IOException JavaDoc {
93             if (name.equals(TEST_RESOURCE)) {
94                 return Collections.enumeration(Collections.singleton(new URL JavaDoc("http://ant.apache.org/" + name)));
95             } else {
96                 return Collections.enumeration(Collections.EMPTY_SET);
97             }
98         }
99         
100     }
101     
102 }
103
Popular Tags