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