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 EmptyTryBlockRuleTest extends SimpleAggregatorTst {
13
14 private Rule rule;
15
16 public void setUp() {
17 rule = new XPathRule();
18 rule.addProperty("xpath", "//TryStatement/Block[1][count(*) = 0]");
19 }
20
21 public void testAll() {
22 runTests(new TestDescriptor[] {
23 new TestDescriptor(TEST1, "bad", 1, rule),
24 new TestDescriptor(TEST2, "bad", 1, rule),
25 new TestDescriptor(TEST3, "ok", 0, rule)
26 });
27 }
28
29 private static final String TEST1 =
30 "public class EmptyTryBlock1 {" + PMD.EOL +
31 " public void foo() {" + PMD.EOL +
32 " try {" + PMD.EOL +
33 " } catch (Exception e) {" + PMD.EOL +
34 " e.printStackTrace();" + PMD.EOL +
35 " }" + PMD.EOL +
36 " }" + PMD.EOL +
37 "}";
38
39 private static final String TEST2 =
40 "public class EmptyTryBlock2 {" + PMD.EOL +
41 " public void foo() {" + PMD.EOL +
42 " try {" + PMD.EOL +
43 " } finally {" + PMD.EOL +
44 " int x = 5;" + PMD.EOL +
45 " }" + PMD.EOL +
46 " }" + PMD.EOL +
47 "}";
48
49 private static final String TEST3 =
50 "public class EmptyTryBlock3 {" + PMD.EOL +
51 " public void foo() {" + PMD.EOL +
52 " try {" + PMD.EOL +
53 " int f =2;" + PMD.EOL +
54 " } finally {" + PMD.EOL +
55 " int x = 5;" + PMD.EOL +
56 " }" + PMD.EOL +
57 " }" + PMD.EOL +
58 "}";
59
60 }
This page was automatically generated by Maven