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