001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.simulator.container;
005:
006: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
007:
008: import com.tc.simulator.app.Application;
009: import com.tc.simulator.app.ApplicationBuilder;
010: import com.tc.simulator.app.ApplicationInstantiationException;
011: import com.tc.simulator.app.ErrorContext;
012: import com.tc.simulator.app.MockApplication;
013: import com.tc.simulator.control.MockControl;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tc.simulator.listener.MockResultsListener;
016: import com.tcsimulator.container.ContainerStateFactoryObject;
017: import com.tctest.runner.TestGlobalIdGenerator;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: public class ContainerTest extends TestCase {
025: private Container container;
026: private MockContainerConfig config;
027: private ContainerStateFactory containerStateFactory;
028: private TestGlobalIdGenerator globalIdGenerator;
029: private MockControl control;
030: private MockApplicationBuilder applicationBuilder;
031: private MockResultsListener resultsListener;
032:
033: protected void setUp() throws Exception {
034: super .setUp();
035:
036: this .config = new MockContainerConfig();
037: this .config.containerStartTimeout = 10 * 1000;
038: this .config.applicationStartTimeout = 10 * 1000;
039: this .config.applicationExecutionTimeout = 10 * 1000;
040: this .config.instanceCount = 10;
041:
042: this .containerStateFactory = new ContainerStateFactoryObject(
043: new LinkedQueue());
044: this .globalIdGenerator = new TestGlobalIdGenerator();
045: this .control = new MockControl();
046: this .resultsListener = new MockResultsListener();
047: this .applicationBuilder = new MockApplicationBuilder();
048: this .applicationBuilder.result = true;
049: this .container = new Container(this .config,
050: this .containerStateFactory, this .globalIdGenerator,
051: this .control, this .resultsListener,
052: this .applicationBuilder);
053: }
054:
055: protected void tearDown() throws Exception {
056: super .tearDown();
057: assertNotifyCompleteCalled();
058: }
059:
060: public void testInvalidContainerStartTimeout() throws Exception {
061: this .config.containerStartTimeout = 0;
062: this .container.run();
063: assertResult(false);
064: assertContainerConfigException(ContainerConfigException.INVALID_CONTAINER_START_TIMEOUT);
065:
066: }
067:
068: public void testInvalidApplicationStartTimeout() throws Exception {
069: this .config.applicationStartTimeout = 0;
070: this .container.run();
071: assertResult(false);
072: assertContainerConfigException(ContainerConfigException.INVALID_APPLICATION_START_TIMEOUT);
073: }
074:
075: public void testInvalidApplicationExecutionTimeout()
076: throws Exception {
077: this .config.applicationExecutionTimeout = 0;
078: this .container.run();
079: assertResult(false);
080: assertContainerConfigException(ContainerConfigException.INVALID_APPLICATION_EXECUTION_TIMEOUT);
081: }
082:
083: public void testApplicationInvalidInstanceCount() throws Exception {
084: this .config.instanceCount = 0;
085: this .container.run();
086: assertResult(false);
087: assertContainerConfigException(ContainerConfigException.INVALID_APPLICATION_INSTANCE_COUNT);
088: }
089:
090: public void testExecutionTimeout() throws Exception {
091: this .config.applicationExecutionTimeout = 500;
092: this .applicationBuilder.waitInterval = 2000;
093:
094: this .container.run();
095:
096: assertTrue(resultsListener.notifyExecutionTimeoutCalled);
097: assertFalse(resultsListener.notifyStartTimeoutCalled);
098: assertTrue(control.notifyCompleteCalled);
099: assertResult(false);
100: }
101:
102: public void testAppBuilderThrowsException() throws Exception {
103: this .applicationBuilder.throwException = true;
104: this .applicationBuilder.exception = new ApplicationInstantiationException(
105: new RuntimeException());
106:
107: this .container.run();
108:
109: assertResult(false);
110: assertSame(this .applicationBuilder.exception, extractError());
111: }
112:
113: public void testNormalRun() throws Exception {
114: this .config.dumpErrors = true;
115: this .config.dumpOutput = true;
116: this .container.run();
117: assertResult(true);
118: ContainerResult result = extractResult();
119: assertEquals(config.getApplicationInstanceCount(), result
120: .getApplicationInstanceCount());
121: }
122:
123: private void assertContainerConfigException(
124: ContainerConfigException.Reason reason) {
125: assertEquals(1, this .resultsListener.errors.size());
126: ErrorContext err = (ErrorContext) this .resultsListener.errors
127: .get(0);
128: assertTrue(err.getThrowable() instanceof ContainerConfigException);
129: ContainerConfigException e = (ContainerConfigException) err
130: .getThrowable();
131: assertSame(reason, e.getReason());
132: }
133:
134: private void assertNotifyCompleteCalled() throws Exception {
135: assertTrue(this .control.notifyCompleteCalled);
136: }
137:
138: private void assertResult(boolean b) throws Exception {
139: ContainerResult result = extractResult();
140: assertNotNull(result);
141: assertEquals(b, result.success());
142: }
143:
144: private ContainerResult extractResult() throws Exception {
145: return (ContainerResult) this .resultsListener.result;
146: }
147:
148: private Throwable extractError() throws Exception {
149: assertEquals(1, this .resultsListener.errors.size());
150: ErrorContext err = (ErrorContext) this .resultsListener.errors
151: .get(0);
152: return err.getThrowable();
153: }
154:
155: static class MockApplicationBuilder implements ApplicationBuilder {
156: public List applications = new ArrayList();
157: public boolean throwException;
158: public ApplicationInstantiationException exception;
159: public long waitInterval;
160: public boolean result;
161:
162: public synchronized Application newApplication(String appId,
163: ListenerProvider listenerProvider)
164: throws ApplicationInstantiationException {
165:
166: if (this .throwException)
167: throw exception;
168:
169: MockApplication rv = new MockApplication();
170: rv.waitInterval = this .waitInterval;
171: rv.result = result;
172: applications.add(rv);
173: return rv;
174: }
175:
176: public ClassLoader getContextClassLoader() {
177: return getClass().getClassLoader();
178: }
179:
180: public void setAppConfigAttribute(String key, String value) {
181: throw new AssertionError(
182: "This method needs to be implemented");
183: }
184:
185: }
186:
187: public class MockContainerConfig implements ContainerConfig {
188: public int instanceCount;
189: public long containerStartTimeout;
190: public long applicationStartTimeout;
191: public long applicationExecutionTimeout;
192: public boolean dumpErrors;
193: public boolean dumpOutput;
194: public boolean aggregate;
195: public boolean stream;
196: public boolean isMaster;
197:
198: public int getApplicationInstanceCount() {
199: return this .instanceCount;
200: }
201:
202: public long getContainerStartTimeout() {
203: return this .containerStartTimeout;
204: }
205:
206: public long getApplicationStartTimeout() {
207: return this .applicationStartTimeout;
208: }
209:
210: public long getApplicationExecutionTimeout() {
211: return this .applicationExecutionTimeout;
212: }
213:
214: public boolean dumpErrors() {
215: return this .dumpErrors;
216: }
217:
218: public boolean dumpOutput() {
219: return this .dumpOutput;
220: }
221:
222: public boolean aggregate() {
223: return this .aggregate;
224: }
225:
226: public boolean stream() {
227: return this .stream;
228: }
229:
230: public boolean isMaster() {
231: return this.isMaster;
232: }
233:
234: }
235:
236: }
|