aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/check/BundleCheckerTest.java
blob: 2267271600346bdf894fa1f3f6109a43c515bfb1 (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
/*******************************************************************************
 * Copyright (c) 2009, 2021 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.report.check;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.ICoverageNode;
import org.jacoco.core.analysis.ICoverageNode.ElementType;
import org.jacoco.core.analysis.IPackageCoverage;
import org.jacoco.core.analysis.ISourceFileCoverage;
import org.jacoco.core.internal.analysis.BundleCoverageImpl;
import org.jacoco.core.internal.analysis.ClassCoverageImpl;
import org.jacoco.core.internal.analysis.CounterImpl;
import org.jacoco.core.internal.analysis.MethodCoverageImpl;
import org.jacoco.core.internal.analysis.PackageCoverageImpl;
import org.jacoco.core.internal.analysis.SourceFileCoverageImpl;
import org.jacoco.report.ILanguageNames;
import org.jacoco.report.JavaNames;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link BundleChecker}.
 */
public class BundleCheckerTest implements IViolationsOutput {

	private List<Rule> rules;
	private ILanguageNames names;
	private List<String> messages;

	@Before
	public void setup() {
		rules = new ArrayList<Rule>();
		names = new JavaNames();
		messages = new ArrayList<String>();
	}

	@Test
	public void testBundleLimit() {
		addRule(ElementType.BUNDLE);
		final BundleChecker checker = new BundleChecker(rules, names, this);
		checker.checkBundle(createBundle());
		assertMessage(
				"Rule violated for bundle Test: instructions covered ratio is 0.50, but expected minimum is 0.75");
	}

	@Test
	public void testPackageLimit() {
		addRule(ElementType.PACKAGE);
		final BundleChecker checker = new BundleChecker(rules, names, this);
		checker.checkBundle(createBundle());
		assertMessage(
				"Rule violated for package org.jacoco.example: instructions covered ratio is 0.50, but expected minimum is 0.75");
	}

	@Test
	public void testSourceFileLimit() {
		addRule(ElementType.SOURCEFILE);
		final BundleChecker checker = new BundleChecker(rules, names, this);
		checker.checkBundle(createBundle());
		assertMessage(
				"Rule violated for source file org/jacoco/example/FooClass.java: instructions covered ratio is 0.50, but expected minimum is 0.75");
	}

	@Test
	public void testClassLimit() {
		addRule(ElementType.CLASS);
		final BundleChecker checker = new BundleChecker(rules, names, this);
		checker.checkBundle(createBundle());
		assertMessage(
				"Rule violated for class org.jacoco.example.FooClass: instructions covered ratio is 0.50, but expected minimum is 0.75");
	}

	@Test
	public void testMethodLimit() {
		addRule(ElementType.METHOD);
		final BundleChecker checker = new BundleChecker(rules, names, this);
		checker.checkBundle(createBundle());
		assertMessage(
				"Rule violated for method org.jacoco.example.FooClass.fooMethod(): instructions covered ratio is 0.50, but expected minimum is 0.75");
	}

	@Test
	public void testGroupLimitNotSupported() {
		addRule(ElementType.GROUP);
		final BundleChecker checker = new BundleChecker(rules, names, this);
		checker.checkBundle(createBundle());
		assertEquals(Collections.emptyList(), messages);
	}

	@Test
	public void testLimitOk() {
		final Rule rule = new Rule();
		rule.setElement(ElementType.BUNDLE);
		final Limit limit = rule.createLimit();
		limit.setMinimum("0.25");
		rules.add(rule);
		final BundleChecker checker = new BundleChecker(rules, names, this);
		checker.checkBundle(createBundle());
		assertEquals(Collections.emptyList(), messages);
	}

	@Test
	public void testBundleNoMatch() {
		addRule(ElementType.BUNDLE).setExcludes("*");
		final BundleChecker checker = new BundleChecker(rules, names, this);
		checker.checkBundle(createBundle());
		assertEquals(Collections.emptyList(), messages);
	}

	private Rule addRule(ElementType elementType) {
		final Rule rule = new Rule();
		rule.setElement(elementType);
		final Limit limit = rule.createLimit();
		limit.setMinimum("0.75");
		rules.add(rule);
		return rule;
	}

	private IBundleCoverage createBundle() {
		final MethodCoverageImpl m = new MethodCoverageImpl("fooMethod", "()V",
				null);
		m.increment(CounterImpl.getInstance(5, 5), CounterImpl.COUNTER_0_0, 1);
		m.incrementMethodCounter();

		final ClassCoverageImpl c = new ClassCoverageImpl(
				"org/jacoco/example/FooClass", 1001, false);
		c.setSourceFileName("FooClass.java");
		c.addMethod(m);

		final SourceFileCoverageImpl s = new SourceFileCoverageImpl(
				"FooClass.java", "org/jacoco/example");
		s.increment(c);

		IPackageCoverage p = new PackageCoverageImpl("org/jacoco/example",
				Collections.singleton((IClassCoverage) c),
				Collections.singleton((ISourceFileCoverage) s));
		return new BundleCoverageImpl("Test", Collections.singleton(p));
	}

	private void assertMessage(String expected) {
		assertEquals(Collections.singletonList(expected), messages);
	}

	public void onViolation(ICoverageNode node, Rule rule, Limit limit,
			String message) {
		messages.add(message);
	}

}