    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 EmptyTryBlockRuleTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("basic", "EmptyTryBlock");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "bad", 1, rule),
    23              new TestDescriptor(TEST2, "bad", 1, rule),
    24              new TestDescriptor(TEST3, "ok", 0, rule)
    25          });
    26      }
    27
    28      private static final String TEST1 =
    29              "public class EmptyTryBlock1 {" + PMD.EOL +
    30              "       public void foo() {" + PMD.EOL +
    31              "               try {" + PMD.EOL +
    32              "               } catch (Exception e) {" + PMD.EOL +
    33              "                       e.printStackTrace();" + PMD.EOL +
    34              "               }" + PMD.EOL +
    35              "       }" + PMD.EOL +
    36              "}";
    37
    38      private static final String TEST2 =
    39              "public class EmptyTryBlock2 {" + PMD.EOL +
    40              "       public void foo() {" + PMD.EOL +
    41              "               try {" + PMD.EOL +
    42              "               } finally {" + PMD.EOL +
    43              "                       int x = 5;" + PMD.EOL +
    44              "               }" + PMD.EOL +
    45              "       }" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST3 =
    49              "public class EmptyTryBlock3 {" + PMD.EOL +
    50              "       public void foo() {" + PMD.EOL +
    51              "               try {" + PMD.EOL +
    52              "                       int f =2;" + PMD.EOL +
    53              "               } finally {" + PMD.EOL +
    54              "                       int x = 5;" + PMD.EOL +
    55              "               }" + PMD.EOL +
    56              "       }" + PMD.EOL +
    57              "}";
    58
    59  }