1 /**
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package test.net.sourceforge.pmd.rules.codesize;
5
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertTrue;
8 import net.sourceforge.pmd.Report;
9 import net.sourceforge.pmd.Rule;
10 import net.sourceforge.pmd.RuleViolation;
11
12 import org.junit.Before;
13 import org.junit.Test;
14
15 import test.net.sourceforge.pmd.testframework.RuleTst;
16 import test.net.sourceforge.pmd.testframework.TestDescriptor;
17
18 import java.util.Iterator;
19
20 public class CyclomaticComplexityTest extends RuleTst {
21 private Rule rule;
22 private TestDescriptor[] tests;
23
24 @Before public void setUp() {
25 rule = findRule("codesize", "CyclomaticComplexity");
26 tests = extractTestsFromXml(rule);
27 }
28
29 @Test
30 public void testOneMethod() throws Throwable {
31 rule.addProperty("reportLevel", "1");
32 Report report = new Report();
33 runTestFromString(tests[0].getCode(), rule, report);
34 Iterator i = report.iterator();
35 RuleViolation rv = (RuleViolation) i.next();
36 assertTrue(rv.getDescription().indexOf("Highest = 1") != -1);
37 }
38
39 @Test
40 public void testNastyComplicatedMethod() throws Throwable {
41 rule.addProperty("reportLevel", "10");
42 Report report = new Report();
43 runTestFromString(tests[1].getCode(), rule, report);
44 Iterator i = report.iterator();
45 RuleViolation rv = (RuleViolation) i.next();
46 assertTrue(rv.getDescription().indexOf("Highest = 11") != -1);
47 }
48
49 @Test
50 public void testConstructor() throws Throwable {
51 rule.addProperty("reportLevel", "1");
52 Report report = new Report();
53 runTestFromString(tests[2].getCode(), rule, report);
54 Iterator i = report.iterator();
55 RuleViolation rv = (RuleViolation) i.next();
56 assertTrue(rv.getDescription().indexOf("Highest = 1") != -1);
57 }
58
59 @Test
60 public void testLessComplicatedThanReportLevel() throws Throwable {
61 rule.addProperty("reportLevel", "10");
62 Report report = new Report();
63 runTestFromString(tests[0].getCode(), rule, report);
64 assertEquals(0, report.size());
65 }
66
67 public static junit.framework.Test suite() {
68 return new junit.framework.JUnit4TestAdapter(CyclomaticComplexityTest.class);
69 }
70 }