source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/testcases/org/apache/tools/ant/AntClassLoaderDelegationTest.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 3.8 KB
Line 
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
18package org.apache.tools.ant;
19
20import java.io.File;
21import java.io.IOException;
22import java.net.URL;
23import java.util.ArrayList;
24import java.util.Arrays;
25import java.util.Collections;
26import java.util.Enumeration;
27import java.util.List;
28import junit.framework.TestCase;
29import org.apache.tools.ant.BuildException;
30import org.apache.tools.ant.Project;
31import org.apache.tools.ant.types.Path;
32import org.apache.tools.ant.util.FileUtils;
33
34/**
35 * Test case for ant class loader
36 *
37 */
38public 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 /** Sample resource present in build/testcases/ */
52 private static final String TEST_RESOURCE = "org/apache/tools/ant/IncludeTest.class";
53
54 public void testFindResources() throws Exception {
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 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 // A special parent loader which is not the system class loader:
63 ClassLoader parent = new ParentLoader();
64 // An AntClassLoader which is supposed to delegate to the parent and then to the disk path:
65 ClassLoader acl = new AntClassLoader(parent, p, path, true);
66 // The intended result URLs:
67 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 // JDK 1.4: return Collections.list(e);
80 List l = new ArrayList();
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 {
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}
Note: See TracBrowser for help on using the repository browser.