001: /*
002: * ========================================================================
003: *
004: * Copyright 2003-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.container;
021:
022: import java.net.URL;
023:
024: import org.apache.tools.ant.BuildException;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Unit tests for {@link ContainerRunner}.
030: *
031: * @version $Id: TestContainerRunner.java 239003 2004-05-31 20:05:27Z vmassol $
032: */
033: public final class TestContainerRunner extends TestCase {
034:
035: // Instance Variables ------------------------------------------------------
036:
037: /**
038: * The dummy HTTP server used to test the container runner.
039: */
040: private MockHttpServer server;
041:
042: // TestCase Implementation -------------------------------------------------
043:
044: /**
045: * @see junit.framework.TestCase#setUp()
046: */
047: protected void setUp() throws Exception {
048: int unusedPort = MockHttpServer.findUnusedLocalPort(
049: "localhost", 8000, 8099);
050: if (unusedPort > 0) {
051: this .server = new MockHttpServer(unusedPort);
052: this .server.expectMethod("GET");
053: this .server.expectUri("/test");
054: }
055: }
056:
057: // Test Methods ------------------------------------------------------------
058:
059: /**
060: * Verifies that an exception is thrown when setting the URL property to a
061: * non-HTTP URL.
062: *
063: * @throws Exception If an unexpected error occurs
064: */
065: public void testSetInvalidUrl() throws Exception {
066: ContainerRunner runner = new ContainerRunner(new MockContainer(
067: this .server));
068: try {
069: runner.setURL(new URL("ftp://ftp.example.com/test/"));
070: fail("Expected IllegalArgumentException");
071: } catch (IllegalArgumentException expected) {
072: // expected
073: }
074: }
075:
076: /**
077: * Verifies that an exception is thrown if startUpContainer() is invoked
078: * before a URL has been set.
079: *
080: * @throws Exception If an unexpected error occurs
081: */
082: public void testStartUpWithoutUrl() throws Exception {
083: ContainerRunner runner = new ContainerRunner(new MockContainer(
084: this .server));
085: try {
086: runner.startUpContainer();
087: fail("Expected IllegalStateException");
088: } catch (IllegalStateException expected) {
089: // expected
090: }
091: }
092:
093: /**
094: * Verifies that an exception is thrown if shutDownContainer() is invoked
095: * before a URL has been set.
096: *
097: * @throws Exception If an unexpected error occurs
098: */
099: public void testShutDownWithoutUrl() throws Exception {
100: ContainerRunner runner = new ContainerRunner(new MockContainer(
101: this .server));
102: try {
103: runner.shutDownContainer();
104: fail("Expected IllegalStateException");
105: } catch (IllegalStateException expected) {
106: // expected
107: }
108: }
109:
110: /**
111: * Verifies that the runner correctly starts and stops a well-behaved
112: * container.
113: *
114: * @throws Exception If an unexpected error occurs
115: */
116: public void testStartUpShutDown() throws Exception {
117: this .server.setResponse("HTTP/1.1 200 Ok\n\n");
118: this .server.expectRequestCount(3);
119:
120: ContainerRunner runner = new ContainerRunner(new MockContainer(
121: this .server));
122: runner.setURL(new URL("http", "localhost", this .server
123: .getPort(), "/test"));
124: runner.setTimeout(0);
125: runner.setCheckInterval(250);
126: runner.setShutDownWait(0);
127: runner.startUpContainer();
128: runner.shutDownContainer();
129:
130: server.verify();
131: }
132:
133: /**
134: * Verifies that the runner throws an exception when a container doesn't
135: * return a success status.
136: *
137: * @throws Exception If an unexpected error occurs
138: */
139: public void testStartUpFailure() throws Exception {
140: this .server
141: .setResponse("HTTP/1.1 500 Internal Server Error\n\n");
142: this .server.expectRequestCount(1);
143:
144: ContainerRunner runner = new ContainerRunner(new MockContainer(
145: this .server));
146: runner.setURL(new URL("http", "localhost", this .server
147: .getPort(), "/test"));
148: runner.setTimeout(0);
149: runner.setCheckInterval(250);
150: runner.setShutDownWait(0);
151: try {
152: runner.startUpContainer();
153: fail("Expected BuildException");
154: } catch (BuildException expected) {
155: // expected
156: }
157:
158: server.stop();
159: server.verify();
160: }
161:
162: /**
163: * Verifies that the runner tries for a time approximately close to the
164: * timeout period to start up the container, and then fails if the container
165: * isn't responding.
166: *
167: * @throws Exception If an unexpected error occurs
168: */
169: public void testStartUpTimeout() throws Exception {
170: this .server
171: .setResponse("HTTP/1.1 500 Internal Server Error\n\n");
172: this .server.expectRequestCount(-1); // can't tell exactly
173:
174: ContainerRunner runner = new ContainerRunner(new MockContainer(
175: this .server));
176: runner.setURL(new URL("http", "localhost", this .server
177: .getPort(), "/test"));
178: runner.setTimeout(5000);
179: runner.setCheckInterval(250);
180: runner.setShutDownWait(0);
181: long startTime = System.currentTimeMillis();
182: try {
183: runner.startUpContainer();
184: fail("Expected BuildException");
185: } catch (BuildException expected) {
186: // expected
187: }
188:
189: long time = System.currentTimeMillis() - startTime;
190: assertTrue("Process finished before the timeout was reached",
191: time > 5000);
192: assertTrue("Process took " + (time - 5000)
193: + "ms longer than the " + "timeout period",
194: time < 10000);
195:
196: server.stop();
197: server.verify();
198: }
199:
200: /**
201: * Verifies that the runner correctly recognizes an already running
202: * container, and does not attempt to shut it down.
203: *
204: * @throws Exception If an unexpected error occurs
205: */
206: public void testIgnoreAlreadyRunning() throws Exception {
207: this .server.setResponse("HTTP/1.1 200 Ok\n\n");
208: this .server.expectRequestCount(1);
209: Thread thread = new Thread(this .server);
210: thread.start();
211:
212: MockContainer container = new MockContainer(this .server);
213: container.expectStartUpCalled(false);
214: container.expectShutDownCalled(false);
215:
216: ContainerRunner runner = new ContainerRunner(container);
217: runner.setURL(new URL("http", "localhost", this .server
218: .getPort(), "/test"));
219: runner.setTimeout(0);
220: runner.setCheckInterval(250);
221: runner.setShutDownWait(0);
222: runner.startUpContainer();
223: runner.shutDownContainer();
224:
225: container.verify();
226:
227: server.stop();
228: server.verify();
229: }
230:
231: }
|