|
1 |
| package com.agical.rmock.core.expectation; |
|
2 |
| |
|
3 |
| import java.util.LinkedList; |
|
4 |
| |
|
5 |
| import com.agical.rmock.core.Action; |
|
6 |
| import com.agical.rmock.core.Expectation; |
|
7 |
| import com.agical.rmock.core.ExpectationVisitor; |
|
8 |
| import com.agical.rmock.core.Section; |
|
9 |
| import com.agical.rmock.core.match.Expression; |
|
10 |
| import com.agical.rmock.core.match.Multiplicity; |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| public class InvocationExpectationVisitor implements ExpectationVisitor{ |
|
22 |
| |
|
23 |
| private final String id; |
|
24 |
| private final String method; |
|
25 |
| private final Object[] arguments; |
|
26 |
| private Action action; |
|
27 |
| private final LinkedList sectionStack = new LinkedList(); |
|
28 |
| private Section lastEnteredSection = null; |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
943
| public InvocationExpectationVisitor(String id, String method, Object[] arguments) {
|
|
36 |
943
| this.id = id;
|
|
37 |
943
| this.method = method;
|
|
38 |
943
| this.arguments = arguments;
|
|
39 |
| } |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
2584
| public boolean visit(Expectation expectation, boolean available) {
|
|
45 |
2584
| if( !available ) {
|
|
46 |
281
| return true;
|
|
47 |
| } |
|
48 |
2303
| Multiplicity multiplicityConstraint = expectation.getMultiplicity();
|
|
49 |
2303
| if( multiplicityConstraint.canMatch() &&
|
|
50 |
| expectation.getId().equals( id ) && |
|
51 |
| expectation.getMethod().equals( method ) && |
|
52 |
| matchArguments( expectation.getArguments() ) ) { |
|
53 |
936
| action = expectation.getAction();
|
|
54 |
936
| multiplicityConstraint.match();
|
|
55 |
936
| return false;
|
|
56 |
| } |
|
57 |
1367
| return true;
|
|
58 |
| } |
|
59 |
| |
|
60 |
939
| private boolean matchArguments(Expression[] expectedArguments ) {
|
|
61 |
939
| if( expectedArguments.length != arguments.length ) {
|
|
62 |
2
| return false;
|
|
63 |
| } |
|
64 |
937
| for (int i = 0; i < expectedArguments.length; i++) {
|
|
65 |
702
| Expression constraint = expectedArguments[i];
|
|
66 |
702
| if ( !constraint.passes( arguments[i] ) ) {
|
|
67 |
1
| return false;
|
|
68 |
| } |
|
69 |
| } |
|
70 |
936
| return true;
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
943
| public Action getMatchedAction() {
|
|
78 |
943
| return action;
|
|
79 |
| } |
|
80 |
| |
|
81 |
2187
| public void enteredSection(Section section) {
|
|
82 |
2187
| sectionStack.addLast(section);
|
|
83 |
2187
| lastEnteredSection = section;
|
|
84 |
| } |
|
85 |
| |
|
86 |
100
| public void exitedSection() {
|
|
87 |
100
| sectionStack.removeLast();
|
|
88 |
| } |
|
89 |
| |
|
90 |
| } |