    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 test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    11  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    12
    13  public class ForLoopShouldBeWhileLoopRuleTest extends SimpleAggregatorTst {
    14
    15      private Rule rule;
    16
    17      public void setUp() throws RuleSetNotFoundException {
    18          rule = findRule("basic", "ForLoopShouldBeWhileLoop");
    19      }
    20
    21      public void testAll() {
    22          runTests(new TestDescriptor[]{
    23              new TestDescriptor(TEST1, "simple failure case", 1, rule),
    24              new TestDescriptor(TEST2, "ok", 0, rule),
    25              new TestDescriptor(TEST3, "for loop like this: for (;;) {} ", 0, rule),
    26          });
    27      }
    28
    29      public void testJDK15ForLoop() throws Throwable {
    30          Report rpt = new Report();
    31          runTestFromString15(TEST4, rule, rpt);
    32          assertEquals(0, rpt.size());
    33      }
    34
    35      private static final String TEST1 =
    36              "public class Foo {" + PMD.EOL +
    37              " void foo() {" + PMD.EOL +
    38              "  int x = 2;" + PMD.EOL +
    39              "  for (;x<5;) { " + PMD.EOL +
    40              "   x++;" + PMD.EOL +
    41              "  }" + PMD.EOL +
    42              " }" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST2 =
    46              "public class Foo {" + PMD.EOL +
    47              " void foo() {" + PMD.EOL +
    48              "  for (int x=2;x<5;) { " + PMD.EOL +
    49              "   x++;" + PMD.EOL +
    50              "  }" + PMD.EOL +
    51              " }" + PMD.EOL +
    52              "}";
    53
    54      private static final String TEST3 =
    55              "public class Foo {" + PMD.EOL +
    56              " void foo() {" + PMD.EOL +
    57              "  for (;;) {}" + PMD.EOL +
    58              " }" + PMD.EOL +
    59              "}";
    60
    61      private static final String TEST4 =
    62              "public class Foo {" + PMD.EOL +
    63              " void foo() {" + PMD.EOL +
    64              "  for (String x : mylist) {}" + PMD.EOL +
    65              " }" + PMD.EOL +
    66              "}";
    67
    68  }