1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.rules.design;
5   
6   import net.sourceforge.pmd.PMD;
7   import net.sourceforge.pmd.Rule;
8   import net.sourceforge.pmd.RuleSetNotFoundException;
9   
10  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
11  import test.net.sourceforge.pmd.testframework.TestDescriptor;
12  
13  public class PreserveStackTraceTest extends SimpleAggregatorTst {
14  
15      private Rule rule;
16  
17      public void setUp() throws RuleSetNotFoundException {
18          rule = findRule("design", "PreserveStackTrace");
19      }
20      
21      public void test() throws Throwable {
22          rule.setMessage("{0}");
23          runTests(new TestDescriptor[]{
24                  new TestDescriptor(TEST1_FAIL, "1, Exception thrown without preserving stack", 1, rule),
25                  new TestDescriptor(TEST2_OK, "2, Exception thrown, stack preserved", 0, rule),
26                  new TestDescriptor(TEST3_OK, "3, Exception thrown, stack preserved", 0, rule),
27                  new TestDescriptor(TEST4_OK, "4, No exception thrown, OK", 0, rule),
28                  new TestDescriptor(TEST5_OK, "5, No exception thrown, OK", 0, rule),
29                  new TestDescriptor(TEST6_OK, "6, No exception thrown, OK", 0, rule),
30                  new TestDescriptor(TEST7_OK, "7, No exception thrown, OK", 0, rule),
31                  new TestDescriptor(TEST8_OK, "8, No exception thrown, OK", 0, rule),
32                  new TestDescriptor(TEST9_OK, "9, Excetion is cast, OK", 0, rule),
33              });
34      }
35  
36      private static final String TEST1_FAIL =
37          "public class Foo {" + PMD.EOL +
38          "    public void foo(String a) {" + PMD.EOL +
39          "        try {" + PMD.EOL +
40          "            int i = Integer.parseInt(a);" + PMD.EOL +
41          "        } catch(Exception e){" + PMD.EOL +
42          "            throw new Exception(e.getMessage());" + PMD.EOL +
43          "        }" + PMD.EOL +
44          "    }" + PMD.EOL +
45          "}";
46  
47      private static final String TEST2_OK =
48          "public class Foo {" + PMD.EOL +
49          "    public void foo(String a) {" + PMD.EOL +
50          "        try {" + PMD.EOL +
51          "            int i = Integer.parseInt(a);" + PMD.EOL +
52          "        } catch(Exception e){" + PMD.EOL +
53          "            throw new Exception(e);" + PMD.EOL +
54          "        }" + PMD.EOL +
55          "    }" + PMD.EOL +
56          "}";
57  
58      private static final String TEST3_OK =
59          "public class Foo {" + PMD.EOL +
60          "    public void foo(String a) {" + PMD.EOL +
61          "        try {" + PMD.EOL +
62          "            int i = Integer.parseInt(a);" + PMD.EOL +
63          "        } catch(Exception e){" + PMD.EOL +
64          "            throw new Exception(e, e.getMessage());" + PMD.EOL +
65          "        }" + PMD.EOL +
66          "    }" + PMD.EOL +
67          "}";
68  
69      private static final String TEST4_OK =
70          "public class Foo {" + PMD.EOL +
71          "    public void foo(String a) {" + PMD.EOL +
72          "        try {" + PMD.EOL +
73          "            int i = Integer.parseInt(a);" + PMD.EOL +
74          "        } catch(Exception e){" + PMD.EOL +
75          "            throw e.fillInStackTrace();" + PMD.EOL +
76          "        }" + PMD.EOL +
77          "    }" + PMD.EOL +
78          "}";
79  
80      private static final String TEST5_OK =
81          "public class Foo {" + PMD.EOL +
82          "    public void foo(String a) {" + PMD.EOL +
83          "        try {" + PMD.EOL +
84          "            int i = Integer.parseInt(a);" + PMD.EOL +
85          "        } catch(Exception e){" + PMD.EOL +
86          "        }" + PMD.EOL +
87          "    }" + PMD.EOL +
88          "}";
89  
90      private static final String TEST6_OK =
91          "public class Foo {" + PMD.EOL +
92          "    public void foo(String a) {" + PMD.EOL +
93          "        try {" + PMD.EOL +
94          "            int i = Integer.parseInt(a);" + PMD.EOL +
95          "        } catch(Exception e){" + PMD.EOL +
96          "            e.printStackTrace();" + PMD.EOL +
97          "        }" + PMD.EOL +
98          "    }" + PMD.EOL +
99          "}";
100 
101     private static final String TEST7_OK =
102         "public class Foo {" + PMD.EOL +
103         "    public void foo(String a) {" + PMD.EOL +
104         "        try {" + PMD.EOL +
105         "            int i = Integer.parseInt(a);" + PMD.EOL +
106         "        } catch(Exception e){" + PMD.EOL +
107         "            throw new Exception(Bar.foo(e),e);" + PMD.EOL +
108         "        }" + PMD.EOL +
109         "    }" + PMD.EOL +
110         "}";
111 
112     private static final String TEST8_FAIL =
113         "public class Foo {" + PMD.EOL +
114         "    public void foo(String a) {" + PMD.EOL +
115         "        try {" + PMD.EOL +
116         "            int i = Integer.parseInt(a);" + PMD.EOL +
117         "        } catch(Exception e){" + PMD.EOL +
118         "            throw new Exception(Bar.foo(e));" + PMD.EOL +
119         "        }" + PMD.EOL +
120         "    }" + PMD.EOL +
121         "}";
122 
123     private static final String TEST8_OK =
124         "public class Foo {" + PMD.EOL +
125         "    public void foo(String a) {" + PMD.EOL +
126         "        try {" + PMD.EOL +
127         "            int i = Integer.parseInt(a);" + PMD.EOL +
128         "        } catch(Exception e){" + PMD.EOL +
129         "            throw (Error)e;" + PMD.EOL +
130         "        }" + PMD.EOL +
131         "    }" + PMD.EOL +
132         "}";
133 
134     private static final String TEST9_OK =
135         "public class Foo {" + PMD.EOL +
136         "    public void foo(String a) {" + PMD.EOL +
137         "        try {" + PMD.EOL +
138         "            int i = Integer.parseInt(a);" + PMD.EOL +
139         "        } catch(Exception e){" + PMD.EOL +
140         "            throw (Error)e.fillInStackTrace();" + PMD.EOL +
141         "        }" + PMD.EOL +
142         "    }" + PMD.EOL +
143         "}";
144 }