KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > ClassLoaderTest


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

17
18 package org.apache.geronimo.jetty6;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.apache.geronimo.testsupport.TestSupport;
24
25 import org.apache.geronimo.kernel.config.MultiParentClassLoader;
26 import org.apache.geronimo.kernel.repository.Artifact;
27
28 /**
29  * Tests loading various classes (as classes and URL resources) with different
30  * settings for contextPriorityClassLoader to make sure the restrictions on
31  * javax.* class loading are honored.
32  *
33  * @version $Rev: 482336 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
34  */

35 public class ClassLoaderTest extends TestSupport {
36     Artifact configId = new Artifact("foo", "bar", "1", "car");
37     ClassLoader JavaDoc cl;
38     URL JavaDoc[] urls;
39     private static final String JavaDoc[] HIDDEN = {"org.apache.geronimo", "org.mortbay", "org.xml", "org.w3c"};
40     private static final String JavaDoc[] NON_OVERRIDABLE = {"java.", "javax."};
41
42     public void setUp() throws Exception JavaDoc {
43         super.setUp();
44         URL JavaDoc url = new File JavaDoc(BASEDIR, "src/test/resources/deployables/cltest/").toURL();
45         urls = new URL JavaDoc[]{url};
46     }
47
48     //todo: try more restricted prefixed besides javax.*
49

50     /**
51      * Tries to load a javax.* class that's not available from the
52      * parent ClassLoader. This should work.
53      */

54     public void testFalseNonexistantJavaxClass() {
55         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
56         try {
57             cl.loadClass("javax.foo.Foo");
58         } catch(ClassNotFoundException JavaDoc e) {
59             fail("Should be able to load a javax.* class that is not defined by my parent CL");
60         }
61     }
62
63     /**
64      * Tries to load a javax.* class that's not available from the
65      * parent ClassLoader. This should work.
66      */

67     public void testTrueNonexistantJavaxClass() {
68         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
69         try {
70             cl.loadClass("javax.foo.Foo");
71         } catch(ClassNotFoundException JavaDoc e) {
72             fail("Should be able to load a javax.* class that is not defined by my parent CL");
73         }
74     }
75
76     /**
77      * Tries to load a javax.* class that is avialable from the parent ClassLoader,
78      * when there's a different definition available from this ClassLoader too.
79      * This should always load the parent's copy.
80      */

81     public void testFalseExistantJavaxClass() {
82         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
83         try {
84             Class JavaDoc cls = cl.loadClass("javax.servlet.Servlet");
85             assertTrue("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",cls.getDeclaredMethods().length > 0);
86         } catch(ClassNotFoundException JavaDoc e) {
87             fail("Problem with test; expecting to have javax.servlet.* on the ClassPath");
88         }
89     }
90
91     /**
92      * Tries to load a javax.* class that is avialable from the parent ClassLoader,
93      * when there's a different definition available from this ClassLoader too.
94      * This should always load the parent's copy.
95      */

96     public void testTrueExistantJavaxClass() {
97         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
98         try {
99             Class JavaDoc cls = cl.loadClass("javax.servlet.Servlet");
100             assertTrue("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",cls.getDeclaredMethods().length > 0);
101         } catch(ClassNotFoundException JavaDoc e) {
102             fail("Problem with test; expecting to have javax.servlet.* on the ClassPath");
103         }
104     }
105
106     /**
107      * Tries to load a non-javax.* class that is aailable form the parent
108      * ClassLoader, when there's a different definition available from this
109      * ClassLoader. This should load the parent's copy when
110      * contextPriorityClassLoader is set to false (as here) and the child's
111      * copy when the contextPriorityClassLoader is set to true.
112      */

113     public void testFalseExistantNonJavaxClass() {
114         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
115         try {
116             Class JavaDoc cls = cl.loadClass("mx4j.MBeanDescription");
117             assertTrue("Should not have overriden parent CL definition of class mx4j.MBeanDescription", cls.getDeclaredMethods().length > 0);
118         } catch(ClassNotFoundException JavaDoc e) {
119             fail("Problem with test; expecting to have mx4j.* on the ClassPath");
120         }
121     }
122
123     /**
124      * Tries to load a non-javax.* class that is aailable form the parent
125      * ClassLoader, when there's a different definition available from this
126      * ClassLoader. This should load the parent's copy when
127      * contextPriorityClassLoader is set to false and the child's copy when
128      * the contextPriorityClassLoader is set to true (as here).
129      */

130     public void testTrueExistantNonJavaxClass() {
131         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
132         try {
133             Class JavaDoc cls = cl.loadClass("mx4j.MBeanDescription");
134             assertTrue("Should be able to override a class that is not in java.*, javax.*, etc.", cls.getDeclaredMethods().length == 0);
135         } catch(ClassNotFoundException JavaDoc e) {
136             fail("Problem with test; expecting to have mx4j.* on the ClassPath");
137         }
138     }
139
140     /**
141      * Tries to load a javax.* class that's not available from the
142      * parent ClassLoader. This should work.
143      */

144     public void testFalseNonexistantJavaxResource() {
145         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
146         URL JavaDoc url = cl.getResource("javax/foo/Foo.class");
147         if(url == null) {
148             fail("Should be able to load a javax.* class that is not defined by my parent CL");
149         }
150         assertEquals(url.getProtocol(), "file");
151     }
152
153     /**
154      * Tries to load a javax.* class that's not available from the
155      * parent ClassLoader. This should work.
156      */

157     public void testTrueNonexistantJavaxResource() {
158         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
159         URL JavaDoc url = cl.getResource("javax/foo/Foo.class");
160         if(url == null) {
161             fail("Should be able to load a javax.* class that is not defined by my parent CL");
162         }
163         assertEquals(url.getProtocol(), "file");
164     }
165
166     /**
167      * Tries to load a javax.* class that is avialable from the parent ClassLoader,
168      * when there's a different definition available from this ClassLoader too.
169      * This should always load the parent's copy.
170      */

171     public void testFalseExistantJavaxResource() {
172         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
173         URL JavaDoc url = cl.getResource("javax/servlet/Servlet.class");
174         if(url == null) {
175             fail("Problem with test; expecting to have javax.servlet.* on the ClassPath");
176         }
177         assertEquals("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet", url.getProtocol(), "jar");
178     }
179
180     /**
181      * Tries to load a javax.* class that is avialable from the parent ClassLoader,
182      * when there's a different definition available from this ClassLoader too.
183      * This should always load the parent's copy.
184      */

185     public void testTrueExistantJavaxResource() {
186         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, HIDDEN, NON_OVERRIDABLE);
187         URL JavaDoc url = cl.getResource("javax/servlet/Servlet.class");
188         if(url == null) {
189             fail("Problem with test; expecting to have javax.servlet.* on the ClassPath");
190         }
191         assertEquals("Loaded wrong class first; expected to find parent CL's copy of javax.servlet.Servlet",url.getProtocol(),"jar");
192     }
193
194     /**
195      * Tries to load a non-javax.* class that is aailable form the parent
196      * ClassLoader, when there's a different definition available from this
197      * ClassLoader. This should load the parent's copy when
198      * contextPriorityClassLoader is set to false (as here) and the child's
199      * copy when the contextPriorityClassLoader is set to true.
200      */

