aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/internal/instr/ProbeVariableInserterTest.java
blob: 1de8facb7c1ee8a49cc70e2a977343f375164829 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*******************************************************************************
 * Copyright (c) 2009, 2011 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 java.util.Arrays;

import org.jacoco.core.internal.flow.LabelInfo;
import org.junit.Test;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.EmptyVisitor;
import org.objectweb.asm.tree.FrameNode;

/**
 * Unit tests for {@link ProbeVariableInserter}.
 */
public class ProbeVariableInserterTest {

	private final MethodVisitor delegate = new EmptyVisitor();

	private int var;

	@Test
	public void testVariableStatic() {
		ProbeVariableInserter i = new ProbeVariableInserter(Opcodes.ACC_STATIC,
				"()V", delegate);
		assertEquals(0, i.variable);
	}

	@Test
	public void testVariableNonStatic() {
		ProbeVariableInserter i = new ProbeVariableInserter(0, "()V", delegate);
		assertEquals(1, i.variable);
	}

	@Test
	public void testVariableNonStatic_IZObject() {
		ProbeVariableInserter i = new ProbeVariableInserter(0,
				"(IZLjava/lang/Object;)V", delegate);
		assertEquals(4, i.variable);
	}

	@Test
	public void testVariableNonStatic_JD() {
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(JD)V",
				delegate);
		assertEquals(5, i.variable);
	}

	@Test
	public void testVisitVarIns() {
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(II)V",
				new EmptyVisitor() {
					@Override
					public void visitVarInsn(final int opcode, final int var) {
						ProbeVariableInserterTest.this.var = var;
					}
				});
		// Argument variables stay at the same position:
		i.visitVarInsn(Opcodes.ALOAD, 0);
		assertEquals(0, var);
		i.visitVarInsn(Opcodes.ALOAD, 1);
		assertEquals(1, var);
		i.visitVarInsn(Opcodes.ALOAD, 2);
		assertEquals(2, var);

		assertEquals(3, i.variable);

		// Local variables are shifted by one:
		i.visitVarInsn(Opcodes.ALOAD, 3);
		assertEquals(4, var);
		i.visitVarInsn(Opcodes.ALOAD, 4);
		assertEquals(5, var);
	}

	@Test
	public void testVisitIincInsn() {
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(II)V",
				new EmptyVisitor() {
					@Override
					public void visitIincInsn(final int var, final int increment) {
						ProbeVariableInserterTest.this.var = var;
					}
				});
		// Argument variables stay at the same position:
		i.visitIincInsn(0, 999);
		assertEquals(0, var);
		i.visitIincInsn(1, 999);
		assertEquals(1, var);
		i.visitIincInsn(2, 999);
		assertEquals(2, var);

		assertEquals(3, i.variable);

		// Local variables are shifted by one:
		i.visitIincInsn(3, 999);
		assertEquals(4, var);
		i.visitIincInsn(4, 999);
		assertEquals(5, var);
	}

	@Test
	public void testVisitLocalVariable() {
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(II)V",
				new EmptyVisitor() {
					@Override
					public void visitLocalVariable(final String name,
							final String desc, final String signature,
							final Label start, final Label end, final int index) {
						ProbeVariableInserterTest.this.var = index;
					}
				});
		// Argument variables stay at the same position:
		i.visitLocalVariable(null, null, null, null, null, 0);
		assertEquals(0, var);
		i.visitLocalVariable(null, null, null, null, null, 1);
		assertEquals(1, var);
		i.visitLocalVariable(null, null, null, null, null, 2);
		assertEquals(2, var);

		assertEquals(3, i.variable);

		// Local variables are shifted by one:
		i.visitLocalVariable(null, null, null, null, null, 3);
		assertEquals(4, var);
		i.visitLocalVariable(null, null, null, null, null, 4);
		assertEquals(5, var);
	}

	@Test
	public void testVisitMaxs() {
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(II)V",
				new EmptyVisitor() {
					@Override
					public void visitMaxs(final int maxStack,
							final int maxLocals) {
						ProbeVariableInserterTest.this.var = maxLocals;
					}
				});
		i.visitMaxs(4, 8);
		assertEquals(9, var);
	}

	@Test
	public void testVisitFrame() {
		final FrameRecorder rec = new FrameRecorder();
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", rec);

		// The first (implicit) frame must not be modified:
		i.visitFrame(Opcodes.F_NEW, 2, new Object[] { "LFoo;", Opcodes.LONG },
				0, new Object[0]);
		assertEquals(Arrays.asList((Object) "LFoo;", Opcodes.LONG),
				rec.frame.local);

		// Starting from the second frame on the probe variable is inserted:
		i.visitFrame(Opcodes.F_NEW, 3, new Object[] { "LFoo;", Opcodes.LONG,
				"Ljava/lang/String;" }, 0, new Object[0]);
		assertEquals(Arrays.asList((Object) "LFoo;", Opcodes.LONG, "[Z",
				"Ljava/lang/String;"), rec.frame.local);
	}

	@Test(expected = IllegalStateException.class)
	public void testVisitFrameNegative() {
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", delegate);
		i.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
	}

	@Test
	public void testInsertProbeFrame() {
		final FrameRecorder rec = new FrameRecorder();
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", rec);

		// The first (implicit) frame:
		i.visitFrame(Opcodes.F_NEW, 2, new Object[] { "LFoo;", Opcodes.LONG },
				0, new Object[0]);

		// There must be a label which is a multi-jump target:
		Label label = new Label();
		LabelInfo.setTarget(label);
		LabelInfo.setTarget(label);
		i.visitLabel(label);

		// Insert a frame for this label:
		i.visitFrame(Opcodes.F_NEW, 3, new Object[] { "LFoo;", Opcodes.LONG,
				"Ljava/lang/String;" }, 0, new Object[0]);

		// Insert this frame again:
		rec.frame = null;
		i.insertProbeFrame(label);

		assertEquals(Arrays.asList((Object) "LFoo;", Opcodes.LONG, "[Z",
				"Ljava/lang/String;"), rec.frame.local);
	}

	@Test
	public void testInsertProbeFrameNoFrameForLabel() {
		final FrameRecorder rec = new FrameRecorder();
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", rec);

		// The first (implicit) frame:
		i.visitFrame(Opcodes.F_NEW, 2, new Object[] { "LFoo;", Opcodes.LONG },
				0, new Object[0]);

		// There must be a label which is a multi-jump target:
		Label label = new Label();
		LabelInfo.setTarget(label);
		LabelInfo.setTarget(label);
		i.visitLabel(label);

		// Insert a frame for this label:
		i.visitFrame(Opcodes.F_NEW, 3, new Object[] { "LFoo;", Opcodes.LONG,
				"Ljava/lang/String;" }, 0, new Object[0]);

		// Try to insert a frame for a different label:
		rec.frame = null;
		i.insertProbeFrame(new Label());
		assertNull(rec.frame);
	}

	@Test
	public void testInsertProbeFrameNoMultiTarget() {
		final FrameRecorder rec = new FrameRecorder();
		ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", rec);

		// The first (implicit) frame:
		i.visitFrame(Opcodes.F_NEW, 2, new Object[] { "LFoo;", Opcodes.LONG },
				0, new Object[0]);

		// The frame for this label isn't required:
		Label label = new Label();
		i.visitLabel(label);

		// Insert a frame for this label:
		i.visitFrame(Opcodes.F_NEW, 3, new Object[] { "LFoo;", Opcodes.LONG,
				"Ljava/lang/String;" }, 0, new Object[0]);

		// Inserting a frame for the label shouldn't work:
		rec.frame = null;
		i.insertProbeFrame(label);
		assertNull(rec.frame);
	}

	private static class FrameRecorder extends EmptyVisitor {

		FrameNode frame;

		@Override
		public void visitFrame(final int type, final int nLocal,
				final Object[] local, final int nStack, final Object[] stack) {
			assertEquals(Opcodes.F_NEW, type);
			this.frame = new FrameNode(type, nLocal, local, nStack, stack);
		}
	}

}