KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > TestContainerRunner


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.container;
21
22
23 import java.net.URL JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26
27 import junit.framework.TestCase;
28
29 /**
30  * Unit tests for {@link ContainerRunner}.
31  *
32  * @version $Id: TestContainerRunner.java,v 1.7 2004/05/31 20:05:23 vmassol Exp $
33  */

34 public final class TestContainerRunner extends TestCase
35 {
36
37     // Instance Variables ------------------------------------------------------
38

39     /**
40      * The dummy HTTP server used to test the container runner.
41      */

42     private MockHttpServer server;
43
44     // TestCase Implementation -------------------------------------------------
45

46     /**
47      * @see junit.framework.TestCase#setUp()
48      */

49     protected void setUp() throws Exception JavaDoc
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     // Test Methods ------------------------------------------------------------
62

63     /**
64      * Verifies that an exception is thrown when setting the URL property to a
65      * non-HTTP URL.
66      *
67      * @throws Exception If an unexpected error occurs
68      */

69     public void testSetInvalidUrl() throws Exception JavaDoc
70     {
71         ContainerRunner runner =
72             new ContainerRunner(new MockContainer(this.server));
73         try
74         {
75             runner.setURL(new URL JavaDoc("ftp://ftp.example.com/test/"));
76             fail("Expected IllegalArgumentException");
77         }
78         catch (IllegalArgumentException JavaDoc expected)
79         {
80             // expected
81
}
82     }
83
84     /**
85      * Verifies that an exception is thrown if startUpContainer() is invoked
86      * before a URL has been set.
87      *
88      * @throws Exception If an unexpected error occurs
89      */

90     public void testStartUpWithoutUrl() throws Exception JavaDoc
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 JavaDoc expected)
100         {
101             // expected
102
}
103     }
104
105     /**
106      * Verifies that an exception is thrown if shutDownContainer() is invoked
107      * before a URL has been set.
108      *
109      * @throws Exception If an unexpected error occurs
110      */

111     public void testShutDownWithoutUrl() throws Exception JavaDoc
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 JavaDoc expected)
121         {
122             // expected
123
}
124     }
125
126     /**
127      * Verifies that the runner correctly starts and stops a well-behaved
128      * container.
129      *
130      * @throws Exception If an unexpected error occurs
131      */

132     public void testStartUpShutDown() throws Exception JavaDoc
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 JavaDoc("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     /**
151      * Verifies that the runner throws an exception when a container doesn't
152      * return a success status.
153      *
154      * @throws Exception If an unexpected error occurs
155      */

156     public void testStartUpFailure() throws Exception JavaDoc
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 JavaDoc("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             // expected
176
}
177
178         server.stop();
179         server.verify();
180     }
181
182     /**
183      * Verifies that the runner tries for a time approximately close to the
184      * timeout period to start up the container, and then fails if the container
185      * isn't responding.
186      *
187      * @throws Exception If an unexpected error occurs
188      */

189     public void testStartUpTimeout() throws Exception JavaDoc
190     {
191         this.server.setResponse("HTTP/1.1 500 Internal Server Error\n\n");
192         this.server.expectRequestCount(-1); // can't tell exactly
193

194         ContainerRunner runner =
195             new ContainerRunner(new MockContainer(this.server));
196         runner.setURL(
197             new URL JavaDoc("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             // expected
210
}
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     /**
223      * Verifies that the runner correctly recognizes an already running
224      * container, and does not attempt to shut it down.
225      *
226      * @throws Exception If an unexpected error occurs
227      */

228     public void testIgnoreAlreadyRunning() throws Exception JavaDoc
229     {
230         this.server.setResponse("HTTP/1.1 200 Ok\n\n");
231         this.server.expectRequestCount(1);
232         Thread JavaDoc thread = new Thread JavaDoc(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 JavaDoc("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