001: /* TestGraphTransformer.java */
002: package org.quilt.cl;
003:
004: import java.util.List;
005: import java.util.Vector;
006: import junit.framework.*;
007: import org.apache.bcel.classfile.Method;
008: import org.apache.bcel.generic.*;
009:
010: import org.quilt.graph.*;
011:
012: /**
013: * Test GraphTransformer using runTest methods from classes synthesized
014: * by ClassFactory.
015: *
016: * @author <a href="jddixon@users.sourceforge.net">Jim Dixon</a>
017: */
018: public class TestGraphTransformer extends TestCase {
019:
020: private ClassFactory factory = ClassFactory.getInstance();
021:
022: private ClassGen clazz;
023:
024: private GraphTransformer xformer;
025:
026: private GraphSpy spy;
027:
028: private GraphTalker talker;
029:
030: private ControlFlowGraph theGraph;
031:
032: public TestGraphTransformer(String name) {
033: super (name);
034: GraphSpy.setName("the spy");
035: }
036:
037: public void setUp() {
038: List gxf = new Vector();
039: spy = new GraphSpy();
040: talker = new GraphTalker();
041: gxf.add(spy);
042: // uncomment if things stop working
043: // gxf.add ( talker );
044: xformer = new GraphTransformer(gxf);
045: }
046:
047: private MethodGen loadAndExtractRunTest(String name) {
048: clazz = factory.makeClass(name, name);
049: Method[] methods = clazz.getMethods();
050: for (int i = 0; i < methods.length; i++) {
051: if (methods[i].getName().equals("runTest")) {
052: return new MethodGen(methods[i], clazz.getClassName(),
053: clazz.getConstantPool());
054: }
055: }
056: return null; // not found
057: }
058:
059: public void testDefault() {
060: MethodGen mgDefault = loadAndExtractRunTest("test.data.TestDefault");
061: assertNotNull("extracted method is null", mgDefault);
062:
063: // build the graph, get the instruction list from it
064: mgDefault.setInstructionList(xformer.xform(clazz, mgDefault));
065: mgDefault.removeNOPs();
066: InstructionList ilist = mgDefault.getInstructionList();
067: Instruction[] inst = ilist.getInstructions();
068: assertEquals("wrong number of instructions", 2, ilist
069: .getLength());
070: assertTrue("", inst[0] instanceof ICONST);
071: assertTrue("", inst[1] instanceof IRETURN);
072:
073: theGraph = GraphSpy.getTheGraph();
074: // DEBUG
075: //GraphTalker talker = new GraphTalker();
076: //talker.xform (null, null, theGraph);
077: // END
078: assertNotNull("failed to get reference to method CFG", theGraph);
079: assertNotNull("graph exit vertex is null", theGraph.getExit());
080: assertEquals("TestDefault graph has wrong size", 3, theGraph
081: .size());
082: }
083:
084: public void testIfThen() {
085: MethodGen mgIfThen = loadAndExtractRunTest("test.data.TestIfThen");
086: assertNotNull("extracted method is null", mgIfThen);
087:
088: // build the graph, get the instruction list from it
089: InstructionList ilist = xformer.xform(clazz, mgIfThen);
090: assertEquals("wrong number of instructions", 6, ilist
091: .getLength());
092: Instruction[] inst = ilist.getInstructions();
093: assertTrue("should be ILOAD: " + inst[0],
094: inst[0] instanceof ILOAD);
095: assertTrue("should be IFGT: " + inst[1],
096: inst[1] instanceof IFGT);
097: assertTrue("should be ICONST: " + inst[2],
098: inst[2] instanceof ICONST);
099: assertTrue("should be IRETURN: " + inst[3],
100: inst[3] instanceof IRETURN);
101: assertTrue("should be ICONST: " + inst[4],
102: inst[4] instanceof ICONST);
103: assertTrue("should be IRETURN: " + inst[5],
104: inst[5] instanceof IRETURN);
105:
106: // EXAMINE GRAPH
107: theGraph = GraphSpy.getTheGraph();
108: assertNotNull("failed to get reference to method CFG", theGraph);
109: assertNotNull("graph exit vertex is null", theGraph.getExit());
110: assertEquals("TestIfThen graph has wrong size", 5, theGraph
111: .size());
112: }
113:
114: public void testWhile() {
115: MethodGen mgWhile = loadAndExtractRunTest("test.data.TestWhile");
116: assertNotNull("extracted method is null", mgWhile);
117:
118: // build the graph, get the instruction list from it
119: mgWhile.setInstructionList(xformer.xform(clazz, mgWhile));
120: mgWhile.removeNOPs();
121: InstructionList ilist = mgWhile.getInstructionList();
122: assertEquals("wrong number of instructions", 7, ilist
123: .getLength());
124: Instruction[] inst = ilist.getInstructions();
125: assertTrue("", inst[0] instanceof ILOAD);
126: assertTrue("", inst[1] instanceof DUP);
127: assertTrue("", inst[2] instanceof IFLE);
128: assertTrue("", inst[3] instanceof ICONST);
129: assertTrue("", inst[4] instanceof ISUB);
130: assertTrue("", inst[5] instanceof GOTO);
131: assertTrue("", inst[6] instanceof IRETURN);
132:
133: // EXAMINE GRAPH
134: theGraph = GraphSpy.getTheGraph();
135: assertNotNull("failed to get reference to method CFG", theGraph);
136: assertNotNull("graph exit vertex is null", theGraph.getExit());
137: assertEquals("TestWhile graph has wrong size", 6, theGraph
138: .size());
139: } // GEEP
140:
141: private void dumpInstructions(Instruction[] ilist) {
142: System.out.println("Instructions returned:");
143: for (int i = 0; i < ilist.length; i++) {
144: System.out.println(" " + ilist[i]);
145: }
146: }
147:
148: public void testNPEWithCatch() {
149: MethodGen mgNPEWithCatch = loadAndExtractRunTest("test.data.TestNPEWithCatch");
150: assertNotNull("extracted method is null", mgNPEWithCatch);
151:
152: // build the graph, get the instruction list from it
153: mgNPEWithCatch.setInstructionList(xformer.xform(clazz,
154: mgNPEWithCatch));
155: mgNPEWithCatch.removeNOPs();
156: InstructionList ilist = mgNPEWithCatch.getInstructionList();
157: Instruction[] inst = ilist.getInstructions();
158:
159: theGraph = GraphSpy.getTheGraph();
160: // talker.xform (null, null, theGraph);
161:
162: assertNotNull("failed to get reference to method CFG", theGraph);
163: assertNotNull("graph exit vertex is null", theGraph.getExit());
164: assertEquals("TestNPEWithCatch graph has wrong size", 8,
165: theGraph.size());
166:
167: assertEquals("wrong number of instructions", 7, ilist
168: .getLength());
169: assertTrue("expected ACONST_NULL",
170: inst[0] instanceof ACONST_NULL);
171: assertTrue("expected ICONST", inst[1] instanceof ICONST);
172: assertTrue("expected INVOKEVIRTUAL",
173: inst[2] instanceof INVOKEVIRTUAL);
174: assertTrue("expected ICONST", inst[3] instanceof ICONST);
175: assertTrue("expected IRETURN", inst[4] instanceof IRETURN);
176: assertTrue("expected ICONST", inst[5] instanceof ICONST);
177: assertTrue("expected IRETURN", inst[6] instanceof IRETURN);
178: }
179:
180: public void testNPENoCatch() {
181: MethodGen mgNPENoCatch = loadAndExtractRunTest("test.data.TestNPENoCatch");
182: assertNotNull("extracted method is null", mgNPENoCatch);
183:
184: // build the graph, get the instruction list from it
185: InstructionList ilist = xformer.xform(clazz, mgNPENoCatch);
186: assertEquals("wrong number of instructions", 5, ilist
187: .getLength());
188: Instruction[] inst = ilist.getInstructions();
189: assertTrue("expected ACONST_NULL",
190: inst[0] instanceof ACONST_NULL);
191: assertTrue("expected ICONST", inst[1] instanceof ICONST);
192: assertTrue("expected INVOKEVIRTUAL",
193: inst[2] instanceof INVOKEVIRTUAL);
194: assertTrue("expected ICONST", inst[3] instanceof ICONST);
195: assertTrue("expected IRETURN", inst[4] instanceof IRETURN);
196:
197: theGraph = GraphSpy.getTheGraph();
198: // DEBUG
199: //GraphTalker talker = new GraphTalker();
200: //talker.xform (null, null, theGraph);
201: // END
202: assertNotNull("failed to get reference to method CFG", theGraph);
203: assertNotNull("graph exit vertex is null", theGraph.getExit());
204: assertEquals("TestNPENoCatch graph has wrong size", 4, theGraph
205: .size());
206: }
207:
208: public void testSelect() {
209: MethodGen mgSelect = loadAndExtractRunTest("test.data.TestSelect");
210: assertNotNull("extracted method is null", mgSelect);
211:
212: // build the graph, get the instruction list from it
213: mgSelect.setInstructionList(xformer.xform(clazz, mgSelect));
214: mgSelect.removeNOPs();
215: InstructionList ilist = mgSelect.getInstructionList();
216: Instruction[] inst = ilist.getInstructions();
217: // DEBUG
218: //dumpInstructions(inst);
219: // END
220: assertEquals("wrong number of instructions", 10, inst.length);
221: assertTrue("expected ILOAD", inst[0] instanceof ILOAD);
222: assertTrue("expected LOOKUPSWITCH",
223: inst[1] instanceof LOOKUPSWITCH);
224: assertTrue("expected SIPUSH", inst[2] instanceof SIPUSH);
225: assertTrue("expected IRETURN", inst[3] instanceof IRETURN);
226: assertTrue("expected SIPUSH", inst[4] instanceof SIPUSH);
227: assertTrue("expected IRETURN", inst[5] instanceof IRETURN);
228: assertTrue("expected SIPUSH", inst[6] instanceof SIPUSH);
229: assertTrue("expected IRETURN", inst[7] instanceof IRETURN);
230: assertTrue("expected SIPUSH", inst[8] instanceof SIPUSH);
231: assertTrue("expected IRETURN", inst[9] instanceof IRETURN);
232:
233: theGraph = GraphSpy.getTheGraph();
234: // DEBUG
235: //GraphTalker talker = new GraphTalker();
236: //talker.xform (null, null, theGraph);
237: // END
238: assertNotNull("failed to get reference to method CFG", theGraph);
239: assertNotNull("graph exit vertex is null", theGraph.getExit());
240: assertEquals("TestSelect graph has wrong size", 7, theGraph
241: .size());
242: }
243: }
|