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 MissingBreakInSwitchTest extends SimpleAggregatorTst {
13      private Rule rule;
14  
15      public void setUp() throws RuleSetNotFoundException {
16          rule = findRule("design", "MissingBreakInSwitch");
17      }
18  
19      public void testAll() {
20          runTests(new TestDescriptor[]{
21              new TestDescriptor(TEST1, "one case", 1, rule),
22              new TestDescriptor(TEST2, "just skip empty switch", 0, rule),
23              new TestDescriptor(TEST3, "one break", 0, rule),
24              new TestDescriptor(TEST4, "each case stmt has a return", 0, rule),
25          });
26      }
27  
28      private static final String TEST1 =
29              "public class Foo {" + PMD.EOL +
30              "	void main() {" + PMD.EOL +
31              "		switch(i) {" + PMD.EOL +
32              "		case 1:" + PMD.EOL +
33              "		default:" + PMD.EOL +
34              "		}" + PMD.EOL +
35              "	}" + PMD.EOL +
36              "}";
37  
38      private static final String TEST2 =
39              "public class Foo {" + PMD.EOL +
40              "	void main() {" + PMD.EOL +
41              "		switch(i) {" + PMD.EOL +
42              "		}" + PMD.EOL +
43              "	}" + PMD.EOL +
44              "}";
45  
46      private static final String TEST3 =
47              "public class Foo {" + PMD.EOL +
48              "	void main() {" + PMD.EOL +
49              "		switch(i) {" + PMD.EOL +
50              "		case 1:" + PMD.EOL +
51              "		case 2:" + PMD.EOL +
52              "			break;" + PMD.EOL +
53              "		default:" + PMD.EOL +
54              "		}" + PMD.EOL +
55              "	}" + PMD.EOL +
56              "}";
57  
58      private static final String TEST4 =
59              "public class Foo {" + PMD.EOL +
60              "	int main() {" + PMD.EOL +
61              "		switch(i) {" + PMD.EOL +
62              "		case '1':" + PMD.EOL +
63              "		 return 1;" + PMD.EOL +
64              "		case '2':" + PMD.EOL +
65              "		 return 2;" + PMD.EOL +
66              "		default:" + PMD.EOL +
67              "		 return 3;" + PMD.EOL +
68              "		}" + PMD.EOL +
69              "	}" + PMD.EOL +
70              "}";
71  
72  }
73