    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 UnnecessaryParenthesesTest extends SimpleAggregatorTst {
    9       private Rule rule;
    10
    11      public void setUp() throws Exception {
    12          rule = findRule("controversial", "UnnecessaryParentheses");
    13      }
    14
    15      public void testAll() {
    16          runTests(new TestDescriptor[]{
    17              new TestDescriptor(TEST1, "failure case, returning literal", 1, rule),
    18              new TestDescriptor(TEST2, "ok, complex expression", 0, rule),
    19              new TestDescriptor(TEST3, "bad, returning variable in parens", 1, rule),
    20              new TestDescriptor(TEST4, "ok, returning comparison", 0, rule),
    21          });
    22      }
    23
    24      private static final String TEST1 =
    25              " public class Foo {" + PMD.EOL +
    26              "  int bar() {" + PMD.EOL +
    27              "   return (2); " + PMD.EOL +
    28              "  }" + PMD.EOL +
    29              " }";
    30
    31      private static final String TEST2 =
    32              " public class Foo {" + PMD.EOL +
    33              "  int bar() {" + PMD.EOL +
    34              "   return (2+2); " + PMD.EOL +
    35              "  }" + PMD.EOL +
    36              " }";
    37
    38      private static final String TEST3 =
    39              " public class Foo {" + PMD.EOL +
    40              "  int bar(int y) {" + PMD.EOL +
    41              "   return (y); " + PMD.EOL +
    42              "  }" + PMD.EOL +
    43              " }";
    44
    45      private static final String TEST4 =
    46              " public class Foo {" + PMD.EOL +
    47              "  int bar(int y) {" + PMD.EOL +
    48              "   return (x=y); " + PMD.EOL +
    49              "  }" + PMD.EOL +
    50              " }";
    51
    52  }