01: package com.mockrunner.example.struts;
02:
03: import com.mockrunner.mock.web.ActionMockObjectFactory;
04: import com.mockrunner.struts.ActionTestModule;
05: import com.mockrunner.struts.BasicActionTestCaseAdapter;
06:
07: /**
08: * Example test for {@link StoreDataAction}.
09: * Demonstrates multithread testing. The use of an inner thread
10: * class and a single test method is a standard pattern that can
11: * be used in any multithreading test for Servlets and actions.
12: * Please note that each thread has to call the action with
13: * different sessions and requests but with one
14: * <code>ServletContext</code> in order to simulate the real
15: * container behaviour. See the constructor of the thread how
16: * to deal with the test module and factory.
17: * Remove the <code>synchronized</code> keyword in
18: * {@link StoreDataAction} and the test
19: * will fail.
20: * {@link com.mockrunner.base.MultiThreadTestSuite} is meant to
21: * make multithread testing much easier, but it is not working
22: * properly yet, so we do not use it here.
23: */
24: public class StoreDataActionTest extends BasicActionTestCaseAdapter {
25: private volatile int numberSuccess;
26:
27: protected void setUp() throws Exception {
28: super .setUp();
29: numberSuccess = 0;
30: }
31:
32: public void testStoreDataAction() throws Exception {
33: StoreTestThread thread1 = new StoreTestThread("thread1");
34: StoreTestThread thread2 = new StoreTestThread("thread2");
35: StoreTestThread thread3 = new StoreTestThread("thread3");
36: StoreTestThread thread4 = new StoreTestThread("thread4");
37: StoreTestThread thread5 = new StoreTestThread("thread5");
38: thread1.start();
39: thread2.start();
40: thread3.start();
41: thread4.start();
42: thread5.start();
43: thread1.join();
44: thread2.join();
45: thread3.join();
46: thread4.join();
47: thread5.join();
48: assertTrue(numberSuccess == 1);
49: }
50:
51: public class StoreTestThread extends Thread {
52: private ActionMockObjectFactory mockFactory;
53: private ActionTestModule module;
54:
55: public StoreTestThread(String name) {
56: super (name);
57: mockFactory = createActionMockObjectFactory(getActionMockObjectFactory());
58: module = createActionTestModule(mockFactory);
59: }
60:
61: public void run() {
62: module.addRequestParameter("id", "id");
63: module.addRequestParameter("data", getName());
64: module.actionPerform(StoreDataAction.class);
65: if (module.getActionForward().getPath().equals("success")) {
66: numberSuccess++;
67: }
68: }
69: }
70: }
|