001: /* TestQuiltClassLoader.java */
002: package org.quilt.cl;
003:
004: import java.io.*;
005: import java.lang.Class;
006: import java.lang.reflect.*;
007: import java.net.*;
008: import junit.framework.*;
009:
010: /**
011: * Tests the basic loadClass/findClass capabilities of the
012: * QuiltClassLoader, without transforming any classes.
013: *
014: * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
015: */
016: public class TestQuiltClassLoader extends TestCase {
017:
018: private URL[] cp = null;
019:
020: // all of these arrays are checked in testConstructor(), so if
021: // you change them here, change the test too
022: private String[] delegating = { "org.quilt" };
023: private String[] include = {
024: // EMPTY -- nothing should be instrumented
025: };
026: private String[] exclude = { "AnonymousClass",
027: "AnonymousClass2Catches", "InnerClass", "NestedTryBlocks",
028: "PrivateClass", "SuperClass", "Wimple" };
029: private QuiltClassLoader qLoader = null;
030:
031: public TestQuiltClassLoader(String name) {
032: super (name);
033: }
034:
035: public void setUp() {
036: File sam1 = new File("target/test-data-classes/");
037: File sam2 = new File("target/classes");
038: File sam3 = new File("target/test-classes");
039:
040: // String fullPath1 = sam1.getAbsolutePath() + "/";
041: // String fullPath2 = sam2.getAbsolutePath() + "/";
042: // String fullPath3 = sam3.getAbsolutePath() + "/";
043: // try {
044: // // Terminating slash is required. Relative paths don't
045: // // work.
046: // URL [] samples = {
047: // new URL ( "file://" + fullPath1),
048: // new URL ( "file://" + fullPath2),
049: // new URL ( "file://" + fullPath3)
050: // };
051: // cp = samples;
052: // } catch (MalformedURLException e) {
053: // e.printStackTrace();
054: // fail ("problem creating class path");
055: // } // GEEP
056:
057: qLoader = new QuiltClassLoader(QuiltClassLoader.cpToURLs(sam1
058: + ":" + sam2 + ":" + sam3), null, // parent
059: delegating, // delegated classes
060: include, // being instrumented
061: exclude); // do NOT instrument
062: }
063:
064: public void testDomainToFileName() {
065: assertEquals("../my.jar", QuiltClassLoader
066: .domainToFileName("../my.jar"));
067: assertEquals("../../target/my.jar", QuiltClassLoader
068: .domainToFileName("../../target/my.jar"));
069: assertEquals("../../../target/my.jar", QuiltClassLoader
070: .domainToFileName("../../../target/my.jar"));
071: assertEquals("ab/cd/ef/gh", QuiltClassLoader
072: .domainToFileName("ab.cd.ef.gh"));
073: assertEquals("./abc/def", QuiltClassLoader
074: .domainToFileName("./abc.def"));
075: assertEquals("./.././abc", QuiltClassLoader
076: .domainToFileName("./.././abc"));
077: // ignore pathological cases like .abc
078: }
079:
080: // NEEDS ELABORATION
081: public void testConstructor() {
082: assertNotNull("Error creating Quilt class loader", qLoader);
083: URL[] cp2 = qLoader.getClassPath();
084: assertEquals("wrong size classpath", 3, cp2.length);
085:
086: String[] del2 = qLoader.getDelegated();
087: assertEquals("wrong number of delegated classes",
088: delegating.length + QuiltClassLoader.DELEGATED.length,
089: del2.length);
090:
091: String[] exc2 = qLoader.getExcluded();
092: assertEquals("wrong number of excluded classes",
093: exclude.length, exc2.length);
094:
095: String[] inc2 = qLoader.getIncluded();
096: assertEquals("wrong number of delegated classes",
097: include.length, inc2.length);
098: }
099:
100: public void testLoader() {
101: Class a1 = null;
102: Class a2 = null;
103: try {
104: a1 = qLoader.loadClass("AnonymousClass");
105: } catch (ClassNotFoundException e) {
106: e.printStackTrace();
107: fail("Error loading AnonymousClass using loadClass");
108: }
109: try {
110: a2 = qLoader.loadClass("AnonymousClass");
111: } catch (ClassNotFoundException e) {
112: fail("Error loading AnonymousClass using loadClass");
113: }
114: assertNotNull("qLoader returned null", a1);
115: assertNotNull("qLoader returned null", a2);
116: assertEquals("second load returned a different class",
117: (Object) a1, (Object) a2);
118: }
119:
120: public void testParentage() {
121: qLoader.setSynthEnabled(true);
122: Class a1 = null;
123: Class a2 = null;
124: try {
125: a1 = qLoader.loadClass("AnonymousClass");
126: } catch (ClassNotFoundException e) {
127: e.printStackTrace();
128: fail("Error loading AnonymousClass using loadClass");
129: }
130: try {
131: a2 = qLoader.loadClass("test.data.TestDefault");
132: } catch (ClassNotFoundException e) {
133: fail("Error loading test.data.TestDefault using loadClass");
134: }
135: assertNotNull("qLoader returned null", a1);
136: assertNotNull("qLoader returned null", a2);
137:
138: assertEquals("AnonymousClass has wrong parent", qLoader, a1
139: .getClassLoader());
140: assertEquals("testDefault has wrong parent", qLoader, a2
141: .getClassLoader());
142: // exploratory
143: assertEquals("qLoader's parent is not my parent", this
144: .getClass().getClassLoader(), qLoader.getClass()
145: .getClassLoader());
146: }
147:
148: public void testInvokeTestData() {
149: Class a1 = null;
150: Class a2 = null;
151: try {
152: a1 = qLoader.loadClass("AnonymousClass");
153: } catch (ClassNotFoundException e) {
154: e.printStackTrace();
155: fail("Error loading AnonymousClass using loadClass");
156: }
157: try {
158: a2 = qLoader.loadClass("BasicLoad");
159: } catch (ClassNotFoundException e) {
160: fail("Error loading BasicLoad using loadClass");
161: }
162: RunTest rt = null;
163: // exercise AnonymousClass ////////////////////////
164: try {
165: rt = (RunTest) a1.newInstance();
166: } catch (InstantiationException e) {
167: fail("error instantiating loaded AnonymousClass");
168: } catch (IllegalAccessException e) {
169: fail("error instantiating loaded AnonymousClass");
170: }
171: // AnonymousClass.runTest(x) returns x
172: assertEquals("AnonymousClass isn't working", 47, rt.runTest(47));
173:
174: // exercise BasicLoad ////////////////////////
175: try {
176: rt = (RunTest) a2.newInstance();
177: } catch (InstantiationException e) {
178: fail("error instantiating loaded BasicLoad");
179: } catch (IllegalAccessException e) {
180: fail("error instantiating loaded BasicLoad");
181: }
182: // BasicLoad.runTest(x) returns x*x
183: assertEquals("BasicLoad isn't working", 49, rt.runTest(7));
184: }
185:
186: public void testSynth() {
187: assertEquals("synthesizing isn't disabled in loader", false,
188: qLoader.getSynthEnabled());
189: qLoader.setSynthEnabled(true);
190: assertEquals("enabling synthesizing failed", true, qLoader
191: .getSynthEnabled());
192:
193: Class a1 = null;
194: Class a2 = null;
195: try {
196: a1 = qLoader.loadClass("test.data.TestDefault");
197: } catch (ClassNotFoundException e) {
198: e.printStackTrace();
199: fail("Error loading Default");
200: }
201: try {
202: a2 = qLoader.loadClass("test.data.TestIfThen");
203: } catch (ClassNotFoundException e) {
204: fail("Error loading testIfThen using loadClass");
205: }
206: RunTest rt = null;
207: // exercise testDefault ///////////////////////////
208: try {
209: rt = (RunTest) a1.newInstance();
210: } catch (InstantiationException e) {
211: fail("error instantiating loaded testDefault");
212: } catch (IllegalAccessException e) {
213: fail("error instantiating loaded testDefault");
214: }
215: // testDefault.runTest(x) returns 2 whatever the input is
216: assertEquals("testDefault isn't working", 2, rt.runTest(47));
217: assertEquals("testDefault isn't working", 2, rt.runTest(-7));
218:
219: // exercise testIfThen ////////////////////////////
220: try {
221: rt = (RunTest) a2.newInstance();
222: } catch (InstantiationException e) {
223: fail("error instantiating loaded testIfThen");
224: } catch (IllegalAccessException e) {
225: fail("error instantiating loaded testIfThen");
226: }
227: // testIfThen.runTest(x) returns 3 if x > 0, 5 otherwise
228: assertEquals("testIfThen isn't working", 3, rt.runTest(47));
229: assertEquals("testIfThen isn't working", 5, rt.runTest(-7));
230:
231: Class xNoCatch = null;
232: Class xWithCatch = null;
233: Class xSelect = null;
234: Class xWhile = null;
235: try {
236: xNoCatch = qLoader.loadClass("test.data.TestNPENoCatch");
237: xWithCatch = qLoader
238: .loadClass("test.data.TestNPEWithCatch");
239: xSelect = qLoader.loadClass("test.data.TestSelect");
240: xWhile = qLoader.loadClass("test.data.TestWhile");
241: } catch (ClassNotFoundException e) {
242: e.printStackTrace();
243: fail("Error loading synthesized class");
244: }
245:
246: // exercise testNPEWithCatch //////////////////////
247: try {
248: rt = (RunTest) xWithCatch.newInstance();
249: } catch (InstantiationException e) {
250: fail("error instantiating loaded testNPEWithCatch");
251: } catch (IllegalAccessException e) {
252: fail("error instantiating loaded testNPEWithCatch");
253: }
254: // testNPEWithCatch.runTest(x) always returns 3
255: assertEquals("testNPEWithCatch isn't working", 3, rt
256: .runTest(47));
257: assertEquals("testNPEWithCatch isn't working", 3, rt
258: .runTest(-7));
259:
260: // exercise testNPENoCatch ////////////////////////
261: try {
262: rt = (RunTest) xNoCatch.newInstance();
263: } catch (InstantiationException e) {
264: fail("error instantiating loaded testNPENoCatch");
265: } catch (IllegalAccessException e) {
266: fail("error instantiating loaded testNPENoCatch");
267: }
268: // testNPENoCatch.runTest(x) always throws a NullPointerException
269: int x;
270: try {
271: x = rt.runTest(47);
272: fail("testNPENoCatch didn't throw exception");
273: } catch (NullPointerException e) {
274: ; // ignore it
275: }
276: try {
277: x = rt.runTest(-7);
278: fail("testNPENoCatch didn't throw exception");
279: } catch (NullPointerException e) {
280: ; // ignore it
281: }
282:
283: // exercise testSelect ////////////////////////////
284: try {
285: rt = (RunTest) xSelect.newInstance();
286: } catch (InstantiationException e) {
287: fail("error instantiating loaded testSelect");
288: } catch (IllegalAccessException e) {
289: fail("error instantiating loaded testSelect");
290: }
291: // testSelect.runTest(x) returns
292: // 1 if x == 1; 3 if x == 2; 5 if x == 3; 2 otherwise
293: assertEquals("testSelect isn't working", 2, rt.runTest(47));
294: assertEquals("testSelect isn't working", 2, rt.runTest(-7));
295: assertEquals("testSelect isn't working", 1, rt.runTest(1));
296: assertEquals("testSelect isn't working", 3, rt.runTest(2));
297: assertEquals("testSelect isn't working", 5, rt.runTest(3));
298:
299: // exercise testWhile /////////////////////////////
300: try {
301: rt = (RunTest) xWhile.newInstance();
302: } catch (InstantiationException e) {
303: fail("error instantiating loaded testWhile");
304: } catch (IllegalAccessException e) {
305: fail("error instantiating loaded testWhile");
306: }
307: // testWhile.runTest(x) returns
308: // 0 if x >= 0, x otherwise
309: assertEquals("testWhile isn't working", 0, rt.runTest(47));
310: assertEquals("testWhile isn't working", -7, rt.runTest(-7));
311:
312: }
313:
314: public void testParentage2() {
315: qLoader.setSynthEnabled(true);
316: }
317:
318: public void testSetters() {
319: qLoader.setClassPath("abc.jar:def:ghi.a.b.c:froggy.pickle.jar");
320: URL[] urls = qLoader.getClassPath();
321:
322: assertNotNull("classpath is null", urls);
323: assertEquals("wrong number of URLs in classpath", 4,
324: urls.length);
325: assertTrue(urls[0].getFile().endsWith("abc.jar"));
326: assertTrue(urls[1].getPath().endsWith("def/"));
327: assertTrue(urls[2].getPath().endsWith("ghi/a/b/c/"));
328: // path and file seem to be identical
329: assertTrue(urls[3].getPath().endsWith("froggy/pickle.jar"));
330: assertTrue(urls[3].getFile().endsWith("froggy/pickle.jar"));
331:
332: qLoader.setExcluded("this,that,theOther");
333: String[] exc = qLoader.getExcluded();
334: assertEquals("wrong excluded count", 3, exc.length);
335: assertEquals("this", exc[0]);
336: assertEquals("theOther", exc[2]);
337:
338: qLoader.setIncluded("eenie,meenie,minee,mo");
339: String[] inc = qLoader.getIncluded();
340: assertEquals("wrong included count", 4, inc.length);
341: assertEquals("eenie", inc[0]);
342: assertEquals("mo", inc[3]);
343: }
344: }
|