/**
* Copyright (c) 2009 Digital Primates IT Consulting Group
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* @author Michael Labriola
* @version
**/
package org.flexunit.async {
import flash.utils.Dictionary;
import org.flexunit.AssertionError;
import org.flexunit.internals.runners.statements.IAsyncHandlingStatement;
/**
* The AsyncLocator is used to keep track of test cases that have implemented asynchronous
* functionality. The test cases are registered with the AsyncLocator and reference an
* object that implements IAsyncHandlingStatement. Test cases are registered using the
* method #registerStatementForTest().
*
* The IAsyncHandlingStatement is then retrieved using the method #getCallableForTest()
* and providing the test case. If a test case has not been registered, an AssertionError will be
* thrown.
*
* Once an asynchronous test has completed, the method #cleanUpCallableForTest should be called in
* order to disassociate the test case and the IAsyncHandlingStatement.
*
* @see org.flexunit.async.Async
*/
public class AsyncLocator {
/**
* @private
*/
private static var asyncHandlerMap:Dictionary = new Dictionary();
/**
* Registers the expectAsyncInstance with the provided testCase.
*
* @param expectAsyncInstance the IAsyncHandlingStatement to be registered.
* @param testCase The test case to associate with the particular expectAsyncInstance.
*/
public static function registerStatementForTest( expectAsyncInstance:IAsyncHandlingStatement, testCase:Object ):void {
asyncHandlerMap[ testCase ] = expectAsyncInstance;
}
/**
* Retrieves the IAsyncHandlingStatement for the provided testCase. If no
* IAsyncHandlingStatement has been registered for the testCase, an
* AssertionError will be thrown.
*
* @param testCase The test case used to retrieve the IAsyncHandlingStatement.
*
* @return an IAsyncHandlingStatement associated with the testCase.
*
* @throws org.flexunit.AssertionError Thrown if an IAsyncHandlingStatement was not registered
* for the provided testCase.
*/
public static function getCallableForTest( testCase:Object ):IAsyncHandlingStatement {
var handler:IAsyncHandlingStatement = asyncHandlerMap[ testCase ];
//If no handler was obtained from the dictionary, the test case was never marked as asynchronous, throw an AssertionError
if ( !handler ) {
throw new AssertionError("Cannot add asynchronous functionality to methods defined by Test,Before or After that are not marked async");
}
return handler;
}
/**
* Removes the registration for the IAsyncHandlingStatement that was associated with the
* provided testCase.
*
* @param testCase The test case to remove the association with the IAsyncHandlingStatement.
*/
public static function cleanUpCallableForTest( testCase:Object ):void {
delete asyncHandlerMap[ testCase ];
}
/* private static var instance:AsyncLocator;
public static function getInstance():AsyncLocator {
if ( !instance ) {
instance = new AsyncLocator();
}
return instance;
}
public function AsyncLocator() {
callableMap = new Dictionary();
}
*/ }
}