KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.jetty;
19
20 import java.net.URL JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.io.File JavaDoc;
23
24 import junit.framework.TestCase;
25
26 /**
27  * Tests loading various classes (as classes and URL resources) with different
28  * settings for contextPriorityClassLoader to make sure the restrictions on
29  * javax.* class loading are honored.
30  *
31  * @version $Rev: 54805 $ $Date: 2004-10-14 17:51:13 -0400 (Thu, 14 Oct 2004) $
32  */

33 public class ClassLoaderTest extends TestCase {
34     JettyClassLoader cl;
35     URL JavaDoc[] urls;
36
37     public void setUp() throws MalformedURLException JavaDoc {
38         URL JavaDoc url = new File JavaDoc("src/test-resources/deployables/cltest/").toURL();
39 // URL url = getClass().getClassLoader().getResource("deployables/cltest/");
40
System.err.println("URL: "+url);
41         urls = new URL JavaDoc[]{url};
42     }
43
44     //todo: try more restricted prefixed besides javax.*
45

46     /**
47      * Tries to load a javax.* class that's not available from the
48      * parent ClassLoader. This should work.
49      */

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

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

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

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

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

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

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

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

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

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

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

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