KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > simulator > container > ExecutionInstanceTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.simulator.container;
5
6 import com.tc.simulator.app.ErrorContext;
7 import com.tc.simulator.app.MockApplication;
8 import com.tc.simulator.control.MockControl;
9 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 JavaDoc;
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 JavaDoc {
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, this.resultsListener, application,
33                                                    new TestStatsListenerFactory());
34   }
35
36   public void testStartTimeoutLessThanOne() throws Exception JavaDoc {
37     this.config.startTimeout = 0;
38     this.executionInstance.run();
39     assertEquals(Boolean.FALSE, this.resultsListener.result);
40     assertEquals(1, resultsListener.errors.size());
41     ErrorContext err = (ErrorContext) resultsListener.errors.get(0);
42     assertTrue(err.getThrowable() instanceof ApplicationRunnerConfigException);
43     assertTrue(control.notifyCompleteCalled);
44   }
45
46   public void testStartTimeout() throws Exception JavaDoc {
47     this.control.throwTimeoutExceptionInWaitForStart = true;
48     this.executionInstance.run();
49     assertEquals(Boolean.FALSE, this.resultsListener.result);
50     assertTrue(this.resultsListener.notifyStartTimeoutCalled);
51     assertTrue(control.notifyCompleteCalled);
52   }
53
54   public void testBasicRun() throws Exception JavaDoc {
55     this.executionInstance.run();
56     assertTrue(this.control.waitForStartCalled);
57     assertTrue(this.control.notifyCompleteCalled);
58
59     assertEquals(0, this.resultsListener.errors.size());
60     assertEquals(Boolean.TRUE, this.resultsListener.result);
61     assertTrue(control.notifyCompleteCalled);
62   }
63
64   public void testExceptionInApplication() throws Exception JavaDoc {
65     this.application.throwException = true;
66     this.application.exception = new RuntimeException JavaDoc("This is a test exception. Don't worry about me.");
67     this.executionInstance.run();
68     assertEquals(1, this.resultsListener.errors.size());
69     ErrorContext err = (ErrorContext) this.resultsListener.errors.get(0);
70     assertSame(application.exception, err.getThrowable());
71     assertEquals(Boolean.FALSE, this.resultsListener.result);
72     assertTrue(control.notifyCompleteCalled);
73   }
74
75   private static class MockExecutionInstanceConfig implements ApplicationRunnerConfig {
76
77     public long startTimeout;
78
79     public long getStartTimeout() {
80       return this.startTimeout;
81     }
82
83   }
84
85   private static final class TestStatsListenerFactory implements StatsListenerFactory {
86
87     public StatsListener newStatsListener(Properties JavaDoc properties) {
88       return new MockStatsListener();
89     }
90     
91   }
92   
93 }
Popular Tags