1 /******************************************************************************* 2 * Copyright (c) 2009, 2018 Mountainminds GmbH & Co. KG and Contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Marc R. Hoffmann - initial API and implementation 10 * 11 *******************************************************************************/ 12 package org.jacoco.report.check; 13 14 import static org.junit.Assert.assertEquals; 15 16 import java.util.ArrayList; 17 import java.util.Collections; 18 import java.util.List; 19 20 import org.jacoco.core.analysis.IBundleCoverage; 21 import org.jacoco.core.analysis.IClassCoverage; 22 import org.jacoco.core.analysis.ICoverageNode; 23 import org.jacoco.core.analysis.ICoverageNode.ElementType; 24 import org.jacoco.core.analysis.IPackageCoverage; 25 import org.jacoco.core.analysis.ISourceFileCoverage; 26 import org.jacoco.core.internal.analysis.BundleCoverageImpl; 27 import org.jacoco.core.internal.analysis.ClassCoverageImpl; 28 import org.jacoco.core.internal.analysis.CounterImpl; 29 import org.jacoco.core.internal.analysis.MethodCoverageImpl; 30 import org.jacoco.core.internal.analysis.PackageCoverageImpl; 31 import org.jacoco.core.internal.analysis.SourceFileCoverageImpl; 32 import org.jacoco.report.ILanguageNames; 33 import org.jacoco.report.JavaNames; 34 import org.junit.Before; 35 import org.junit.Test; 36 37 /** 38 * Unit tests for {@link BundleChecker}. 39 */ 40 public class BundleCheckerTest implements IViolationsOutput { 41 42 private List<Rule> rules; 43 private ILanguageNames names; 44 private List<String> messages; 45 46 @Before 47 public void setup() { 48 rules = new ArrayList<Rule>(); 49 names = new JavaNames(); 50 messages = new ArrayList<String>(); 51 } 52 53 @Test 54 public void testBundleLimit() { 55 addRule(ElementType.BUNDLE); 56 final BundleChecker checker = new BundleChecker(rules, names, this); 57 checker.checkBundle(createBundle()); 58 assertMessage("Rule violated for bundle Test: instructions covered ratio is 0.50, but expected minimum is 0.75"); 59 } 60 61 @Test 62 public void testPackageLimit() { 63 addRule(ElementType.PACKAGE); 64 final BundleChecker checker = new BundleChecker(rules, names, this); 65 checker.checkBundle(createBundle()); 66 assertMessage("Rule violated for package org.jacoco.example: instructions covered ratio is 0.50, but expected minimum is 0.75"); 67 } 68 69 @Test 70 public void testSourceFileLimit() { 71 addRule(ElementType.SOURCEFILE); 72 final BundleChecker checker = new BundleChecker(rules, names, this); 73 checker.checkBundle(createBundle()); 74 assertMessage("Rule violated for source file org/jacoco/example/FooClass.java: instructions covered ratio is 0.50, but expected minimum is 0.75"); 75 } 76 77 @Test 78 public void testClassLimit() { 79 addRule(ElementType.CLASS); 80 final BundleChecker checker = new BundleChecker(rules, names, this); 81 checker.checkBundle(createBundle()); 82 assertMessage("Rule violated for class org.jacoco.example.FooClass: instructions covered ratio is 0.50, but expected minimum is 0.75"); 83 } 84 85 @Test 86 public void testMethodLimit() { 87 addRule(ElementType.METHOD); 88 final BundleChecker checker = new BundleChecker(rules, names, this); 89 checker.checkBundle(createBundle()); 90 assertMessage("Rule violated for method org.jacoco.example.FooClass.fooMethod(): instructions covered ratio is 0.50, but expected minimum is 0.75"); 91 } 92 93 @Test 94 public void testGroupLimitNotSupported() { 95 addRule(ElementType.GROUP); 96 final BundleChecker checker = new BundleChecker(rules, names, this); 97 checker.checkBundle(createBundle()); 98 assertEquals(Collections.emptyList(), messages); 99 } 100 101 @Test 102 public void testLimitOk() { 103 final Rule rule = new Rule(); 104 rule.setElement(ElementType.BUNDLE); 105 final Limit limit = rule.createLimit(); 106 limit.setMinimum("0.25"); 107 rules.add(rule); 108 final BundleChecker checker = new BundleChecker(rules, names, this); 109 checker.checkBundle(createBundle()); 110 assertEquals(Collections.emptyList(), messages); 111 } 112 113 @Test 114 public void testBundleNoMatch() { 115 addRule(ElementType.BUNDLE).setExcludes("*"); 116 final BundleChecker checker = new BundleChecker(rules, names, this); 117 checker.checkBundle(createBundle()); 118 assertEquals(Collections.emptyList(), messages); 119 } 120 121 private Rule addRule(ElementType elementType) { 122 final Rule rule = new Rule(); 123 rule.setElement(elementType); 124 final Limit limit = rule.createLimit(); 125 limit.setMinimum("0.75"); 126 rules.add(rule); 127 return rule; 128 } 129 130 private IBundleCoverage createBundle() { 131 final MethodCoverageImpl m = new MethodCoverageImpl("fooMethod", "()V", 132 null); 133 m.increment(CounterImpl.getInstance(5, 5), CounterImpl.COUNTER_0_0, 1); 134 m.incrementMethodCounter(); 135 136 final ClassCoverageImpl c = new ClassCoverageImpl( 137 "org/jacoco/example/FooClass", 1001, false); 138 c.setSourceFileName("FooClass.java"); 139 c.addMethod(m); 140 141 final SourceFileCoverageImpl s = new SourceFileCoverageImpl( 142 "FooClass.java", "org/jacoco/example"); 143 s.increment(c); 144 145 IPackageCoverage p = new PackageCoverageImpl("org/jacoco/example", 146 Collections.singleton((IClassCoverage) c), 147 Collections.singleton((ISourceFileCoverage) s)); 148 return new BundleCoverageImpl("Test", Collections.singleton(p)); 149 } 150 151 private void assertMessage(String expected) { 152 assertEquals(Collections.singletonList(expected), messages); 153 } 154 155 public void onViolation(ICoverageNode node, Rule rule, Limit limit, 156 String message) { 157 messages.add(message); 158 } 159 160 } 161