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 IfStmtsMustUseBracesRuleTest extends SimpleAggregatorTst {
13
14 private Rule rule;
15
16 public void setUp() throws RuleSetNotFoundException {
17 rule = findRule("rulesets/braces.xml", "IfStmtsMustUseBraces");
18 }
19
20 public void testAll() {
21 runTests(new TestDescriptor[] {
22 new TestDescriptor(TEST1, "simple failure case", 1, rule),
23 new TestDescriptor(TEST2, "ok", 0, rule),
24 new TestDescriptor(TEST3, "nested ifs", 1, rule),
25 });
26 }
27
28 private static final String TEST1 =
29 "public class IfStmtsMustUseBraces1 {" + PMD.EOL +
30 " public void foo() {" + PMD.EOL +
31 " int x = 0; " + PMD.EOL +
32 " if (true) x=2;" + PMD.EOL +
33 " }" + PMD.EOL +
34 "}";
35
36 private static final String TEST2 =
37 "public class IfStmtsMustUseBraces2 {" + PMD.EOL +
38 " public void foo() { " + PMD.EOL +
39 " if (true) {" + PMD.EOL +
40 " int x=2;" + PMD.EOL +
41 " }" + PMD.EOL +
42 " }" + PMD.EOL +
43 "}";
44
45 private static final String TEST3 =
46 "public class IfStmtsMustUseBraces3 {" + PMD.EOL +
47 " public void foo() { " + PMD.EOL +
48 " if (true) {" + PMD.EOL +
49 " if (true) bar();" + PMD.EOL +
50 " }" + PMD.EOL +
51 " }" + PMD.EOL +
52 "}";
53
54 }