01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.simulator.container;
05:
06: import com.tc.simulator.app.ErrorContext;
07: import com.tc.simulator.app.MockApplication;
08: import com.tc.simulator.control.MockControl;
09: import com.tc.simulator.listener.MockResultsListener;
10: import com.tc.simulator.listener.MockStatsListener;
11: import com.tc.simulator.listener.StatsListener;
12: import com.tc.simulator.listener.StatsListenerFactory;
13:
14: import java.util.Properties;
15:
16: import junit.framework.TestCase;
17:
18: public class ExecutionInstanceTest extends TestCase {
19:
20: ApplicationRunner executionInstance;
21: private MockExecutionInstanceConfig config;
22: private MockControl control;
23: private MockResultsListener resultsListener;
24: private MockApplication application;
25:
26: public void setUp() throws Exception {
27: this .config = new MockExecutionInstanceConfig();
28: this .config.startTimeout = 1;
29: this .control = new MockControl();
30: this .resultsListener = new MockResultsListener();
31: this .application = new MockApplication();
32: this .executionInstance = new ApplicationRunner(config, control,
33: this .resultsListener, application,
34: new TestStatsListenerFactory());
35: }
36:
37: public void testStartTimeoutLessThanOne() throws Exception {
38: this .config.startTimeout = 0;
39: this .executionInstance.run();
40: assertEquals(Boolean.FALSE, this .resultsListener.result);
41: assertEquals(1, resultsListener.errors.size());
42: ErrorContext err = (ErrorContext) resultsListener.errors.get(0);
43: assertTrue(err.getThrowable() instanceof ApplicationRunnerConfigException);
44: assertTrue(control.notifyCompleteCalled);
45: }
46:
47: public void testBasicRun() throws Exception {
48: this .executionInstance.run();
49: assertTrue(this .control.waitForStartCalled);
50: assertTrue(this .control.notifyCompleteCalled);
51:
52: assertEquals(0, this .resultsListener.errors.size());
53: assertEquals(Boolean.TRUE, this .resultsListener.result);
54: assertTrue(control.notifyCompleteCalled);
55: }
56:
57: public void testExceptionInApplication() throws Exception {
58: this .application.throwException = true;
59: this .application.exception = new RuntimeException(
60: "This is a test exception. Don't worry about me.");
61: this .executionInstance.run();
62: assertEquals(1, this .resultsListener.errors.size());
63: ErrorContext err = (ErrorContext) this .resultsListener.errors
64: .get(0);
65: assertSame(application.exception, err.getThrowable());
66: assertEquals(Boolean.FALSE, this .resultsListener.result);
67: assertTrue(control.notifyCompleteCalled);
68: }
69:
70: private static class MockExecutionInstanceConfig implements
71: ApplicationRunnerConfig {
72:
73: public long startTimeout;
74:
75: public long getStartTimeout() {
76: return this .startTimeout;
77: }
78:
79: }
80:
81: private static final class TestStatsListenerFactory implements
82: StatsListenerFactory {
83:
84: public StatsListener newStatsListener(Properties properties) {
85: return new MockStatsListener();
86: }
87:
88: }
89:
90: }
|