View Javadoc
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.rules.XPathRule; 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() { 17 rule = new XPathRule(); 18 rule.addProperty( 19 "xpath", 20 "//TryStatement[@Finally='true']/Block[position() = last()][count(*) = 0]"); 21 } 22 23 public void testAll() { 24 runTests(new TestDescriptor[] { 25 new TestDescriptor(TEST1, "", 1, rule), 26 new TestDescriptor(TEST2, "", 1, rule), 27 new TestDescriptor(TEST3, "", 0, rule), 28 new TestDescriptor(TEST4, "multiple catch blocks with finally", 1, rule), 29 }); 30 } 31 32 private static final String TEST1 = 33 "public class EmptyFinallyBlock1 {" + PMD.EOL + 34 " public void foo() {" + PMD.EOL + 35 " try {" + PMD.EOL + 36 " } catch (Exception e) {} finally {}" + PMD.EOL + 37 " }" + PMD.EOL + 38 "}"; 39 40 private static final String TEST2 = 41 "public class EmptyFinallyBlock2 {" + PMD.EOL + 42 " public void foo() {" + PMD.EOL + 43 " try {" + PMD.EOL + 44 " } finally {}" + PMD.EOL + 45 " }" + PMD.EOL + 46 "}"; 47 48 private static final String TEST3 = 49 "public class EmptyFinallyBlock3 {" + PMD.EOL + 50 " public void foo() {" + PMD.EOL + 51 " try {" + PMD.EOL + 52 " } finally {int x =2;}" + PMD.EOL + 53 " }" + PMD.EOL + 54 "}"; 55 56 private static final String TEST4 = 57 "public class EmptyFinallyBlock4 {" + PMD.EOL + 58 " public void foo() {" + PMD.EOL + 59 " try {" + PMD.EOL + 60 " } catch (IOException e ){" + PMD.EOL + 61 " } catch (Exception e ) {" + PMD.EOL + 62 " } catch (Throwable t ) {" + PMD.EOL + 63 " } finally{" + PMD.EOL + 64 " }" + PMD.EOL + 65 " }" + PMD.EOL + 66 "}"; 67 68 }

This page was automatically generated by Maven