1 /* TestTransformer.java */ 2 package org.quilt.cl; 3 4 import java.io.*; 5 import java.lang.Class; 6 import java.lang.reflect.*; 7 import java.net.*; 8 import junit.framework.*; 9 10 /*** 11 * A somewhat prettified TestQuiltClassLoader with transforming 12 * enabled. 13 * 14 * XXX KNOWN PROBLEMS: NestedTryBlocks and SuperClass generate 15 * control flow graphs with serious errors. 16 * 17 * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a> 18 */ 19 public class TestTransformer extends TestCase { 20 21 private URL [] cp = null; 22 23 private String[] delegating = { 24 // NOTHING beyond standard defaults 25 }; 26 // we want everything to be instrumented 27 private String[] include = { 28 "test.data.", 29 "AnonymousClass", "AnonymousClass2Catches", 30 "BasicLoad", 31 "ExceptionLoad", "ExceptionThrow", 32 "InnerClass", "NestedTryBlocks", 33 "PrivateClass", "SuperClass", 34 "Wimple" 35 }; 36 private String[] exclude = { 37 // NOTHING 38 }; 39 40 private GraphXformer spy; 41 private GraphXformer talker; 42 43 private QuiltClassLoader qLoader = null; 44 45 public TestTransformer (String name) { 46 super(name); 47 } 48 49 50 public void setUp () { 51 File sam1 = new File ("target/test-data-classes/"); 52 String fullPath1 = sam1.getAbsolutePath() + "/"; 53 File sam2 = new File ("target/classes"); 54 String fullPath2 = sam2.getAbsolutePath() + "/"; 55 File sam3 = new File ("target/test-classes"); 56 String fullPath3 = sam3.getAbsolutePath() + "/"; 57 try { 58 // Terminating slash is required. Relative paths don't 59 // work. 60 URL [] samples = { 61 new URL ( "file://" + fullPath1), 62 new URL ( "file://" + fullPath2), 63 new URL ( "file://" + fullPath3) 64 }; 65 cp = samples; 66 } catch (MalformedURLException e) { 67 e.printStackTrace(); 68 fail ("problem creating class path"); 69 } 70 qLoader = new QuiltClassLoader( 71 cp, 72 null, // parent 73 delegating, // delegated classes 74 include, // being instrumented 75 exclude); // do NOT instrument 76 77 // this is sufficient to force graph generation 78 spy = new GraphSpy(); 79 qLoader.addGraphXformer(spy); 80 // this makes for a very verbose output 81 // talker = new GraphTalker(); 82 // qLoader.addGraphXformer(talker); 83 } 84 public void testLoader() { 85 Class a1 = null; 86 try { 87 a1 = qLoader.loadClass("AnonymousClass"); 88 } catch (ClassNotFoundException e) { 89 e.printStackTrace(); 90 fail ("Error loading AnonymousClass using loadClass"); 91 } 92 assertNotNull("qLoader returned null", a1); 93 } 94 95 private RunTest loadAsRunTest (String name) { 96 Class clazz = null; 97 try { 98 clazz = qLoader.loadClass(name); 99 } catch (ClassNotFoundException e) { 100 e.printStackTrace(); 101 fail ("exception loading " + name + " using loadClass"); 102 } 103 RunTest rt = null; 104 try { 105 rt = (RunTest) clazz.newInstance(); 106 } catch ( InstantiationException e ) { 107 fail ("InstantiationException instantiating loaded class " + name); 108 } catch ( IllegalAccessException e ) { 109 fail ("IllegalAccessException instantiating loaded class " + name); 110 } catch (ClassCastException e) { 111 fail ("ClassCastException instantiating loaded class " + name); 112 } 113 return rt; 114 } 115 public void testInvokeTestData() { 116 RunTest rt = loadAsRunTest("AnonymousClass"); 117 // AnonymousClass.runTest(x) returns x 118 assertEquals ("AnonymousClass isn't working", 47, rt.runTest(47)); 119 120 rt = loadAsRunTest("BasicLoad"); 121 // BasicLoad.runTest(x) returns x*x 122 assertEquals ("BasicLoad isn't working", 49, rt.runTest(7)); 123 124 // XXX causes ClassCastException when instantiating 125 rt = loadAsRunTest("ExceptionLoad"); 126 // ExceptionLoad.runTest(x) also returns x*x 127 assertEquals ("ExceptionLoad isn't working", 121, rt.runTest(11)); 128 } 129 public void testInvokeTestData2() { 130 RunTest 131 rt = loadAsRunTest("Finally"); 132 // Finally.runTest(x) returns -1 133 assertEquals ("Finally isn't working", -1, rt.runTest(11)); 134 assertEquals ("Finally isn't working", -1, rt.runTest(1)); 135 136 rt = loadAsRunTest("Finally2Catches"); 137 // what Finally.runTest(x) returns is a bit complicated ... 138 assertEquals ("Finally2Catches isn't working", 3600, rt.runTest(11)); 139 140 rt = loadAsRunTest("InnerClass"); 141 // InnerClass.runTest(x) also returns x*x 142 assertEquals ("InnerClass isn't working", 9, rt.runTest(3)); 143 144 rt = loadAsRunTest("NestedTryBlocks"); 145 assertEquals ("NestedTryBlocks isn't working", 22, rt.runTest(7)); 146 } 147 148 public void testInvokeTestData3() { 149 RunTest 150 rt = loadAsRunTest("PrivateClass"); 151 // returns 4 152 assertEquals ("PrivateClass isn't working", 4, rt.runTest(7)); 153 154 rt = loadAsRunTest("SuperClass"); 155 // returns 3*x 156 assertEquals ("SuperClass isn't working", 21, rt.runTest(7)); 157 158 rt = loadAsRunTest("Wimple"); 159 // returns ?? 160 assertEquals ("Wimple isn't working", 92, rt.runTest(7)); 161 } 162 163 public void testInvokeTestData4 () { 164 RunTest 165 rt = loadAsRunTest("ComplicatedConstructor"); 166 assertEquals("ComplicatedConstructor isn't working", 167 61, rt.runTest(3)); 168 rt = loadAsRunTest("Looper"); 169 assertEquals("Looper isn't working", 127008000, rt.runTest(5)); 170 171 rt = loadAsRunTest("StaticInit"); 172 assertEquals("StaticInit isn't working", 10, rt.runTest(7)); 173 } 174 175 // test all synthesized classes 176 public void testSynth () { 177 assertEquals ("synthesizing isn't disabled in loader", 178 false, qLoader.getSynthEnabled() ); 179 qLoader.setSynthEnabled(true); 180 assertEquals ("enabling synthesizing failed", 181 true, qLoader.getSynthEnabled() ); 182 183 RunTest rt = loadAsRunTest("test.data.TestDefault"); 184 // testDefault.runTest(x) returns 2 whatever the input is 185 assertEquals ("testDefault isn't working", 2, rt.runTest(47)); 186 assertEquals ("testDefault isn't working", 2, rt.runTest(-7)); 187 188 rt = loadAsRunTest("test.data.TestIfThen"); 189 // testIfThen.runTest(x) returns 3 if x > 0, 5 otherwise 190 assertEquals ("testIfThen isn't working", 3, rt.runTest(47)); 191 assertEquals ("testIfThen isn't working", 5, rt.runTest(-7)); 192 193 rt = loadAsRunTest("test.data.TestNPEWithCatch"); 194 // testNPEWithCatch.runTest(x) always returns 3 195 assertEquals ("testNPEWithCatch isn't working", 3, rt.runTest(47)); 196 assertEquals ("testNPEWithCatch isn't working", 3, rt.runTest(-7)); 197 198 rt = loadAsRunTest("test.data.TestNPENoCatch"); 199 // testNPENoCatch.runTest(x) always throws a NullPointerException 200 int x; 201 try { 202 x = rt.runTest(47); 203 fail ("testNPENoCatch didn't throw exception"); 204 } catch (NullPointerException e) { 205 ; // ignore it 206 } 207 try { 208 x = rt.runTest(-7); 209 fail ("testNPENoCatch didn't throw exception"); 210 } catch (NullPointerException e) { 211 ; // ignore it 212 } 213 214 rt = loadAsRunTest("test.data.TestSelect"); 215 // testSelect.runTest(x) returns 216 // 1 if x == 1; 3 if x == 2; 5 if x == 3; 2 otherwise 217 assertEquals ("testSelect isn't working", 2, rt.runTest(47)); 218 assertEquals ("testSelect isn't working", 2, rt.runTest(-7)); 219 assertEquals ("testSelect isn't working", 1, rt.runTest(1)); 220 assertEquals ("testSelect isn't working", 3, rt.runTest(2)); 221 assertEquals ("testSelect isn't working", 5, rt.runTest(3)); 222 223 rt = loadAsRunTest("test.data.TestWhile"); 224 // testWhile.runTest(x) returns 225 // 0 if x >= 0, x otherwise 226 assertEquals ("testWhile isn't working", 0, rt.runTest(47)); 227 assertEquals ("testWhile isn't working",-7, rt.runTest(-7)); 228 } 229 230 }

This page was automatically generated by Maven