1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package junitx.ddtunit;
39
40 import java.lang.reflect.InvocationTargetException;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.Map;
44 import java.util.Map.Entry;
45
46 import junit.framework.AssertionFailedError;
47 import junitx.ddtunit.data.AssertObject;
48 import junitx.ddtunit.data.ExceptionAsserter;
49 import junitx.ddtunit.data.TypedObject;
50 import junitx.ddtunit.data.TypedObjectMap;
51
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55
56
57
58
59
60 class ExceptionHandler {
61
62 private Logger log = LoggerFactory.getLogger(ExceptionHandler.class);
63
64 private String methodName;
65
66 private Map methodTestFailure;
67
68 private Map methodTestError;
69
70 private static final String LF = System.getProperty("line.separator");
71
72
73
74
75
76
77 public ExceptionHandler(String methodName) {
78 this.methodName = methodName;
79 this.methodTestFailure = new HashMap();
80 this.methodTestError = new HashMap();
81 }
82
83
84
85
86
87
88
89
90
91 public void process(String testId, Throwable aThrowable,
92 TypedObjectMap assertMap) throws Throwable {
93 log.debug("process(" + methodName + ", " + testId + ", "
94 + aThrowable.getMessage() + ") - START");
95 if (InvocationTargetException.class.isInstance(aThrowable)) {
96 InvocationTargetException myEx = (InvocationTargetException) aThrowable;
97 aThrowable.fillInStackTrace();
98 addErrorFailure(testId, myEx.getTargetException(), assertMap);
99 } else {
100 addErrorFailure(testId, aThrowable, assertMap);
101 }
102 log.debug("process(" + methodName + ", " + testId + ", "
103 + aThrowable.getMessage() + ") - END");
104 }
105
106
107
108
109
110
111 private void addErrorFailure(String testId, Throwable aThrowable,
112 TypedObjectMap assertMap) throws Throwable {
113 if (!ignoreExpected(testId, aThrowable, assertMap)) {
114 if (AssertionFailedError.class.isInstance(aThrowable)) {
115 AssertionFailedError assertError = (AssertionFailedError) aThrowable;
116 this.methodTestFailure.put(testId, new DDTTestFailure(null,
117 testId, assertError));
118 throw assertError;
119 } else {
120 this.methodTestError.put(testId, new DDTTestFailure(null,
121 testId, aThrowable));
122 throw aThrowable;
123 }
124 }
125 }
126
127
128
129
130
131
132
133
134 private boolean ignoreExpected(String testId, Throwable aThrowable,
135 TypedObjectMap assertMap) {
136 log.debug("filter(" + methodName + ", " + testId + ", "
137 + aThrowable.getMessage() + ") - START");
138 boolean caughtExpectedException = false;
139
140 if (assertMap != null) {
141 Throwable lastCaughtException = null;
142 for (Iterator iter = assertMap.entrySet().iterator(); iter
143 .hasNext();) {
144 Entry assertEntry = (Entry) iter.next();
145 AssertObject assertObj = (AssertObject) assertEntry.getValue();
146 if (ExceptionAsserter.class.isInstance(assertObj)) {
147 ExceptionAsserter exAssert = (ExceptionAsserter) assertObj;
148 try {
149 exAssert.setActualObject(aThrowable);
150 exAssert.validate(false);
151 caughtExpectedException = true;
152 break;
153
154
155
156
157
158 } catch (Throwable ex) {
159
160 lastCaughtException = ex;
161 log
162 .debug(
163 "Exception caught during check on expected exception.",
164 lastCaughtException);
165 }
166 }
167 }
168 if (caughtExpectedException) {
169 log.debug("Caught expected exception in test " + testId + ":"
170 + aThrowable.getMessage());
171 }
172 }
173 log.debug("filter(" + methodName + ", " + testId + ", "
174 + aThrowable.getMessage() + ") found: "
175 + caughtExpectedException + "- END");
176 return caughtExpectedException;
177 }
178
179
180
181
182
183
184
185
186
187 void summarizeProblems(int testCount) {
188 int errorCount = this.methodTestError.size();
189 int failureCount = this.methodTestFailure.size();
190 if (errorCount == 0 && failureCount == 0) {
191 return;
192 }
193 StringBuffer sb = new StringBuffer("method ").append(this.methodName)
194 .append(" - ");
195 sb.append("Total: " + testCount + ", Errors: " + errorCount
196 + ", Failures: " + failureCount + LF);
197 Throwable testError = null;
198 if (errorCount > 0) {
199 testError = summariesErrors(sb, testError);
200 }
201 if (failureCount > 0) {
202 if (errorCount > 0) {
203 sb.append(LF);
204 }
205 testError = summariesFailures(sb, testError);
206 }
207
208 if (errorCount == 0) {
209 AssertionFailedError afEx = new AssertionFailedError(sb.toString());
210 afEx.setStackTrace(testError.getStackTrace());
211 throw afEx;
212 } else {
213 DDTException processError = new DDTException(sb.toString(),
214 testError);
215 processError.setStackTrace(testError.getStackTrace());
216 throw processError;
217 }
218 }
219
220
221
222
223
224
225
226
227
228 private Throwable summariesFailures(StringBuffer sb, Throwable testError) {
229 DDTTestFailure testFailure;
230 for (Iterator iter = this.methodTestFailure.values().iterator(); iter
231 .hasNext();) {
232 testFailure = (DDTTestFailure) iter.next();
233 sb.append("F-(").append(testFailure.getMethodTest()).append(") ")
234 .append(testFailure.exceptionMessage());
235 if (iter.hasNext()) {
236 sb.append(LF);
237 }
238 if (testError == null) {
239 testError = testFailure.thrownException();
240 }
241 }
242 return testError;
243 }
244
245
246
247
248
249
250
251
252
253
254 private Throwable summariesErrors(StringBuffer sb, Throwable testError) {
255 DDTTestFailure testFailure;
256 for (Iterator iter = this.methodTestError.values().iterator(); iter
257 .hasNext();) {
258 testFailure = (DDTTestFailure) iter.next();
259 sb.append("E-(").append(testFailure.getMethodTest()).append(") ")
260 .append(testFailure.exceptionMessage());
261 if (iter.hasNext()) {
262 sb.append(LF);
263 }
264 if (testError == null) {
265 testError = testFailure.thrownException();
266 }
267 }
268 return testError;
269 }
270
271
272
273
274
275
276
277
278
279
280 public void checkOnExpectedException(String testId, TypedObjectMap assertMap) {
281 if (assertMap != null) {
282 int numberOfExpectedExceptions = 0;
283 Throwable firstException = null;
284 for (Iterator iter = assertMap.entrySet().iterator(); iter
285 .hasNext();) {
286 Entry assertEntry = (Entry) iter.next();
287 String exceptKey = (String) assertEntry.getKey();
288 TypedObject assertObj = assertMap.get(exceptKey);
289 if (ExceptionAsserter.class.isInstance(assertObj)) {
290 numberOfExpectedExceptions++;
291 firstException = (Throwable) assertObj.getValue();
292 }
293 }
294 if (numberOfExpectedExceptions > 0) {
295 StringBuffer sb = new StringBuffer("There is/are ").append(
296 numberOfExpectedExceptions).append(
297 " expected exception(s) defined in test '").append(testId)
298 .append("'");
299 sb.append(LF).append(" (last as hint): ").append(
300 firstException.getClass().getName());
301 if (firstException.getMessage() != null) {
302 sb.append(" - ").append(firstException.getMessage());
303 }
304 throw new AssertionFailedError(sb.toString());
305 }
306 }
307 }
308 }