PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1/* Copyright (c) 2006, Sriram Srinivasan
  2 *
  3 * You may distribute this software under the terms of the license 
  4 * specified in the file "License"
  5 */
  6
  7package kilim.test;
  8
  9import java.util.ArrayList;
 10
 11import junit.framework.TestCase;
 12import kilim.analysis.BasicBlock;
 13import kilim.analysis.ClassFlow;
 14import kilim.analysis.KilimContext;
 15import kilim.analysis.MethodFlow;
 16import kilim.mirrors.Detector;
 17
 18import org.objectweb.asm.tree.AbstractInsnNode;
 19import org.objectweb.asm.tree.MethodInsnNode;
 20
 21public class Base extends TestCase {
 22    private static ArrayList<MethodFlow> stflows;
 23    private static String                lastClassName = null;
 24    KilimContext context = KilimContext.DEFAULT;
 25
 26    protected void cache(String className) throws Exception {
 27        if (lastClassName != className) {
 28            ClassFlow cf = new ClassFlow(
 29                    context,
 30                    ClassLoader.getSystemResourceAsStream(className.replace('.', '/')+".class"));
 31            stflows = cf.analyze(/* forceAnalysis = */true);
 32            lastClassName = className;
 33        }
 34    }
 35
 36    protected MethodFlow getFlow(String methodName) {
 37        for (int i = 0; i < stflows.size(); i++) {
 38            MethodFlow flow = stflows.get(i);
 39            if (flow.name.equals(methodName)) {
 40                return flow;
 41            }
 42        }
 43        fail("No method called " + methodName);
 44        return null;
 45    }
 46
 47    /**
 48     * Returns the first basic block in the flow that has a method invocation of
 49     * <methodName>
 50     */
 51    protected BasicBlock getBBForMethod(MethodFlow flow, String methodName) {
 52        for (BasicBlock bb : flow.getBasicBlocks()) {
 53            AbstractInsnNode ainode = bb.getInstruction(bb.startPos);
 54            if (ainode instanceof MethodInsnNode
 55                    && ((MethodInsnNode) ainode).name.equals(methodName)) {
 56                return bb;
 57            }
 58        }
 59        fail("No method invocation found for " + methodName);
 60        return null;
 61    }
 62
 63    protected ArrayList<MethodFlow> getFlows() {
 64        return stflows;
 65    }
 66
 67    protected void checkCov(String methodName) {
 68        MethodFlow flow = getFlow(methodName);
 69        if (flow == null)
 70            return;
 71        ArrayList<BasicBlock> bbs = flow.getBasicBlocks();
 72        // Verify that all instructions are covered and that the only ones that
 73        // aren't are labelnodes. Also verify that there are no overlaps.
 74        int size = flow.instructions.size();
 75        boolean coverage[] = new boolean[size];
 76        for (int i = 0; i < size; i++) {
 77            coverage[i] = false;
 78        }
 79        for (BasicBlock bb : bbs) {
 80            /*
 81             * if (bb.startFrame == null) { fail("BB doesn't have a starting
 82             * frame"); return; }
 83             */
 84            int end = bb.endPos;
 85            for (int i = bb.startPos; i <= end; i++) {
 86                if (coverage[i]) {
 87                    fail("BasicBlock overlap");
 88                    return;
 89                }
 90                coverage[i] = true;
 91            }
 92        }
 93        for (int i = 0; i < size; i++) {
 94            if (!coverage[i]) {
 95                fail("Instruction " + i + " not covered");
 96                return;
 97            }
 98        }
 99    }
100
101}