1 package test.net.sourceforge.pmd.rules;
2
3 import net.sourceforge.pmd.PMD;
4 import net.sourceforge.pmd.Report;
5 import net.sourceforge.pmd.RuleViolation;
6 import net.sourceforge.pmd.rules.CyclomaticComplexityRule;
7
8 import java.util.Iterator;
9
10 public class CyclomaticComplexityRuleTest extends RuleTst {
11
12 private CyclomaticComplexityRule rule = new CyclomaticComplexityRule();
13
14 public void setUp() {
15 rule.setMessage("The {0} ''{1}'' has a Cyclomatic Complexity of {2}.");
16 }
17
18 public void testOneMethod() throws Throwable {
19 rule.addProperty("reportLevel", "1");
20 Report report = new Report();
21 runTestFromString(TEST1, rule, report);
22 Iterator i = report.iterator();
23 RuleViolation rv = (RuleViolation) i.next();
24 assertTrue(rv.getDescription().indexOf("Highest = 1") != -1);
25 }
26
27 public void testNastyComplicatedMethod() throws Throwable {
28 rule.addProperty("reportLevel", "10");
29 Report report = new Report();
30 runTestFromString(TEST2, rule, report);
31 Iterator i = report.iterator();
32 RuleViolation rv = (RuleViolation) i.next();
33 assertTrue(rv.getDescription().indexOf("Highest = 12") != -1);
34 }
35
36 public void testConstructor() throws Throwable {
37 rule.addProperty("reportLevel", "1");
38 Report report = new Report();
39 runTestFromString(TEST3, rule, report);
40 Iterator i = report.iterator();
41 RuleViolation rv = (RuleViolation) i.next();
42 assertTrue(rv.getDescription().indexOf("Highest = 1") != -1);
43 }
44
45 public void testLessComplicatedThanReportLevel() throws Throwable {
46 rule.addProperty("reportLevel", "10");
47 Report report = new Report();
48 runTestFromString(TEST1, rule, report);
49 assertEquals(0, report.size());
50 }
51
52 private static final String TEST1 =
53 "public class CyclomaticComplexity1 {" + PMD.EOL +
54 " public void foo() {}" + PMD.EOL +
55 "}";
56
57 private static final String TEST2 =
58 "public class CyclomaticComplexity2 {" + PMD.EOL +
59 " public void example() {" + PMD.EOL +
60 " int x = 0;" + PMD.EOL +
61 " int a = 0;" + PMD.EOL +
62 " int b = 0;" + PMD.EOL +
63 " int c = 0;" + PMD.EOL +
64 " int d = 0;" + PMD.EOL +
65 " int a1 = 0;" + PMD.EOL +
66 " int a2 = 0;" + PMD.EOL +
67 " int b1 = 0;" + PMD.EOL +
68 " int b2 = 0;" + PMD.EOL +
69 " int z = 0;" + PMD.EOL +
70 " int h = 0;" + PMD.EOL +
71 " int e = 0;" + PMD.EOL +
72 " int f = 0;" + PMD.EOL +
73 "" + PMD.EOL +
74 " if (a == b) {" + PMD.EOL +
75 " if (a1 == b1) {" + PMD.EOL +
76 " x=2;" + PMD.EOL +
77 " } else if (a2 == b2) {" + PMD.EOL +
78 " x=2;" + PMD.EOL +
79 " }" + PMD.EOL +
80 " else" + PMD.EOL +
81 " {" + PMD.EOL +
82 " x=2;" + PMD.EOL +
83 " }" + PMD.EOL +
84 " }" + PMD.EOL +
85 " else if (c == d)" + PMD.EOL +
86 " {" + PMD.EOL +
87 " while (c == d)" + PMD.EOL +
88 " {" + PMD.EOL +
89 " x=2;" + PMD.EOL +
90 " }" + PMD.EOL +
91 " }" + PMD.EOL +
92 " else if (e == f)" + PMD.EOL +
93 " {" + PMD.EOL +
94 " for (int n = 0; n < h; n++)" + PMD.EOL +
95 " {" + PMD.EOL +
96 " x=2;" + PMD.EOL +
97 " }" + PMD.EOL +
98 " }" + PMD.EOL +
99 " else" + PMD.EOL +
100 " {" + PMD.EOL +
101 " switch (z)" + PMD.EOL +
102 " {" + PMD.EOL +
103 " case 1:" + PMD.EOL +
104 " x=2;" + PMD.EOL +
105 " break;" + PMD.EOL +
106 "" + PMD.EOL +
107 " case 2:" + PMD.EOL +
108 " x=2;" + PMD.EOL +
109 " break;" + PMD.EOL +
110 "" + PMD.EOL +
111 " case 3:" + PMD.EOL +
112 " x=2;" + PMD.EOL +
113 " break;" + PMD.EOL +
114 "" + PMD.EOL +
115 " default:" + PMD.EOL +
116 " x=2;" + PMD.EOL +
117 " break;" + PMD.EOL +
118 " }" + PMD.EOL +
119 " }" + PMD.EOL +
120 " }" + PMD.EOL +
121 "}";
122
123 private static final String TEST3 =
124 "public class CyclomaticComplexity3 {" + PMD.EOL +
125 " public CyclomaticComplexity3() {}" + PMD.EOL +
126 "}";
127
128 }
This page was automatically generated by Maven