diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2018-10-03 21:16:27 +0200 |
---|---|---|
committer | Evgeny Mandrikov <Godin@users.noreply.github.com> | 2018-10-03 21:16:27 +0200 |
commit | 01b961b755bd47164739896d2d9c0960e882fd40 (patch) | |
tree | d0e9a32f95837032d3c14a326a84a76435be1a6e /org.jacoco.core.test/src/org | |
parent | 9dfd348cc084a469e41623ee335aaaace97692e3 (diff) | |
download | jacoco-01b961b755bd47164739896d2d9c0960e882fd40.tar.gz |
No need to modify class bytes for Java 10 support (#740)
Diffstat (limited to 'org.jacoco.core.test/src/org')
8 files changed, 22 insertions, 110 deletions
diff --git a/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java b/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java index d8828340..b52c6e32 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java @@ -11,6 +11,7 @@ *******************************************************************************/ package org.jacoco.core.analysis; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; @@ -36,7 +37,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.jacoco.core.data.ExecutionDataStore; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.internal.data.CRC64; import org.jacoco.core.test.TargetLoader; import org.junit.Before; @@ -90,12 +90,16 @@ public class AnalyzerTest { } @Test - public void should_analyze_java10_class() throws Exception { - final byte[] bytes = createClass(BytecodeVersion.V10); + public void should_not_modify_class_bytes_to_support_next_version() + throws Exception { + final byte[] originalBytes = createClass(Opcodes.V12); + final byte[] bytes = new byte[originalBytes.length]; + System.arraycopy(originalBytes, 0, bytes, 0, originalBytes.length); final long expectedClassId = CRC64.classId(bytes); analyzer.analyzeClass(bytes, ""); + assertArrayEquals(originalBytes, bytes); assertEquals(expectedClassId, classes.get("Foo").getId()); } diff --git a/org.jacoco.core.test/src/org/jacoco/core/instr/ClassFileVersionsTest.java b/org.jacoco.core.test/src/org/jacoco/core/instr/ClassFileVersionsTest.java index 38bd8083..338d85d3 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/instr/ClassFileVersionsTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/instr/ClassFileVersionsTest.java @@ -22,6 +22,7 @@ import static org.objectweb.asm.Opcodes.INVOKESPECIAL; import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL; import static org.objectweb.asm.Opcodes.POP; import static org.objectweb.asm.Opcodes.RETURN; +import static org.objectweb.asm.Opcodes.V10; import static org.objectweb.asm.Opcodes.V11; import static org.objectweb.asm.Opcodes.V12; import static org.objectweb.asm.Opcodes.V1_1; @@ -36,7 +37,6 @@ import static org.objectweb.asm.Opcodes.V9; import java.io.IOException; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.internal.instr.InstrSupport; import org.jacoco.core.runtime.IRuntime; import org.jacoco.core.runtime.SystemPropertiesRuntime; @@ -100,7 +100,7 @@ public class ClassFileVersionsTest { @Test public void test_10() throws IOException { - testVersion(BytecodeVersion.V10, true); + testVersion(V10, true); } @Test @@ -124,8 +124,6 @@ public class ClassFileVersionsTest { } private void assertFrames(byte[] source, final boolean expected) { - int version = BytecodeVersion.get(source); - source = BytecodeVersion.downgradeIfNeeded(version, source); new ClassReader(source) .accept(new ClassVisitor(InstrSupport.ASM_API_VERSION) { diff --git a/org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java b/org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java index 85882352..94828edf 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/instr/InstrumenterTest.java @@ -35,7 +35,6 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.jacoco.core.analysis.AnalyzerTest; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.internal.data.CRC64; import org.jacoco.core.internal.instr.InstrSupport; import org.jacoco.core.runtime.IExecutionDataAccessorGenerator; @@ -97,17 +96,16 @@ public class InstrumenterTest { } @Test - public void should_instrument_java10_class() throws Exception { - final byte[] originalBytes = createClass(BytecodeVersion.V10); + public void should_not_modify_class_bytes_to_support_next_version() + throws Exception { + final byte[] originalBytes = createClass(Opcodes.V12); final byte[] bytes = new byte[originalBytes.length]; System.arraycopy(originalBytes, 0, bytes, 0, originalBytes.length); final long expectedClassId = CRC64.classId(bytes); - final byte[] instrumentedBytes = instrumenter.instrument(bytes, ""); + instrumenter.instrument(bytes, ""); assertArrayEquals(originalBytes, bytes); - assertEquals(BytecodeVersion.V10, - BytecodeVersion.get(instrumentedBytes)); assertEquals(expectedClassId, accessorGenerator.classId); } diff --git a/org.jacoco.core.test/src/org/jacoco/core/instr/ResizeInstructionsTest.java b/org.jacoco.core.test/src/org/jacoco/core/instr/ResizeInstructionsTest.java index fb639ca3..02016445 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/instr/ResizeInstructionsTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/instr/ResizeInstructionsTest.java @@ -15,8 +15,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import org.jacoco.core.instr.Instrumenter; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.internal.instr.InstrSupport; import org.jacoco.core.runtime.IRuntime; import org.jacoco.core.runtime.RuntimeData; @@ -62,8 +60,6 @@ public class ResizeInstructionsTest { @Test public void should_not_loose_InnerClasses_attribute() throws Exception { byte[] source = TargetLoader.getClassDataAsBytes(Inner.class); - final int version = BytecodeVersion.get(source); - source = BytecodeVersion.downgradeIfNeeded(version, source); final ClassReader cr = new ClassReader(source); final ClassWriter cw = new ClassWriter(0); @@ -81,7 +77,6 @@ public class ResizeInstructionsTest { } }, 0); source = cw.toByteArray(); - BytecodeVersion.set(source, version); final byte[] bytes = instrumenter.instrument(source, ""); diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/BytecodeVersionTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/BytecodeVersionTest.java deleted file mode 100644 index 6bc4ca9e..00000000 --- a/org.jacoco.core.test/src/org/jacoco/core/internal/BytecodeVersionTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* - * 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: - * Evgeny Mandrikov - initial API and implementation - * - *******************************************************************************/ -package org.jacoco.core.internal; - -import org.junit.Test; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Opcodes; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; - -public class BytecodeVersionTest { - - @Test - public void should_get_and_set_major_version() { - final byte[] bytes = createClass(Opcodes.V1_1); - assertEquals(45, BytecodeVersion.get(bytes)); - - BytecodeVersion.set(bytes, Opcodes.V1_2); - assertEquals(46, BytecodeVersion.get(bytes)); - } - - @Test - public void should_return_original_when_not_java10() { - final byte[] originalBytes = createClass(Opcodes.V9); - - final byte[] bytes = BytecodeVersion.downgradeIfNeeded(Opcodes.V9, - originalBytes); - - assertSame(originalBytes, bytes); - } - - @Test - public void should_return_copy_when_java10() { - final byte[] originalBytes = createClass(BytecodeVersion.V10); - - final byte[] bytes = BytecodeVersion - .downgradeIfNeeded(BytecodeVersion.V10, originalBytes); - - assertNotSame(originalBytes, bytes); - assertEquals(Opcodes.V9, BytecodeVersion.get(bytes)); - assertEquals(BytecodeVersion.V10, BytecodeVersion.get(originalBytes)); - } - - private static byte[] createClass(final int version) { - final ClassWriter cw = new ClassWriter(0); - cw.visit(version, 0, "Foo", null, "java/lang/Object", null); - cw.visitEnd(); - return cw.toByteArray(); - } - -} diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/data/CRC64Test.java b/org.jacoco.core.test/src/org/jacoco/core/internal/data/CRC64Test.java index 39f7d508..ffb60801 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/internal/data/CRC64Test.java +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/data/CRC64Test.java @@ -16,7 +16,6 @@ import static org.junit.Assert.assertEquals; import java.io.UnsupportedEncodingException; import org.jacoco.core.data.ExecutionDataWriter; -import org.jacoco.core.internal.BytecodeVersion; import org.junit.Test; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; @@ -29,7 +28,7 @@ public class CRC64Test { @Test public void except_java_9_checksums_should_be_different_for_different_bytecode_versions() { assertEquals(0x589E9080A572741EL, - CRC64.classId(createClass(BytecodeVersion.V10))); + CRC64.classId(createClass(Opcodes.V10))); // should remove workaround for Java 9 // during change of exec file version diff --git a/org.jacoco.core.test/src/org/jacoco/core/internal/instr/InstrSupportTest.java b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/InstrSupportTest.java index c1b3044b..dda12bfd 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/internal/instr/InstrSupportTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/internal/instr/InstrSupportTest.java @@ -15,7 +15,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import org.jacoco.core.internal.BytecodeVersion; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -43,6 +42,13 @@ public class InstrSupportTest { } @Test + public void getVersionMajor_should_return_major_version_number() { + final byte[] bytes = new byte[] { (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, + (byte) 0xBE, /* minor */ 0x00, 0x03, /* major */ 0x00, 0x2D }; + assertEquals(45, InstrSupport.getVersionMajor(bytes)); + } + + @Test public void needFrames_should_return_false_for_versions_less_than_1_6() { assertFalse(InstrSupport.needsFrames(Opcodes.V1_1)); assertFalse(InstrSupport.needsFrames(Opcodes.V1_2)); @@ -57,7 +63,7 @@ public class InstrSupportTest { assertTrue(InstrSupport.needsFrames(Opcodes.V1_7)); assertTrue(InstrSupport.needsFrames(Opcodes.V1_8)); assertTrue(InstrSupport.needsFrames(Opcodes.V9)); - assertTrue(InstrSupport.needsFrames(BytecodeVersion.V10)); + assertTrue(InstrSupport.needsFrames(Opcodes.V10)); assertTrue(InstrSupport.needsFrames(Opcodes.V11)); assertTrue(InstrSupport.needsFrames(Opcodes.V12)); } diff --git a/org.jacoco.core.test/src/org/jacoco/core/runtime/ModifiedSystemClassRuntimeTest.java b/org.jacoco.core.test/src/org/jacoco/core/runtime/ModifiedSystemClassRuntimeTest.java index 03a58018..8a968840 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/runtime/ModifiedSystemClassRuntimeTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/runtime/ModifiedSystemClassRuntimeTest.java @@ -25,12 +25,8 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; -import org.jacoco.core.internal.BytecodeVersion; import org.jacoco.core.test.TargetLoader; import org.junit.Test; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.tree.ClassNode; /** * Unit tests for {@link ModifiedSystemClassRuntime}. @@ -49,28 +45,6 @@ public class ModifiedSystemClassRuntimeTest extends RuntimeTestBase { ModifiedSystemClassRuntime.createFor(inst, TARGET_CLASS_NAME); } - @Test - public void should_instrument_java10_class() { - final byte[] bytes = createClass(BytecodeVersion.V10); - - byte[] instrumented = ModifiedSystemClassRuntime.instrument(bytes, - "accessField"); - - assertEquals(BytecodeVersion.V10, BytecodeVersion.get(instrumented)); - instrumented = BytecodeVersion.downgradeIfNeeded(BytecodeVersion.V10, - instrumented); - final ClassNode classNode = new ClassNode(); - new ClassReader(instrumented).accept(classNode, 0); - assertEquals("accessField", classNode.fields.get(0).name); - } - - private static byte[] createClass(final int version) { - final ClassWriter cw = new ClassWriter(0); - cw.visit(version, 0, "Foo", null, "java/lang/Object", null); - cw.visitEnd(); - return cw.toByteArray(); - } - /** This static member emulate the instrumented system class. */ public static Object accessField; |