1
2
3
4 package net.sourceforge.pmd.lang.java.rule.codesize;
5
6 import static org.junit.Assert.assertEquals;
7
8 import java.util.Iterator;
9
10 import net.sourceforge.pmd.Report;
11 import net.sourceforge.pmd.Rule;
12 import net.sourceforge.pmd.RuleViolation;
13 import net.sourceforge.pmd.testframework.RuleTst;
14 import net.sourceforge.pmd.testframework.TestDescriptor;
15
16 import org.junit.Before;
17 import org.junit.Test;
18
19
20 public class NPathComplexityTest extends RuleTst {
21 private Rule rule;
22 private TestDescriptor[] tests;
23
24 @Before
25 public void setUp() {
26 rule = findRule("java-codesize", "NPathComplexity");
27 tests = extractTestsFromXml(rule);
28 }
29
30 @Test
31 public void testViolationMessage() throws Throwable {
32 rule.setProperty(NPathComplexityRule.MINIMUM_DESCRIPTOR, 1.0);
33 Report report = new Report();
34 runTestFromString(tests[0].getCode(), rule, report);
35 Iterator<RuleViolation> i = report.iterator();
36 RuleViolation rv = (RuleViolation) i.next();
37 assertEquals("correct violation message", "The method bar() has an NPath complexity of 2", rv.getDescription());
38 }
39
40
41
42
43
44 @Test
45 public void testReturnValueComplexity() throws Exception {
46 rule.setProperty(NPathComplexityRule.MINIMUM_DESCRIPTOR, 25.0);
47 Report report = new Report();
48 runTestFromString(tests[2].getCode(), rule, report);
49 Iterator<RuleViolation> i = report.iterator();
50 String descriptions = "";
51 while (i.hasNext()) {
52 RuleViolation violation = i.next();
53 descriptions += violation.getDescription() + "\n";
54 }
55 assertEquals("expected violations", 2, report.size());
56 assertEquals("The method x() has an NPath complexity of 25\nThe method y() has an NPath complexity of 25\n",
57 descriptions);
58 }
59
60 public static junit.framework.Test suite() {
61 return new junit.framework.JUnit4TestAdapter(NPathComplexityTest.class);
62 }
63 }