Skip to content

Commit 197f0ca

Browse files
author
Andy Adamczak
committed
Working on getting tests running in 2008 Microsoft test framework.
1 parent 0d20877 commit 197f0ca

29 files changed

+2668
-0
lines changed
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/*
2+
PureMVC C# Port by Andy Adamczak <andy.adamczak@puremvc.org>, et al.
3+
PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
4+
Your reuse is governed by the Creative Commons Attribution 3.0 License
5+
*/
6+
using System;
7+
using System.Threading;
8+
using System.Collections.Generic;
9+
10+
using NUnitLite;
11+
using NUnit.Framework;
12+
13+
using PureMVC.Interfaces;
14+
using PureMVC.Patterns;
15+
using PureMVC.Core;
16+
17+
namespace PureMVC.Tests.Core
18+
{
19+
/**
20+
* Test the PureMVC Controller class.
21+
*/
22+
[TestFixture]
23+
public class ControllerTest : TestCase
24+
{
25+
/**
26+
* Constructor.
27+
*
28+
* @param methodName the name of the test method an instance to run
29+
*/
30+
public ControllerTest(string methodName)
31+
: base(methodName)
32+
{ }
33+
34+
/**
35+
* Create the TestSuite.
36+
*/
37+
public static ITest Suite
38+
{
39+
get
40+
{
41+
TestSuite ts = new TestSuite(typeof(ControllerTest));
42+
43+
ts.AddTest(new ControllerTest("TestGetInstance"));
44+
ts.AddTest(new ControllerTest("TestRegisterAndExecuteCommand"));
45+
ts.AddTest(new ControllerTest("TestRegisterAndRemoveCommand"));
46+
ts.AddTest(new ControllerTest("TestHasCommand"));
47+
ts.AddTest(new ControllerTest("TestReregisterAndExecuteCommand"));
48+
ts.AddTest(new ControllerTest("TestMultiThreadedOperations"));
49+
50+
return ts;
51+
}
52+
}
53+
54+
/**
55+
* Tests the Controller Singleton Factory Method
56+
*/
57+
public void TestGetInstance()
58+
{
59+
// Test Factory Method
60+
IController controller = Controller.Instance;
61+
62+
// test assertions
63+
Assert.NotNull(controller, "Expecting instance not null");
64+
Assert.True(controller is IController, "Expecting instance implements IController");
65+
}
66+
67+
/**
68+
* Tests Command registration and execution.
69+
*
70+
* <P>
71+
* This test gets the Singleton Controller instance
72+
* and registers the ControllerTestCommand class
73+
* to handle 'ControllerTest' Notifications.<P>
74+
*
75+
* <P>
76+
* It then constructs such a Notification and tells the
77+
* Controller to execute the associated Command.
78+
* Success is determined by evaluating a property
79+
* on an object passed to the Command, which will
80+
* be modified when the Command executes.</P>
81+
*
82+
*/
83+
public void TestRegisterAndExecuteCommand()
84+
{
85+
// Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
86+
IController controller = Controller.Instance;
87+
string name = "ControllerTest" + Thread.CurrentThread.Name;
88+
controller.RegisterCommand(name, typeof(ControllerTestCommand));
89+
90+
// Create a 'ControllerTest' note
91+
ControllerTestVO vo = new ControllerTestVO(12);
92+
INotification note = new Notification(name, vo);
93+
94+
// Tell the controller to execute the Command associated with the note
95+
// the ControllerTestCommand invoked will multiply the vo.input value
96+
// by 2 and set the result on vo.result
97+
controller.ExecuteCommand(note);
98+
99+
// test assertions
100+
Assert.True(vo.result == 24, "Expecting vo.result == 24");
101+
}
102+
103+
/**
104+
* Tests Command registration and removal.
105+
*
106+
* <P>
107+
* Tests that once a Command is registered and verified
108+
* working, it can be removed from the Controller.</P>
109+
*/
110+
public void TestRegisterAndRemoveCommand()
111+
{
112+
113+
// Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
114+
IController controller = Controller.Instance;
115+
string name = "ControllerRemoveTest" + Thread.CurrentThread.Name;
116+
controller.RegisterCommand(name, typeof(ControllerTestCommand));
117+
118+
// Create a 'ControllerTest' note
119+
ControllerTestVO vo = new ControllerTestVO(12);
120+
INotification note = new Notification(name, vo);
121+
122+
// Tell the controller to execute the Command associated with the note
123+
// the ControllerTestCommand invoked will multiply the vo.input value
124+
// by 2 and set the result on vo.result
125+
controller.ExecuteCommand(note);
126+
127+
// test assertions
128+
Assert.True(vo.result == 24, "Expecting vo.result == 24");
129+
130+
// Reset result
131+
vo.result = 0;
132+
133+
// Remove the Command from the Controller
134+
controller.RemoveCommand(name);
135+
136+
// Tell the controller to execute the Command associated with the
137+
// note. This time, it should not be registered, and our vo result
138+
// will not change
139+
controller.ExecuteCommand(note);
140+
141+
// test assertions
142+
Assert.True(vo.result == 0, "Expecting vo.result == 0");
143+
144+
}
145+
146+
/**
147+
* Test hasCommand method.
148+
*/
149+
public void TestHasCommand() {
150+
// register the ControllerTestCommand to handle 'hasCommandTest' notes
151+
IController controller = Controller.Instance;
152+
string name = "HasCommandTest" + Thread.CurrentThread.Name;
153+
controller.RegisterCommand(name, typeof(ControllerTestCommand));
154+
155+
// test that hasCommand returns true for hasCommandTest notifications
156+
Assert.True(controller.HasCommand(name) == true, "Expecting controller.HasCommand(name) == true");
157+
158+
// Remove the Command from the Controller
159+
controller.RemoveCommand(name);
160+
161+
// test that hasCommand returns false for hasCommandTest notifications
162+
Assert.True(controller.HasCommand(name) == false, "Expecting controller.HasCommand(name) == false");
163+
}
164+
165+
/**
166+
* Tests Removing and Reregistering a Command
167+
*
168+
* <P>
169+
* Tests that when a Command is re-registered that it isn't fired twice.
170+
* This involves, minimally, registration with the controller but
171+
* notification via the View, rather than direct execution of
172+
* the Controller's executeCommand method as is done above in
173+
* testRegisterAndRemove. The bug under test was fixed in AS3 Standard
174+
* Version 2.0.2. If you run the unit tests with 2.0.1 this
175+
* test will fail.</P>
176+
*/
177+
public void TestReregisterAndExecuteCommand() {
178+
179+
// Fetch the controller, register the ControllerTestCommand2 to handle 'ControllerTest2' notes
180+
IController controller = Controller.Instance;
181+
string name = "ControllerTest2" + Thread.CurrentThread.Name;
182+
controller.RegisterCommand(name, typeof(ControllerTestCommand2));
183+
184+
// Remove the Command from the Controller
185+
controller.RemoveCommand(name);
186+
187+
// Re-register the Command with the Controller
188+
controller.RegisterCommand(name, typeof(ControllerTestCommand2));
189+
190+
// Create a 'ControllerTest2' note
191+
ControllerTestVO vo = new ControllerTestVO(12);
192+
Notification note = new Notification(name, vo);
193+
194+
// retrieve a reference to the View.
195+
IView view = View.Instance;
196+
197+
// send the Notification
198+
view.NotifyObservers(note);
199+
200+
// test assertions
201+
// if the command is executed once the value will be 24
202+
Assert.True(vo.result == 24, "Expecting vo.result == 24");
203+
204+
// Prove that accumulation works in the VO by sending the notification again
205+
view.NotifyObservers(note);
206+
207+
// if the command is executed twice the value will be 48
208+
Assert.True(vo.result == 48, "Expecting vo.result == 48");
209+
}
210+
211+
/// <summary>
212+
/// Test all of the function above using many threads at once.
213+
/// </summary>
214+
public void TestMultiThreadedOperations()
215+
{
216+
count = 20;
217+
IList<Thread> threads = new List<Thread>();
218+
219+
for (int i = 0; i < count; i++) {
220+
Thread t = new Thread(new ThreadStart(MultiThreadedTestFunction));
221+
t.Name = "ControllerTest" + i;
222+
threads.Add(t);
223+
}
224+
225+
foreach (Thread t in threads)
226+
{
227+
t.Start();
228+
}
229+
230+
while (true)
231+
{
232+
if (count <= 0) break;
233+
Thread.Sleep(100);
234+
}
235+
}
236+
237+
private int count = 0;
238+
239+
private int threadIterationCount = 10000;
240+
241+
private void MultiThreadedTestFunction()
242+
{
243+
for (int i = 0; i < threadIterationCount; i++)
244+
{
245+
// All we need to do is test the registration and removal of commands.
246+
TestRegisterAndExecuteCommand();
247+
TestRegisterAndRemoveCommand();
248+
TestHasCommand();
249+
TestReregisterAndExecuteCommand();
250+
}
251+
252+
count--;
253+
}
254+
}
255+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
PureMVC C# Port by Andy Adamczak <andy.adamczak@puremvc.org>, et al.
3+
PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
4+
Your reuse is governed by the Creative Commons Attribution 3.0 License
5+
*/
6+
using System;
7+
8+
using PureMVC.Interfaces;
9+
using PureMVC.Patterns;
10+
11+
namespace PureMVC.Tests.Core
12+
{
13+
/**
14+
* A SimpleCommand subclass used by ControllerTest.
15+
*
16+
* @see org.puremvc.csharp.core.controller.ControllerTest ControllerTest
17+
* @see org.puremvc.csharp.core.controller.ControllerTestVO ControllerTestVO
18+
*/
19+
public class ControllerTestCommand : SimpleCommand
20+
{
21+
/**
22+
* Constructor.
23+
*/
24+
public ControllerTestCommand()
25+
: base()
26+
{ }
27+
28+
/**
29+
* Fabricate a result by multiplying the input by 2
30+
*
31+
* @param note the note carrying the ControllerTestVO
32+
*/
33+
override public void Execute( INotification note )
34+
{
35+
36+
ControllerTestVO vo = (ControllerTestVO) note.Body;
37+
38+
// Fabricate a result
39+
vo.result = 2 * vo.input;
40+
41+
}
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
PureMVC C# Port by Andy Adamczak <andy.adamczak@puremvc.org>, et al.
3+
PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
4+
Your reuse is governed by the Creative Commons Attribution 3.0 License
5+
*/
6+
using System;
7+
8+
using PureMVC.Interfaces;
9+
using PureMVC.Patterns;
10+
11+
namespace PureMVC.Tests.Core
12+
{
13+
/**
14+
* A SimpleCommand subclass used by ControllerTest.
15+
*
16+
* @see org.puremvc.csharp.core.controller.ControllerTest ControllerTest
17+
* @see org.puremvc.csharp.core.controller.ControllerTestVO ControllerTestVO
18+
*/
19+
public class ControllerTestCommand2 : SimpleCommand
20+
{
21+
/**
22+
* Constructor.
23+
*/
24+
public ControllerTestCommand2()
25+
: base()
26+
{ }
27+
28+
/**
29+
* Fabricate a result by multiplying the input by 2
30+
*
31+
* @param note the note carrying the ControllerTestVO
32+
*/
33+
override public void Execute( INotification note )
34+
{
35+
36+
ControllerTestVO vo = (ControllerTestVO) note.Body;
37+
38+
// Fabricate a result
39+
vo.result = vo.result + (2 * vo.input);
40+
41+
}
42+
}
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
PureMVC C# Port by Andy Adamczak <andy.adamczak@puremvc.org>, et al.
3+
PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
4+
Your reuse is governed by the Creative Commons Attribution 3.0 License
5+
*/
6+
using System;
7+
8+
namespace PureMVC.Tests.Core
9+
{
10+
/**
11+
* A utility class used by ControllerTest.
12+
*
13+
* @see org.puremvc.csharp.core.controller.ControllerTest ControllerTest
14+
* @see org.puremvc.csharp.core.controller.ControllerTestCommand ControllerTestCommand
15+
*/
16+
public class ControllerTestVO
17+
{
18+
/**
19+
* Constructor.
20+
*
21+
* @param input the number to be fed to the ControllerTestCommand
22+
*/
23+
public ControllerTestVO (int input)
24+
{
25+
this.input = input;
26+
}
27+
28+
public int input;
29+
public int result;
30+
}
31+
}

0 commit comments

Comments
 (0)