    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.Rule;
    8   import net.sourceforge.pmd.RuleSetNotFoundException;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class JumbledIncrementerRuleTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("basic", "JumbledIncrementer");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "1", 1, rule),
    23              new TestDescriptor(TEST2, "2", 0, rule),
    24              new TestDescriptor(TEST3, "3", 0, rule),
    25              new TestDescriptor(TEST4, "using outer loop incrementor as array index is OK", 0, rule),
    26          });
    27      }
    28
    29      private static final String TEST1 =
    30              "public class Foo {" + PMD.EOL +
    31              " void foo() { " + PMD.EOL +
    32              "  for (int i = 0; i < 10; i++) { " + PMD.EOL +
    33              "   for (int k = 0; k < 20; i++) { " + PMD.EOL +
    34              "    int x = 2; " + PMD.EOL +
    35              "   } " + PMD.EOL +
    36              "  } " + PMD.EOL +
    37              " } " + PMD.EOL +
    38              "}";
    39
    40      private static final String TEST2 =
    41              "public class Foo {" + PMD.EOL +
    42              " void foo() { " + PMD.EOL +
    43              "  for (int i = 0; i < 10; i++) { " + PMD.EOL +
    44              "   for (int k = 0; k < 20; k++) { " + PMD.EOL +
    45              "    int x = 2; " + PMD.EOL +
    46              "   } " + PMD.EOL +
    47              "  } " + PMD.EOL +
    48              " } " + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST3 =
    52              "public class Foo {" + PMD.EOL +
    53              " void foo() { " + PMD.EOL +
    54              "  for (int i=0; i<5; ) {" + PMD.EOL +
    55              "   i++;" + PMD.EOL +
    56              "  }" + PMD.EOL +
    57              "  for (int i=0;;) {" + PMD.EOL +
    58              "   if (i<5) {" + PMD.EOL +
    59              "    break;" + PMD.EOL +
    60              "   }" + PMD.EOL +
    61              "   i++;" + PMD.EOL +
    62              "  }" + PMD.EOL +
    63              "  for (;;) {" + PMD.EOL +
    64              "   int x =5;" + PMD.EOL +
    65              "  }" + PMD.EOL +
    66              "  for (int i=0; i<5;i++) ;" + PMD.EOL +
    67              "  for (int i=0; i<5;i++) " + PMD.EOL +
    68              "   foo();" + PMD.EOL +
    69              " } " + PMD.EOL +
    70              "}";
    71
    72      private static final String TEST4 =
    73              "public class Foo {" + PMD.EOL +
    74              " void foo() { " + PMD.EOL +
    75              "  for (int i = 0; i < 10; i++) { " + PMD.EOL +
    76              "   for (int k = 0; k < 20; j[i]++) { " + PMD.EOL +
    77              "    int x = 2; " + PMD.EOL +
    78              "   } " + PMD.EOL +
    79              "  } " + PMD.EOL +
    80              " } " + PMD.EOL +
    81              "}";
    82
    83  }