Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
384 additions
and
4 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/AssertFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.analysis.filter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.jacoco.core.internal.instr.InstrSupport; | ||
import org.junit.Test; | ||
import org.objectweb.asm.Label; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
public class AssertFilterTest implements IFilterOutput { | ||
|
||
private final IFilter filter = new AssertFilter(); | ||
|
||
private AbstractInsnNode fromInclusive; | ||
private AbstractInsnNode toInclusive; | ||
|
||
@Test | ||
public void should_filter_initialize() { | ||
final Label disable = new Label(); | ||
final Label init = new Label(); | ||
|
||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"<clinit>", "()V", null, null); | ||
m.visitLdcInsn(Type.getObjectType("p/C")); | ||
final AbstractInsnNode fromInclusive = m.instructions.getLast(); | ||
|
||
m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", | ||
"desiredAssertionStatus", "()Z", false); | ||
m.visitJumpInsn(Opcodes.IFNE, disable); | ||
m.visitInsn(Opcodes.ICONST_1); | ||
m.visitJumpInsn(Opcodes.GOTO, init); | ||
|
||
m.visitLabel(disable); | ||
m.visitInsn(Opcodes.ICONST_0); | ||
|
||
m.visitLabel(init); | ||
m.visitFieldInsn(Opcodes.PUTSTATIC, "p/C", "$assertionsDisabled", "Z"); | ||
final AbstractInsnNode toInclusive = m.instructions.getLast(); | ||
|
||
filter.filter("p/C", "java/lang/Object", m, this); | ||
assertEquals(fromInclusive, this.fromInclusive); | ||
assertEquals(toInclusive, this.toInclusive); | ||
} | ||
|
||
@Test | ||
public void should_filter_assert() { | ||
final Label disabled = new Label(); | ||
|
||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"m", "(Z)V", null, null); | ||
m.visitFieldInsn(Opcodes.GETSTATIC, "p/C", "$assertionsDisabled", "Z"); | ||
final AbstractInsnNode fromInclusive = m.instructions.getLast(); | ||
|
||
m.visitJumpInsn(Opcodes.IFNE, disabled); | ||
final AbstractInsnNode toInclusive = m.instructions.getLast(); | ||
|
||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IFNE, disabled); | ||
m.visitTypeInsn(Opcodes.NEW, "java/lang/AssertionError"); | ||
m.visitInsn(Opcodes.DUP); | ||
m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/AssertionError", | ||
"<init>", "()V", false); | ||
m.visitInsn(Opcodes.ATHROW); | ||
m.visitLabel(disabled); | ||
|
||
filter.filter("p/C", "java/lang/Object", m, this); | ||
assertEquals(fromInclusive, this.fromInclusive); | ||
assertEquals(toInclusive, this.toInclusive); | ||
} | ||
|
||
@Test | ||
public void should_filter_assert_message() { | ||
final Label disabled = new Label(); | ||
|
||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"m", "(Z)V", null, null); | ||
m.visitFieldInsn(Opcodes.GETSTATIC, "p/C", "$assertionsDisabled", "Z"); | ||
final AbstractInsnNode fromInclusive = m.instructions.getLast(); | ||
|
||
m.visitJumpInsn(Opcodes.IFNE, disabled); | ||
final AbstractInsnNode toInclusive = m.instructions.getLast(); | ||
|
||
m.visitVarInsn(Opcodes.ILOAD, 1); | ||
m.visitJumpInsn(Opcodes.IFNE, disabled); | ||
m.visitTypeInsn(Opcodes.NEW, "java/lang/AssertionError"); | ||
m.visitInsn(Opcodes.DUP); | ||
m.visitLdcInsn("m"); | ||
m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/AssertionError", | ||
"<init>", "(Ljava/lang/Object;)V", false); | ||
m.visitInsn(Opcodes.ATHROW); | ||
m.visitLabel(disabled); | ||
|
||
filter.filter("p/C", "java/lang/Object", m, this); | ||
assertEquals(fromInclusive, this.fromInclusive); | ||
assertEquals(toInclusive, this.toInclusive); | ||
} | ||
|
||
public void ignore(final AbstractInsnNode fromInclusive, | ||
final AbstractInsnNode toInclusive) { | ||
assertNull(this.fromInclusive); | ||
this.fromInclusive = fromInclusive; | ||
this.toInclusive = toInclusive; | ||
} | ||
|
||
public void merge(final AbstractInsnNode i1, final AbstractInsnNode i2) { | ||
fail(); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
org.jacoco.core.test/src/org/jacoco/core/test/filter/AssertTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.test.filter; | ||
|
||
import org.jacoco.core.analysis.ICounter; | ||
import org.jacoco.core.test.filter.targets.Assert; | ||
import org.jacoco.core.test.validation.ValidationTestBase; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test of filtering of a bytecode that is generated for an assert statement. | ||
*/ | ||
public class AssertTest extends ValidationTestBase { | ||
|
||
public AssertTest() { | ||
super(Assert.class); | ||
} | ||
|
||
/** | ||
* {@link Assert} | ||
*/ | ||
@Test | ||
public void clinit() { | ||
assertMethod("<clinit>", ICounter.FULLY_COVERED); | ||
} | ||
|
||
/** | ||
* {@link Assert#simple()} | ||
*/ | ||
@Test | ||
public void simple() { | ||
assertLine("simple", ICounter.PARTLY_COVERED, 1, 1, 4, 2); | ||
} | ||
|
||
/** | ||
* {@link Assert#message()} | ||
*/ | ||
@Test | ||
public void message() { | ||
assertLine("message", ICounter.PARTLY_COVERED, 1, 1, 5, 2); | ||
} | ||
|
||
/** | ||
* {@link Assert.SimpleClinit} | ||
*/ | ||
@Test | ||
public void clinit_simple() { | ||
assertLine("clinit", ICounter.PARTLY_COVERED, 1, 1, 4, 2); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Assert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.test.filter.targets; | ||
|
||
public class Assert { | ||
|
||
public static boolean b = true; | ||
|
||
public static void simple() { | ||
assert b; // $line-simple$ | ||
} | ||
|
||
public static void message() { | ||
assert b : "m"; // $line-message$ | ||
} | ||
|
||
public static class SimpleClinit { | ||
static { | ||
assert b; // $line-clinit$ | ||
} | ||
|
||
public static void init() { | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
simple(); | ||
message(); | ||
SimpleClinit.init(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.