KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* TestNestedTryBlocks.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  * A stripped-down TestTransformer.
12  *
13  * @author <a HREF="jddixon@users.sourceforge.net">Jim Dixon</a>
14  */

15 public class TestNestedTryBlocks extends TestCase {
16
17     private URL [] cp = null;
18
19     private String JavaDoc[] delegating = {
20         // NOTHING beyond standard defaults
21
};
22     // we want everything to be instrumented
23
private String JavaDoc[] include = {
24         "test.data.",
25         "AnonymousClass", "AnonymousClass2Catches",
26         "BasicLoad",
27         "ExceptionLoad", "ExceptionThrow",
28         "InnerClass", "NestedTryBlocks",
29         "PrivateClass", "SuperClass",
30         "Wimple"
31     };
32     private String JavaDoc[] exclude = {
33         // NOTHING
34
};
35     private QuiltClassLoader qLoader = null;
36
37     private GraphXformer spy;
38     private GraphXformer talker;
39     public TestNestedTryBlocks (String JavaDoc name) {
40         super(name);
41     }
42
43     public void setUp () {
44         File sam1 = new File ("target/test-data-classes/");
45         String JavaDoc fullPath1 = sam1.getAbsolutePath() + "/";
46         File sam2 = new File ("target/classes");
47         String JavaDoc fullPath2 = sam2.getAbsolutePath() + "/";
48         File sam3 = new File ("target/test-classes");
49         String JavaDoc fullPath3 = sam3.getAbsolutePath() + "/";
50         try {
51             // Terminating slash is required. Relative paths don't
52
// work.
53
URL [] samples = {
54                 new URL ( "file://" + fullPath1),
55                 new URL ( "file://" + fullPath2),
56                 new URL ( "file://" + fullPath3)
57             };
58             cp = samples;
59         } catch (MalformedURLException e) {
60             e.printStackTrace();
61             fail ("problem creating class path");
62         }
63         qLoader = new QuiltClassLoader(
64                         cp,
65                         null, // parent
66
delegating, // delegated classes
67
include, // being instrumented
68
exclude); // do NOT instrument
69
spy = new GraphSpy();
70         qLoader.addGraphXformer(spy);
71         talker = new GraphTalker();
72         qLoader.addGraphXformer(talker);
73     }
74
75     private RunTest loadAsRunTest (String JavaDoc name) {
76         Class JavaDoc clazz = null;
77         try {
78             clazz = qLoader.loadClass(name);
79         } catch (ClassNotFoundException JavaDoc e) {
80             e.printStackTrace();
81             fail ("exception loading " + name + " using loadClass");
82         }
83         RunTest rt = null;
84         try {
85             rt = (RunTest) clazz.newInstance();
86         } catch ( InstantiationException JavaDoc e ) {
87             fail ("InstantiationException instantiating loaded class " + name);
88         } catch ( IllegalAccessException JavaDoc e ) {
89             fail ("IllegalAccessException instantiating loaded class " + name);
90         } catch (ClassCastException JavaDoc e) {
91             fail ("ClassCastException instantiating loaded class " + name);
92         }
93         return rt;
94     }
95     public void testNestedTries() {
96         RunTest rt = loadAsRunTest("NestedTryBlocks");
97         assertEquals ("NestedTryBlocks isn't working", 22, rt.runTest(7));
98     }
99
100 }
101
Popular Tags