comparison bindings/as3/ext/asunit/framework/RemotingTestCase.as @ 732:3a0b9700b3d2

* Initial AS3 commit
author mas01mj
date Tue, 14 Sep 2010 16:47:10 +0000
parents
children
comparison
equal deleted inserted replaced
731:65134dd772fc 732:3a0b9700b3d2
1 package asunit.framework
2 {
3 import flash.errors.IllegalOperationError;
4 import flash.events.IOErrorEvent;
5 import flash.events.NetStatusEvent;
6 import flash.events.SecurityErrorEvent;
7 import flash.net.NetConnection;
8 import flash.net.ObjectEncoding;
9 import flash.net.Responder;
10
11 import asunit.framework.TestCase;
12 import asunit.util.ArrayIterator;
13
14 /**
15 * RemotingTestCase
16 * @author Jens Krause [www.websector.de]
17 * @date 11/29/07
18 *
19 */
20 public class RemotingTestCase extends TestCase
21 {
22
23 protected var connection: NetConnection;
24 /**
25 * Constructor
26 * @param testMethod String Name of the test case
27 *
28 */
29 public function RemotingTestCase(testMethod: String = null)
30 {
31 super(testMethod);
32 }
33
34 /**
35 * Inits a netConnection instance and add all necessary event listeners
36 *
37 */
38 protected function initConnection():void
39 {
40 if (connection == null)
41 {
42 connection = new NetConnection();
43
44 connection.addEventListener(NetStatusEvent.NET_STATUS, connectionStatusHandler);
45 connection.addEventListener(IOErrorEvent.IO_ERROR, connectionIOErrorHandler);
46 connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR , connectionSecurityErrorHandler);
47 }
48 }
49
50 /**
51 * Dispose the netConnection instance
52 *
53 */
54 protected function disposeConnection():void
55 {
56 if (connection != null)
57 {
58 connection.removeEventListener(NetStatusEvent.NET_STATUS, connectionStatusHandler);
59 connection.removeEventListener(IOErrorEvent.IO_ERROR, connectionIOErrorHandler);
60 connection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR , connectionSecurityErrorHandler);
61
62 connection = null;
63 }
64 }
65
66 /**
67 * Callback handler for receiving SecurityErrorEvent
68 * @param event SecurityErrorEvent
69 *
70 */
71 protected function connectionSecurityErrorHandler(event: SecurityErrorEvent): void
72 {
73 result.addError(this, new IllegalOperationError(event.toString()));
74 isComplete = true;
75 }
76
77 /**
78 * Callback handler for receiving IOErrorEvent
79 * @param event IOErrorEvent
80 *
81 */
82 protected function connectionIOErrorHandler(event: IOErrorEvent): void
83 {
84 result.addError(this, new IllegalOperationError(event.toString()));
85 isComplete = true;
86 }
87
88 /**
89 * Callback handler for receiving NetStatusEvent
90 * @param event NetStatusEvent
91 *
92 */
93 protected function connectionStatusHandler(event: NetStatusEvent): void
94 {
95
96 }
97
98 /**
99 * Connects the gateway
100 *
101 * @param $gateway String Remote gateway
102 * @param $encoding uint Object encoding using either AMF0 or AMF3
103 *
104 */
105 protected function connect ($gateway: String = null, $encoding: uint = 0): void
106 {
107 initConnection();
108
109 connection.objectEncoding = ($encoding > ObjectEncoding.AMF0) ? $encoding : ObjectEncoding.AMF0;
110
111 try {
112 connection.connect($gateway);
113 }
114 catch(error: Error)
115 {
116 result.addError(this, error);
117 }
118 };
119
120 /**
121 * Calls a remote service method and test it
122 *
123 * @param $method String Remote service
124 * @param $responder Responder Responder to handle remoting calls
125 * @param $arguments Array Rest paramaters (optional)
126 *
127 */
128 protected function call ($method: String = null, $responder: Responder = null, ...$arguments): void
129 {
130 var hasReferenceError: Boolean = false;
131
132 // parameters for calling connection.call();
133 // To avoid using the type unsafe ...rest operator I decided to use type safe parameters within RemotingTestCase.call()
134 // and apply these later to connection.call();
135 var params: Array = [];
136
137 // check remote method
138 if ($method != null)
139 {
140 params.push($method);
141 }
142 else
143 {
144 result.addError(this, new ReferenceError("RemotingTestCase.call() has to defined a remote method."));
145 hasReferenceError = true;
146 }
147
148 // check responder
149 if ($responder != null)
150 {
151 params.push($responder);
152 }
153 else
154 {
155 result.addError(this, new ReferenceError("RemotingTestCase.call() has to defined a responder to handling its results."));
156 hasReferenceError = true;
157 }
158
159 // In case of a reference error invoke test running instantly
160 // to show the errors created above and return
161 if (hasReferenceError)
162 {
163 super.run();
164 return;
165 }
166
167
168 var arrIterator: ArrayIterator = new ArrayIterator($arguments);
169 while (arrIterator.hasNext())
170 {
171 params.push(arrIterator.next());
172 }
173
174 // call remote service
175 try {
176 connection.call.apply(null, params);
177 }
178 catch(error: Error)
179 {
180 result.addError(this, error);
181 }
182
183
184 };
185 }
186 }