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: */package org.apache.geronimo.jetty6;
017:
018: import java.io.File;
019: import java.net.URL;
020:
021: import org.apache.geronimo.testsupport.TestSupport;
022:
023: import org.apache.geronimo.kernel.config.MultiParentClassLoader;
024: import org.apache.geronimo.kernel.repository.Artifact;
025:
026: /**
027: * Tests loading various classes (as classes and URL resources) with different
028: * settings for contextPriorityClassLoader to make sure the restrictions on
029: * javax.* class loading are honored.
030: *
031: * @version $Rev: 513131 $ $Date: 2007-02-28 20:31:29 -0800 (Wed, 28 Feb 2007) $
032: */
033: public class ClassLoaderTest extends TestSupport {
034: Artifact configId = new Artifact("foo", "bar", "1", "car");
035: ClassLoader cl;
036: URL[] urls;
037: private static final String[] HIDDEN = { "org.apache.geronimo",
038: "org.mortbay", "org.xml", "org.w3c" };
039: private static final String[] NON_OVERRIDABLE = { "java.", "javax." };
040:
041: public void setUp() throws Exception {
042: super .setUp();
043: URL url = new File(BASEDIR,
044: "src/test/resources/deployables/cltest/").toURL();
045: urls = new URL[] { url };
046: }
047:
048: //todo: try more restricted prefixed besides javax.*
049:
050: /**
051: * Tries to load a javax.* class that's not available from the
052: * parent ClassLoader. This should work.
053: */
054: public void testFalseNonexistantJavaxClass() {
055: cl = new MultiParentClassLoader(configId, urls, getClass()
056: .getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
057: try {
058: cl.loadClass("javax.foo.Foo");
059: } catch (ClassNotFoundException e) {
060: fail("Should be able to load a javax.* class that is not defined by my parent CL");
061: }
062: }
063:
064: /**
065: * Tries to load a javax.* class that's not available from the
066: * parent ClassLoader. This should work.
067: */
068: public void testTrueNonexistantJavaxClass() {
069: cl = new MultiParentClassLoader(configId, urls, getClass()
070: .getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
071: try {
072: cl.loadClass("javax.foo.Foo");
073: } catch (ClassNotFoundException e) {
074: fail("Should be able to load a javax.* class that is not defined by my parent CL");
075: }
076: }
077:
078: /**
079: * Tries to load a javax.* class that is avialable from the parent ClassLoader,
080: * when there's a different definition available from this ClassLoader too.
081: * This should always load the parent's copy.
082: */
083: public void testFalseExistantJavaxClass() {
084: cl = new MultiParentClassLoader(configId, urls, getClass()
085: .getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
086: try {
087: Class cls = cl.loadClass("javax.servlet.Servlet");
088: assertTrue(
089: "Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",
090: cls.getDeclaredMethods().length > 0);
091: } catch (ClassNotFoundException e) {
092: fail("Problem with test; expecting to have javax.servlet.* on the ClassPath");
093: }
094: }
095:
096: /**
097: * Tries to load a javax.* class that is avialable from the parent ClassLoader,
098: * when there's a different definition available from this ClassLoader too.
099: * This should always load the parent's copy.
100: */
101: public void testTrueExistantJavaxClass() {
102: cl = new MultiParentClassLoader(configId, urls, getClass()
103: .getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
104: try {
105: Class cls = cl.loadClass("javax.servlet.Servlet");
106: assertTrue(
107: "Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",
108: cls.getDeclaredMethods().length > 0);
109: } catch (ClassNotFoundException e) {
110: fail("Problem with test; expecting to have javax.servlet.* on the ClassPath");
111: }
112: }
113:
114: /**
115: * Tries to load a non-javax.* class that is aailable form the parent
116: * ClassLoader, when there's a different definition available from this
117: * ClassLoader. This should load the parent's copy when
118: * contextPriorityClassLoader is set to false (as here) and the child's
119: * copy when the contextPriorityClassLoader is set to true.
120: */
121: public void xtestFalseExistantNonJavaxClass() {
122: cl = new MultiParentClassLoader(configId, urls, getClass()
123: .getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
124: try {
125: Class cls = cl.loadClass("mx4j.MBeanDescription");
126: assertTrue(
127: "Should not have overriden parent CL definition of class mx4j.MBeanDescription",
128: cls.getDeclaredMethods().length > 0);
129: } catch (ClassNotFoundException e) {
130: fail("Problem with test; expecting to have mx4j.* on the ClassPath");
131: }
132: }
133:
134: /**
135: * Tries to load a non-javax.* class that is aailable form the parent
136: * ClassLoader, when there's a different definition available from this
137: * ClassLoader. This should load the parent's copy when
138: * contextPriorityClassLoader is set to false and the child's copy when
139: * the contextPriorityClassLoader is set to true (as here).
140: */
141: public void xtestTrueExistantNonJavaxClass() {
142: cl = new MultiParentClassLoader(configId, urls, getClass()
143: .getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
144: try {
145: Class cls = cl.loadClass("mx4j.MBeanDescription");
146: assertTrue(
147: "Should be able to override a class that is not in java.*, javax.*, etc.",
148: cls.getDeclaredMethods().length == 0);
149: } catch (ClassNotFoundException e) {
150: fail("Problem with test; expecting to have mx4j.* on the ClassPath");
151: }
152: }
153:
154: /**
155: * Tries to load a javax.* class that's not available from the
156: * parent ClassLoader. This should work.
157: */
158: public void testFalseNonexistantJavaxResource() {
159: cl = new MultiParentClassLoader(configId, urls, getClass()
160: .getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
161: URL url = cl.getResource("javax/foo/Foo.class");
162: if (url == null) {
163: fail("Should be able to load a javax.* class that is not defined by my parent CL");
164: }
165: assertEquals(url.getProtocol(), "file");
166: }
167:
168: /**
169: * Tries to load a javax.* class that's not available from the
170: * parent ClassLoader. This should work.
171: */
172: public void testTrueNonexistantJavaxResource() {
173: cl = new MultiParentClassLoader(configId, urls, getClass()
174: .getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
175: URL url = cl.getResource("javax/foo/Foo.class");
176: if (url == null) {
177: fail("Should be able to load a javax.* class that is not defined by my parent CL");
178: }
179: assertEquals(url.getProtocol(), "file");
180: }
181:
182: /**
183: * Tries to load a javax.* class that is avialable from the parent ClassLoader,
184: * when there's a different definition available from this ClassLoader too.
185: * This should always load the parent's copy.
186: */
187: public void testFalseExistantJavaxResource() {
188: cl = new MultiParentClassLoader(configId, urls, getClass()
189: .getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
190: URL url = cl.getResource("javax/servlet/Servlet.class");
191: if (url == null) {
192: fail("Problem with test; expecting to have javax.servlet.* on the ClassPath");
193: }
194: assertEquals(
195: "Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",
196: url.getProtocol(), "jar");
197: }
198:
199: /**
200: * Tries to load a javax.* class that is avialable from the parent ClassLoader,
201: * when there's a different definition available from this ClassLoader too.
202: * This should always load the parent's copy.
203: */
204: public void testTrueExistantJavaxResource() {
205: cl = new MultiParentClassLoader(configId, urls, getClass()
206: .getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
207: URL url = cl.getResource("javax/servlet/Servlet.class");
208: if (url == null) {
209: fail("Problem with test; expecting to have javax.servlet.* on the ClassPath");
210: }
211: assertEquals(
212: "Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",
213: url.getProtocol(), "jar");
214: }
215:
216: /**
217: * Tries to load a non-javax.* class that is aailable form the parent
218: * ClassLoader, when there's a different definition available from this
219: * ClassLoader. This should load the parent's copy when
220: * contextPriorityClassLoader is set to false (as here) and the child's
221: * copy when the contextPriorityClassLoader is set to true.
222: */
223: public void xtestFalseExistantNonJavaxResource() {
224: cl = new MultiParentClassLoader(configId, urls, getClass()
225: .getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
226: URL url = cl.getResource("mx4j/MBeanDescription.class");
227: if (url == null) {
228: fail("Problem with test; expecting to have mx4j.* on the ClassPath");
229: }
230: assertEquals(
231: "Should not have overriden parent CL definition of class mx4j.MBeanDescription",
232: url.getProtocol(), "jar");
233: }
234:
235: /**
236: * Tries to load a non-javax.* class that is aailable form the parent
237: * ClassLoader, when there's a different definition available from this
238: * ClassLoader. This should load the parent's copy when
239: * contextPriorityClassLoader is set to false and the child's copy when
240: * the contextPriorityClassLoader is set to true (as here).
241: */
242: public void testTrueExistantNonJavaxResource() {
243: cl = new MultiParentClassLoader(configId, urls, getClass()
244: .getClassLoader(), true, new String[] {},
245: NON_OVERRIDABLE);
246: URL url = cl.getResource("mx4j/MBeanDescription.class");
247: if (url == null) {
248: fail("Problem with test; expecting to have mx4j.* on the ClassPath");
249: }
250: assertEquals(
251: "Should be able to override a class that is not in java.*, javax.*, etc.",
252: url.getProtocol(), "file");
253: }
254: }
|