1 20 package org.apache.cactus.integration.ant.container; 21 22 23 import java.net.URL ; 24 25 import org.apache.tools.ant.BuildException; 26 27 import junit.framework.TestCase; 28 29 34 public final class TestContainerRunner extends TestCase 35 { 36 37 39 42 private MockHttpServer server; 43 44 46 49 protected void setUp() throws Exception 50 { 51 int unusedPort = 52 MockHttpServer.findUnusedLocalPort("localhost", 8000, 8099); 53 if (unusedPort > 0) 54 { 55 this.server = new MockHttpServer(unusedPort); 56 this.server.expectMethod("GET"); 57 this.server.expectUri("/test"); 58 } 59 } 60 61 63 69 public void testSetInvalidUrl() throws Exception 70 { 71 ContainerRunner runner = 72 new ContainerRunner(new MockContainer(this.server)); 73 try 74 { 75 runner.setURL(new URL ("ftp://ftp.example.com/test/")); 76 fail("Expected IllegalArgumentException"); 77 } 78 catch (IllegalArgumentException expected) 79 { 80 } 82 } 83 84 90 public void testStartUpWithoutUrl() throws Exception 91 { 92 ContainerRunner runner = 93 new ContainerRunner(new MockContainer(this.server)); 94 try 95 { 96 runner.startUpContainer(); 97 fail("Expected IllegalStateException"); 98 } 99 catch (IllegalStateException expected) 100 { 101 } 103 } 104 105 111 public void testShutDownWithoutUrl() throws Exception 112 { 113 ContainerRunner runner = 114 new ContainerRunner(new MockContainer(this.server)); 115 try 116 { 117 runner.shutDownContainer(); 118 fail("Expected IllegalStateException"); 119 } 120 catch (IllegalStateException expected) 121 { 122 } 124 } 125 126 132 public void testStartUpShutDown() throws Exception 133 { 134 this.server.setResponse("HTTP/1.1 200 Ok\n\n"); 135 this.server.expectRequestCount(3); 136 137 ContainerRunner runner = 138 new ContainerRunner(new MockContainer(this.server)); 139 runner.setURL( 140 new URL ("http", "localhost", this.server.getPort(), "/test")); 141 runner.setTimeout(0); 142 runner.setCheckInterval(250); 143 runner.setShutDownWait(0); 144 runner.startUpContainer(); 145 runner.shutDownContainer(); 146 147 server.verify(); 148 } 149 150 156 public void testStartUpFailure() throws Exception 157 { 158 this.server.setResponse("HTTP/1.1 500 Internal Server Error\n\n"); 159 this.server.expectRequestCount(1); 160 161 ContainerRunner runner = 162 new ContainerRunner(new MockContainer(this.server)); 163 runner.setURL( 164 new URL ("http", "localhost", this.server.getPort(), "/test")); 165 runner.setTimeout(0); 166 runner.setCheckInterval(250); 167 runner.setShutDownWait(0); 168 try 169 { 170 runner.startUpContainer(); 171 fail("Expected BuildException"); 172 } 173 catch (BuildException expected) 174 { 175 } 177 178 server.stop(); 179 server.verify(); 180 } 181 182 189 public void testStartUpTimeout() throws Exception 190 { 191 this.server.setResponse("HTTP/1.1 500 Internal Server Error\n\n"); 192 this.server.expectRequestCount(-1); 194 ContainerRunner runner = 195 new ContainerRunner(new MockContainer(this.server)); 196 runner.setURL( 197 new URL ("http", "localhost", this.server.getPort(), "/test")); 198 runner.setTimeout(5000); 199 runner.setCheckInterval(250); 200 runner.setShutDownWait(0); 201 long startTime = System.currentTimeMillis(); 202 try 203 { 204 runner.startUpContainer(); 205 fail("Expected BuildException"); 206 } 207 catch (BuildException expected) 208 { 209 } 211 212 long time = System.currentTimeMillis() - startTime; 213 assertTrue("Process finished before the timeout was reached", 214 time > 5000); 215 assertTrue("Process took " + (time - 5000) + "ms longer than the " 216 + "timeout period", time < 10000); 217 218 server.stop(); 219 server.verify(); 220 } 221 222 228 public void testIgnoreAlreadyRunning() throws Exception 229 { 230 this.server.setResponse("HTTP/1.1 200 Ok\n\n"); 231 this.server.expectRequestCount(1); 232 Thread thread = new Thread (this.server); 233 thread.start(); 234 235 MockContainer container = new MockContainer(this.server); 236 container.expectStartUpCalled(false); 237 container.expectShutDownCalled(false); 238 239 ContainerRunner runner = new ContainerRunner(container); 240 runner.setURL( 241 new URL ("http", "localhost", this.server.getPort(), "/test")); 242 runner.setTimeout(0); 243 runner.setCheckInterval(250); 244 runner.setShutDownWait(0); 245 runner.startUpContainer(); 246 runner.shutDownContainer(); 247 248 container.verify(); 249 250 server.stop(); 251 server.verify(); 252 } 253 254 } 255 | Popular Tags |