KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* TestQuiltClassLoader.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  * Tests the basic loadClass/findClass capabilities of the
12  * QuiltClassLoader, without transforming any classes.
13  *
14  * @author <a HREF="jddixon@users.sourceforge.net">Jim Dixon</a>
15  */

16 public class TestQuiltClassLoader extends TestCase {
17
18     private URL [] cp = null;
19
20     // all of these arrays are checked in testConstructor(), so if
21
// you change them here, change the test too
22
private String JavaDoc[] delegating = {
23         "org.quilt"
24     };
25     private String JavaDoc[] include = {
26         // EMPTY -- nothing should be instrumented
27
};
28     private String JavaDoc[] exclude = {
29         "AnonymousClass", "AnonymousClass2Catches",
30         "InnerClass", "NestedTryBlocks",
31         "PrivateClass", "SuperClass",
32         "Wimple"
33     };
34     private QuiltClassLoader qLoader = null;
35
36     public TestQuiltClassLoader (String JavaDoc name) {
37         super(name);
38     }
39
40     public void setUp () {
41         File sam1 = new File ("target/test-data-classes/");
42         File sam2 = new File ("target/classes");
43         File sam3 = new File ("target/test-classes");
44
45 // String fullPath1 = sam1.getAbsolutePath() + "/";
46
// String fullPath2 = sam2.getAbsolutePath() + "/";
47
// String fullPath3 = sam3.getAbsolutePath() + "/";
48
// try {
49
// // Terminating slash is required. Relative paths don't
50
// // work.
51
// URL [] samples = {
52
// new URL ( "file://" + fullPath1),
53
// new URL ( "file://" + fullPath2),
54
// new URL ( "file://" + fullPath3)
55
// };
56
// cp = samples;
57
// } catch (MalformedURLException e) {
58
// e.printStackTrace();
59
// fail ("problem creating class path");
60
// } // GEEP
61

62         qLoader = new QuiltClassLoader(
63                         QuiltClassLoader.cpToURLs(
64                                         sam1 + ":" + sam2 + ":" + sam3),
65                         null, // parent
66
delegating, // delegated classes
67
include, // being instrumented
68
exclude); // do NOT instrument
69
}
70     public void testDomainToFileName() {
71         assertEquals ("../my.jar",
72                 QuiltClassLoader.domainToFileName("../my.jar"));
73         assertEquals ("../../target/my.jar",
74                 QuiltClassLoader.domainToFileName("../../target/my.jar"));
75         assertEquals ("../../../target/my.jar",
76                 QuiltClassLoader.domainToFileName("../../../target/my.jar"));
77         assertEquals ("ab/cd/ef/gh",
78                 QuiltClassLoader.domainToFileName("ab.cd.ef.gh"));
79         assertEquals ("./abc/def",
80                 QuiltClassLoader.domainToFileName("./abc.def"));
81         assertEquals ("./.././abc",
82                 QuiltClassLoader.domainToFileName("./.././abc"));
83         // ignore pathological cases like .abc
84
}
85     // NEEDS ELABORATION
86
public void testConstructor() {
87         assertNotNull("Error creating Quilt class loader", qLoader);
88         URL[] cp2 = qLoader.getClassPath();
89         assertEquals("wrong size classpath", 3, cp2.length);
90
91         String JavaDoc[] del2 = qLoader.getDelegated();
92         assertEquals ("wrong number of delegated classes",
93                                 delegating.length
94                                     + QuiltClassLoader.DELEGATED.length,
95                                 del2.length);
96
97         String JavaDoc[] exc2 = qLoader.getExcluded();
98         assertEquals ("wrong number of excluded classes",
99                                 exclude.length, exc2.length);
100
101         String JavaDoc[] inc2 = qLoader.getIncluded();
102         assertEquals ("wrong number of delegated classes",
103                                 include.length, inc2.length);
104     }
105     public void testLoader() {
106         Class JavaDoc a1 = null;
107         Class JavaDoc a2 = null;
108         try {
109             a1 = qLoader.loadClass("AnonymousClass");
110         } catch (ClassNotFoundException JavaDoc e) {
111             e.printStackTrace();
112             fail ("Error loading AnonymousClass using loadClass");
113         }
114         try {
115             a2 = qLoader.loadClass("AnonymousClass");
116         } catch (ClassNotFoundException JavaDoc e) {
117             fail ("Error loading AnonymousClass using loadClass");
118         }
119         assertNotNull("qLoader returned null", a1);
120         assertNotNull("qLoader returned null", a2);
121         assertEquals ("second load returned a different class",
122                 (Object JavaDoc)a1, (Object JavaDoc)a2);
123     }
124     public void testParentage() {
125         qLoader.setSynthEnabled(true);
126         Class JavaDoc a1 = null;
127         Class JavaDoc a2 = null;
128         try {
129             a1 = qLoader.loadClass("AnonymousClass");
130         } catch (ClassNotFoundException JavaDoc e) {
131             e.printStackTrace();
132             fail ("Error loading AnonymousClass using loadClass");
133         }
134         try {
135             a2 = qLoader.loadClass("test.data.TestDefault");
136         } catch (ClassNotFoundException JavaDoc e) {
137             fail ("Error loading test.data.TestDefault using loadClass");
138         }
139         assertNotNull("qLoader returned null", a1);
140         assertNotNull("qLoader returned null", a2);
141
142         assertEquals ("AnonymousClass has wrong parent",
143                                         qLoader, a1.getClassLoader());
144         assertEquals ("testDefault has wrong parent",
145                                         qLoader, a2.getClassLoader());
146         // exploratory
147
assertEquals ("qLoader's parent is not my parent",
148                                 this.getClass().getClassLoader(),
149                                 qLoader.getClass().getClassLoader() );
150     }
151   
152     public void testInvokeTestData() {
153         Class JavaDoc a1 = null;
154         Class JavaDoc a2 = null;
155         try {
156             a1 = qLoader.loadClass("AnonymousClass");
157         } catch (ClassNotFoundException JavaDoc e) {
158             e.printStackTrace();
159             fail ("Error loading AnonymousClass using loadClass");
160         }
161         try {
162             a2 = qLoader.loadClass("BasicLoad");
163         } catch (ClassNotFoundException JavaDoc e) {
164             fail ("Error loading BasicLoad using loadClass");
165         }
166         RunTest rt = null;
167         // exercise AnonymousClass ////////////////////////
168
try {
169             rt = (RunTest) a1.newInstance();
170         } catch ( InstantiationException JavaDoc e ) {
171             fail ("error instantiating loaded AnonymousClass");
172         } catch ( IllegalAccessException JavaDoc e ) {
173             fail ("error instantiating loaded AnonymousClass");
174         }
175         // AnonymousClass.runTest(x) returns x
176
assertEquals ("AnonymousClass isn't working", 47, rt.runTest(47));
177
178         // exercise BasicLoad ////////////////////////
179
try {
180             rt = (RunTest) a2.newInstance();
181         } catch ( InstantiationException JavaDoc e ) {
182             fail ("error instantiating loaded BasicLoad");
183         } catch ( IllegalAccessException JavaDoc e ) {
184             fail ("error instantiating loaded BasicLoad");
185         }
186         // BasicLoad.runTest(x) returns x*x
187
assertEquals ("BasicLoad isn't working", 49, rt.runTest(7));
188     }
189
190     public void testSynth () {
191         assertEquals ("synthesizing isn't disabled in loader",
192                 false, qLoader.getSynthEnabled() );
193         qLoader.setSynthEnabled(true);
194         assertEquals ("enabling synthesizing failed",
195                 true, qLoader.getSynthEnabled() );
196
197         Class JavaDoc a1 = null;
198         Class JavaDoc a2 = null;
199         try {
200             a1 = qLoader.loadClass("test.data.TestDefault");
201         } catch (ClassNotFoundException JavaDoc e) {
202             e.printStackTrace();
203             fail ("Error loading Default");
204         }
205         try {
206             a2 = qLoader.loadClass("test.data.TestIfThen");
207         } catch (ClassNotFoundException JavaDoc e) {
208             fail ("Error loading testIfThen using loadClass");
209         }
210         RunTest rt = null;
211         // exercise testDefault ///////////////////////////
212
try {
213             rt = (RunTest) a1.newInstance();
214         } catch ( InstantiationException JavaDoc e ) {
215             fail ("error instantiating loaded testDefault");
216         } catch ( IllegalAccessException JavaDoc e ) {
217             fail ("error instantiating loaded testDefault");
218         }
219         // testDefault.runTest(x) returns 2 whatever the input is
220
assertEquals ("testDefault isn't working", 2, rt.runTest(47));
221         assertEquals ("testDefault isn't working", 2, rt.runTest(-7));
222
223         // exercise testIfThen ////////////////////////////
224
try {
225             rt = (RunTest) a2.newInstance();
226         } catch ( InstantiationException JavaDoc e ) {
227             fail ("error instantiating loaded testIfThen");
228         } catch ( IllegalAccessException JavaDoc e ) {
229             fail ("error instantiating loaded testIfThen");
230         }
231         // testIfThen.runTest(x) returns 3 if x > 0, 5 otherwise
232
assertEquals ("testIfThen isn't working", 3, rt.runTest(47));
233         assertEquals ("testIfThen isn't working", 5, rt.runTest(-7));
234
235         Class JavaDoc xNoCatch = null;
236         Class JavaDoc xWithCatch = null;
237         Class JavaDoc xSelect = null;
238         Class JavaDoc xWhile = null;
239         try {
240             xNoCatch = qLoader.loadClass("test.data.TestNPENoCatch");
241             xWithCatch = qLoader.loadClass("test.data.TestNPEWithCatch");
242             xSelect = qLoader.loadClass("test.data.TestSelect");
243             xWhile = qLoader.loadClass("test.data.TestWhile");
244         } catch (ClassNotFoundException JavaDoc e) {
245             e.printStackTrace();
246             fail ("Error loading synthesized class");
247         }
248
249         // exercise testNPEWithCatch //////////////////////
250
try {
251             rt = (RunTest) xWithCatch.newInstance();
252         } catch ( InstantiationException JavaDoc e ) {
253             fail ("error instantiating loaded testNPEWithCatch");
254         } catch ( IllegalAccessException JavaDoc e ) {
255             fail ("error instantiating loaded testNPEWithCatch");
256         }
257         // testNPEWithCatch.runTest(x) always returns 3
258
assertEquals ("testNPEWithCatch isn't working", 3, rt.runTest(47));
259         assertEquals ("testNPEWithCatch isn't working", 3, rt.runTest(-7));
260
261         // exercise testNPENoCatch ////////////////////////
262
try {
263             rt = (RunTest) xNoCatch.newInstance();
264         } catch ( InstantiationException JavaDoc e ) {
265             fail ("error instantiating loaded testNPENoCatch");
266         } catch ( IllegalAccessException JavaDoc e ) {
267             fail ("error instantiating loaded testNPENoCatch");
268         }
269         // testNPENoCatch.runTest(x) always throws a NullPointerException
270
int x;
271         try {
272             x = rt.runTest(47);
273             fail ("testNPENoCatch didn't throw exception");
274         } catch (NullPointerException JavaDoc e) {
275             ; // ignore it
276
}
277         try {
278             x = rt.runTest(-7);
279             fail ("testNPENoCatch didn't throw exception");
280         } catch (NullPointerException JavaDoc e) {
281             ; // ignore it
282
}
283
284         // exercise testSelect ////////////////////////////
285
try {
286             rt = (RunTest) xSelect.newInstance();
287         } catch ( InstantiationException JavaDoc e ) {
288             fail ("error instantiating loaded testSelect");
289         } catch ( IllegalAccessException JavaDoc e ) {
290             fail ("error instantiating loaded testSelect");
291         }
292         // testSelect.runTest(x) returns
293
// 1 if x == 1; 3 if x == 2; 5 if x == 3; 2 otherwise
294
assertEquals ("testSelect isn't working", 2, rt.runTest(47));
295         assertEquals ("testSelect isn't working", 2, rt.runTest(-7));
296         assertEquals ("testSelect isn't working", 1, rt.runTest(1));
297         assertEquals ("testSelect isn't working", 3, rt.runTest(2));
298         assertEquals ("testSelect isn't working", 5, rt.runTest(3));
299
300         // exercise testWhile /////////////////////////////
301
try {
302             rt = (RunTest) xWhile.newInstance();
303         } catch ( InstantiationException JavaDoc e ) {
304             fail ("error instantiating loaded testWhile");
305         } catch ( IllegalAccessException JavaDoc e ) {
306             fail ("error instantiating loaded testWhile");
307         }
308         // testWhile.runTest(x) returns
309
// 0 if x >= 0, x otherwise
310
assertEquals ("testWhile isn't working", 0, rt.runTest(47));
311         assertEquals ("testWhile isn't working",-7, rt.runTest(-7));
312
313     }
314     public void testParentage2() {
315         qLoader.setSynthEnabled(true);
316     }
317     public void testSetters() {
318         qLoader.setClassPath("abc.jar:def:ghi.a.b.c:froggy.pickle.jar");
319         URL[] urls = qLoader.getClassPath();
320
321         assertNotNull("classpath is null", urls);
322         assertEquals ("wrong number of URLs in classpath", 4, urls.length);
323         assertTrue(urls[0].getFile().endsWith("abc.jar"));
324         assertTrue(urls[1].getPath().endsWith("def/"));
325         assertTrue(urls[2].getPath().endsWith("ghi/a/b/c/"));
326         // path and file seem to be identical
327
assertTrue(urls[3].getPath().endsWith("froggy/pickle.jar"));
328         assertTrue(urls[3].getFile().endsWith("froggy/pickle.jar"));
329                 
330         qLoader.setExcluded("this,that,theOther");
331         String JavaDoc[] exc = qLoader.getExcluded();
332         assertEquals("wrong excluded count", 3, exc.length);
333         assertEquals("this", exc[0]);
334         assertEquals("theOther", exc[2]);
335
336         qLoader.setIncluded("eenie,meenie,minee,mo");
337         String JavaDoc[] inc = qLoader.getIncluded();
338         assertEquals("wrong included count", 4, inc.length);
339         assertEquals("eenie", inc[0]);
340         assertEquals("mo", inc[3]);
341     }
342 }
343
Popular Tags