1
16
17 import org.as2lib.core.BasicClass;
18 import org.as2lib.data.holder.Map;
19 import org.as2lib.data.holder.map.PrimitiveTypeMap;
20 import org.as2lib.env.except.IllegalArgumentException;
21 import org.as2lib.test.mock.MethodBehavior;
22 import org.as2lib.test.mock.Behavior;
23 import org.as2lib.test.mock.MethodBehaviorFactory;
24 import org.as2lib.test.mock.MethodCall;
25 import org.as2lib.test.mock.support.DefaultMethodBehavior;
26
27
33 class org.as2lib.test.mock.support.DefaultBehavior extends BasicClass implements Behavior {
34
35
36 private var methodBehaviors:Map;
37
38
39 private var methodBehaviorFactory:MethodBehaviorFactory;
40
41
42 private var lastMethodName:String;
43
44
47 public function DefaultBehavior(Void) {
48 methodBehaviors = new PrimitiveTypeMap();
49 }
50
51
57 public function getMethodBehaviorFactory(Void):MethodBehaviorFactory {
58 if (!methodBehaviorFactory) methodBehaviorFactory = getDefaultMethodBehaviorFactory();
59 return methodBehaviorFactory;
60 }
61
62
68 private function getDefaultMethodBehaviorFactory(Void):MethodBehaviorFactory {
69 var result:MethodBehaviorFactory = getBlankMethodBehaviorFactory();
70 result.getMethodBehavior = function(expectedMethodCall:MethodCall):MethodBehavior {
71 return new DefaultMethodBehavior(expectedMethodCall);
72 };
73 return result;
74 }
75
76
82 private function getBlankMethodBehaviorFactory(Void):MethodBehaviorFactory {
83 var result = new Object();
84 result.__proto__ = MethodBehaviorFactory["prototype"];
85 result.__constructor__ = MethodBehaviorFactory;
86 return result;
87 }
88
89
97 public function setMethodBehaviorFactory(methodBehaviorFactory:MethodBehaviorFactory):Void {
98 this.methodBehaviorFactory = methodBehaviorFactory;
99 }
100
101
112 public function addMethodBehavior(methodName:String, methodBehavior:MethodBehavior):Void {
113 if (!methodBehavior) throw new IllegalArgumentException("Method behavior must not be null or undefined.", this, arguments);
114 if (methodName == null || methodName == "") methodName = methodBehavior.getExpectedMethodCall().getMethodName();
115 if (methodName == null || methodName == "") methodName = "[unknown]";
116 lastMethodName = methodName;
117 if (!methodBehaviors.containsKey(methodName)) methodBehaviors.put(methodName, new Array());
118 var behaviors:Array = methodBehaviors.get(methodName);
119 behaviors.push(methodBehavior);
120 }
121
122
129 public function createMethodBehavior(expectedMethodCall:MethodCall):MethodBehavior {
130 return getMethodBehaviorFactory().getMethodBehavior(expectedMethodCall);
131 }
132
133
144 public function getMethodBehavior(actualMethodCall:MethodCall):MethodBehavior {
145 if (!actualMethodCall) return null;
146 var methodName:String = actualMethodCall.getMethodName();
147 if (methodName == null || methodName == "") methodName = "[unknown]";
148 var behaviors:Array = methodBehaviors.get(methodName);
149 var matchingBehaviors:Array = new Array();
150 for (var i:Number = 0; i < behaviors.length; i++) {
151 var behavior:MethodBehavior = behaviors[i];
152 if (behavior.getExpectedMethodCall().matches(actualMethodCall)) {
153 matchingBehaviors.push(behavior);
154 }
155 }
156 if (matchingBehaviors.length < 1) return null;
157 if (matchingBehaviors.length < 2) return matchingBehaviors[0];
158 var result:MethodBehavior = matchingBehaviors[matchingBehaviors.length-1];
159 for (var i:Number = behaviors.length-1; i > -1; i--) {
160 var behavior:MethodBehavior = behaviors[i];
161 if (behavior.expectsAnotherMethodCall()) {
162 result = behavior;
163 }
164 }
165 return result;
166 }
167
168
173 public function getLastMethodBehavior(Void):MethodBehavior {
174 var behaviors:Array = methodBehaviors.get(lastMethodName);
175 return behaviors[behaviors.length-1];
176 }
177
178
181 public function removeAllBehaviors(Void):Void {
182 methodBehaviors.clear();
183 }
184
185
188 public function verify(Void):Void {
189 var behaviors:Array = methodBehaviors.getValues();
190 for (var i:Number = 0; i < behaviors.length; i++) {
191 for (var k:Number = 0; k < behaviors[i].length; k++) {
192 MethodBehavior(behaviors[i][k]).verify();
193 }
194 }
195 }
196
197 }