201     public void testFalseExistantNonJavaxResource() {
202         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), false, HIDDEN, NON_OVERRIDABLE);
203         URL JavaDoc url = cl.getResource("mx4j/MBeanDescription.class");
204         if(url == null) {
205             fail("Problem with test; expecting to have mx4j.* on the ClassPath");
206         }
207         assertEquals("Should not have overriden parent CL definition of class mx4j.MBeanDescription", url.getProtocol(), "jar");
208     }
209
210     /**
211      * Tries to load a non-javax.* class that is aailable form the parent
212      * ClassLoader, when there's a different definition available from this
213      * ClassLoader. This should load the parent's copy when
214      * contextPriorityClassLoader is set to false and the child's copy when
215      * the contextPriorityClassLoader is set to true (as here).
216      */

217     public void testTrueExistantNonJavaxResource() {
218         cl = new MultiParentClassLoader(configId, urls, getClass().getClassLoader(), true, new String JavaDoc[] {}, NON_OVERRIDABLE);
219         URL JavaDoc url = cl.getResource("mx4j/MBeanDescription.class");
220         if(url == null) {
221             fail("Problem with test; expecting to have mx4j.* on the ClassPath");
222         }
223         assertEquals("Should be able to override a class that is not in java.*, javax.*, etc.", url.getProtocol(), "file");
224     }
225 }
226
Popular Tags