1 2 package org.quilt.cl; 3 4 import java.util.List ; 5 import java.util.Vector ; 6 import junit.framework.*; 7 import org.apache.bcel.classfile.Method; 8 import org.apache.bcel.generic.*; 9 10 import org.quilt.graph.*; 11 12 18 public class TestGraphTransformer extends TestCase { 19 20 private ClassFactory factory = ClassFactory.getInstance(); 21 22 private ClassGen clazz; 23 24 private GraphTransformer xformer; 25 26 private GraphSpy spy; 27 28 private GraphTalker talker; 29 30 private ControlFlowGraph theGraph; 31 32 public TestGraphTransformer (String name) { 33 super(name); 34 GraphSpy.setName("the spy"); 35 } 36 37 public void setUp () { 38 List gxf = new Vector (); 39 spy = new GraphSpy(); 40 talker = new GraphTalker(); 41 gxf.add ( spy ); 42 xformer = new GraphTransformer ( gxf ); 45 } 46 47 private MethodGen loadAndExtractRunTest (String name) { 48 clazz = factory.makeClass(name, name); 49 Method [] methods = clazz.getMethods(); 50 for (int i = 0 ; i < methods.length; i++) { 51 if ( methods[i].getName().equals("runTest") ) { 52 return new MethodGen (methods[i], clazz.getClassName(), 53 clazz.getConstantPool()); 54 } 55 } 56 return null; } 58 public void testDefault () { 59 MethodGen 60 mgDefault = loadAndExtractRunTest ("test.data.TestDefault"); 61 assertNotNull ("extracted method is null", mgDefault); 62 63 mgDefault.setInstructionList(xformer.xform ( clazz, mgDefault)); 65 mgDefault.removeNOPs(); 66 InstructionList ilist = mgDefault.getInstructionList(); 67 Instruction [] inst = ilist.getInstructions(); 68 assertEquals ("wrong number of instructions", 2, ilist.getLength() ); 69 assertTrue ("", inst[0] instanceof ICONST); 70 assertTrue ("", inst[1] instanceof IRETURN); 71 72 theGraph = GraphSpy.getTheGraph(); 73 assertNotNull ("failed to get reference to method CFG", theGraph); 78 assertNotNull ("graph exit vertex is null", theGraph.getExit()); 79 assertEquals ("TestDefault graph has wrong size", 3, theGraph.size() ); 80 } 81 82 public void testIfThen () { 83 MethodGen 84 mgIfThen = loadAndExtractRunTest ("test.data.TestIfThen"); 85 assertNotNull ("extracted method is null", mgIfThen); 86 87 InstructionList ilist = xformer.xform ( clazz, mgIfThen); 89 assertEquals ("wrong number of instructions", 6, ilist.getLength() ); 90 Instruction [] inst = ilist.getInstructions(); 91 assertTrue ("should be ILOAD: " + inst[0], 92 inst[0] instanceof ILOAD); 93 assertTrue ("should be IFGT: " + inst[1], 94 inst[1] instanceof IFGT); 95 assertTrue ("should be ICONST: " + inst[2], 96 inst[2] instanceof ICONST); 97 assertTrue ("should be IRETURN: " + inst[3], 98 inst[3] instanceof IRETURN); 99 assertTrue ("should be ICONST: " + inst[4], 100 inst[4] instanceof ICONST); 101 assertTrue ("should be IRETURN: " + inst[5], 102 inst[5] instanceof IRETURN); 103 104 theGraph = GraphSpy.getTheGraph(); 106 assertNotNull ("failed to get reference to method CFG", theGraph); 107 assertNotNull ("graph exit vertex is null", theGraph.getExit()); 108 assertEquals ("TestIfThen graph has wrong size", 5, theGraph.size() ); 109 } 110 111 public void testWhile () { 112 MethodGen 113 mgWhile = loadAndExtractRunTest ("test.data.TestWhile"); 114 assertNotNull ("extracted method is null", mgWhile); 115 116 mgWhile.setInstructionList(xformer.xform ( clazz, mgWhile)); 118 mgWhile.removeNOPs(); 119 InstructionList ilist = mgWhile.getInstructionList(); 120 assertEquals ("wrong number of instructions", 7, ilist.getLength() ); 121 Instruction [] inst = ilist.getInstructions(); 122 assertTrue ("", inst[0] instanceof ILOAD); 123 assertTrue ("", inst[1] instanceof DUP); 124 assertTrue ("", inst[2] instanceof IFLE); 125 assertTrue ("", inst[3] instanceof ICONST); 126 assertTrue ("", inst[4] instanceof ISUB); 127 assertTrue ("", inst[5] instanceof GOTO); 128 assertTrue ("", inst[6] instanceof IRETURN); 129 130 theGraph = GraphSpy.getTheGraph(); 132 assertNotNull ("failed to get reference to method CFG", theGraph); 133 assertNotNull ("graph exit vertex is null", theGraph.getExit()); 134 assertEquals ("TestWhile graph has wrong size", 6, theGraph.size() ); 135 } 137 private void dumpInstructions (Instruction [] ilist) { 138 System.out.println("Instructions returned:"); 139 for (int i = 0; i < ilist.length; i++) { 140 System.out.println (" " + ilist[i] ); 141 } 142 } 143 public void testNPEWithCatch () { 144 MethodGen 145 mgNPEWithCatch = loadAndExtractRunTest ("test.data.TestNPEWithCatch"); 146 assertNotNull ("extracted method is null", mgNPEWithCatch); 147 148 mgNPEWithCatch.setInstructionList(xformer.xform ( clazz, 150 mgNPEWithCatch)); 151 mgNPEWithCatch.removeNOPs(); 152 InstructionList ilist = mgNPEWithCatch.getInstructionList(); 153 Instruction [] inst = ilist.getInstructions(); 154 155 theGraph = GraphSpy.getTheGraph(); 156 158 assertNotNull ("failed to get reference to method CFG", theGraph); 159 assertNotNull ("graph exit vertex is null", theGraph.getExit()); 160 assertEquals ("TestNPEWithCatch graph has wrong size", 161 8, theGraph.size() ); 162 163 assertEquals ("wrong number of instructions", 7, ilist.getLength() ); 164 assertTrue ("expected ACONST_NULL", inst[0] instanceof ACONST_NULL); 165 assertTrue ("expected ICONST", inst[1] instanceof ICONST); 166 assertTrue ("expected INVOKEVIRTUAL", inst[2] instanceof INVOKEVIRTUAL); 167 assertTrue ("expected ICONST", inst[3] instanceof ICONST); 168 assertTrue ("expected IRETURN", inst[4] instanceof IRETURN); 169 assertTrue ("expected ICONST", inst[5] instanceof ICONST); 170 assertTrue ("expected IRETURN", inst[6] instanceof IRETURN); 171 } 172 173 public void testNPENoCatch () { 174 MethodGen 175 mgNPENoCatch = loadAndExtractRunTest ("test.data.TestNPENoCatch"); 176 assertNotNull ("extracted method is null", mgNPENoCatch); 177 178 InstructionList ilist = xformer.xform ( clazz, mgNPENoCatch); 180 assertEquals ("wrong number of instructions", 5, ilist.getLength() ); 181 Instruction [] inst = ilist.getInstructions(); 182 assertTrue ("expected ACONST_NULL", inst[0] instanceof ACONST_NULL); 183 assertTrue ("expected ICONST", inst[1] instanceof ICONST); 184 assertTrue ("expected INVOKEVIRTUAL", inst[2] instanceof INVOKEVIRTUAL); 185 assertTrue ("expected ICONST", inst[3] instanceof ICONST); 186 assertTrue ("expected IRETURN", inst[4] instanceof IRETURN); 187 188 theGraph = GraphSpy.getTheGraph(); 189 assertNotNull ("failed to get reference to method CFG", theGraph); 194 assertNotNull ("graph exit vertex is null", theGraph.getExit()); 195 assertEquals ("TestNPENoCatch graph has wrong size", 196 4, theGraph.size() ); 197 } 198 199 public void testSelect () { 200 MethodGen 201 mgSelect = loadAndExtractRunTest ("test.data.TestSelect"); 202 assertNotNull ("extracted method is null", mgSelect); 203 204 mgSelect.setInstructionList(xformer.xform ( clazz, mgSelect)); 206 mgSelect.removeNOPs(); 207 InstructionList ilist = mgSelect.getInstructionList(); 208 Instruction [] inst = ilist.getInstructions(); 209 assertEquals ("wrong number of instructions", 10, inst.length ); 213 assertTrue ("expected ILOAD", inst[0] instanceof ILOAD); 214 assertTrue ("expected LOOKUPSWITCH", inst[1] instanceof LOOKUPSWITCH); 215 assertTrue ("expected SIPUSH", inst[2] instanceof SIPUSH); 216 assertTrue ("expected IRETURN", inst[3] instanceof IRETURN); 217 assertTrue ("expected SIPUSH", inst[4] instanceof SIPUSH); 218 assertTrue ("expected IRETURN", inst[5] instanceof IRETURN); 219 assertTrue ("expected SIPUSH", inst[6] instanceof SIPUSH); 220 assertTrue ("expected IRETURN", inst[7] instanceof IRETURN); 221 assertTrue ("expected SIPUSH", inst[8] instanceof SIPUSH); 222 assertTrue ("expected IRETURN", inst[9] instanceof IRETURN); 223 224 theGraph = GraphSpy.getTheGraph(); 225 assertNotNull ("failed to get reference to method CFG", theGraph); 230 assertNotNull ("graph exit vertex is null", theGraph.getExit()); 231 assertEquals ("TestSelect graph has wrong size", 7, theGraph.size() ); 232 } 233 } 234 | Popular Tags |