KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > tests > runner > TestCaseClassLoaderTest


1 package junit.tests.runner;
2
3 import java.lang.reflect.*;
4 import junit.framework.*;
5 import junit.runner.*;
6 import java.net.URL JavaDoc;
7
8 /**
9  * A TestCase for testing the TestCaseClassLoader
10  *
11  */

12 public class TestCaseClassLoaderTest extends TestCase {
13
14     public void testClassLoading() throws Exception JavaDoc {
15         TestCaseClassLoader loader= new TestCaseClassLoader();
16         Class JavaDoc loadedClass= loader.loadClass("junit.tests.runner.ClassLoaderTest", true);
17         Object JavaDoc o= loadedClass.newInstance();
18         //
19
// Invoke the assertClassLoaders method via reflection.
20
// We use reflection since the class is loaded by
21
// another class loader and we can't do a successfull downcast to
22
// ClassLoaderTestCase.
23
//
24
Method method= loadedClass.getDeclaredMethod("verify", new Class JavaDoc[0]);
25         method.invoke(o, new Class JavaDoc[0]);
26     }
27
28     public void testJarClassLoading() throws Exception JavaDoc {
29         URL JavaDoc url= getClass().getResource("test.jar");
30         assertNotNull("Cannot find test.jar", url);
31         String JavaDoc path= url.getFile();
32         TestCaseClassLoader loader= new TestCaseClassLoader(path);
33         Class JavaDoc loadedClass= loader.loadClass("junit.tests.runner.LoadedFromJar", true);
34         Object JavaDoc o= loadedClass.newInstance();
35         //
36
// Invoke the assertClassLoaders method via reflection.
37
// We use reflection since the class is loaded by
38
// another class loader and we can't do a successfull downcast to
39
// ClassLoaderTestCase.
40
//
41
Method method= loadedClass.getDeclaredMethod("verify", new Class JavaDoc[0]);
42         method.invoke(o, new Class JavaDoc[0]);
43     }
44 }
Popular Tags