annotate bindings/as3/ext/asunit/framework/RemotingTestCase.as @ 770:c54bc2ffbf92 tip

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