/test/kilim/test/Base.java
http://github.com/kilim/kilim · Java · 101 lines · 75 code · 11 blank · 15 comment · 15 complexity · 0dd5e2c9c39394e0fbec188b5e95c92b MD5 · raw file
- /* Copyright (c) 2006, Sriram Srinivasan
- *
- * You may distribute this software under the terms of the license
- * specified in the file "License"
- */
- package kilim.test;
- import java.util.ArrayList;
- import junit.framework.TestCase;
- import kilim.analysis.BasicBlock;
- import kilim.analysis.ClassFlow;
- import kilim.analysis.KilimContext;
- import kilim.analysis.MethodFlow;
- import kilim.mirrors.Detector;
- import org.objectweb.asm.tree.AbstractInsnNode;
- import org.objectweb.asm.tree.MethodInsnNode;
- public class Base extends TestCase {
- private static ArrayList<MethodFlow> stflows;
- private static String lastClassName = null;
- KilimContext context = KilimContext.DEFAULT;
- protected void cache(String className) throws Exception {
- if (lastClassName != className) {
- ClassFlow cf = new ClassFlow(
- context,
- ClassLoader.getSystemResourceAsStream(className.replace('.', '/')+".class"));
- stflows = cf.analyze(/* forceAnalysis = */true);
- lastClassName = className;
- }
- }
- protected MethodFlow getFlow(String methodName) {
- for (int i = 0; i < stflows.size(); i++) {
- MethodFlow flow = stflows.get(i);
- if (flow.name.equals(methodName)) {
- return flow;
- }
- }
- fail("No method called " + methodName);
- return null;
- }
- /**
- * Returns the first basic block in the flow that has a method invocation of
- * <methodName>
- */
- protected BasicBlock getBBForMethod(MethodFlow flow, String methodName) {
- for (BasicBlock bb : flow.getBasicBlocks()) {
- AbstractInsnNode ainode = bb.getInstruction(bb.startPos);
- if (ainode instanceof MethodInsnNode
- && ((MethodInsnNode) ainode).name.equals(methodName)) {
- return bb;
- }
- }
- fail("No method invocation found for " + methodName);
- return null;
- }
- protected ArrayList<MethodFlow> getFlows() {
- return stflows;
- }
- protected void checkCov(String methodName) {
- MethodFlow flow = getFlow(methodName);
- if (flow == null)
- return;
- ArrayList<BasicBlock> bbs = flow.getBasicBlocks();
- // Verify that all instructions are covered and that the only ones that
- // aren't are labelnodes. Also verify that there are no overlaps.
- int size = flow.instructions.size();
- boolean coverage[] = new boolean[size];
- for (int i = 0; i < size; i++) {
- coverage[i] = false;
- }
- for (BasicBlock bb : bbs) {
- /*
- * if (bb.startFrame == null) { fail("BB doesn't have a starting
- * frame"); return; }
- */
- int end = bb.endPos;
- for (int i = bb.startPos; i <= end; i++) {
- if (coverage[i]) {
- fail("BasicBlock overlap");
- return;
- }
- coverage[i] = true;
- }
- }
- for (int i = 0; i < size; i++) {
- if (!coverage[i]) {
- fail("Instruction " + i + " not covered");
- return;
- }
- }
- }
- }