mas01mj@732: package asunit.framework { mas01mj@732: import flash.events.*; mas01mj@732: import flash.net.URLLoader; mas01mj@732: import flash.net.URLRequest; mas01mj@732: mas01mj@732: /** mas01mj@732: * This example is built on the following mock data: mas01mj@732: * mas01mj@732: * mas01mj@732: * mas01mj@732: * mas01mj@732: * mas01mj@732: * mas01mj@732: * mas01mj@732: * mas01mj@732: * mas01mj@732: * This example was created to illustrate how one can build an synchronous mas01mj@732: * TestCase - one that relies on remote data of some sort. mas01mj@732: * This use case is now diminished by the creation of E4X and easily mas01mj@732: * readable/editable XML data directly in source form. mas01mj@732: * But asynchronous tests will probably need to be built at some point mas01mj@732: * by somebody... If you're them, maybe you can use this as a template. mas01mj@732: */ mas01mj@732: mas01mj@732: public class AsynchronousTestCaseExample extends AsynchronousTestCase { mas01mj@732: private var source:String = "asunit/framework/MockData.xml"; mas01mj@732: private var dataSource:XML; mas01mj@732: private var instance:Object; mas01mj@732: mas01mj@732: // Override the run method and begin the request for remote data mas01mj@732: public override function run():void { mas01mj@732: var request:URLRequest = new URLRequest(source); mas01mj@732: var loader:URLLoader = new URLLoader(); mas01mj@732: // configureListeners is a method on the AsynchronousTestCase mas01mj@732: // and it will handle error states by failing loudly... mas01mj@732: configureListeners(loader); mas01mj@732: loader.load(request); mas01mj@732: mas01mj@732: // call super.run() to start network duration: mas01mj@732: super.run(); mas01mj@732: } mas01mj@732: mas01mj@732: protected override function setDataSource(event:Event):void { mas01mj@732: // put a copy of the data into a member reference mas01mj@732: if (event == null) mas01mj@732: { mas01mj@732: dataSource = null; mas01mj@732: } mas01mj@732: else mas01mj@732: { mas01mj@732: dataSource = XML(event.target.data).copy(); mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: protected override function setUp():void { mas01mj@732: // create a new instance of the class under test mas01mj@732: instance = new Object(); mas01mj@732: if (dataSource != null) // i.e. there was no IOError or SecurityError mas01mj@732: { mas01mj@732: // copy the data into a member or method of the _instance mas01mj@732: instance.data = dataSource.copy(); mas01mj@732: } mas01mj@732: } mas01mj@732: mas01mj@732: protected override function tearDown():void { mas01mj@732: // destroy the class under test instance mas01mj@732: instance = null; mas01mj@732: } mas01mj@732: mas01mj@732: public function testBookCount():void { mas01mj@732: var data:XML = XML(instance.data); mas01mj@732: var list:XMLList = data..book; mas01mj@732: assertTrue("list.length() == " + list.length() + " (6?)", list.length() == 6); mas01mj@732: } mas01mj@732: mas01mj@732: public function testOReillyBookCount():void { mas01mj@732: var data:XML = XML(instance.data); mas01mj@732: var list:XMLList = data..book.(@publisher == "O'Reilly Media"); mas01mj@732: assertTrue("list.length() == " + list.length() + " (2?)", list.length() == 2); mas01mj@732: } mas01mj@732: } mas01mj@732: }