    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.design;
    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 OnlyOneReturnRuleTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("controversial", "OnlyOneReturn");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "two returns", 1, rule),
    23              new TestDescriptor(TEST2, "one", 0, rule),
    24              new TestDescriptor(TEST3, "none", 0, rule),
    25              new TestDescriptor(TEST4, "void", 0, rule),
    26              new TestDescriptor(TEST5, "finally", 0, rule),
    27              new TestDescriptor(TEST6, "return inside anonymous inner class", 0, rule),
    28          });
    29      }
    30
    31      private static final String TEST1 =
    32              "public class Foo {" + PMD.EOL +
    33              " public int foo(int x) {    " + PMD.EOL +
    34              "  if (x > 0) {" + PMD.EOL +
    35              "   return 3;" + PMD.EOL +
    36              "  }" + PMD.EOL +
    37              "  return 2;" + PMD.EOL +
    38              " }" + PMD.EOL +
    39              "}";
    40
    41
    42      private static final String TEST2 =
    43              "public class Foo {" + PMD.EOL +
    44              " public int foo(int x) {    " + PMD.EOL +
    45              "  return 2;" + PMD.EOL +
    46              " }" + PMD.EOL +
    47              "}";
    48
    49      private static final String TEST3 =
    50              "public class Foo {" + PMD.EOL +
    51              " public void foo(int x) {      " + PMD.EOL +
    52              "  int y =2;" + PMD.EOL +
    53              " }" + PMD.EOL +
    54              "}";
    55
    56      private static final String TEST4 =
    57              "public class Foo {" + PMD.EOL +
    58              " public void foo(int x) {      " + PMD.EOL +
    59              "  if (x>2) {" + PMD.EOL +
    60              "    return;" + PMD.EOL +
    61              "  }" + PMD.EOL +
    62              "  int y =2;" + PMD.EOL +
    63              " }" + PMD.EOL +
    64              "}";
    65
    66      private static final String TEST5 =
    67              "public class Foo {" + PMD.EOL +
    68              " public int foo(int x) {" + PMD.EOL +
    69              "  try {" + PMD.EOL +
    70              "   x += 2;" + PMD.EOL +
    71              "   return x;" + PMD.EOL +
    72              "  } finally {" + PMD.EOL +
    73              "    int y;" + PMD.EOL +
    74              "  }" + PMD.EOL +
    75              " }" + PMD.EOL +
    76              "}";
    77
    78      private static final String TEST6 =
    79              "public class Foo {" + PMD.EOL +
    80              " public int foo() {" + PMD.EOL +
    81              "  FileFilter f = new FileFilter() {" + PMD.EOL +
    82              "   public boolean accept(File file) {" + PMD.EOL +
    83              "    return false;" + PMD.EOL +
    84              "   }" + PMD.EOL +
    85              "  };" + PMD.EOL +
    86              "  return 2;" + PMD.EOL +
    87              " }" + PMD.EOL +
    88              "}";
    89  }