From 72793f84314a393b86c5dc344499888b7113d20b Mon Sep 17 00:00:00 2001 From: "Marc R. Hoffmann" Date: Thu, 1 May 2014 06:15:21 +0200 Subject: GitHub #201: Fixed failure with default methods in in Java 8 interfaces --- .../core/internal/instr/ClassInstrumenterTest.java | 20 +- .../instr/ProbeArrayStrategyFactoryTest.java | 229 +++++++++++++++++++++ .../core/internal/instr/ProbeCounterTest.java | 58 ++++++ .../core/internal/instr/ProbeInserterTest.java | 2 +- 4 files changed, 300 insertions(+), 9 deletions(-) create mode 100644 org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeArrayStrategyFactoryTest.java create mode 100644 org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeCounterTest.java (limited to 'org.jacoco.core.test/src/org/jacoco/core/internal/instr') diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ClassInstrumenterTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ClassInstrumenterTest.java index 374a7d54..87aab3bc 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ClassInstrumenterTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ClassInstrumenterTest.java @@ -14,8 +14,6 @@ package org.jacoco.core.internal.instr; import static org.junit.Assert.assertNull; import org.jacoco.core.JaCoCo; -import org.jacoco.core.runtime.IRuntime; -import org.jacoco.core.runtime.LoggerRuntime; import org.junit.Before; import org.junit.Test; import org.objectweb.asm.ClassVisitor; @@ -24,16 +22,13 @@ import org.objectweb.asm.MethodVisitor; /** * Unit tests for {@link ClassInstrumenter}. */ -public class ClassInstrumenterTest { - - private IRuntime runtime; +public class ClassInstrumenterTest implements IProbeArrayStrategy { private ClassInstrumenter instrumenter; @Before public void setup() { - runtime = new LoggerRuntime(); - instrumenter = new ClassInstrumenter(123, runtime, new ClassVisitor( + instrumenter = new ClassInstrumenter(this, new ClassVisitor( JaCoCo.ASM_API_VERSION) { }); } @@ -54,7 +49,7 @@ public class ClassInstrumenterTest { @Test public void testNoMethodVisitor() { - instrumenter = new ClassInstrumenter(123, runtime, new ClassVisitor( + instrumenter = new ClassInstrumenter(this, new ClassVisitor( JaCoCo.ASM_API_VERSION) { @Override public MethodVisitor visitMethod(int access, String name, @@ -65,4 +60,13 @@ public class ClassInstrumenterTest { assertNull(instrumenter.visitMethod(0, "foo", "()V", null, null)); } + // === IProbeArrayStrategy === + + public int storeInstance(MethodVisitor mv, int variable) { + return 0; + } + + public void addMembers(ClassVisitor cv, int probeCount) { + } + } diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeArrayStrategyFactoryTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeArrayStrategyFactoryTest.java new file mode 100644 index 00000000..a63b34f0 --- /dev/null +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeArrayStrategyFactoryTest.java @@ -0,0 +1,229 @@ +/******************************************************************************* + * Copyright (c) 2009, 2014 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: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.internal.instr; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.jacoco.core.runtime.IExecutionDataAccessorGenerator; +import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator; +import org.junit.Before; +import org.junit.Test; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.FieldVisitor; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +/** + * Unit tests for {@link ProbeArrayStrategyFactory} and the + * {@link IProbeArrayStrategy} implementations. The verifies the behaviour of + * the returned {@link IProbeArrayStrategy} instances for different classes. + */ +public class ProbeArrayStrategyFactoryTest { + + private IExecutionDataAccessorGenerator generator; + private ClassVisitorMock cv; + + @Before + public void setup() { + generator = new OfflineInstrumentationAccessGenerator(); + cv = new ClassVisitorMock(); + } + + @Test + public void testClass1() { + test(Opcodes.V1_1, 0, false, true); + assertDataField(InstrSupport.DATAFIELD_ACC); + assertInitMethod(false); + } + + @Test + public void testClass2() { + test(Opcodes.V1_2, 0, false, true); + assertDataField(InstrSupport.DATAFIELD_ACC); + assertInitMethod(false); + } + + @Test + public void testClass3() { + test(Opcodes.V1_3, 0, false, true); + assertDataField(InstrSupport.DATAFIELD_ACC); + assertInitMethod(false); + } + + @Test + public void testClass4() { + test(Opcodes.V1_4, 0, false, true); + assertDataField(InstrSupport.DATAFIELD_ACC); + assertInitMethod(false); + } + + @Test + public void testClass5() { + test(Opcodes.V1_5, 0, false, true); + assertDataField(InstrSupport.DATAFIELD_ACC); + assertInitMethod(false); + } + + @Test + public void testClass6() { + test(Opcodes.V1_6, 0, false, true); + assertDataField(InstrSupport.DATAFIELD_ACC); + assertInitMethod(true); + } + + @Test + public void testClass7() { + test(Opcodes.V1_7, 0, false, true); + assertDataField(InstrSupport.DATAFIELD_ACC); + assertInitMethod(true); + } + + @Test + public void testClass8() { + test(Opcodes.V1_8, 0, false, true); + assertDataField(InstrSupport.DATAFIELD_ACC); + assertInitMethod(true); + } + + @Test + public void testInterface7() { + test(Opcodes.V1_7, Opcodes.ACC_INTERFACE, true, false); + assertNoDataField(); + assertNoInitMethod(); + } + + @Test + public void testEmptyInterface7() { + test(Opcodes.V1_7, Opcodes.ACC_INTERFACE, false, false); + assertNoDataField(); + assertNoInitMethod(); + } + + @Test(expected = UnsupportedOperationException.class) + public void testEmptyInterface7StoreInstance() { + IProbeArrayStrategy strategy = test(Opcodes.V1_7, + Opcodes.ACC_INTERFACE, false, false); + strategy.storeInstance(null, 0); + } + + @Test + public void testInterface8() { + test(Opcodes.V1_8, Opcodes.ACC_INTERFACE, false, true); + assertDataField(InstrSupport.DATAFIELD_INTF_ACC); + assertInitMethod(true); + } + + @Test + public void testEmptyInterface8() { + test(Opcodes.V1_8, Opcodes.ACC_INTERFACE, false, false); + assertNoDataField(); + assertNoInitMethod(); + } + + @Test + public void testClinitInterface8() { + test(Opcodes.V1_8, Opcodes.ACC_INTERFACE, true, false); + assertNoDataField(); + assertNoInitMethod(); + } + + private IProbeArrayStrategy test(int version, int access, boolean clinit, + boolean method) { + ClassWriter writer = new ClassWriter(0); + writer.visit(version, access, "Foo", "java/lang/Object", null, null); + if (clinit) { + MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC + | Opcodes.ACC_STATIC, "", "()V", null, null); + mv.visitCode(); + mv.visitInsn(Opcodes.RETURN); + mv.visitMaxs(0, 0); + mv.visitEnd(); + } + if (method) { + MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC + | Opcodes.ACC_STATIC, "doit", "()V", null, null); + mv.visitCode(); + mv.visitInsn(Opcodes.RETURN); + mv.visitMaxs(0, 0); + mv.visitEnd(); + } + writer.visitEnd(); + + final IProbeArrayStrategy strategy = ProbeArrayStrategyFactory + .createFor(new ClassReader(writer.toByteArray()), generator); + + strategy.addMembers(cv, 123); + return strategy; + } + + private static class ClassVisitorMock extends ClassVisitor { + + private int fieldAccess; + private String fieldName; + + private int methodAccess; + private String methodName; + + private boolean frames; + + ClassVisitorMock() { + super(Opcodes.ASM5); + } + + @Override + public FieldVisitor visitField(int access, String name, String desc, + String signature, Object value) { + assertNull(fieldName); + fieldAccess = access; + fieldName = name; + return null; + } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, + String signature, String[] exceptions) { + assertNull(methodName); + methodAccess = access; + methodName = name; + return new MethodVisitor(Opcodes.ASM5) { + @Override + public void visitFrame(int type, int nLocal, Object[] local, + int nStack, Object[] stack) { + frames = true; + } + }; + } + } + + void assertDataField(int access) { + assertEquals(InstrSupport.DATAFIELD_NAME, cv.fieldName); + assertEquals(access, cv.fieldAccess); + } + + void assertNoDataField() { + assertNull(cv.fieldName); + } + + void assertInitMethod(boolean frames) { + assertEquals(InstrSupport.INITMETHOD_NAME, cv.methodName); + assertEquals(InstrSupport.INITMETHOD_ACC, cv.methodAccess); + assertEquals(Boolean.valueOf(frames), Boolean.valueOf(cv.frames)); + } + + void assertNoInitMethod() { + assertNull(cv.methodName); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeCounterTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeCounterTest.java new file mode 100644 index 00000000..9b16d3b4 --- /dev/null +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeCounterTest.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2009, 2014 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: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.core.internal.instr; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests for {@link ProbeCounter}. + */ +public class ProbeCounterTest { + + private ProbeCounter counter; + + @Before + public void setup() { + counter = new ProbeCounter(); + } + + @Test + public void testInitial() { + assertFalse(counter.hasMethods()); + assertEquals(0, counter.getCount()); + } + + @Test + public void testVisitTotalProbeCount() { + counter.visitTotalProbeCount(42); + assertEquals(42, counter.getCount()); + } + + @Test + public void testVisitClinitMethod() { + assertNull(counter.visitMethod(0, "", null, null, null)); + assertFalse(counter.hasMethods()); + } + + @Test + public void testVisitMethod() { + assertNull(counter.visitMethod(0, "foo", null, null, null)); + assertTrue(counter.hasMethods()); + } + +} diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeInserterTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeInserterTest.java index 47cd043c..44cde233 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeInserterTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeInserterTest.java @@ -45,7 +45,7 @@ public class ProbeInserterTest { return 5; } - public void addMembers(ClassVisitor delegate) { + public void addMembers(ClassVisitor delegate, int probeCount) { } }; } -- cgit v1.2.3