    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.Rule;
    8   import net.sourceforge.pmd.RuleSetNotFoundException;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class UnnecessaryReturnTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("basic", "UnnecessaryReturn");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "bad", 1, rule),
    23              new TestDescriptor(TEST2, "ok since method is not void", 0, rule),
    24              new TestDescriptor(TEST3, "ok since return is in sub block", 0, rule),
    25              new TestDescriptor(TEST4, "interface methods don't have return statements", 0, rule),
    26              new TestDescriptor(TEST5, "abstract methods don't have return statements", 0, rule),
    27          });
    28      }
    29
    30      private static final String TEST1 =
    31              "public class Foo {" + PMD.EOL +
    32              " void bar() {" + PMD.EOL +
    33              "  int y = 3;" + PMD.EOL +
    34              "  return;" + PMD.EOL +
    35              " }" + PMD.EOL +
    36              "}";
    37
    38      private static final String TEST2 =
    39              "public class Foo {" + PMD.EOL +
    40              " int bar() {" + PMD.EOL +
    41              "  return 2;" + PMD.EOL +
    42              " }" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST3 =
    46              "public class Foo {" + PMD.EOL +
    47              " void bar() {" + PMD.EOL +
    48              "  if (false) return;" + PMD.EOL +
    49              " }" + PMD.EOL +
    50              "}";
    51
    52      private static final String TEST4 =
    53              "public interface Foo {" + PMD.EOL +
    54              " void bar();" + PMD.EOL +
    55              "}";
    56
    57      private static final String TEST5 =
    58              "public class Foo {" + PMD.EOL +
    59              " abstract void bar();" + PMD.EOL +
    60              "}";
    61  }