1 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 19 public class TestTransformer extends TestCase { 20 21 private URL [] cp = null; 22 23 private String [] delegating = { 24 }; 26 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 }; 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 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, delegating, include, exclude); 77 spy = new GraphSpy(); 79 qLoader.addGraphXformer(spy); 80 } 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 assertEquals ("AnonymousClass isn't working", 47, rt.runTest(47)); 119 120 rt = loadAsRunTest("BasicLoad"); 121 assertEquals ("BasicLoad isn't working", 49, rt.runTest(7)); 123 124 rt = loadAsRunTest("ExceptionLoad"); 126 assertEquals ("ExceptionLoad isn't working", 121, rt.runTest(11)); 128 } 129 public void testInvokeTestData2() { 130 RunTest 131 rt = loadAsRunTest("Finally"); 132 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 assertEquals ("Finally2Catches isn't working", 3600, rt.runTest(11)); 139 140 rt = loadAsRunTest("InnerClass"); 141 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 assertEquals ("PrivateClass isn't working", 4, rt.runTest(7)); 153 154 rt = loadAsRunTest("SuperClass"); 155 assertEquals ("SuperClass isn't working", 21, rt.runTest(7)); 157 158 rt = loadAsRunTest("Wimple"); 159 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 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 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 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 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 int x; 201 try { 202 x = rt.runTest(47); 203 fail ("testNPENoCatch didn't throw exception"); 204 } catch (NullPointerException e) { 205 ; } 207 try { 208 x = rt.runTest(-7); 209 fail ("testNPENoCatch didn't throw exception"); 210 } catch (NullPointerException e) { 211 ; } 213 214 rt = loadAsRunTest("test.data.TestSelect"); 215 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 assertEquals ("testWhile isn't working", 0, rt.runTest(47)); 227 assertEquals ("testWhile isn't working",-7, rt.runTest(-7)); 228 } 229 230 } 231 | Popular Tags |