    1   package test.net.sourceforge.pmd.rules;
    2
    3   import net.sourceforge.pmd.PMD;
    4   import net.sourceforge.pmd.Rule;
    5   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    6   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    7
    8   public class UselessOperationOnImmutableTest extends SimpleAggregatorTst {
    9       private Rule rule;
    10
    11      public void setUp() {
    12          rule = findRule("basic", "UselessOperationOnImmutable");
    13      }
    14
    15      // TODO - should use symbol table to catch all misuses of these types
    16      public void testAll() {
    17          runTests(new TestDescriptor[]{
    18              new TestDescriptor(TEST1, "useless operation on BigDecimal", 1, rule),
    19              new TestDescriptor(TEST2, "useless operation on BigInteger", 1, rule),
    20              new TestDescriptor(TEST3, "using the result, so OK", 0, rule),
    21              new TestDescriptor(TEST4, "using the result in a method call, so OK", 0, rule),
    22          });
    23      }
    24
    25      private static final String TEST1 =
    26              "public class Foo {" + PMD.EOL +
    27              " public void foo() {" + PMD.EOL +
    28              "  BigDecimal bd = new BigDecimal(5);" + PMD.EOL +
    29              "  bd.add(new BigDecimal(5));" + PMD.EOL +
    30              " }" + PMD.EOL +
    31              "}";
    32
    33      private static final String TEST2 =
    34              "public class Foo {" + PMD.EOL +
    35              " public void foo() {" + PMD.EOL +
    36              "  BigInteger bi = new BigInteger(5);" + PMD.EOL +
    37              "  bi.add(new BigInteger(5));" + PMD.EOL +
    38              " }" + PMD.EOL +
    39              "}";
    40
    41      private static final String TEST3 =
    42              "public class Foo {" + PMD.EOL +
    43              " public void foo() {" + PMD.EOL +
    44              "  BigInteger bi = new BigInteger(5);" + PMD.EOL +
    45              "  bi = bi.add(new BigInteger(5));" + PMD.EOL +
    46              " }" + PMD.EOL +
    47              "}";
    48
    49      private static final String TEST4 =
    50              "public class Foo {" + PMD.EOL +
    51              " public void foo() {" + PMD.EOL +
    52              "  BigInteger bi = new BigInteger(5);" + PMD.EOL +
    53              "  bar(bi.add(new BigInteger(5)));" + PMD.EOL +
    54              " }" + PMD.EOL +
    55              "}";
    56
    57  }