1 2 package org.quilt.cover.stmt; 3 4 import java.io.*; 5 import java.lang.Class ; 6 import java.lang.reflect.*; 7 import java.net.*; 8 9 import java.lang.reflect.Field ; 11 import java.lang.reflect.Method ; 12 14 import junit.framework.*; 15 import org.apache.bcel.generic.*; 16 import org.quilt.cl.*; 17 import org.quilt.cover.stmt.*; 18 19 27 public class TestStmtCoverage extends TestCase { 28 29 private ControlFlowGraph cfg; 30 31 32 private URL [] cp = null; 34 private String [] delegating = { 35 }; 37 private String [] include = { 39 "test.data.", 40 "AnonymousClass", "AnonymousClass2Catches", 41 "BasicLoad", "ComplicatedConstructor", 42 "ExceptionLoad", "ExceptionThrow", 43 "Finally", "Finally2Classes", 44 "InnerClass", "Looper", 45 "NestedTryBlocks", "OddSwitches", 46 "PrivateClass", "StaticInit", 47 "SuperClass", "SwitchLoad", 48 "Wimple" 49 }; 50 private String [] exclude = { 51 }; 53 54 private GraphXformer spy; 55 private GraphXformer talker, talker2; 56 57 private StmtRegistry stmtReg; 58 59 private QuiltClassLoader qLoader = null; 60 61 public TestStmtCoverage (String name) { 62 super(name); 63 } 64 65 public void setUp () { 66 File sam1 = new File ("target/test-data-classes/"); 67 String fullPath1 = sam1.getAbsolutePath() + "/"; 68 File sam2 = new File ("target/classes"); 69 String fullPath2 = sam2.getAbsolutePath() + "/"; 70 File sam3 = new File ("target/test-classes"); 71 String fullPath3 = sam3.getAbsolutePath() + "/"; 72 try { 73 URL [] samples = { 76 new URL ( "file://" + fullPath1), 77 new URL ( "file://" + fullPath2), 78 new URL ( "file://" + fullPath3) 79 }; 80 cp = samples; 81 } catch (MalformedURLException e) { 82 e.printStackTrace(); 83 fail ("problem creating class path"); 84 } 85 ClassLoader parent = ClassLoader.getSystemClassLoader(); 86 qLoader = new QuiltClassLoader( 87 cp, 88 parent, delegating, include, exclude); 93 spy = new GraphSpy(); 95 qLoader.addGraphXformer(spy); 96 97 talker = new GraphTalker(); 99 qLoader.addGraphXformer(talker); 100 101 stmtReg = (StmtRegistry) qLoader.addQuiltRegistry( 104 "org.quilt.cover.stmt.StmtRegistry"); 105 109 talker2 = new GraphTalker(); 111 qLoader.addGraphXformer(talker2); 112 } 113 114 private RunTest loadAsRunTest (String name) { 116 Class clazz = null; 117 try { 118 clazz = qLoader.loadClass(name); 119 126 } catch (ClassNotFoundException e) { 139 e.printStackTrace(); 140 fail ("exception loading " + name + " using loadClass"); 141 } 142 RunTest rt = null; 143 try { 144 rt = (RunTest) clazz.newInstance(); 145 } catch ( InstantiationException e ) { 146 fail ("InstantiationException instantiating loaded class " + name); 147 } catch ( IllegalAccessException e ) { 148 fail ("IllegalAccessException instantiating loaded class " + name); 149 } catch (ClassCastException e) { 150 fail ("ClassCastException instantiating loaded class " + name); 151 } 152 return rt; 153 } 154 161 private void checkInstrumentation(Object rt) { 162 String name = rt.getClass().getName(); 163 System.out.println("checkInstrumentation for class " + name); 164 try { 165 Field qField = rt.getClass().getField("q$$q"); 166 if (qField == null) { 167 System.out.println(name + " has no hit count array"); 168 fail(name + " has NO hit count array"); 169 } else try { 170 int [] hitCounts = (int[]) qField.get(null); 171 assertNotNull("q$$q has not been initialized", hitCounts); 172 } catch (IllegalAccessException e) { 173 e.printStackTrace(); 174 } 175 qField = rt.getClass().getField("q$$qVer"); 177 if (qField == null) { 178 System.out.println(name + " has no version field"); 179 fail(name + " has NO version field"); 180 } else try { 181 int version = qField.getInt(qField); 182 assertEquals("q$$q has wrong version number", 0, version); 183 } catch (IllegalAccessException e) { 184 e.printStackTrace(); 185 } 186 188 } catch (NoSuchFieldException e) { 189 fail (name + " has no q$$q field"); 190 } 191 } 192 193 public void testGetReg() { 195 StmtRegistry regInLoader = (StmtRegistry)qLoader.getRegistry( 196 "org.quilt.cover.stmt.StmtRegistry"); 197 assertNotNull("qLoader StmtRegistry is null", regInLoader); 198 assertSame ("qLoader has different StmtRegistry", 199 stmtReg, regInLoader); 200 } 201 public void testLoader() { 202 Class a1 = null; 203 try { 204 a1 = qLoader.loadClass("AnonymousClass"); 205 } catch (ClassNotFoundException e) { 206 e.printStackTrace(); 207 fail ("Error loading AnonymousClass using loadClass"); 208 } 209 assertNotNull("qLoader returned null", a1); 210 } 212 public void testInvokeTestData() { 225 RunTest 226 rt = loadAsRunTest("AnonymousClass"); 227 checkInstrumentation(rt); 228 assertEquals ("AnonymousClass isn't working", 47, rt.runTest(47)); 230 231 rt = loadAsRunTest("BasicLoad"); 232 checkInstrumentation(rt); 233 assertEquals ("BasicLoad isn't working", 49, rt.runTest(7)); 235 236 rt = loadAsRunTest("ComplicatedConstructor"); 237 checkInstrumentation(rt); 238 assertEquals("ComplicatedConstructor isn't working", 239 61, rt.runTest(3)); 240 rt = loadAsRunTest("ExceptionLoad"); 241 checkInstrumentation(rt); 242 assertEquals ("ExceptionLoad isn't working", 121, rt.runTest(11)); 244 245 rt = loadAsRunTest("InnerClass"); 246 checkInstrumentation(rt); 247 assertEquals ("InnerClass isn't working", 9, rt.runTest(3)); 249 250 rt = loadAsRunTest("Looper"); 251 assertEquals("Looper isn't working", 127008000, rt.runTest(5)); 252 253 263 rt = loadAsRunTest("PrivateClass"); 264 checkInstrumentation(rt); 265 assertEquals ("PrivateClass isn't working", 4, rt.runTest(7)); 267 268 rt = loadAsRunTest("SuperClass"); 269 checkInstrumentation(rt); 270 assertEquals ("SuperClass isn't working", 21, rt.runTest(7)); 272 273 String runResults = stmtReg.getReport(); 278 System.out.println("\nQuilt coverage report:\n" + runResults); 279 } 281 289 299 305 308 314 331 341 349 363 366 380 387 392 399 403 413 428 } 429 | Popular Tags |