001: /* TestTransformer.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: * A somewhat prettified TestQuiltClassLoader with transforming
012: * enabled.
013: *
014: * XXX KNOWN PROBLEMS: NestedTryBlocks and SuperClass generate
015: * control flow graphs with serious errors.
016: *
017: * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
018: */
019: public class TestTransformer extends TestCase {
020:
021: private URL[] cp = null;
022:
023: private String[] delegating = {
024: // NOTHING beyond standard defaults
025: };
026: // we want everything to be instrumented
027: private String[] include = { "test.data.", "AnonymousClass",
028: "AnonymousClass2Catches", "BasicLoad", "ExceptionLoad",
029: "ExceptionThrow", "InnerClass", "NestedTryBlocks",
030: "PrivateClass", "SuperClass", "Wimple" };
031: private String[] exclude = {
032: // NOTHING
033: };
034:
035: private GraphXformer spy;
036: private GraphXformer talker;
037:
038: private QuiltClassLoader qLoader = null;
039:
040: public TestTransformer(String name) {
041: super (name);
042: }
043:
044: public void setUp() {
045: File sam1 = new File("target/test-data-classes/");
046: String fullPath1 = sam1.getAbsolutePath() + "/";
047: File sam2 = new File("target/classes");
048: String fullPath2 = sam2.getAbsolutePath() + "/";
049: File sam3 = new File("target/test-classes");
050: String fullPath3 = sam3.getAbsolutePath() + "/";
051: try {
052: // Terminating slash is required. Relative paths don't
053: // work.
054: URL[] samples = { new URL("file://" + fullPath1),
055: new URL("file://" + fullPath2),
056: new URL("file://" + fullPath3) };
057: cp = samples;
058: } catch (MalformedURLException e) {
059: e.printStackTrace();
060: fail("problem creating class path");
061: }
062: qLoader = new QuiltClassLoader(cp, null, // parent
063: delegating, // delegated classes
064: include, // being instrumented
065: exclude); // do NOT instrument
066:
067: // this is sufficient to force graph generation
068: spy = new GraphSpy();
069: qLoader.addGraphXformer(spy);
070: // this makes for a very verbose output
071: // talker = new GraphTalker();
072: // qLoader.addGraphXformer(talker);
073: }
074:
075: public void testLoader() {
076: Class a1 = null;
077: try {
078: a1 = qLoader.loadClass("AnonymousClass");
079: } catch (ClassNotFoundException e) {
080: e.printStackTrace();
081: fail("Error loading AnonymousClass using loadClass");
082: }
083: assertNotNull("qLoader returned null", a1);
084: }
085:
086: private RunTest loadAsRunTest(String name) {
087: Class clazz = null;
088: try {
089: clazz = qLoader.loadClass(name);
090: } catch (ClassNotFoundException e) {
091: e.printStackTrace();
092: fail("exception loading " + name + " using loadClass");
093: }
094: RunTest rt = null;
095: try {
096: rt = (RunTest) clazz.newInstance();
097: } catch (InstantiationException e) {
098: fail("InstantiationException instantiating loaded class "
099: + name);
100: } catch (IllegalAccessException e) {
101: fail("IllegalAccessException instantiating loaded class "
102: + name);
103: } catch (ClassCastException e) {
104: fail("ClassCastException instantiating loaded class "
105: + name);
106: }
107: return rt;
108: }
109:
110: public void testInvokeTestData() {
111: RunTest rt = loadAsRunTest("AnonymousClass");
112: // AnonymousClass.runTest(x) returns x
113: assertEquals("AnonymousClass isn't working", 47, rt.runTest(47));
114:
115: rt = loadAsRunTest("BasicLoad");
116: // BasicLoad.runTest(x) returns x*x
117: assertEquals("BasicLoad isn't working", 49, rt.runTest(7));
118:
119: // XXX causes ClassCastException when instantiating
120: rt = loadAsRunTest("ExceptionLoad");
121: // ExceptionLoad.runTest(x) also returns x*x
122: assertEquals("ExceptionLoad isn't working", 121, rt.runTest(11));
123: }
124:
125: public void testInvokeTestData2() {
126: RunTest rt = loadAsRunTest("Finally");
127: // Finally.runTest(x) returns -1
128: assertEquals("Finally isn't working", -1, rt.runTest(11));
129: assertEquals("Finally isn't working", -1, rt.runTest(1));
130:
131: rt = loadAsRunTest("Finally2Catches");
132: // what Finally.runTest(x) returns is a bit complicated ...
133: assertEquals("Finally2Catches isn't working", 3600, rt
134: .runTest(11));
135:
136: rt = loadAsRunTest("InnerClass");
137: // InnerClass.runTest(x) also returns x*x
138: assertEquals("InnerClass isn't working", 9, rt.runTest(3));
139:
140: rt = loadAsRunTest("NestedTryBlocks");
141: assertEquals("NestedTryBlocks isn't working", 22, rt.runTest(7));
142: }
143:
144: public void testInvokeTestData3() {
145: RunTest rt = loadAsRunTest("PrivateClass");
146: // returns 4
147: assertEquals("PrivateClass isn't working", 4, rt.runTest(7));
148:
149: rt = loadAsRunTest("SuperClass");
150: // returns 3*x
151: assertEquals("SuperClass isn't working", 21, rt.runTest(7));
152:
153: rt = loadAsRunTest("Wimple");
154: // returns ??
155: assertEquals("Wimple isn't working", 92, rt.runTest(7));
156: }
157:
158: public void testInvokeTestData4() {
159: RunTest rt = loadAsRunTest("ComplicatedConstructor");
160: assertEquals("ComplicatedConstructor isn't working", 61, rt
161: .runTest(3));
162: rt = loadAsRunTest("Looper");
163: assertEquals("Looper isn't working", 127008000, rt.runTest(5));
164:
165: rt = loadAsRunTest("StaticInit");
166: assertEquals("StaticInit isn't working", 10, rt.runTest(7));
167: }
168:
169: // test all synthesized classes
170: public void testSynth() {
171: assertEquals("synthesizing isn't disabled in loader", false,
172: qLoader.getSynthEnabled());
173: qLoader.setSynthEnabled(true);
174: assertEquals("enabling synthesizing failed", true, qLoader
175: .getSynthEnabled());
176:
177: RunTest rt = loadAsRunTest("test.data.TestDefault");
178: // testDefault.runTest(x) returns 2 whatever the input is
179: assertEquals("testDefault isn't working", 2, rt.runTest(47));
180: assertEquals("testDefault isn't working", 2, rt.runTest(-7));
181:
182: rt = loadAsRunTest("test.data.TestIfThen");
183: // testIfThen.runTest(x) returns 3 if x > 0, 5 otherwise
184: assertEquals("testIfThen isn't working", 3, rt.runTest(47));
185: assertEquals("testIfThen isn't working", 5, rt.runTest(-7));
186:
187: rt = loadAsRunTest("test.data.TestNPEWithCatch");
188: // testNPEWithCatch.runTest(x) always returns 3
189: assertEquals("testNPEWithCatch isn't working", 3, rt
190: .runTest(47));
191: assertEquals("testNPEWithCatch isn't working", 3, rt
192: .runTest(-7));
193:
194: rt = loadAsRunTest("test.data.TestNPENoCatch");
195: // testNPENoCatch.runTest(x) always throws a NullPointerException
196: int x;
197: try {
198: x = rt.runTest(47);
199: fail("testNPENoCatch didn't throw exception");
200: } catch (NullPointerException e) {
201: ; // ignore it
202: }
203: try {
204: x = rt.runTest(-7);
205: fail("testNPENoCatch didn't throw exception");
206: } catch (NullPointerException e) {
207: ; // ignore it
208: }
209:
210: rt = loadAsRunTest("test.data.TestSelect");
211: // testSelect.runTest(x) returns
212: // 1 if x == 1; 3 if x == 2; 5 if x == 3; 2 otherwise
213: assertEquals("testSelect isn't working", 2, rt.runTest(47));
214: assertEquals("testSelect isn't working", 2, rt.runTest(-7));
215: assertEquals("testSelect isn't working", 1, rt.runTest(1));
216: assertEquals("testSelect isn't working", 3, rt.runTest(2));
217: assertEquals("testSelect isn't working", 5, rt.runTest(3));
218:
219: rt = loadAsRunTest("test.data.TestWhile");
220: // testWhile.runTest(x) returns
221: // 0 if x >= 0, x otherwise
222: assertEquals("testWhile isn't working", 0, rt.runTest(47));
223: assertEquals("testWhile isn't working", -7, rt.runTest(-7));
224: }
225:
226: }
|