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("basic", "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 28 private static final String TEST1 = 29 "public class Foo {" + PMD.EOL + 30 " String bugga() {" + PMD.EOL + 31 " try {" + PMD.EOL + 32 " throw new Exception( \"My Exception\" );" + PMD.EOL + 33 " } catch (Exception e) {" + PMD.EOL + 34 " throw e;" + PMD.EOL + 35 " } finally {" + PMD.EOL + 36 " return \"A. O. K.\"; // Very bad." + PMD.EOL + 37 " }" + PMD.EOL + 38 " }" + PMD.EOL + 39 "}"; 40 41 private static final String TEST2 = 42 "public class Foo {" + PMD.EOL + 43 " String getBar() {" + PMD.EOL + 44 " try {" + PMD.EOL + 45 " return \"buz\";" + PMD.EOL + 46 " } catch (Exception e) {" + PMD.EOL + 47 " return \"biz\";" + PMD.EOL + 48 " } finally {" + PMD.EOL + 49 " return \"fiddle!\"; // bad!" + PMD.EOL + 50 " }" + PMD.EOL + 51 " }" + PMD.EOL + 52 "}"; 53 54 private static final String TEST3 = 55 "public class Foo {" + PMD.EOL + 56 " String getBar() {" + PMD.EOL + 57 " try {" + PMD.EOL + 58 " return \"buz\";" + PMD.EOL + 59 " } finally {" + PMD.EOL + 60 " }" + PMD.EOL + 61 " }" + PMD.EOL + 62 "}"; 63 64 }