KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > tests > TestCaseClassLoaderTest


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