    1   package test.net.sourceforge.pmd.rules.design;
    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 UnnecessaryLocalBeforeReturnRuleTest extends SimpleAggregatorTst {
    9
    10      private Rule rule;
    11
    12      public void setUp() {
    13          rule = findRule("design", "UnnecessaryLocalBeforeReturn");
    14      }
    15
    16      public void testAll() {
    17          runTests(new TestDescriptor[]{
    18              new TestDescriptor(TEST1, "skip void/native/abstract methods", 0, rule),
    19              new TestDescriptor(TEST2, "skip literal returns", 0, rule),
    20              new TestDescriptor(TEST3, "simple failure case", 1, rule),
    21              new TestDescriptor(TEST4, "skip complicated returns", 0, rule),
    22          });
    23      }
    24
    25      private static final String TEST1 =
    26              "public class Foo {" + PMD.EOL +
    27              " public void bar() {}" + PMD.EOL +
    28              " public native int buz();" + PMD.EOL +
    29              " public abstract int baz();" + PMD.EOL +
    30              "}";
    31
    32      private static final String TEST2 =
    33              "public class Foo {" + PMD.EOL +
    34              " public int bar() { return 5; }" + PMD.EOL +
    35              "}";
    36
    37      private static final String TEST3 =
    38              "public class Foo {" + PMD.EOL +
    39              " public int bar() {" + PMD.EOL +
    40              "  int x = doSomething();" + PMD.EOL +
    41              "  return x;" + PMD.EOL +
    42              " }" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST4 =
    46              "public class Foo {" + PMD.EOL +
    47              " public int bar() {" + PMD.EOL +
    48              "  int x = doSomething();" + PMD.EOL +
    49              "  return x == null ? foo : bar;" + PMD.EOL +
    50              " }" + PMD.EOL +
    51              "}";
    52
    53  }