01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package test.net.sourceforge.pmd.rules.codesize;
04:
05: import static org.junit.Assert.assertEquals;
06: import static org.junit.Assert.assertTrue;
07: import net.sourceforge.pmd.Report;
08: import net.sourceforge.pmd.Rule;
09: import net.sourceforge.pmd.RuleViolation;
10:
11: import org.junit.Before;
12: import org.junit.Test;
13:
14: import test.net.sourceforge.pmd.testframework.RuleTst;
15: import test.net.sourceforge.pmd.testframework.TestDescriptor;
16:
17: import java.util.Iterator;
18:
19: public class CyclomaticComplexityTest extends RuleTst {
20: private Rule rule;
21: private TestDescriptor[] tests;
22:
23: @Before
24: 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(
69: CyclomaticComplexityTest.class);
70: }
71: }
|