|
1 |
| package com.agical.rmock.core.expectation; |
|
2 |
| |
|
3 |
| import com.agical.rmock.core.Action; |
|
4 |
| import com.agical.rmock.core.InvocationHandler; |
|
5 |
| import com.agical.rmock.core.MethodHandle; |
|
6 |
| import com.agical.rmock.core.Section; |
|
7 |
| import com.agical.rmock.core.exception.RMockSystemException; |
|
8 |
| import com.agical.rmock.core.exception.UnexpectedInvocationException; |
|
9 |
| import com.agical.rmock.core.exception.manager.ExceptionVerifier; |
|
10 |
| |
|
11 |
| |
|
12 |
| class InvocationVerifier implements InvocationHandler { |
|
13 |
| private final Section expectations; |
|
14 |
| private ExceptionVerifier exceptionVerifier; |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
549
| InvocationVerifier(Section expectations) {
|
|
19 |
549
| this.expectations = expectations;
|
|
20 |
| } |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
940
| public Object invocation(String id, Class objectType, String method, Object[] arguments, MethodHandle methodHandle) throws Throwable {
|
|
27 |
940
| InvocationExpectationVisitor visitor = new InvocationExpectationVisitor(id, method, arguments);
|
|
28 |
| |
|
29 |
940
| expectations.accept(visitor, true);
|
|
30 |
940
| Action action = visitor.getMatchedAction();
|
|
31 |
| |
|
32 |
940
| if (action == null) {
|
|
33 |
5
| StringBuffer message = new StringBuffer();
|
|
34 |
5
| message.append(id).append('.').append(method);
|
|
35 |
5
| message.append('(');
|
|
36 |
5
| for (int i = 0; i < arguments.length; i++) {
|
|
37 |
2
| message.append('<').append(arguments[i]).append('>');
|
|
38 |
2
| if (i < arguments.length-1) {
|
|
39 |
1
| message.append(", ");
|
|
40 |
| } |
|
41 |
| } |
|
42 |
5
| message.append(')');
|
|
43 |
| |
|
44 |
5
| RMockSystemException unexpectedInvocationException = getException(message.toString());
|
|
45 |
5
| if( exceptionVerifier != null ) {
|
|
46 |
1
| exceptionVerifier.addSystemException( unexpectedInvocationException );
|
|
47 |
| } |
|
48 |
5
| throw unexpectedInvocationException;
|
|
49 |
| } |
|
50 |
| |
|
51 |
935
| Object returnValue = action.invocation(arguments, methodHandle);
|
|
52 |
924
| return returnValue;
|
|
53 |
| } |
|
54 |
| |
|
55 |
5
| RMockSystemException getException(String message) {
|
|
56 |
5
| UnexpectedInvocationException unexpectedInvocationException = new UnexpectedInvocationException(expectations, false, message );
|
|
57 |
5
| return unexpectedInvocationException;
|
|
58 |
| } |
|
59 |
| |
|
60 |
549
| public void setExceptionVerifier(ExceptionVerifier exceptionVerifier) {
|
|
61 |
549
| this.exceptionVerifier = exceptionVerifier;
|
|
62 |
| } |
|
63 |
| } |