    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.rules.IdempotentOperations;
    8   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    9   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    10
    11  public class IdempotentOperationsTest extends SimpleAggregatorTst {
    12
    13      public void testAll() {
    14          runTests(new TestDescriptor[]{
    15              new TestDescriptor(TEST1, "assignment of a variable (local or field) to itself", 1, new IdempotentOperations()),
    16              new TestDescriptor(TEST2, "assignment of one array element to another", 0, new IdempotentOperations()),
    17              new TestDescriptor(TEST3, "qualified names causing NPE troubleshooting", 0, new IdempotentOperations()),
    18              new TestDescriptor(TEST4, "check for method calls", 0, new IdempotentOperations()),
    19              new TestDescriptor(TEST5, "compound assignments are OK", 0, new IdempotentOperations())
    20          });
    21      }
    22
    23      private static final String TEST1 =
    24              "public class Foo {" + PMD.EOL +
    25              " private void bar() { " + PMD.EOL +
    26              "  x = x;" + PMD.EOL +
    27              " }" + PMD.EOL +
    28              "}";
    29
    30      private static final String TEST2 =
    31              "public class Foo {" + PMD.EOL +
    32              " private void bar() { " + PMD.EOL +
    33              "  int[] x = {2,3};" + PMD.EOL +
    34              "  x = x[1];" + PMD.EOL +
    35              " }" + PMD.EOL +
    36              "}";
    37
    38      private static final String TEST3 =
    39              "public class Foo {" + PMD.EOL +
    40              "  void bar() {this.x = foo;}" + PMD.EOL +
    41              "  void buz() {foo = this.x;}" + PMD.EOL +
    42              "}";
    43
    44      private static final String TEST4 =
    45              "public class Foo {" + PMD.EOL +
    46              "  void bar() {x = x();}" + PMD.EOL +
    47              "}";
    48
    49      private static final String TEST5 =
    50              "public class Foo {" + PMD.EOL +
    51              "  void bar() {x += x;}" + PMD.EOL +
    52              "}";
    53
    54  }