001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.net.URL;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.Collections;
027: import java.util.Enumeration;
028: import java.util.List;
029: import junit.framework.TestCase;
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.types.Path;
032: import org.apache.tools.ant.util.FileUtils;
033:
034: /**
035: * Test case for ant class loader
036: *
037: */
038: public class AntClassLoaderDelegationTest extends TestCase {
039:
040: /** Instance of a utility class to use for file operations. */
041: private static final FileUtils FILE_UTILS = FileUtils
042: .getFileUtils();
043:
044: private Project p;
045:
046: public AntClassLoaderDelegationTest(String name) {
047: super (name);
048: }
049:
050: public void setUp() {
051: p = new Project();
052: p.init();
053: }
054:
055: /** Sample resource present in build/testcases/ */
056: private static final String TEST_RESOURCE = "apache/tools/ant/IncludeTest.class";
057:
058: public void testFindResources() throws Exception {
059: // This path should contain the class files for these testcases:
060: String buildTestcases = System.getProperty("build.tests");
061: assertNotNull("defined ${build.tests}", buildTestcases);
062: assertTrue("have a dir " + buildTestcases, new File(
063: buildTestcases).isDirectory());
064: Path path = new Path(p, buildTestcases + "/org");
065: // A special parent loader which is not the system class loader:
066: ClassLoader parent = new ParentLoader();
067: // An AntClassLoader which is supposed to delegate to
068: // the parent and then to the disk path:
069: ClassLoader acl = new AntClassLoader(parent, p, path, true);
070: // The intended result URLs:
071: URL urlFromPath = new URL(FILE_UTILS.toURI(buildTestcases)
072: + "org/" + TEST_RESOURCE);
073: URL urlFromParent = new URL("http://ant.apache.org/"
074: + TEST_RESOURCE);
075: assertEquals(
076: "correct resources (regular delegation order)",
077: Arrays.asList(new URL[] { urlFromParent, urlFromPath }),
078: enum2List(acl.getResources(TEST_RESOURCE)));
079: acl = new AntClassLoader(parent, p, path, false);
080: assertEquals(
081: "correct resources (reverse delegation order)",
082: Arrays.asList(new URL[] { urlFromPath, urlFromParent }),
083: enum2List(acl.getResources(TEST_RESOURCE)));
084: }
085:
086: public void testFindIsolateResources() throws Exception {
087: String buildTestcases = System.getProperty("build.tests");
088: assertNotNull("defined ${build.tests}", buildTestcases);
089: assertTrue("have a dir " + buildTestcases, new File(
090: buildTestcases).isDirectory());
091: Path path = new Path(p, buildTestcases + "/org");
092: // A special parent loader which is not the system class loader:
093: ClassLoader parent = new ParentLoader();
094:
095: URL urlFromPath = new URL(FILE_UTILS.toURI(buildTestcases)
096: + "org/" + TEST_RESOURCE);
097: AntClassLoader acl = new AntClassLoader(parent, p, path, false);
098: acl.setIsolated(true);
099: assertEquals("correct resources (reverse delegation order)",
100: Arrays.asList(new URL[] { urlFromPath }), enum2List(acl
101: .getResources(TEST_RESOURCE)));
102: }
103:
104: private static List enum2List(Enumeration e) {
105: // JDK 1.4: return Collections.list(e);
106: List l = new ArrayList();
107: while (e.hasMoreElements()) {
108: l.add(e.nextElement());
109: }
110: return l;
111: }
112:
113: /** Special loader that just knows how to find TEST_RESOURCE. */
114: private static final class ParentLoader extends ClassLoader {
115:
116: public ParentLoader() {
117: }
118:
119: protected Enumeration findResources(String name)
120: throws IOException {
121: if (name.equals(TEST_RESOURCE)) {
122: return Collections.enumeration(Collections
123: .singleton(new URL("http://ant.apache.org/"
124: + name)));
125: } else {
126: return Collections.enumeration(Collections.EMPTY_SET);
127: }
128: }
129:
130: }
131:
132: }
|