View Javadoc
1 package test.net.sourceforge.pmd.rules; 2 3 import net.sourceforge.pmd.PMD; 4 import net.sourceforge.pmd.Rule; 5 import net.sourceforge.pmd.rules.XPathRule; 6 7 public class ReturnFromFinallyBlockTest extends SimpleAggregatorTst { 8 9 private Rule rule; 10 11 public void setUp() { 12 rule = new XPathRule(); 13 rule.addProperty("xpath", "//TryStatement[@Finally='true']/Block[position() = last()]//ReturnStatement"); 14 } 15 16 public void testAll() { 17 runTests(new TestDescriptor[] { 18 new TestDescriptor(TEST1, "throw exception but return from finally", 1, rule), 19 new TestDescriptor(TEST2, "lots of returns", 1, rule), 20 new TestDescriptor(TEST3, "ok", 0, rule), 21 }); 22 } 23 private static final String TEST1 = 24 "public class ReturnFromFinallyBlock1 {" + PMD.EOL + 25 " public String bugga() {" + PMD.EOL + 26 " try {" + PMD.EOL + 27 " throw new Exception( \"My Exception\" );" + PMD.EOL + 28 " } catch (Exception e) {" + PMD.EOL + 29 " throw e;" + PMD.EOL + 30 " } finally {" + PMD.EOL + 31 " return \"A. O. K.\"; // Very bad." + PMD.EOL + 32 " }" + PMD.EOL + 33 " }" + PMD.EOL + 34 "}"; 35 36 private static final String TEST2 = 37 "public class ReturnFromFinallyBlock2 {" + PMD.EOL + 38 " public String getBar() {" + PMD.EOL + 39 " try {" + PMD.EOL + 40 " return \"buz\";" + PMD.EOL + 41 " } catch (Exception e) {" + PMD.EOL + 42 " return \"biz\";" + PMD.EOL + 43 " } finally {" + PMD.EOL + 44 " return \"fiddle!\"; // bad!" + PMD.EOL + 45 " }" + PMD.EOL + 46 " }" + PMD.EOL + 47 "}"; 48 49 private static final String TEST3 = 50 "public class ReturnFromFinallyBlock3 {" + PMD.EOL + 51 " public String getBar() {" + PMD.EOL + 52 " try {" + PMD.EOL + 53 " return \"buz\";" + PMD.EOL + 54 " } finally {" + PMD.EOL + 55 " }" + PMD.EOL + 56 " }" + PMD.EOL + 57 "}"; 58 59 }

This page was automatically generated by Maven