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