|
1 |
| package com.agical.rmock.core.exception.manager; |
|
2 |
| |
|
3 |
| import java.util.ArrayList; |
|
4 |
| import java.util.Iterator; |
|
5 |
| import java.util.List; |
|
6 |
| |
|
7 |
| import com.agical.rmock.core.Verifiable; |
|
8 |
| import com.agical.rmock.core.event.ExceptionVerifierListener; |
|
9 |
| import com.agical.rmock.core.event.TestCaseListener; |
|
10 |
| import com.agical.rmock.core.exception.ExpectedThrowableNotThrownException; |
|
11 |
| import com.agical.rmock.core.exception.RMockSystemException; |
|
12 |
| import com.agical.rmock.core.exception.RMockSystemExceptionInterceptedException; |
|
13 |
| import com.agical.rmock.core.exception.StrategyTerminatingException; |
|
14 |
| import com.agical.rmock.core.match.Expression; |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| public class ExceptionVerifierImpl implements Verifiable, ExceptionVerifier, TestCaseListener { |
|
22 |
| |
|
23 |
| private List systemThrowables = new ArrayList(); |
|
24 |
| private Expression expression = null; |
|
25 |
| |
|
26 |
| private Throwable userThrowable; |
|
27 |
| private ExceptionVerifierListener exceptionVerifierListener = ExceptionVerifierListener.NULL; |
|
28 |
| |
|
29 |
18
| public ExceptionVerifierImpl() {
|
|
30 |
18
| super();
|
|
31 |
| } |
|
32 |
| |
|
33 |
6
| public void addSystemException(RMockSystemException systemException) {
|
|
34 |
6
| this.systemThrowables.add( systemException );
|
|
35 |
| } |
|
36 |
| |
|
37 |
537
| public void beginVerify() {
|
|
38 |
| } |
|
39 |
| |
|
40 |
1069
| public void endVerify() {
|
|
41 |
1069
| if( expression != null ) {
|
|
42 |
1
| throw new ExpectedThrowableNotThrownException( "The expected exception was never thrown. Expression was:\n" + expression );
|
|
43 |
| } |
|
44 |
1068
| if( systemThrowables.size() > 0 ) {
|
|
45 |
1
| throwExceptionInterceptedException();
|
|
46 |
| } |
|
47 |
| } |
|
48 |
| |
|
49 |
547
| public void beforeTestCase(Object testInstance, String testName) {
|
|
50 |
547
| reset();
|
|
51 |
| } |
|
52 |
| |
|
53 |
547
| private void reset() {
|
|
54 |
547
| systemThrowables.clear();
|
|
55 |
547
| userThrowable = null;
|
|
56 |
547
| expression = null;
|
|
57 |
| } |
|
58 |
| |
|
59 |
547
| public void afterTestCase() {
|
|
60 |
| } |
|
61 |
| |
|
62 |
25
| public void setUserExceptionExpectation(Expression expression) {
|
|
63 |
25
| this.expression = expression;
|
|
64 |
| } |
|
65 |
| |
|
66 |
30
| public void verifyActualThrowable(Throwable throwable) throws Throwable {
|
|
67 |
30
| exceptionVerifierListener.throwableThrown(throwable);
|
|
68 |
30
| if( systemThrowables.size() > 1 ) {
|
|
69 |
1
| throwExceptionInterceptedException( );
|
|
70 |
| } |
|
71 |
29
| if( expression != null ) {
|
|
72 |
24
| if( !expression.passes( throwable ) ) {
|
|
73 |
2
| throw throwable;
|
|
74 |
| } else { |
|
75 |
22
| if( systemThrowables.size() == 1 &&
|
|
76 |
| systemThrowables.get(0).equals(throwable) ) { |
|
77 |
| |
|
78 |
1
| systemThrowables.clear();
|
|
79 |
| } |
|
80 |
22
| expression = null;
|
|
81 |
| |
|
82 |
22
| throw new StrategyTerminatingException();
|
|
83 |
| } |
|
84 |
| } |
|
85 |
5
| if( systemThrowables.size() == 1 ) {
|
|
86 |
1
| if( systemThrowables.get(0).equals(throwable) ) {
|
|
87 |
1
| throw throwable;
|
|
88 |
| } |
|
89 |
0
| throwExceptionInterceptedException( );
|
|
90 |
| } |
|
91 |
4
| throw throwable;
|
|
92 |
| } |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
2
| private void throwExceptionInterceptedException( ) {
|
|
97 |
2
| StringBuffer exceptions = new StringBuffer();
|
|
98 |
2
| for (Iterator iter = systemThrowables.iterator(); iter.hasNext();) {
|
|
99 |
3
| Throwable exception = (Throwable) iter.next();
|
|
100 |
3
| exceptions.append( exception.getClass().getName() ).append( "\n" );
|
|
101 |
| } |
|
102 |
2
| Throwable firstThrowable = (Throwable) systemThrowables.get(0);
|
|
103 |
2
| throw new RMockSystemExceptionInterceptedException( "\nThe following RMock system exceptions were thrown by the system:\n" + exceptions + "\n" +
|
|
104 |
| "signalling a failing test. One or more of them was caught by your code\n" + |
|
105 |
| "which indicatates a bad exception strategy in your code.\n" + |
|
106 |
| "Good advice is that you subclass Javas Exception/RuntimeException/Error to be specific to your code base,\n" + |
|
107 |
| "and that you never catch the Java base exceptions, only your own subclasses.", firstThrowable ); |
|
108 |
| } |
|
109 |
| |
|
110 |
11
| public void setExceptionVerifierListener(ExceptionVerifierListener exceptionVerifierListener) {
|
|
111 |
11
| this.exceptionVerifierListener = exceptionVerifierListener;
|
|
112 |
| } |
|
113 |
| |
|
114 |
| |
|
115 |
| } |