Search in sources :

Example 1 with Source

use of org.jacoco.core.test.validation.Source in project jacoco by jacoco.

the class FinallyTest method gotos.

/**
 * This test studies placement of GOTO instructions.
 */
@Test
public void gotos() throws IOException {
    final Source source = Source.getSourceFor("src", Finally.class);
    byte[] b = TargetLoader.getClassDataAsBytes(Finally.class);
    b = BytecodeVersion.downgradeIfNeeded(BytecodeVersion.get(b), b);
    final ClassNode classNode = new ClassNode();
    new ClassReader(b).accept(classNode, 0);
    final Set<String> tags = new HashSet<String>();
    for (final MethodNode m : classNode.methods) {
        if ("main".equals(m.name)) {
            // skip it
            continue;
        }
        int lineNumber = -1;
        for (AbstractInsnNode i = m.instructions.getFirst(); i != null; i = i.getNext()) {
            if (AbstractInsnNode.LINE == i.getType()) {
                lineNumber = ((LineNumberNode) i).line;
            }
            if (Opcodes.GOTO == i.getOpcode()) {
                final String line = source.getLine(lineNumber);
                if (line.indexOf('$') < 0) {
                    throw new AssertionError("No tag at line " + lineNumber);
                }
                final String tag = line.substring(line.indexOf('$') + "$line-".length(), line.lastIndexOf('$'));
                tags.add(tag);
            }
        }
    }
    final Set<String> expected = new HashSet<String>();
    if (isJDKCompiler) {
        expected.add("example.2");
    } else {
        expected.add("example.0");
    }
    expected.add("breakStatement.for");
    if (isJDKCompiler) {
        if (JAVA_VERSION.isBefore("10")) {
            // https://bugs.openjdk.java.net/browse/JDK-8180141
            expected.add("breakStatement.1");
        } else {
            expected.add("breakStatement");
        }
        expected.add("breakStatement.2");
    } else {
        expected.add("breakStatement");
    }
    if (isJDKCompiler) {
        expected.add("emptyCatch.2");
    } else {
        expected.add("emptyCatch");
        expected.add("emptyCatch.1");
    }
    if (isJDKCompiler) {
        expected.add("catchNotExecuted.2");
    } else {
        expected.add("catchNotExecuted");
        expected.add("catchNotExecuted.1");
    }
    if (isJDKCompiler) {
        expected.add("nested.5");
        expected.add("nested.6");
    } else {
        expected.add("nested.0");
        expected.add("nested.3");
    }
    if (isJDKCompiler && JAVA_VERSION.isBefore("1.8")) {
        expected.add("emptyTry.2");
    }
    if (!isJDKCompiler) {
        expected.add("alwaysCompletesAbruptly.0");
    }
    assertEquals(expected, tags);
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) Source(org.jacoco.core.test.validation.Source) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HashSet (java.util.HashSet)1 Source (org.jacoco.core.test.validation.Source)1 Test (org.junit.Test)1 ClassReader (org.objectweb.asm.ClassReader)1 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)1 ClassNode (org.objectweb.asm.tree.ClassNode)1 MethodNode (org.objectweb.asm.tree.MethodNode)1