comparison bindings/as3/ext/asunit/framework/AsynchronousTestCaseExample.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 import flash.events.*;
3 import flash.net.URLLoader;
4 import flash.net.URLRequest;
5
6 /**
7 * This example is built on the following mock data:
8 * <books>
9 * <book publisher="Addison-Wesley" name="Design Patterns" />
10 * <book publisher="Addison-Wesley" name="The Pragmattic Programmer" />
11 * <book publisher="Addison-Wesley" name="Test Driven Development" />
12 * <book publisher="Addison-Wesley" name="Refactoring to Patterns" />
13 * <book publisher="O'Reilly Media" name="The Cathedral & the Bazaar" />
14 * <book publisher="O'Reilly Media" name="Unit Test Frameworks" />
15 * </books>
16 *
17 * This example was created to illustrate how one can build an synchronous
18 * TestCase - one that relies on remote data of some sort.
19 * This use case is now diminished by the creation of E4X and easily
20 * readable/editable XML data directly in source form.
21 * But asynchronous tests will probably need to be built at some point
22 * by somebody... If you're them, maybe you can use this as a template.
23 */
24
25 public class AsynchronousTestCaseExample extends AsynchronousTestCase {
26 private var source:String = "asunit/framework/MockData.xml";
27 private var dataSource:XML;
28 private var instance:Object;
29
30 // Override the run method and begin the request for remote data
31 public override function run():void {
32 var request:URLRequest = new URLRequest(source);
33 var loader:URLLoader = new URLLoader();
34 // configureListeners is a method on the AsynchronousTestCase
35 // and it will handle error states by failing loudly...
36 configureListeners(loader);
37 loader.load(request);
38
39 // call super.run() to start network duration:
40 super.run();
41 }
42
43 protected override function setDataSource(event:Event):void {
44 // put a copy of the data into a member reference
45 if (event == null)
46 {
47 dataSource = null;
48 }
49 else
50 {
51 dataSource = XML(event.target.data).copy();
52 }
53 }
54
55 protected override function setUp():void {
56 // create a new instance of the class under test
57 instance = new Object();
58 if (dataSource != null) // i.e. there was no IOError or SecurityError
59 {
60 // copy the data into a member or method of the _instance
61 instance.data = dataSource.copy();
62 }
63 }
64
65 protected override function tearDown():void {
66 // destroy the class under test instance
67 instance = null;
68 }
69
70 public function testBookCount():void {
71 var data:XML = XML(instance.data);
72 var list:XMLList = data..book;
73 assertTrue("list.length() == " + list.length() + " (6?)", list.length() == 6);
74 }
75
76 public function testOReillyBookCount():void {
77 var data:XML = XML(instance.data);
78 var list:XMLList = data..book.(@publisher == "O'Reilly Media");
79 assertTrue("list.length() == " + list.length() + " (2?)", list.length() == 2);
80 }
81 }
82 }