KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > RunServerTestsTask


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-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;
21
22 import java.net.URL JavaDoc;
23
24 import org.apache.cactus.integration.ant.container.ContainerRunner;
25 import org.apache.cactus.integration.ant.container.GenericContainer;
26 import org.apache.cactus.integration.ant.util.AntLog;
27 import org.apache.cactus.integration.ant.util.DefaultAntTaskFactory;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Task;
30
31 /**
32  * Task to automate running in-container unit test. It has the following
33  * syntax when used in Ant :
34  * <code><pre>
35  * &lt;runservertests testURL="&t;url&gt;"
36  * starttarget="&lt;start target name&gt;"
37  * stoptarget="&lt;stop target name&gt;"
38  * testtarget="&lt;test target name&gt;"/>
39  * </pre></code>
40  * where <code>&lt;url&gt;</code> is the URL that is used by this task to
41  * ensure that the server is running. Indeed, the algorithm is as follow :
42  * <ul>
43  * <li>Checks if server is running by trying to open an HTTP connection to
44  * the URL,</li>
45  * <li>If it fails, call the start target and loop until the HTTP connection
46  * the URL can be established,</li>
47  * <li>Call the test target. This target is supposed to start the test,
48  * usually by running the junit Ant task,</li>
49  * <li>When the tests are finished, call the stop target to stop the server.
50  * Note: The stop target is called only if the server was not already running
51  * when this task was executed.</li>
52  * </ul>
53  *
54  * @since Cactus 1.5
55  * @version $Id: RunServerTestsTask.java,v 1.13 2004/05/31 20:05:24 vmassol Exp $
56  */

57 public class RunServerTestsTask extends Task
58 {
59
60     // Instance Variables ------------------------------------------------------
61

62     /**
63      * The generic container.
64      */

65     private GenericContainer container = new GenericContainer();
66
67     /**
68      * The hook that is called when the tests should be run.
69      */

70     private GenericContainer.Hook testHook;
71
72     /**
73      * The URL that is continuously pinged to verify if the server is running.
74      */

75     private URL JavaDoc testURL;
76
77     /**
78      * Timeout after which we stop trying to connect to the test URL (in ms).
79      */

80     private long timeout = 180000;
81     
82     // Task Implementation -----------------------------------------------------
83

84     /**
85      * @see Task#execute()
86      */

87     public void execute() throws BuildException
88     {
89         if (!this.container.isStartUpSet())
90         {
91             throw new BuildException("You must specify either a nested [start] "
92                 + "element or the [starttarget] attribute");
93         }
94         
95         if (!this.container.isShutDownSet())
96         {
97             throw new BuildException("You must specify either a nested [stop] "
98                 + "element or the [stoptarget] attribute");
99         }
100
101         if (this.testHook == null)
102         {
103             throw new BuildException("You must specify either a nested [test] "
104                 + "element or the [testtarget] attribute");
105         }
106
107         // Verify that a test URL has been specified
108
if (this.testURL == null)
109         {
110             throw new BuildException(
111                 "The [testurl] attribute must be specified");
112         }
113
114         this.container.setAntTaskFactory(new DefaultAntTaskFactory(
115             getProject(), getTaskName(), getLocation(), getOwningTarget()));
116
117         ContainerRunner runner = new ContainerRunner(this.container);
118         runner.setLog(new AntLog(this));
119         runner.setURL(this.testURL);
120         runner.setTimeout(this.timeout);
121         runner.startUpContainer();
122         try
123         {
124             this.testHook.execute();
125         }
126         finally
127         {
128             runner.shutDownContainer();
129         }
130     }
131
132     // Public Methods ----------------------------------------------------------
133

134     /**
135      * Creates a nested start element.
136      *
137      * @return The start element
138      */

139     public final GenericContainer.Hook createStart()
140     {
141         if (this.container.isStartUpSet())
142         {
143             throw new BuildException(
144                 "This task supports only one nested [start] element");
145         }
146         return this.container.createStartUp();
147     }
148
149     /**
150      * Sets the target to call to start the server.
151      *
152      * @param theStartTarget the Ant target to call
153      */

154     public void setStartTarget(String JavaDoc theStartTarget)
155     {
156         if (this.container.isStartUpSet())
157         {
158             throw new BuildException("Either specify the [starttarget] "
159                 + "attribute or the nested [start] element, but not both");
160         }
161         this.container.setStartUpTarget(theStartTarget);
162     }
163
164     /**
165      * Creates a nested stop element.
166      *
167      * @return The stop element
168      */

169     public final GenericContainer.Hook createStop()
170     {
171         if (this.container.isShutDownSet())
172         {
173             throw new BuildException(
174                 "This task supports only one nested [stop] element");
175         }
176         return this.container.createShutDown();
177     }
178
179     /**
180      * Sets the target to call to stop the server.
181      *
182      * @param theStopTarget the Ant target to call
183      */

184     public void setStopTarget(String JavaDoc theStopTarget)
185     {
186         if (this.container.isShutDownSet())
187         {
188             throw new BuildException("Either specify the [stoptarget] "
189                 + "attribute or the nested [stop] element, but not both");
190         }
191         this.container.setShutDownTarget(theStopTarget);
192     }
193
194     /**
195      * Creates a nested test element.
196      *
197      * @return The test element
198      */

199     public final GenericContainer.Hook createTest()
200     {
201         if (this.testHook != null)
202         {
203             throw new BuildException(
204                 "This task supports only one nested [test] element");
205         }
206         this.testHook = container.new Hook();
207         return this.testHook;
208     }
209
210     /**
211      * Sets the target to call to run the tests.
212      *
213      * @param theTestTarget the Ant target to call
214      */

215     public void setTestTarget(String JavaDoc theTestTarget)
216     {
217         if (this.testHook != null)
218         {
219             throw new BuildException("Either specify the [testtarget] "
220                 + "attribute or the nested [test] element, but not both");
221         }
222         this.testHook = container.new Hook();
223         this.testHook.setTarget(theTestTarget);
224     }
225
226     /**
227      * Sets the URL to call for testing if the server is running.
228      *
229      * @param theTestURL the test URL to ping
230      */

231     public void setTestURL(URL JavaDoc theTestURL)
232     {
233         this.testURL = theTestURL;
234     }
235
236     /**
237      * @param theTimeout the timeout after which we stop trying to call the test
238      * URL.
239      */

240     public void setTimeout(long theTimeout)
241     {
242         this.timeout = theTimeout;
243     }
244
245 }
246
Popular Tags