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 ReturnFromFinallyBlockTest extends SimpleAggregatorTst {
13
14 private Rule rule;
15
16 public void setUp() throws RuleSetNotFoundException {
17 rule = findRule("rulesets/basic.xml", "ReturnFromFinallyBlock");
18 }
19
20 public void testAll() {
21 runTests(new TestDescriptor[] {
22 new TestDescriptor(TEST1, "throw exception but return from finally", 1, rule),
23 new TestDescriptor(TEST2, "lots of returns", 1, rule),
24 new TestDescriptor(TEST3, "ok", 0, rule),
25 });
26 }
27 private static final String TEST1 =
28 "public class Foo {" + PMD.EOL +
29 " String bugga() {" + PMD.EOL +
30 " try {" + PMD.EOL +
31 " throw new Exception( \"My Exception\" );" + PMD.EOL +
32 " } catch (Exception e) {" + PMD.EOL +
33 " throw e;" + PMD.EOL +
34 " } finally {" + PMD.EOL +
35 " return \"A. O. K.\"; // Very bad." + PMD.EOL +
36 " }" + PMD.EOL +
37 " }" + PMD.EOL +
38 "}";
39
40 private static final String TEST2 =
41 "public class Foo {" + PMD.EOL +
42 " String getBar() {" + PMD.EOL +
43 " try {" + PMD.EOL +
44 " return \"buz\";" + PMD.EOL +
45 " } catch (Exception e) {" + PMD.EOL +
46 " return \"biz\";" + PMD.EOL +
47 " } finally {" + PMD.EOL +
48 " return \"fiddle!\"; // bad!" + PMD.EOL +
49 " }" + PMD.EOL +
50 " }" + PMD.EOL +
51 "}";
52
53 private static final String TEST3 =
54 "public class Foo {" + PMD.EOL +
55 " String getBar() {" + PMD.EOL +
56 " try {" + PMD.EOL +
57 " return \"buz\";" + PMD.EOL +
58 " } finally {" + PMD.EOL +
59 " }" + PMD.EOL +
60 " }" + PMD.EOL +
61 "}";
62
63 }