mas01mj@732: package asunit.framework mas01mj@732: { mas01mj@732: import flash.errors.IllegalOperationError; mas01mj@732: import flash.events.IOErrorEvent; mas01mj@732: import flash.events.NetStatusEvent; mas01mj@732: import flash.events.SecurityErrorEvent; mas01mj@732: import flash.net.NetConnection; mas01mj@732: import flash.net.ObjectEncoding; mas01mj@732: import flash.net.Responder; mas01mj@732: mas01mj@732: import asunit.framework.TestCase; mas01mj@732: import asunit.util.ArrayIterator; mas01mj@732: mas01mj@732: /** mas01mj@732: * RemotingTestCase mas01mj@732: * @author Jens Krause [www.websector.de] mas01mj@732: * @date 11/29/07 mas01mj@732: * mas01mj@732: */ mas01mj@732: public class RemotingTestCase extends TestCase mas01mj@732: { mas01mj@732: mas01mj@732: protected var connection: NetConnection; mas01mj@732: /** mas01mj@732: * Constructor mas01mj@732: * @param testMethod String Name of the test case mas01mj@732: * mas01mj@732: */ mas01mj@732: public function RemotingTestCase(testMethod: String = null) mas01mj@732: { mas01mj@732: super(testMethod); mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Inits a netConnection instance and add all necessary event listeners mas01mj@732: * mas01mj@732: */ mas01mj@732: protected function initConnection():void mas01mj@732: { mas01mj@732: if (connection == null) mas01mj@732: { mas01mj@732: connection = new NetConnection(); mas01mj@732: mas01mj@732: connection.addEventListener(NetStatusEvent.NET_STATUS, connectionStatusHandler); mas01mj@732: connection.addEventListener(IOErrorEvent.IO_ERROR, connectionIOErrorHandler); mas01mj@732: connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR , connectionSecurityErrorHandler); mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Dispose the netConnection instance mas01mj@732: * mas01mj@732: */ mas01mj@732: protected function disposeConnection():void mas01mj@732: { mas01mj@732: if (connection != null) mas01mj@732: { mas01mj@732: connection.removeEventListener(NetStatusEvent.NET_STATUS, connectionStatusHandler); mas01mj@732: connection.removeEventListener(IOErrorEvent.IO_ERROR, connectionIOErrorHandler); mas01mj@732: connection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR , connectionSecurityErrorHandler); mas01mj@732: mas01mj@732: connection = null; mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Callback handler for receiving SecurityErrorEvent mas01mj@732: * @param event SecurityErrorEvent mas01mj@732: * mas01mj@732: */ mas01mj@732: protected function connectionSecurityErrorHandler(event: SecurityErrorEvent): void mas01mj@732: { mas01mj@732: result.addError(this, new IllegalOperationError(event.toString())); mas01mj@732: isComplete = true; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Callback handler for receiving IOErrorEvent mas01mj@732: * @param event IOErrorEvent mas01mj@732: * mas01mj@732: */ mas01mj@732: protected function connectionIOErrorHandler(event: IOErrorEvent): void mas01mj@732: { mas01mj@732: result.addError(this, new IllegalOperationError(event.toString())); mas01mj@732: isComplete = true; mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Callback handler for receiving NetStatusEvent mas01mj@732: * @param event NetStatusEvent mas01mj@732: * mas01mj@732: */ mas01mj@732: protected function connectionStatusHandler(event: NetStatusEvent): void mas01mj@732: { mas01mj@732: mas01mj@732: } mas01mj@732: mas01mj@732: /** mas01mj@732: * Connects the gateway mas01mj@732: * mas01mj@732: * @param $gateway String Remote gateway mas01mj@732: * @param $encoding uint Object encoding using either AMF0 or AMF3 mas01mj@732: * mas01mj@732: */ mas01mj@732: protected function connect ($gateway: String = null, $encoding: uint = 0): void mas01mj@732: { mas01mj@732: initConnection(); mas01mj@732: mas01mj@732: connection.objectEncoding = ($encoding > ObjectEncoding.AMF0) ? $encoding : ObjectEncoding.AMF0; mas01mj@732: mas01mj@732: try { mas01mj@732: connection.connect($gateway); mas01mj@732: } mas01mj@732: catch(error: Error) mas01mj@732: { mas01mj@732: result.addError(this, error); mas01mj@732: } mas01mj@732: }; mas01mj@732: mas01mj@732: /** mas01mj@732: * Calls a remote service method and test it mas01mj@732: * mas01mj@732: * @param $method String Remote service mas01mj@732: * @param $responder Responder Responder to handle remoting calls mas01mj@732: * @param $arguments Array Rest paramaters (optional) mas01mj@732: * mas01mj@732: */ mas01mj@732: protected function call ($method: String = null, $responder: Responder = null, ...$arguments): void mas01mj@732: { mas01mj@732: var hasReferenceError: Boolean = false; mas01mj@732: mas01mj@732: // parameters for calling connection.call(); mas01mj@732: // To avoid using the type unsafe ...rest operator I decided to use type safe parameters within RemotingTestCase.call() mas01mj@732: // and apply these later to connection.call(); mas01mj@732: var params: Array = []; mas01mj@732: mas01mj@732: // check remote method mas01mj@732: if ($method != null) mas01mj@732: { mas01mj@732: params.push($method); mas01mj@732: } mas01mj@732: else mas01mj@732: { mas01mj@732: result.addError(this, new ReferenceError("RemotingTestCase.call() has to defined a remote method.")); mas01mj@732: hasReferenceError = true; mas01mj@732: } mas01mj@732: mas01mj@732: // check responder mas01mj@732: if ($responder != null) mas01mj@732: { mas01mj@732: params.push($responder); mas01mj@732: } mas01mj@732: else mas01mj@732: { mas01mj@732: result.addError(this, new ReferenceError("RemotingTestCase.call() has to defined a responder to handling its results.")); mas01mj@732: hasReferenceError = true; mas01mj@732: } mas01mj@732: mas01mj@732: // In case of a reference error invoke test running instantly mas01mj@732: // to show the errors created above and return mas01mj@732: if (hasReferenceError) mas01mj@732: { mas01mj@732: super.run(); mas01mj@732: return; mas01mj@732: } mas01mj@732: mas01mj@732: mas01mj@732: var arrIterator: ArrayIterator = new ArrayIterator($arguments); mas01mj@732: while (arrIterator.hasNext()) mas01mj@732: { mas01mj@732: params.push(arrIterator.next()); mas01mj@732: } mas01mj@732: mas01mj@732: // call remote service mas01mj@732: try { mas01mj@732: connection.call.apply(null, params); mas01mj@732: } mas01mj@732: catch(error: Error) mas01mj@732: { mas01mj@732: result.addError(this, error); mas01mj@732: } mas01mj@732: mas01mj@732: mas01mj@732: }; mas01mj@732: } mas01mj@732: }