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      }
15  
16      public void testAll() {
17          runTests(new TestDescriptor[]{
18              new TestDescriptor(TEST1, "failure case", 1, rule),
19              new TestDescriptor(TEST2, "normal throw catch", 0, rule),
20              new TestDescriptor(TEST3, "BUG 996007", 0, rule),
21              new TestDescriptor(TEST4, "NPE", 0, rule)
22          });
23      }
24  
25      private static final String TEST1 =
26              "public class Foo {" + PMD.EOL +
27              " void bar() {" + PMD.EOL +
28              "  try {" + PMD.EOL +
29              "   try {" + PMD.EOL +
30              "   } catch (Exception e) {" + PMD.EOL +
31              "    throw new WrapperException(e);" + PMD.EOL +
32              "    // this is essentially a GOTO to the WrapperException catch block" + PMD.EOL +
33              "   }" + PMD.EOL +
34              "  } catch (WrapperException e) {" + PMD.EOL +
35              "   // do some more stuff " + PMD.EOL +
36              "  }" + PMD.EOL +
37              " }" + PMD.EOL +
38              "}";
39  
40      private static final String TEST2 =
41              "public class Foo {" + PMD.EOL +
42              " void bar() {" + PMD.EOL +
43              "  try {} catch (Exception e) {}" + PMD.EOL +
44              " }" + PMD.EOL +
45              "}";
46  
47      private static final String TEST3 =
48              "public class Foo {" + PMD.EOL +
49              " void bar() {" + PMD.EOL +
50              "  try {} catch (IOException e) {" + PMD.EOL +
51              "  if (foo!=null) " + PMD.EOL +
52              "       throw new IOException(foo.getResponseMessage()); " + PMD.EOL +
53              "  else  " + PMD.EOL +
54              "       throw e; " + PMD.EOL +
55              "  " + PMD.EOL +
56              "  }" + PMD.EOL +
57              " }" + PMD.EOL +
58              "}";
59  
60      private static final String TEST4 =
61              "public class Foo {" + PMD.EOL +
62              " void bar() {" + PMD.EOL +
63              "  switch(foo) {" + PMD.EOL +
64              "   default:" + PMD.EOL +
65              "    throw new IllegalArgumentException();" + PMD.EOL +
66              "  }" + PMD.EOL +
67              " }" + PMD.EOL +
68              "}";
69  
70  }