1   package test.net.sourceforge.pmd.rules.strictexception;
2   
3   import net.sourceforge.pmd.PMD;
4   import net.sourceforge.pmd.Rule;
5   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
6   import test.net.sourceforge.pmd.testframework.TestDescriptor;
7   
8   public class ExceptionAsFlowControlTest extends SimpleAggregatorTst {
9   
10      private Rule rule;
11  
12      public void setUp() throws Exception {
13          rule = findRule("strictexception", "ExceptionAsFlowControl");
14          rule.setMessage("Avoid this stuff -> ''{0}''");
15      }
16      
17      public void testAll() {
18         runTests(new TestDescriptor[] {
19             new TestDescriptor(TEST1, "failure case", 1, rule),
20             new TestDescriptor(TEST2, "normal throw catch", 0, rule),
21             new TestDescriptor(TEST3, "BUG 996007", 0, rule),
22             new TestDescriptor(TEST4, "NPE", 0, rule)
23         });
24      }
25  
26      private static final String TEST1 =
27      "public class Foo {" + PMD.EOL +
28      " void bar() {" + PMD.EOL +
29      "  try {" + PMD.EOL +
30      "   try {" + PMD.EOL +
31      "   } catch (Exception e) {" + PMD.EOL +
32      "    throw new WrapperException(e);" + PMD.EOL +
33      "    // this is essentially a GOTO to the WrapperException catch block" + PMD.EOL +
34      "   }" + PMD.EOL +
35      "  } catch (WrapperException e) {" + PMD.EOL +
36      "   // do some more stuff " + PMD.EOL +
37      "  }" + PMD.EOL +
38      " }" + PMD.EOL +
39      "}";
40  
41      private static final String TEST2 =
42      "public class Foo {" + PMD.EOL +
43      " void bar() {" + PMD.EOL +
44      "  try {} catch (Exception e) {}" + PMD.EOL +
45      " }" + PMD.EOL +
46      "}";
47      
48      private static final String TEST3 =
49          "public class Foo {" + PMD.EOL +
50          " void bar() {" + PMD.EOL +
51          "  try {} catch (IOException e) {" + PMD.EOL +
52          "  if (foo!=null) " + PMD.EOL +
53          "       throw new IOException(foo.getResponseMessage()); " + PMD.EOL +
54          "  else  " + PMD.EOL +
55          "       throw e; " + PMD.EOL +
56          "  " + PMD.EOL +
57          "  }" + PMD.EOL +
58          " }" + PMD.EOL +
59          "}";
60  
61      private static final String TEST4 =
62      "public class Foo {" + PMD.EOL +
63      " void bar() {" + PMD.EOL +
64      "  switch(foo) {" + PMD.EOL +
65      "   default:" + PMD.EOL +
66      "    throw new IllegalArgumentException();" + PMD.EOL +
67      "  }" + PMD.EOL +
68      " }" + PMD.EOL +
69      "}";
70  
71  }