KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quilt > cl > TestQCLJars


1 /* TestQCLJars.java */
2 package org.quilt.cl;
3
4 import java.io.*;
5 import java.lang.Class JavaDoc;
6 import java.lang.reflect.*;
7 import java.net.*;
8 import junit.framework.*;
9
10 /**
11  * At the moment this is identical to TestQuiltClassLoader except that
12  * test-data.jar is on the classpath and test-data-classes/ is not.
13  *
14  * @author <a HREF="jddixon@users.sourceforge.net">Jim Dixon</a>
15  */

16 public class TestQCLJars extends TestCase {
17
18     private URL [] cp = null;
19
20     private String JavaDoc[] delegating = {
21         // EMPTY -- nothing additional to defaults
22
};
23     private String JavaDoc[] include = {
24         // EMPTY -- nothing being instrumented
25
};
26     private String JavaDoc[] exclude = {
27         "AnonymousClass", "AnonymousClass2Catches",
28         "InnerClass", "NestedTryBlocks",
29         "PrivateClass", "SuperClass",
30         "Wimple"
31     };
32     private QuiltClassLoader qLoader = null;
33
34     public TestQCLJars (String JavaDoc name) {
35         super(name);
36     }
37
38     public void setUp () {
39         // TAKE CARE NO TERMINATING SLASH (/) ON THE JAR
40
File sam1 = new File ("target/test-data.jar");
41         String JavaDoc fullPath1 = sam1.getAbsolutePath();
42
43         File sam2 = new File ("target/classes");
44         String JavaDoc fullPath2 = sam2.getAbsolutePath() + "/";
45         File sam3 = new File ("target/test-classes");
46         String JavaDoc fullPath3 = sam3.getAbsolutePath() + "/";
47         try {
48             // Terminating slash is required. Relative paths don't
49
// work.
50
URL [] samples = {
51                 new URL ( "file://" + fullPath1),
52                 new URL ( "file://" + fullPath2),
53                 new URL ( "file://" + fullPath3)
54             };
55             cp = samples;
56         } catch (MalformedURLException e) {
57             e.printStackTrace();
58             fail ("problem creating class path");
59         }
60         qLoader = new QuiltClassLoader(
61                         cp,
62                         null, // parent
63
delegating, // delegated classes
64
include, // being instrumented
65
exclude); // do NOT instrument
66
}
67     /////////////////////////////////////////////////////////////////
68
// AnonymousClass comes from test-data.jar, as does BasicLoad,
69
// used in testInvokeTestData
70
// //////////////////////////////////////////////////////////////
71
public void testLoader() {
72         Class JavaDoc a1 = null;
73         Class JavaDoc a2 = null;
74         try {
75             a1 = qLoader.loadClass("AnonymousClass");
76         } catch (ClassNotFoundException JavaDoc e) {
77             e.printStackTrace();
78             fail ("Error loading AnonymousClass using loadClass");
79         }
80         try {
81             a2 = qLoader.loadClass("AnonymousClass");
82         } catch (ClassNotFoundException JavaDoc e) {
83             fail ("Error loading AnonymousClass using loadClass");
84         }
85         assertNotNull("qLoader returned null", a1);
86         assertNotNull("qLoader returned null", a2);
87         assertEquals ("second load returned a different class",
88                 (Object JavaDoc)a1, (Object JavaDoc)a2);
89     }
90     
91     public void testInvokeTestData() {
92         Class JavaDoc a1 = null;
93         Class JavaDoc a2 = null;
94         try {
95             a1 = qLoader.loadClass("AnonymousClass");
96         } catch (ClassNotFoundException JavaDoc e) {
97             e.printStackTrace();
98             fail ("Error loading AnonymousClass using loadClass");
99         }
100         try {
101             a2 = qLoader.loadClass("BasicLoad");
102         } catch (ClassNotFoundException JavaDoc e) {
103             fail ("Error loading BasicLoad using loadClass");
104         }
105         RunTest rt = null;
106         // exercise AnonymousClass ////////////////////////
107
try {
108             rt = (RunTest) a1.newInstance();
109         } catch ( InstantiationException JavaDoc e ) {
110             fail ("error instantiating loaded AnonymousClass");
111         } catch ( IllegalAccessException JavaDoc e ) {
112             fail ("error instantiating loaded AnonymousClass");
113         }
114         // AnonymousClass.runTest(x) returns x
115
assertEquals ("AnonymousClass isn't working", 47, rt.runTest(47));
116
117         // exercise BasicLoad ////////////////////////
118
try {
119             rt = (RunTest) a2.newInstance();
120         } catch ( InstantiationException JavaDoc e ) {
121             fail ("error instantiating loaded BasicLoad");
122         } catch ( IllegalAccessException JavaDoc e ) {
123             fail ("error instantiating loaded BasicLoad");
124         }
125         // BasicLoad.runTest(x) returns x*x
126
assertEquals ("BasicLoad isn't working", 49, rt.runTest(7));
127     }
128 }
129
Popular Tags