Home | History | Annotate | Download | only in check
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2015 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.io.IOException;
     17 import java.util.ArrayList;
     18 import java.util.Arrays;
     19 import java.util.List;
     20 
     21 import org.jacoco.core.analysis.ICounter.CounterValue;
     22 import org.jacoco.core.analysis.ICoverageNode;
     23 import org.jacoco.core.analysis.ICoverageNode.ElementType;
     24 import org.jacoco.report.ILanguageNames;
     25 import org.jacoco.report.ReportStructureTestDriver;
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 
     29 /**
     30  * Unit tests for {@link Limit}.
     31  */
     32 public class RulesCheckerTest implements IViolationsOutput {
     33 
     34 	private RulesChecker checker;
     35 	private ReportStructureTestDriver driver;
     36 	private List<String> messages;
     37 
     38 	@Before
     39 	public void setup() {
     40 		checker = new RulesChecker();
     41 		driver = new ReportStructureTestDriver();
     42 		messages = new ArrayList<String>();
     43 	}
     44 
     45 	@Test
     46 	public void testSetRules() throws IOException {
     47 		Rule rule = new Rule();
     48 		Limit limit = rule.createLimit();
     49 		limit.setValue(CounterValue.MISSEDCOUNT.name());
     50 		limit.setMaximum("5");
     51 		checker.setRules(Arrays.asList(rule));
     52 		driver.sendGroup(checker.createVisitor(this));
     53 		assertEquals(
     54 				Arrays.asList("Rule violated for bundle bundle: instructions missed count is 10, but expected maximum is 5"),
     55 				messages);
     56 	}
     57 
     58 	@Test
     59 	public void testSetLanguageNames() throws IOException {
     60 		Rule rule = new Rule();
     61 		rule.setElement(ElementType.CLASS);
     62 		Limit limit = rule.createLimit();
     63 		limit.setValue(CounterValue.MISSEDCOUNT.name());
     64 		limit.setMaximum("5");
     65 		checker.setRules(Arrays.asList(rule));
     66 
     67 		checker.setLanguageNames(new ILanguageNames() {
     68 			public String getQualifiedClassName(String vmname) {
     69 				return "MyClass";
     70 			}
     71 
     72 			public String getQualifiedMethodName(String vmclassname,
     73 					String vmmethodname, String vmdesc, String vmsignature) {
     74 				return null;
     75 			}
     76 
     77 			public String getPackageName(String vmname) {
     78 				return null;
     79 			}
     80 
     81 			public String getMethodName(String vmclassname,
     82 					String vmmethodname, String vmdesc, String vmsignature) {
     83 				return null;
     84 			}
     85 
     86 			public String getClassName(String vmname, String vmsignature,
     87 					String vmsuperclass, String[] vminterfaces) {
     88 				return null;
     89 			}
     90 		});
     91 
     92 		driver.sendGroup(checker.createVisitor(this));
     93 		assertEquals(
     94 				Arrays.asList("Rule violated for class MyClass: instructions missed count is 10, but expected maximum is 5"),
     95 				messages);
     96 	}
     97 
     98 	public void onViolation(ICoverageNode node, Rule rule, Limit limit,
     99 			String message) {
    100 		messages.add(message);
    101 	}
    102 
    103 }
    104