1 package org.codehaus.classworlds;
2
3 /*
4 $Id: ClassRealmImplTest.java,v 1.4 2003/09/23 18:11:30 jvanzyl Exp $
5
6 Copyright 2002 (C) The Werken Company. All Rights Reserved.
7
8 Redistribution and use of this software and associated documentation
9 ("Software"), with or without modification, are permitted provided
10 that the following conditions are met:
11
12 1. Redistributions of source code must retain copyright
13 statements and notices. Redistributions must also contain a
14 copy of this document.
15
16 2. Redistributions in binary form must reproduce the
17 above copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
20
21 3. The name "classworlds" must not be used to endorse or promote
22 products derived from this Software without prior written
23 permission of The Werken Company. For written permission,
24 please contact bob@werken.com.
25
26 4. Products derived from this Software may not be called "classworlds"
27 nor may "classworlds" appear in their names without prior written
28 permission of The Werken Company. "classworlds" is a registered
29 trademark of The Werken Company.
30
31 5. Due credit should be given to The Werken Company.
32 (http://classworlds.werken.com/).
33
34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */
48
49 import junit.framework.TestCase;
50
51 import java.net.URL;
52 import java.net.MalformedURLException;
53
54 public class ClassRealmImplTest extends TestCase
55 {
56 private ClassWorld world;
57
58 public ClassRealmImplTest(String name)
59 {
60 super( name );
61 }
62
63 public void setUp()
64 {
65 this.world = new ClassWorld();
66 }
67
68 public void tearDown()
69 {
70 this.world = null;
71 }
72
73 public void testNewRealm() throws Exception
74 {
75 ClassRealm realm = this.world.newRealm( "foo" );
76
77 assertNotNull( realm );
78
79 assertSame( this.world,
80 realm.getWorld() );
81
82 assertEquals( "foo",
83 realm.getId() );
84 }
85
86 public void testLocateSourceRealm_NoImports() throws Exception
87 {
88 DefaultClassRealm realm = new DefaultClassRealm( this.world,
89 "foo" );
90
91 assertSame( realm,
92 realm.locateSourceRealm( "com.werken.Stuff" ) );
93 }
94
95 public void testLocateSourceRealm_SimpleImport() throws Exception
96 {
97 DefaultClassRealm mainRealm = (DefaultClassRealm) this.world.newRealm( "main" );
98
99 ClassRealm werkflowRealm = this.world.newRealm( "werkflow" );
100
101 mainRealm.importFrom( "werkflow",
102 "com.werken.werkflow" );
103
104 assertSame( werkflowRealm,
105 mainRealm.locateSourceRealm( "com.werken.werkflow.WerkflowEngine" ) );
106
107 assertSame( werkflowRealm,
108 mainRealm.locateSourceRealm( "com.werken.werkflow.process.ProcessManager" ) );
109
110 assertSame( mainRealm,
111 mainRealm.locateSourceRealm( "com.werken.blissed.Process" ) );
112
113 assertSame( mainRealm,
114 mainRealm.locateSourceRealm( "java.lang.Object" ) );
115
116 assertSame( mainRealm,
117 mainRealm.locateSourceRealm( "NoviceProgrammerClass" ) );
118 }
119
120 public void testLocateSourceRealm_MultipleImport() throws Exception
121 {
122 DefaultClassRealm mainRealm = (DefaultClassRealm) this.world.newRealm( "main" );
123
124 ClassRealm werkflowRealm = this.world.newRealm( "werkflow" );
125
126 ClassRealm blissedRealm = this.world.newRealm( "blissed" );
127
128 mainRealm.importFrom( "werkflow",
129 "com.werken.werkflow" );
130
131 mainRealm.importFrom( "blissed",
132 "com.werken.blissed" );
133
134 assertSame( werkflowRealm,
135 mainRealm.locateSourceRealm( "com.werken.werkflow.WerkflowEngine" ) );
136
137 assertSame( werkflowRealm,
138 mainRealm.locateSourceRealm( "com.werken.werkflow.process.ProcessManager" ) );
139
140 assertSame( blissedRealm,
141 mainRealm.locateSourceRealm( "com.werken.blissed.Process" ) );
142
143 assertSame( blissedRealm,
144 mainRealm.locateSourceRealm( "com.werken.blissed.guard.BooleanGuard" ) );
145
146 assertSame( mainRealm,
147 mainRealm.locateSourceRealm( "java.lang.Object" ) );
148
149 assertSame( mainRealm,
150 mainRealm.locateSourceRealm( "NoviceProgrammerClass" ) );
151 }
152
153 public void testLocateSourceRealm_Hierachy() throws Exception
154 {
155 DefaultClassRealm mainRealm = (DefaultClassRealm) this.world.newRealm( "main" );
156
157 ClassRealm fooRealm = this.world.newRealm( "foo" );
158 ClassRealm fooBarRealm = this.world.newRealm( "fooBar" );
159 ClassRealm fooBarBazRealm = this.world.newRealm( "fooBarBaz" );
160
161 mainRealm.importFrom( "foo",
162 "foo" );
163
164 mainRealm.importFrom( "fooBar",
165 "foo.bar" );
166
167 mainRealm.importFrom( "fooBarBaz",
168 "foo.bar.baz" );
169
170 assertSame( fooRealm,
171 mainRealm.locateSourceRealm( "foo.Goober" ) );
172
173 assertSame( fooRealm,
174 mainRealm.locateSourceRealm( "foo.cheese.Goober" ) );
175
176 assertSame( fooBarRealm,
177 mainRealm.locateSourceRealm( "foo.bar.Goober" ) );
178
179 assertSame( fooBarRealm,
180 mainRealm.locateSourceRealm( "foo.bar.cheese.Goober" ) );
181
182 assertSame( fooBarBazRealm,
183 mainRealm.locateSourceRealm( "foo.bar.baz.Goober" ) );
184
185 assertSame( fooBarBazRealm,
186 mainRealm.locateSourceRealm( "foo.bar.baz.cheese.Goober" ) );
187
188 assertSame( mainRealm,
189 mainRealm.locateSourceRealm( "java.lang.Object" ) );
190
191 assertSame( mainRealm,
192 mainRealm.locateSourceRealm( "NoviceProgrammerClass" ) );
193 }
194
195 public void testLocateSourceRealm_Hierachy_Reverse() throws Exception
196 {
197 ClassRealm fooBarBazRealm = this.world.newRealm( "fooBarBaz" );
198 ClassRealm fooBarRealm = this.world.newRealm( "fooBar" );
199 ClassRealm fooRealm = this.world.newRealm( "foo" );
200
201 DefaultClassRealm mainRealm = (DefaultClassRealm) this.world.newRealm( "main" );
202
203 mainRealm.importFrom( "fooBarBaz",
204 "foo.bar.baz" );
205
206 mainRealm.importFrom( "fooBar",
207 "foo.bar" );
208
209 mainRealm.importFrom( "foo",
210 "foo" );
211
212 assertSame( fooRealm,
213 mainRealm.locateSourceRealm( "foo.Goober" ) );
214
215 assertSame( fooRealm,
216 mainRealm.locateSourceRealm( "foo.cheese.Goober" ) );
217
218 assertSame( fooBarRealm,
219 mainRealm.locateSourceRealm( "foo.bar.Goober" ) );
220
221 assertSame( fooBarRealm,
222 mainRealm.locateSourceRealm( "foo.bar.cheese.Goober" ) );
223
224 assertSame( fooBarBazRealm,
225 mainRealm.locateSourceRealm( "foo.bar.baz.Goober" ) );
226
227 assertSame( fooBarBazRealm,
228 mainRealm.locateSourceRealm( "foo.bar.baz.cheese.Goober" ) );
229
230 assertSame( mainRealm,
231 mainRealm.locateSourceRealm( "java.lang.Object" ) );
232
233 assertSame( mainRealm,
234 mainRealm.locateSourceRealm( "NoviceProgrammerClass" ) );
235 }
236
237 public void testLoadClass_SystemClass() throws Exception
238 {
239 ClassRealm mainRealm = this.world.newRealm( "main" );
240
241 Class cls = mainRealm.loadClass( "java.lang.Object" );
242
243 assertNotNull( cls );
244 }
245
246 public void testLoadClass_NonSystemClass() throws Exception
247 {
248 ClassRealm mainRealm = this.world.newRealm( "main" );
249
250 try
251 {
252 mainRealm.loadClass( "com.werken.projectz.UberThing" );
253
254 fail( "throw ClassNotFoundException" );
255 }
256 catch (ClassNotFoundException e)
257 {
258 // expected and correct
259 }
260 }
261
262 public void testLoadClass_ClassWorldsClass() throws Exception
263 {
264 ClassRealm mainRealm = this.world.newRealm( "main" );
265
266 Class cls = mainRealm.loadClass( "org.codehaus.classworlds.ClassWorld" );
267
268 assertNotNull( cls );
269
270 assertSame( ClassWorld.class,
271 cls );
272 }
273
274 public void testLoadClass_Local() throws Exception
275 {
276 ClassRealm mainRealm = this.world.newRealm( "main" );
277
278 try
279 {
280 mainRealm.loadClass( "a.A" );
281 }
282 catch (ClassNotFoundException e)
283 {
284 // expected and correct
285 }
286
287 mainRealm.addConstituent( getJarUrl( "a.jar" ) );
288
289 Class classA = mainRealm.loadClass( "a.A" );
290
291 assertNotNull( classA );
292
293 ClassRealm otherRealm = this.world.newRealm( "other" );
294
295 try
296 {
297 otherRealm.loadClass( "a.A" );
298 }
299 catch (ClassNotFoundException e)
300 {
301 // expected and correct
302 }
303 }
304
305 public void testLoadClass_Imported() throws Exception
306 {
307 ClassRealm mainRealm = this.world.newRealm( "main" );
308 ClassRealm realmA = this.world.newRealm( "realmA" );
309
310 try
311 {
312 realmA.loadClass( "a.A" );
313 fail("realmA.loadClass(a.A) should have thrown a ClassNotFoundException");
314 }
315 catch (ClassNotFoundException e)
316 {
317 // expected and correct
318 }
319
320 realmA.addConstituent( getJarUrl( "a.jar" ) );
321
322 try
323 {
324 mainRealm.loadClass( "a.A" );
325 fail("mainRealm.loadClass(a.A) should have thrown a ClassNotFoundException");
326 }
327 catch (ClassNotFoundException e)
328 {
329 // expected and correct
330 }
331
332 mainRealm.importFrom( "realmA",
333 "a" );
334
335 Class classA = realmA.loadClass( "a.A" );
336
337 assertNotNull( classA );
338
339 assertEquals( realmA.getClassLoader(),
340 classA.getClassLoader() );
341
342 Class classMain = mainRealm.loadClass( "a.A" );
343
344 assertNotNull( classMain );
345
346 assertEquals( realmA.getClassLoader(),
347 classMain.getClassLoader() );
348
349 assertSame( classA,
350 classMain );
351 }
352
353 public void testLoadClass_Complex() throws Exception
354 {
355 ClassRealm realmA = this.world.newRealm( "realmA" );
356 ClassRealm realmB = this.world.newRealm( "realmB" );
357 ClassRealm realmC = this.world.newRealm( "realmC" );
358
359 realmA.addConstituent( getJarUrl( "a.jar" ) );
360 realmB.addConstituent( getJarUrl( "b.jar" ) );
361 realmC.addConstituent( getJarUrl( "c.jar" ) );
362
363 realmC.importFrom( "realmA",
364 "a" );
365
366 realmC.importFrom( "realmB",
367 "b" );
368
369 realmA.importFrom( "realmC",
370 "c" );
371
372 Class classA_A = realmA.loadClass( "a.A" );
373 Class classB_B = realmB.loadClass( "b.B" );
374 Class classC_C = realmC.loadClass( "c.C" );
375
376 assertNotNull( classA_A );
377 assertNotNull( classB_B );
378 assertNotNull( classC_C );
379
380 assertEquals( realmA.getClassLoader(),
381 classA_A.getClassLoader() );
382
383 assertEquals( realmB.getClassLoader(),
384 classB_B.getClassLoader() );
385
386 assertEquals( realmC.getClassLoader(),
387 classC_C.getClassLoader() );
388
389 // load from C
390
391 Class classA_C = realmC.loadClass( "a.A" );
392
393 assertNotNull( classA_C );
394
395 assertSame( classA_A,
396 classA_C );
397
398 assertEquals( realmA.getClassLoader(),
399 classA_C.getClassLoader() );
400
401 Class classB_C = realmC.loadClass( "b.B" );
402
403 assertNotNull( classB_C );
404
405 assertSame( classB_B,
406 classB_C );
407
408 assertEquals( realmB.getClassLoader(),
409 classB_C.getClassLoader() );
410
411 // load from A
412
413 Class classC_A = realmA.loadClass( "c.C" );
414
415 assertNotNull( classC_A );
416
417 assertSame( classC_C,
418 classC_A );
419
420 assertEquals( realmC.getClassLoader(),
421 classC_A.getClassLoader() );
422
423 try
424 {
425 realmA.loadClass( "b.B" );
426 fail( "throw ClassNotFoundException" );
427 }
428 catch (ClassNotFoundException e)
429 {
430 // expected and correct
431 }
432
433 // load from B
434
435 try
436 {
437 realmB.loadClass( "a.A" );
438 fail( "throw ClassNotFoundException" );
439 }
440 catch (ClassNotFoundException e)
441 {
442 // expected and correct
443 }
444
445 try
446 {
447 realmB.loadClass( "c.C" );
448 fail( "throw ClassNotFoundException" );
449 }
450 catch (ClassNotFoundException e)
451 {
452 // expected and correct
453 }
454 }
455
456 protected URL getJarUrl(String jarName) throws MalformedURLException
457 {
458 return TestUtil.getTestResourceUrl(jarName);
459 }
460
461
462 public void testLoadClass_ClassWorldsClassRepeatedly() throws Exception
463 {
464 ClassRealm mainRealm = this.world.newRealm( "main" );
465
466 for (int i = 0; i < 100; i++) {
467 Class cls = mainRealm.loadClass( "org.codehaus.classworlds.ClassWorld" );
468
469 assertNotNull( cls );
470
471 assertSame( ClassWorld.class,
472 cls );
473 }
474 }
475 }
This page was automatically generated by Maven