From c63563d1955934b5d4593c6d057351fc2dd008de Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Tue, 9 May 2017 19:45:34 +0200 Subject: Add filter for private empty constructors that do not have arguments (#529) --- .../PrivateEmptyNoArgConstructorFilterTest.java | 51 +++++++++++++++ .../jacoco/core/test/filter/ConstructorTest.java | 63 ++++++++++++++++++ .../org/jacoco/core/test/filter/SyntheticTest.java | 2 +- .../core/test/filter/targets/Constructor.java | 76 ++++++++++++++++++++++ .../jacoco/core/test/filter/targets/Synthetic.java | 6 ++ .../PrivateEmptyDefaultConstructorTest.java | 4 +- 6 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/PrivateEmptyNoArgConstructorFilterTest.java create mode 100644 org.jacoco.core.test/src/org/jacoco/core/test/filter/ConstructorTest.java create mode 100644 org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Constructor.java (limited to 'org.jacoco.core.test') diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/PrivateEmptyNoArgConstructorFilterTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/PrivateEmptyNoArgConstructorFilterTest.java new file mode 100644 index 00000000..90262081 --- /dev/null +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/PrivateEmptyNoArgConstructorFilterTest.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * 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: + * Evgeny Mandrikov - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.internal.analysis.filter; + +import org.jacoco.core.internal.instr.InstrSupport; +import org.junit.Test; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AbstractInsnNode; +import org.objectweb.asm.tree.MethodNode; + +import static org.junit.Assert.assertEquals; + +public class PrivateEmptyNoArgConstructorFilterTest implements IFilterOutput { + + private final IFilter filter = new PrivateEmptyNoArgConstructorFilter(); + + private AbstractInsnNode fromInclusive; + private AbstractInsnNode toInclusive; + + @Test + public void test() { + final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, + Opcodes.ACC_PRIVATE, "", "()V", null, null); + + m.visitVarInsn(Opcodes.ALOAD, 0); + m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", + "()V", false); + m.visitInsn(Opcodes.RETURN); + + filter.filter("Foo", "java/lang/Object", m, this); + + assertEquals(m.instructions.getFirst(), fromInclusive); + assertEquals(m.instructions.getLast(), toInclusive); + } + + public void ignore(final AbstractInsnNode fromInclusive, + final AbstractInsnNode toInclusive) { + this.fromInclusive = fromInclusive; + this.toInclusive = toInclusive; + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/filter/ConstructorTest.java b/org.jacoco.core.test/src/org/jacoco/core/test/filter/ConstructorTest.java new file mode 100644 index 00000000..a74d4671 --- /dev/null +++ b/org.jacoco.core.test/src/org/jacoco/core/test/filter/ConstructorTest.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * 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: + * Evgeny Mandrikov - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.filter; + +import org.jacoco.core.analysis.ICounter; +import org.jacoco.core.test.filter.targets.Constructor; +import org.jacoco.core.test.validation.ValidationTestBase; +import org.junit.Test; + +/** + * Test of filtering of a bytecode that is generated for a private empty + * constructors that do not have no arguments. + */ +public class ConstructorTest extends ValidationTestBase { + + public ConstructorTest() { + super(Constructor.class); + } + + @Test + public void testCoverageResult() { + // not filtered because not private: + assertLine("packageLocal", ICounter.FULLY_COVERED); + + // not filtered because has argument: + assertLine("arg", ICounter.FULLY_COVERED); + + // not filtered because not empty - prepares arguments for super + // constructor: + assertLine("super", ICounter.FULLY_COVERED); + + // not filtered because contains initialization of a field to hold + // reference to an instance of outer class that is passed as an + // argument: + assertLine("inner", ICounter.FULLY_COVERED); + + // not filtered because not empty - contains initialization of + // a field: + assertLine("innerStatic", ICounter.FULLY_COVERED); + + // not filtered because default constructor for not private inner + // classes is not private: + assertLine("publicDefault", ICounter.FULLY_COVERED); + assertLine("packageLocalDefault", ICounter.FULLY_COVERED); + + assertLine("privateDefault", ICounter.EMPTY); + + assertLine("privateNonEmptyNoArg", ICounter.FULLY_COVERED); + + assertLine("privateEmptyNoArg", ICounter.EMPTY); + assertLine("return", ICounter.EMPTY); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/filter/SyntheticTest.java b/org.jacoco.core.test/src/org/jacoco/core/test/filter/SyntheticTest.java index a0b54e0b..8f8d2538 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/filter/SyntheticTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/filter/SyntheticTest.java @@ -27,7 +27,7 @@ public class SyntheticTest extends ValidationTestBase { @Test public void testCoverageResult() { - assertMethodCount(6); + assertMethodCount(5); assertLine("classdef", ICounter.EMPTY); assertLine("field", ICounter.EMPTY); diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Constructor.java b/org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Constructor.java new file mode 100644 index 00000000..8623d2dd --- /dev/null +++ b/org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Constructor.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * 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: + * Evgeny Mandrikov - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.test.filter.targets; + +import static org.jacoco.core.test.validation.targets.Stubs.nop; + +/** + * This test target is a constructors. + */ +public class Constructor { + + Constructor() { // $line-packageLocal$ + } + + private Constructor(Object arg) { // $line-arg$ + } + + private static class Super extends Constructor { + private Super() { + super(null); // $line-super$ + } + } + + private class Inner { + private Inner() { // $line-inner$ + } + } + + private static class InnerStatic { + private Object field = this; + + private InnerStatic() { // $line-innerStatic$ + } + } + + public static class PublicDefault { // $line-publicDefault$ + } + + static class PackageLocalDefault { // $line-packageLocalDefault$ + } + + private static class PrivateDefault { // $line-privateDefault$ + } + + private static class PrivateNonEmptyNoArg { + private PrivateNonEmptyNoArg() { + nop(); // $line-privateNonEmptyNoArg$ + } + } + + private static class PrivateEmptyNoArg { + private PrivateEmptyNoArg() { // $line-privateEmptyNoArg$ + } // $line-return$ + } + + public static void main(String[] args) { + new Super(); + new Constructor().new Inner(); + new InnerStatic(); + new PublicDefault(); + new PackageLocalDefault(); + new PrivateDefault(); + new PrivateNonEmptyNoArg(); + new PrivateEmptyNoArg(); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Synthetic.java b/org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Synthetic.java index 783495d9..43826a8b 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Synthetic.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/filter/targets/Synthetic.java @@ -18,6 +18,12 @@ public class Synthetic { // $line-classdef$ private static int counter; // $line-field$ + /** + * {@link org.jacoco.core.test.validation.targets.Target06 Default + * constructor will refer to a line of class definition}, so that we define + * constructor explicitly in order to verify that we filter all other + * constructions here that might refer to line of class definition. + */ private Synthetic() { } diff --git a/org.jacoco.core.test/src/org/jacoco/core/test/validation/PrivateEmptyDefaultConstructorTest.java b/org.jacoco.core.test/src/org/jacoco/core/test/validation/PrivateEmptyDefaultConstructorTest.java index 410f1df2..ac9b9271 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/test/validation/PrivateEmptyDefaultConstructorTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/test/validation/PrivateEmptyDefaultConstructorTest.java @@ -30,8 +30,8 @@ public class PrivateEmptyDefaultConstructorTest extends ValidationTestBase { public void testCoverageResult() { assertLine("classdef", ICounter.EMPTY); - assertLine("super", ICounter.NOT_COVERED); - assertLine("constructor", ICounter.NOT_COVERED); + assertLine("super", ICounter.EMPTY); + assertLine("constructor", ICounter.EMPTY); } -- cgit v1.2.3