aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/test/validation/StatementExecutorTest.java
blob: 3aa3540fadde9d1846647827c587e5a4c134cab7 (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
/*******************************************************************************
 * Copyright (c) 2009, 2020 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.core.test.validation;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
 * Unit tests for {@link StatementExecutor}.
 */
public class StatementExecutorTest {

	@Rule
	public ExpectedException exception = ExpectedException.none();

	private Map<String, List<?>> invocations;

	@Before
	public void setup() {
		invocations = new HashMap<String, List<?>>();
	}

	@Test
	public void should_prefix_arguments() {
		StatementExecutor executor = new StatementExecutor(this, "Hello",
				"world");

		executor.visitInvocation("ctx", "target1", "!");

		assertEquals(Arrays.asList("Hello", "world", "!"),
				invocations.get("target1"));
	}

	@Test
	public void should_call_method_with_int_argument() {
		StatementExecutor executor = new StatementExecutor(this);

		executor.visitInvocation("ctx", "target2", Integer.valueOf(42));

		assertEquals(Arrays.asList(Integer.valueOf(42)),
				invocations.get("target2"));
	}

	@Test
	public void should_preserve_AssertionError() {
		exception.expect(AssertionError.class);
		exception.expectMessage("Original AssertionError.");
		StatementExecutor executor = new StatementExecutor(this);

		executor.visitInvocation("ctx", "target3");
	}

	@Test
	public void should_wrap_other_exceptions() {
		exception.expect(RuntimeException.class);
		exception.expectMessage("Invocation error (ctx)");
		StatementExecutor executor = new StatementExecutor(this);

		executor.visitInvocation("ctx", "target4");
	}

	@Test
	public void should_throw_RuntimeException_when_method_cannot_be_invoked() {
		exception.expect(RuntimeException.class);
		exception.expectMessage("Invocation error (ctx)");
		StatementExecutor executor = new StatementExecutor(this);

		executor.visitInvocation("ctx", "doesNotExist");
	}

	public void target1(String a, String b, String c) {
		invocations.put("target1", Arrays.asList(a, b, c));
	}

	public void target2(int i) {
		invocations.put("target2", Arrays.asList(Integer.valueOf(i)));
	}

	public void target3() {
		throw new AssertionError("Original AssertionError.");
	}

	public void target4() throws IOException {
		throw new IOException("Original IOException.");
	}

}