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 EmptyCatchBlockRuleTest extends SimpleAggregatorTst { 13 14 private Rule rule; 15 private Rule commentsRule; 16 17 public void setUp() throws RuleSetNotFoundException { 18 rule = findRule("basic", "EmptyCatchBlock"); 19 commentsRule = findRule("basic", "EmptyCatchBlock"); 20 commentsRule.addProperty("allowCommentedBlocks", "true"); 21 } 22 23 public void testAll() { 24 runTests(new TestDescriptor[]{ 25 new TestDescriptor(TEST1, "simple failure", 1, rule), 26 new TestDescriptor(TEST2, "ok", 0, rule), 27 new TestDescriptor(TEST3, "no catch with nested catch in finally", 1, rule), 28 new TestDescriptor(TEST4, "multiple catch blocks", 2, rule), 29 new TestDescriptor(TEST5, "empty try with finally", 0, rule), 30 new TestDescriptor(TEST6, "InterruptedException is OK", 0, rule), 31 new TestDescriptor(TEST7, "CloneNotSupportedException is OK", 0, rule), 32 }); 33 } 34 35 public void testCommentedBlocksDisallowed() { 36 runTests(new TestDescriptor[]{ 37 new TestDescriptor(TEST8, "single-line comment is not OK", 1, rule), 38 new TestDescriptor(TEST9, "multiple-line comment is not OK", 1, rule), 39 new TestDescriptor(TEST10, "Javadoc comment is not OK", 1, rule), 40 }); 41 } 42 43 public void testCommentedBlocksAllowed() { 44 runTests(new TestDescriptor[]{ 45 new TestDescriptor(TEST8, "single-line comment is OK", 0, commentsRule), 46 new TestDescriptor(TEST9, "multiple-line comment is OK", 0, commentsRule), 47 new TestDescriptor(TEST10, "Javadoc comment is OK", 0, commentsRule), 48 }); 49 } 50 51 public static final String TEST1 = 52 "public class Foo {" + PMD.EOL + 53 " void bar() {" + PMD.EOL + 54 " try {} catch (Exception e) {}" + PMD.EOL + 55 " }" + PMD.EOL + 56 "}"; 57 58 private static final String TEST2 = 59 "public class Foo {" + PMD.EOL + 60 " void bar() {" + PMD.EOL + 61 " try {} catch (RuntimeException e) {e.getMessage();}" + PMD.EOL + 62 " }" + PMD.EOL + 63 "}"; 64 65 private static final String TEST3 = 66 "public class Foo {" + PMD.EOL + 67 " void foo() {" + PMD.EOL + 68 " try {} finally { " + PMD.EOL + 69 " try {" + PMD.EOL + 70 " int x =2;" + PMD.EOL + 71 " } catch (Exception e) {}" + PMD.EOL + 72 " }" + PMD.EOL + 73 " }" + PMD.EOL + 74 "}"; 75 76 private static final String TEST4 = 77 "public class Foo {" + PMD.EOL + 78 " void foo() {" + PMD.EOL + 79 " try {" + PMD.EOL + 80 " } catch (Exception e) {" + PMD.EOL + 81 " } catch (Throwable t) {" + PMD.EOL + 82 " }" + PMD.EOL + 83 " }" + PMD.EOL + 84 "}"; 85 86 private static final String TEST5 = 87 "public class Foo {" + PMD.EOL + 88 " void foo() {" + PMD.EOL + 89 " try {" + PMD.EOL + 90 " } catch (Exception e) {" + PMD.EOL + 91 " ;" + PMD.EOL + 92 " } finally {}" + PMD.EOL + 93 " }" + PMD.EOL + 94 "}"; 95 96 private static final String TEST6 = 97 "public class Foo {" + PMD.EOL + 98 " void foo() {" + PMD.EOL + 99 " try {" + PMD.EOL + 100 " } catch (InterruptedException e) {}" + PMD.EOL + 101 " }" + PMD.EOL + 102 "}"; 103 104 private static final String TEST7 = 105 "public class Foo {" + PMD.EOL + 106 " void foo() {" + PMD.EOL + 107 " try {" + PMD.EOL + 108 " } catch (CloneNotSupportedException e) {}" + PMD.EOL + 109 " }" + PMD.EOL + 110 "}"; 111 112 public static final String TEST8 = 113 "public class Foo {" + PMD.EOL + 114 " void bar() {" + PMD.EOL + 115 " try {} catch (Exception e) { // Commented " + PMD.EOL + 116 " }" + PMD.EOL + 117 " }" + PMD.EOL + 118 "}"; 119 120 public static final String TEST9 = 121 "public class Foo {" + PMD.EOL + 122 " void bar() {" + PMD.EOL + 123 " try {} catch (Exception e) { /* Commented */" + PMD.EOL + 124 " }" + PMD.EOL + 125 " }" + PMD.EOL + 126 "}"; 127 128 public static final String TEST10 = 129 "public class Foo {" + PMD.EOL + 130 " void bar() {" + PMD.EOL + 131 " try {} catch (Exception e) { /** Commented */" + PMD.EOL + 132 " }" + PMD.EOL + 133 " }" + PMD.EOL + 134 "}"; 135 } 136