1
16
17 import org.as2lib.core.BasicClass;
18 import org.as2lib.env.except.IllegalArgumentException;
19 import org.as2lib.env.except.IllegalStateException;
20 import org.as2lib.test.mock.MethodCallRangeError;
21 import org.as2lib.test.mock.MethodBehavior;
22 import org.as2lib.test.mock.MethodResponse;
23 import org.as2lib.test.mock.MethodCallRange;
24 import org.as2lib.test.mock.MethodCall;
25 import org.as2lib.test.mock.ArgumentsMatcher;
26
27
33 class org.as2lib.test.mock.support.DefaultMethodBehavior extends BasicClass implements MethodBehavior {
34
35
36 private var expectedMethodCall:MethodCall;
37
38
39 private var actualMethodCalls:Array;
40
41
42 private var methodCallRanges:Array;
43
44
45 private var methodResponses:Array;
46
47
57 public function DefaultMethodBehavior(expectedMethodCall:MethodCall) {
58 this.expectedMethodCall = expectedMethodCall;
59 actualMethodCalls = new Array();
60 methodCallRanges = new Array();
61 methodResponses = new Array();
62 }
63
64
69 public function getExpectedMethodCall(Void):MethodCall {
70 return expectedMethodCall;
71 }
72
73
86 public function addActualMethodCall(actualMethodCall:MethodCall):Void {
87 if (!actualMethodCall) throw new IllegalArgumentException("Actual method call is not allowed to be null or undefined.", this, arguments);
88 if (!expectedMethodCall.matches(actualMethodCall) && expectedMethodCall) {
89 var error:MethodCallRangeError = new MethodCallRangeError("Unexpected method call", this, arguments);
90 error.addMethodCall(actualMethodCall, new MethodCallRange(0), new MethodCallRange(1));
91 error.addMethodCall(expectedMethodCall, new MethodCallRange(getTotalMinimumMethodCallCount(), getTotalMaximumMethodCallCount()), new MethodCallRange(actualMethodCalls.length));
92 throw error;
93 }
94 actualMethodCalls.push(actualMethodCall);
95 if (!expectedMethodCall) {
96 var error:MethodCallRangeError = new MethodCallRangeError("Unexpected method call", this, arguments);
97 error.addMethodCall(actualMethodCall, new MethodCallRange(0), new MethodCallRange(actualMethodCalls.length));
98 throw error;
99 }
100 if (actualMethodCalls.length > getTotalMaximumMethodCallCount()) {
101 var error:MethodCallRangeError = new MethodCallRangeError("Unexpected method call", this, arguments);
102 error.addMethodCall(actualMethodCall, new MethodCallRange(getTotalMinimumMethodCallCount(), getTotalMaximumMethodCallCount()), new MethodCallRange(actualMethodCalls.length));
103 throw error;
104 }
105 }
106
107
112 private function getTotalMinimumMethodCallCount(Void):Number {
113 if (!expectedMethodCall) return 0;
114 if (methodCallRanges.length < 1) return 1;
115 var result:Number = 0;
116 for (var i:Number = 0; i < methodCallRanges.length; i++) {
117 result += MethodCallRange(methodCallRanges[i]).getMinimum();
118 }
119 return result;
120 }
121
122
127 private function getTotalMaximumMethodCallCount(Void):Number {
128 if (!expectedMethodCall) return 0;
129 if (methodCallRanges.length < 1) return 1;
130 var result:Number = 0;
131 for (var i:Number = 0; i < methodCallRanges.length; i++) {
132 result += MethodCallRange(methodCallRanges[i]).getMaximum();
133 }
134 return result;
135 }
136
137
149 public function addMethodResponse(methodResponse:MethodResponse, methodCallRange:MethodCallRange):Void {
150 if (!expectedMethodCall) throw new IllegalStateException("It is not possible to set a response for an not-expected method call.", this, arguments);
151 methodResponses.push(methodResponse);
152 methodCallRanges.push(methodCallRange);
153 }
154
155
160 public function setArgumentsMatcher(argumentsMatcher:ArgumentsMatcher):Void {
161 expectedMethodCall.setArgumentsMatcher(argumentsMatcher);
162 }
163
164
169 public function expectsAnotherMethodCall(Void):Boolean {
170 if (!expectedMethodCall) return false;
171 if (methodCallRanges.length < 1) {
172 if (actualMethodCalls.length < 1) return true;
173 else return false;
174 }
175 return (getCurrentMethodCallRangeIndex() > -1);
176 }
177
178
183 private function getCurrentMethodCallRangeIndex(Void):Number {
184 var maximum:Number = 0;
185 for (var i:Number = 0; i < methodCallRanges.length; i++) {
186 var methodCallRange:MethodCallRange = methodCallRanges[i];
187 if (methodCallRange) {
188 maximum += methodCallRange.getMaximum();
189 } else {
190 maximum += Number.POSITIVE_INFINITY;
191 }
192 if (actualMethodCalls.length < maximum) {
193 return i;
194 }
195 }
196 return -1;
197 }
198
199
205 public function response(Void) {
206 return MethodResponse(methodResponses[getCurrentMethodResponseIndex()]).response();
207 }
208
209
214 private function getCurrentMethodResponseIndex(Void):Number {
215 var maximum:Number = 0;
216 for (var i:Number = 0; i < methodCallRanges.length; i++) {
217 maximum += MethodCallRange(methodCallRanges[i]).getMaximum();
218 if (actualMethodCalls.length <= maximum) {
219 return i;
220 }
221 }
222 return -1;
223 }
224
225
230 public function verify(Void):Void {
231 if (actualMethodCalls.length < getTotalMinimumMethodCallCount()) {
232 var error:MethodCallRangeError = new MethodCallRangeError("Expectation failure on verify", this, arguments);
233 error.addMethodCall(expectedMethodCall, new MethodCallRange(getTotalMinimumMethodCallCount(), getTotalMaximumMethodCallCount()), new MethodCallRange(actualMethodCalls.length));
234 throw error;
235 }
236 }
237
238 }