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