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 EmptyFinallyBlockRuleTest extends SimpleAggregatorTst {
13  
14      private Rule rule;
15  
16      public void setUp() throws RuleSetNotFoundException {
17          rule = findRule("basic", "EmptyFinallyBlock");
18      }
19  
20      public void testAll() {
21         runTests(new TestDescriptor[] {
22             new TestDescriptor(TEST1, "empty try/catch/finally", 1, rule),
23             new TestDescriptor(TEST2, "try/finally, no catch", 1, rule),
24             new TestDescriptor(TEST3, "finally block with contents", 0, rule),
25             new TestDescriptor(TEST4, "multiple catch blocks with finally", 1, rule),
26         });
27      }
28  
29      private static final String TEST1 =
30      "public class EmptyFinallyBlock1 {" + PMD.EOL +
31      "    public void foo() {" + PMD.EOL +
32      "       try {" + PMD.EOL +
33      "       } catch (Exception e) {} finally {}" + PMD.EOL +
34      "    }" + PMD.EOL +
35      "}";
36  
37      private static final String TEST2 =
38      "public class EmptyFinallyBlock2 {" + PMD.EOL +
39      "    public void foo() {" + PMD.EOL +
40      "       try {" + PMD.EOL +
41      "       } finally {}" + PMD.EOL +
42      "    }" + PMD.EOL +
43      "}";
44  
45      private static final String TEST3 =
46      "public class EmptyFinallyBlock3 {" + PMD.EOL +
47      "    public void foo() {" + PMD.EOL +
48      "       try {" + PMD.EOL +
49      "       } finally {int x =2;}" + PMD.EOL +
50      "    }" + PMD.EOL +
51      "}";
52  
53      private static final String TEST4 =
54      "public class EmptyFinallyBlock4 {" + PMD.EOL +
55      " public void foo() {" + PMD.EOL +
56      "  try {" + PMD.EOL +
57      "  } catch (IOException e ){" + PMD.EOL +
58      "  } catch (Exception e ) {" + PMD.EOL +
59      "  } catch (Throwable t ) {" + PMD.EOL +
60      "  } finally{" + PMD.EOL +
61      "  }" + PMD.EOL +
62      " }" + PMD.EOL +
63      "}";
64  
65  }