aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test.validation.java5/src/org/jacoco/core/test/validation/java5/FramesTest.java
blob: a8bd1bca5c4cb35953921b89854b057d1e1f1c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************************************
 * 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:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.core.test.validation.java5;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.internal.instr.InstrSupport;
import org.jacoco.core.runtime.IRuntime;
import org.jacoco.core.runtime.SystemPropertiesRuntime;
import org.jacoco.core.test.TargetLoader;
import org.jacoco.core.test.validation.java5.targets.BooleanExpressionsTarget;
import org.jacoco.core.test.validation.java5.targets.ClassInitializerTarget;
import org.jacoco.core.test.validation.java5.targets.ConstructorsTarget;
import org.jacoco.core.test.validation.java5.targets.ControlStructureBeforeSuperConstructorTarget;
import org.jacoco.core.test.validation.java5.targets.ControlStructuresTarget;
import org.jacoco.core.test.validation.java5.targets.ExceptionsTarget;
import org.jacoco.core.test.validation.java5.targets.ExplicitInitialFrameTarget;
import org.jacoco.core.test.validation.java5.targets.FieldInitializationInTwoConstructorsTarget;
import org.jacoco.core.test.validation.java5.targets.ImplicitFieldInitializationTarget;
import org.jacoco.core.test.validation.java5.targets.InterfaceClassInitializerTarget;
import org.jacoco.core.test.validation.java5.targets.StructuredLockingTarget;
import org.junit.Test;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.util.TraceClassVisitor;

/**
 * Tests whether stackmap frames are correctly adjusted.
 */
public class FramesTest {

	/**
	 * Stack sizes calculated for instrumented classes might be sometimes bigger
	 * than actually needed. This is an acceptable tradeoff in favor of keeping
	 * track of the actual stack sizes. For test assertions we need to replace
	 * max stack sizes with constant value.
	 */
	private static class MaxStackEliminator extends ClassVisitor {
		public MaxStackEliminator(ClassVisitor cv) {
			super(InstrSupport.ASM_API_VERSION, cv);
		}

		@Override
		public MethodVisitor visitMethod(int access, String name, String desc,
				String signature, String[] exceptions) {
			final MethodVisitor mv = super.visitMethod(access, name, desc,
					signature, exceptions);
			return new MethodVisitor(InstrSupport.ASM_API_VERSION, mv) {
				@Override
				public void visitMaxs(int maxStack, int maxLocals) {
					super.visitMaxs(-1, maxLocals);
				}
			};
		}
	}

	private void testFrames(Class<?> target) throws IOException {
		testFrames(TargetLoader.getClassDataAsBytes(target));
	}

	private void testFrames(byte[] source) throws IOException {
		IRuntime runtime = new SystemPropertiesRuntime();
		Instrumenter instrumenter = new Instrumenter(runtime);
		source = calculateFrames(source);
		byte[] actual = instrumenter.instrument(source, "TestTarget");
		byte[] expected = calculateFrames(actual);

		assertEquals(dump(expected), dump(actual));
	}

	private byte[] calculateFrames(byte[] source) {
		ClassReader rc = new ClassReader(source);
		ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

		// Adjust Version to 1.6 to enable frames:
		rc.accept(new ClassVisitor(InstrSupport.ASM_API_VERSION, cw) {

			@Override
			public void visit(int version, int access, String name,
					String signature, String superName, String[] interfaces) {
				super.visit(Opcodes.V1_6, access, name, signature, superName,
						interfaces);
			}
		}, 0);
		return cw.toByteArray();
	}

	private String dump(byte[] bytes) {
		final StringWriter buffer = new StringWriter();
		final PrintWriter writer = new PrintWriter(buffer);
		new ClassReader(bytes).accept(
				new MaxStackEliminator(new TraceClassVisitor(writer)),
				ClassReader.EXPAND_FRAMES);
		return buffer.toString();
	}

	@Test
	public void boolean_expressions() throws IOException {
		testFrames(BooleanExpressionsTarget.class);
	}

	@Test
	public void class_initializer() throws IOException {
		testFrames(ClassInitializerTarget.class);
	}

	@Test
	public void constructors() throws IOException {
		testFrames(ConstructorsTarget.class);
	}

	@Test
	public void control_structures() throws IOException {
		testFrames(ControlStructuresTarget.class);
	}

	@Test
	public void control_structure_before_super_constructor()
			throws IOException {
		testFrames(ControlStructureBeforeSuperConstructorTarget.class);
	}

	@Test
	public void exceptions() throws IOException {
		testFrames(ExceptionsTarget.class);
	}

	@Test
	public void explicit_initial_frame() throws IOException {
		testFrames(ExplicitInitialFrameTarget.class);
	}

	@Test
	public void field_initialization_in_two_constructors() throws IOException {
		testFrames(FieldInitializationInTwoConstructorsTarget.class);
	}

	@Test
	public void implicit_field_initialization() throws IOException {
		testFrames(ImplicitFieldInitializationTarget.class);
	}

	@Test
	public void interface_class_initializer() throws IOException {
		testFrames(InterfaceClassInitializerTarget.class);
	}

	@Test
	public void structured_locking() throws IOException {
		testFrames(StructuredLockingTarget.class);
	}

}