01: /*
02: *
03: * Copyright (c) 2003 Adrian Price. All rights reserved.
04: */
05:
06: package org.obe.test.standalone;
07:
08: import junit.framework.TestCase;
09: import junit.framework.TestSuite;
10: import org.apache.commons.logging.Log;
11: import org.apache.commons.logging.LogFactory;
12: import org.obe.spi.service.AsyncManager;
13: import org.obe.spi.service.ServiceManager;
14:
15: /**
16: * Tests the AsyncManager
17: *
18: * @see AsyncManager
19: * @author Adrian Price
20: */
21: public class AsyncManagerTest extends TestCase {
22: private static final Log _logger = LogFactory
23: .getLog(AsyncManagerTest.class);
24: private static ServiceManager _svcMgr;
25: private static AsyncManager _asyncMgr;
26: private static final String[] TESTS = { "testAsyncRequest" };
27: private static boolean _initialized;
28:
29: private static class TestRequest implements
30: AsyncManager.AsyncRequest {
31: public boolean ran;
32:
33: public void run() {
34: ran = true;
35: }
36: }
37:
38: // It is necessary to build the test suite explicitly because JUnit
39: // sometimes changes the order in which the tests are executed. The tests
40: // in this suite depend upon each other, and must therefore be executed in
41: // the correct sequence.
42: public static junit.framework.Test suite() {
43: TestSuite suite = new TestSuite();
44: for (int i = 0; i < TESTS.length; i++)
45: suite.addTest(new AsyncManagerTest(TESTS[i]));
46: return suite;
47: }
48:
49: public AsyncManagerTest(String name) {
50: super (name);
51: }
52:
53: public void setUp() {
54: if (!_initialized) {
55: // Must use the default OBE Service Manager configuration, to avoid
56: // any problems caused, for example, by attempting to initialize
57: // J2EE-based services outside of a J2EE environment.
58: _svcMgr = new ServiceManager(ServiceManager.getDefaults());
59: _asyncMgr = _svcMgr.getAsyncManager();
60: _initialized = true;
61: _logger.info("_svcMgr = " + _svcMgr);
62: _logger.info("_asyncMgr = " + _asyncMgr);
63: }
64: }
65:
66: public void testAsyncRequest() {
67: TestRequest request = new TestRequest();
68: for (int i = 0; i < 25; i++) {
69: _logger.info("Executing async request #" + i);
70: _asyncMgr.asyncRequest(request);
71: try {
72: Thread.sleep(10);
73: } catch (InterruptedException e) {
74: }
75: assertTrue("Request did not execute", request.ran);
76: request.ran = false;
77: }
78: }
79: }
|