1 4 package com.tc.simulator.container; 5 6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 7 8 import com.tc.simulator.app.Application; 9 import com.tc.simulator.app.ApplicationBuilder; 10 import com.tc.simulator.app.ApplicationInstantiationException; 11 import com.tc.simulator.app.ErrorContext; 12 import com.tc.simulator.app.MockApplication; 13 import com.tc.simulator.control.MockControl; 14 import com.tc.simulator.listener.ListenerProvider; 15 import com.tc.simulator.listener.MockResultsListener; 16 import com.tcsimulator.container.ContainerStateFactoryObject; 17 import com.tctest.runner.TestGlobalIdGenerator; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import junit.framework.TestCase; 23 24 public class ContainerTest extends TestCase { 25 private Container container; 26 private MockContainerConfig config; 27 private ContainerStateFactory containerStateFactory; 28 private TestGlobalIdGenerator globalIdGenerator; 29 private MockControl control; 30 private MockApplicationBuilder applicationBuilder; 31 private MockResultsListener resultsListener; 32 33 protected void setUp() throws Exception { 34 super.setUp(); 35 36 this.config = new MockContainerConfig(); 37 this.config.containerStartTimeout = 10 * 1000; 38 this.config.applicationStartTimeout = 10 * 1000; 39 this.config.applicationExecutionTimeout = 10 * 1000; 40 this.config.instanceCount = 10; 41 42 this.containerStateFactory = new ContainerStateFactoryObject(new LinkedQueue()); 43 this.globalIdGenerator = new TestGlobalIdGenerator(); 44 this.control = new MockControl(); 45 this.resultsListener = new MockResultsListener(); 46 this.applicationBuilder = new MockApplicationBuilder(); 47 this.applicationBuilder.result = true; 48 this.container = new Container(this.config, this.containerStateFactory, this.globalIdGenerator, this.control, 49 this.resultsListener, this.applicationBuilder); 50 } 51 52 protected void tearDown() throws Exception { 53 super.tearDown(); 54 assertNotifyCompleteCalled(); 55 } 56 57 public void testInvalidContainerStartTimeout() throws Exception { 58 this.config.containerStartTimeout = 0; 59 this.container.run(); 60 assertResult(false); 61 assertContainerConfigException(ContainerConfigException.INVALID_CONTAINER_START_TIMEOUT); 62 63 } 64 65 public void testInvalidApplicationStartTimeout() throws Exception { 66 this.config.applicationStartTimeout = 0; 67 this.container.run(); 68 assertResult(false); 69 assertContainerConfigException(ContainerConfigException.INVALID_APPLICATION_START_TIMEOUT); 70 } 71 72 public void testInvalidApplicationExecutionTimeout() throws Exception { 73 this.config.applicationExecutionTimeout = 0; 74 this.container.run(); 75 assertResult(false); 76 assertContainerConfigException(ContainerConfigException.INVALID_APPLICATION_EXECUTION_TIMEOUT); 77 } 78 79 public void testApplicationInvalidInstanceCount() throws Exception { 80 this.config.instanceCount = 0; 81 this.container.run(); 82 assertResult(false); 83 assertContainerConfigException(ContainerConfigException.INVALID_APPLICATION_INSTANCE_COUNT); 84 } 85 86 public void testContainerStartTimeout() throws Exception { 87 this.control.throwTimeoutExceptionInWaitForStart = true; 88 this.container.run(); 89 assertResult(false); 90 assertTrue(resultsListener.notifyStartTimeoutCalled); 91 } 92 93 public void testExecutionTimeout() throws Exception { 94 this.config.applicationExecutionTimeout = 500; 95 this.applicationBuilder.waitInterval = 2000; 96 97 this.container.run(); 98 99 assertTrue(resultsListener.notifyExecutionTimeoutCalled); 100 assertFalse(resultsListener.notifyStartTimeoutCalled); 101 assertTrue(control.notifyCompleteCalled); 102 assertResult(false); 103 } 104 105 public void testAppBuilderThrowsException() throws Exception { 106 this.applicationBuilder.throwException = true; 107 this.applicationBuilder.exception = new ApplicationInstantiationException(new RuntimeException ()); 108 109 this.container.run(); 110 111 assertResult(false); 112 assertSame(this.applicationBuilder.exception, extractError()); 113 } 114 115 public void testNormalRun() throws Exception { 116 this.config.dumpErrors = true; 117 this.config.dumpOutput = true; 118 this.container.run(); 119 assertResult(true); 120 ContainerResult result = extractResult(); 121 assertEquals(config.getApplicationInstanceCount(), result.getApplicationInstanceCount()); 122 } 123 124 private void assertContainerConfigException(ContainerConfigException.Reason reason) { 125 assertEquals(1, this.resultsListener.errors.size()); 126 ErrorContext err = (ErrorContext) this.resultsListener.errors.get(0); 127 assertTrue(err.getThrowable() instanceof ContainerConfigException); 128 ContainerConfigException e = (ContainerConfigException) err.getThrowable(); 129 assertSame(reason, e.getReason()); 130 } 131 132 private void assertNotifyCompleteCalled() throws Exception { 133 assertTrue(this.control.notifyCompleteCalled); 134 } 135 136 private void assertResult(boolean b) throws Exception { 137 ContainerResult result = extractResult(); 138 assertNotNull(result); 139 assertEquals(b, result.success()); 140 } 141 142 private ContainerResult extractResult() throws Exception { 143 return (ContainerResult) this.resultsListener.result; 144 } 145 146 private Throwable extractError() throws Exception { 147 assertEquals(1, this.resultsListener.errors.size()); 148 ErrorContext err = (ErrorContext) this.resultsListener.errors.get(0); 149 return err.getThrowable(); 150 } 151 152 static class MockApplicationBuilder implements ApplicationBuilder { 153 public List applications = new ArrayList (); 154 public boolean throwException; 155 public ApplicationInstantiationException exception; 156 public long waitInterval; 157 public boolean result; 158 159 public synchronized Application newApplication(String appId, ListenerProvider listenerProvider) 160 throws ApplicationInstantiationException { 161 162 if (this.throwException) throw exception; 163 164 MockApplication rv = new MockApplication(); 165 rv.waitInterval = this.waitInterval; 166 rv.result = result; 167 applications.add(rv); 168 return rv; 169 } 170 171 public ClassLoader getContextClassLoader() { 172 return getClass().getClassLoader(); 173 } 174 175 } 176 177 public class MockContainerConfig implements ContainerConfig { 178 public int instanceCount; 179 public long containerStartTimeout; 180 public long applicationStartTimeout; 181 public long applicationExecutionTimeout; 182 public boolean dumpErrors; 183 public boolean dumpOutput; 184 public boolean aggregate; 185 public boolean stream; 186 public boolean isMaster; 187 188 public int getApplicationInstanceCount() { 189 return this.instanceCount; 190 } 191 192 public long getContainerStartTimeout() { 193 return this.containerStartTimeout; 194 } 195 196 public long getApplicationStartTimeout() { 197 return this.applicationStartTimeout; 198 } 199 200 public long getApplicationExecutionTimeout() { 201 return this.applicationExecutionTimeout; 202 } 203 204 public boolean dumpErrors() { 205 return this.dumpErrors; 206 } 207 208 public boolean dumpOutput() { 209 return this.dumpOutput; 210 } 211 212 public boolean aggregate() { 213 return this.aggregate; 214 } 215 216 public boolean stream() { 217 return this.stream; 218 } 219 220 public boolean isMaster() { 221 return this.isMaster; 222 } 223 224 } 225 226 } | Popular Tags |