8000 Add filter for assert statement by bjkail · Pull Request #613 · jacoco/jacoco · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add filter for assert statement #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2009, 2018 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjkail I started working on getting this done - wanted to mention your contribution in our changelog and realized that you didn't wrote name in headers. So wondering if this was on purpose or just was forgotten? is there any IP/legal issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't recall, I probably copied from somewhere, cleared it out, and then just didn't bother to fill in my name :-). There are no IP/legal issues.

*
*******************************************************************************/
package org.jacoco.core.test.validation.java5;

import org.jacoco.core.analysis.ICounter;
import org.jacoco.core.test.validation.ValidationTestB 8000 ase;
import org.jacoco.core.test.validation.java5.targets.AssertTarget;
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(AssertTarget.class);
}

@Test
public void clinit() {
assertMethod("<clinit>", ICounter.FULLY_COVERED);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2009, 2018 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.validation.java5.targets;

public class AssertTarget {

public static boolean b = true;

public static void simple() {
assert b; // assertPartlyCovered(1, 1)
}

public static void message() {
assert b : "m"; // assertPartlyCovered(1, 1)
}

public static class SimpleClinit {
static {
assert b; // assertPartlyCovered(1, 1)
}

public static void init() {
}
}

public static void main(String[] args) {
simple();
message();
SimpleClinit.init();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*******************************************************************************
* Copyright (c) 2009, 2018 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 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 extends FilterTestBase {

private final IFilter filter = new AssertFilter();

@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("Foo"));
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, "Foo", "$assertionsDisabled", "Z");
final AbstractInsnNode toInclusive = m.instructions.getLast();

filter.filter(m, context, output);

assertIgnored(new Range(fromInclusive, 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, "Foo", "$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(m, context, output);

assertIgnored(new Range(fromInclusive, 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, "Foo", "$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(m, context, output);

assertIgnored(new Range(fromInclusive, toInclusive));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,38 @@ public String getComment() {

@Override
public String toString() {
return Source.this.coverage.getName() + ":" + nr;
return Source.this.sourceCoverage.getName() + ":" + nr;
}

}

private final List<Line> lines;

private final ISourceFileCoverage coverage;
private final IClassCoverage classCoverage;

private final ISourceFileCoverage sourceCoverage;

/**
* Reads a source file from the given reader.
*
* @param reader
* the reader to read from, will be closed
* @param coverage
* @param classCoverage
* coverage of the target type
* @param sourceCoverage
* corresponding coverage data
* @throws IOException
* if an I/O error occurs
*/
public Source(final Reader reader, ISourceFileCoverage coverage)
throws IOException {
public Source(final Reader reader, IClassCoverage classCoverage,
ISourceFileCoverage sourceCoverage) throws IOException {
this.lines = new ArrayList<Line>();
this.coverage = coverage;
this.classCoverage = classCoverage;
this.sourceCoverage = sourceCoverage;
final BufferedReader buffer = new BufferedReader(reader);
int nr = 1;
for (String l = buffer.readLine(); l != null; l = buffer.readLine()) {
lines.add(new Line(nr, l, coverage.getLine(nr)));
lines.add(new Line(nr, l, sourceCoverage.getLine(nr)));
nr++;
}
buffer.close();
Expand All @@ -120,10 +125,17 @@ public List<Line> getLines() {
}

/**
* @return the corresponding coverage node
* @return class coverage node of the target class
*/
public IClassCoverage getClassCoverage() {
return classCoverage;
}

/**
* @return the corresponding source coverage node
*/
public ISourceFileCoverage getCoverage() {
return coverage;
public ISourceFileCoverage getSourceCoverage() {
return sourceCoverage;
}

/**
Expand All @@ -144,7 +156,7 @@ public static Source load(Class<?> target, IBundleCoverage bundle)
final ISourceFileCoverage srcCov = findByName(pkgCov.getSourceFiles(),
clsCov.getSourceFileName());
return new Source(open(SRC_LOCATION + pkgCov.getName() + "/"
+ clsCov.getSourceFileName()), srcCov);
+ clsCov.getSourceFileName()), clsCov, srcCov);
}

private static <T extends ICoverageNode> T findByName(Collection<T> nodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.StringReader;
import java.util.List;

import org.jacoco.core.internal.analysis.ClassCoverageImpl;
import org.jacoco.core.internal.analysis.CounterImpl;
import org.jacoco.core.internal.analysis.SourceFileCoverageImpl;
import org.jacoco.core.test.validation.Source.Line;
Expand All @@ -33,7 +34,8 @@ public void should_parse_lines() throws IOException {
String src = "aaa\nbbb\n;";

final Source s = new Source(new StringReader(src),
new SourceFileCoverageImpl("Foo", "foo"));
new ClassCoverageImpl("Foo", 0x1234, false),
new SourceFileCoverageImpl("Foo.java", "foo"));

List<Line> lines = s.getLines();
assertEquals(3, lines.size());
Expand All @@ -47,7 +49,8 @@ public void should_parse_empty_lines() throws IOException {
String src = "\naaa\n\nbbb\n";

final Source s = new Source(new StringReader(src),
new SourceFileCoverageImpl("Foo", "foo"));
new ClassCoverageImpl("Foo", 0x1234, false),
new SourceFileCoverageImpl("Foo.java", "foo"));

List<Line> lines = s.getLines();
assertEquals(4, lines.size());
Expand All @@ -62,7 +65,8 @@ public void should_parse_crnl_separator() throws IOException {
String src = "aaa\r\nbbb";

final Source s = new Source(new StringReader(src),
new SourceFileCoverageImpl("Foo", "foo"));
new ClassCoverageImpl("Foo", 0x1234, false),
new SourceFileCoverageImpl("Foo.java", "foo"));

List<Line> lines = s.getLines();
assertEquals(2, lines.size());
Expand All @@ -75,7 +79,8 @@ public void should_calculate_line_numbers() throws IOException {
String src = "a\nb\nc";

final Source s = new Source(new StringReader(src),
new SourceFileCoverageImpl("Foo", "foo"));
new ClassCoverageImpl("Foo", 0x1234, false),
new SourceFileCoverageImpl("Foo.java", "foo"));

List<Line> lines = s.getLines();
assertEquals(3, lines.size());
Expand All @@ -89,24 +94,27 @@ public void line_should_implement_toString() throws IOException {
String src = "a\nb";

final Source s = new Source(new StringReader(src),
new SourceFileCoverageImpl("Foo", "foo"));
new ClassCoverageImpl("Foo", 0x1234, false),
new SourceFileCoverageImpl("Foo.java", "foo"));

List<Line> lines = s.getLines();
assertEquals(2, lines.size());
assertEquals("Foo:1", lines.get(0).toString());
assertEquals("Foo:2", lines.get(1).toString());
assertEquals("Foo.java:1", lines.get(0).toString());
assertEquals("Foo.java:2", lines.get(1).toString());
}

@Test
public void line_should_provide_corresponding_coverage()
throws IOException {
String src = "a\nb\nc";
SourceFileCoverageImpl sc = new SourceFileCoverageImpl("Foo", "foo");
SourceFileCoverageImpl sc = new SourceFileCoverageImpl("Foo.java",
"foo");
sc.increment(CounterImpl.getInstance(1, 0), CounterImpl.COUNTER_0_0, 1);
sc.increment(CounterImpl.getInstance(2, 0), CounterImpl.COUNTER_0_0, 2);
sc.increment(CounterImpl.getInstance(3, 0), CounterImpl.COUNTER_0_0, 3);

final Source s = new Source(new StringReader(src), sc);
final Source s = new Source(new StringReader(src),
new ClassCoverageImpl("Foo", 0x1234, false), sc);

List<Line> lines = s.getLines();
assertEquals(3, lines.size());
Expand All @@ -123,7 +131,8 @@ public void line_should_return_comment() throws IOException {
String src = "aaa\nbbb // test()\n}//nospaces()\n/* http://jacoco.org/ */";

final Source s = new Source(new StringReader(src),
new SourceFileCoverageImpl("Foo", "foo"));
new ClassCoverageImpl("Foo", 0x1234, false),
new SourceFileCoverageImpl("Foo.java", "foo"));

List<Line> lines = s.getLines();
assertEquals(4, lines.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.jacoco.core.test.validation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.lang.reflect.Method;
Expand All @@ -21,6 +22,7 @@
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.ICounter;
import org.jacoco.core.analysis.ILine;
import org.jacoco.core.analysis.IMethodCoverage;
import org.jacoco.core.data.ExecutionData;
import org.jacoco.core.data.ExecutionDataStore;
import org.jacoco.core.internal.analysis.CounterImpl;
Expand Down Expand Up @@ -69,6 +71,7 @@ public void setup() throws Exception {

private ExecutionDataStore execute() throws Exception {
loader = new InstrumentingLoader(target);
loader.setDefaultAssertionStatus(true);
run(loader.loadClass(target.getName()));
return loader.collect();
}
Expand Down Expand Up @@ -172,9 +175,21 @@ protected void assertLogEvents(String... events) throws Exception {
assertEquals("Log events", Arrays.asList(events), getter.invoke(null));
}

protected void assertMethod(final String name, final int status) {
for (IMethodCoverage method : source.getClassCoverage().getMethods()) {
if (method.getName().equals(name)) {
int methodStatus = method.getInstructionCounter().getStatus();
assertEquals("Instruction status in method " + name,
STATUS_NAME[status], STATUS_NAME[methodStatus]);
return;
}
}
fail("No method node for " + name);
}

protected void assertMethodCount(final int expectedTotal) {
assertEquals(expectedTotal,
source.getCoverage().getMethodCounter().getTotalCount());
source.getSourceCoverage().getMethodCounter().getTotalCount());
}

}
Loading
0