KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > tools > ant > axis > RunAxisFunctionalTestsTask


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

16 package org.apache.axis.tools.ant.axis;
17
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.Task;
20 import org.apache.tools.ant.taskdefs.CallTarget;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.HttpURLConnection JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.Socket JavaDoc;
27 import java.net.URL JavaDoc;
28
29 /**
30  * Ant task for starting / stopping servers and running junit in the middle.
31  * Based on the Cactus org.apache.commons.cactus.ant package, heavily munged
32  * and cruftily dumped into one file.
33  * <p>
34  * <i>For Axis development; there is no support or stability associated
35  * with this task</i>
36  * @ant.task category="axis"
37  * @author Rob Jellinghaus (robj@unrealities.com)
38  */

39 public class RunAxisFunctionalTestsTask extends Task
40 {
41     private String JavaDoc tcpServerTarget = null;
42     private String JavaDoc httpServerTarget = null;
43     private String JavaDoc testTarget;
44     private String JavaDoc httpStopTarget = null;
45     private URL JavaDoc url = null;
46
47     /**
48      * Executes the task.
49      */

50     public void execute() throws BuildException
51     {
52         try {
53             callStart(tcpServerTarget);
54             callStart(httpServerTarget);
55             callTests();
56         } finally {
57             // Make sure we stop the server
58
callStop();
59         }
60     }
61
62     /**
63      * Call the start server task
64      */

65     private void callStart(String JavaDoc startTarget)
66     {
67         if (startTarget == null) {
68             return;
69         }
70
71         // Execute the ant target
72
new Thread JavaDoc(new TaskRunnable(startTarget)).start();
73
74         if (! startTarget.equals(tcpServerTarget))
75             return;
76         
77         // try a ping for the TCP server
78
while (true) {
79             try {
80                 Thread.sleep(500);
81             } catch (InterruptedException JavaDoc ex) {
82             }
83             try {
84                 sendOnSocket("ping\r\n");
85                 // if no exception, return
86
System.out.println("RunAxisFunctionalTestsTask.callStart successfully pinged server.");
87                 return;
88             } catch (Exception JavaDoc ex) {
89                 // loop & try again
90
}
91         }
92
93         // NOTREACHED since the while loop returns if it successfully pings
94
}
95
96     /**
97      * Call the run tests target
98      */

99     private void callTests()
100     {
101         antcall(testTarget);
102     }
103
104     /**
105      * Call the stop server task
106      */

107     private void callStop()
108     {
109         try {
110             // first, stop the tcp server
111
if (tcpServerTarget != null) {
112                 sendOnSocket("quit\r\n");
113             }
114             
115             
116             // second, and more involvedly, stop the http server
117
// Try connecting in case the server is already stopped.
118
if (httpServerTarget != null) {
119                 URL JavaDoc url = new URL JavaDoc("http://localhost:8080/");
120                 try {
121                     HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc)url.openConnection();
122                     connection.connect();
123                     readFully(connection);
124                     connection.disconnect();
125                 } catch (IOException JavaDoc e) {
126                     // Server is not running. Make this task a no-op.
127
System.out.println("Error from HTTP read: " + e);
128                     return;
129                 }
130             }
131
132             // Call the target that stops the server
133
antcall(httpStopTarget);
134
135             // Wait a few ms more (just to make sure)
136
try {
137                 Thread.sleep(500);
138             } catch (InterruptedException JavaDoc e) {
139                 throw new BuildException("Interruption during sleep", e);
140             }
141
142             /*
143              // Continuously try calling the test URL until it fails
144             while (true) {
145 System.out.println("Trying localhost:8080...");
146                 try {
147                     HttpURLConnection connection = (HttpURLConnection)url.openConnection();
148                     connection.connect();
149                     this.readFully(connection);
150                     connection.disconnect();
151                 } catch (IOException e) {
152                     break;
153                 }
154
155                 try {
156                     Thread.sleep(500);
157                 } catch (InterruptedException ee) {
158                     throw new BuildException("Interruption during sleep", ee);
159                 }
160
161             }
162
163             // Wait a few ms more (just to be sure !)
164             try {
165                 Thread.sleep(500);
166             } catch (InterruptedException e) {
167                 throw new BuildException("Interruption during sleep", e);
168             }
169              */

170             System.out.println("RunAxisFunctionalTestsTask.callStop successfully sent quit message.");
171         } catch (Exception JavaDoc ex) {
172             // ignore; if socket not there, presume dead already
173
}
174     }
175
176
177     /**
178      * Call the selected ant task.
179      */

180     private void antcall (String JavaDoc taskName) {
181         CallTarget callee;
182         callee = (CallTarget) getProject().createTask("antcall");
183         callee.setOwningTarget(getOwningTarget());
184         callee.setTaskName(getTaskName());
185         callee.setLocation(getLocation());
186         callee.init();
187         callee.setTarget(taskName);
188         callee.execute();
189     }
190
191     /**
192      * Make a socket to the url, and send the given string
193      */

194     private void sendOnSocket (String JavaDoc str) throws Exception JavaDoc {
195         if (url == null)
196             return;
197         
198         Socket JavaDoc sock = null;
199         try {
200             sock = new Socket JavaDoc(url.getHost(), url.getPort());
201             sock.getOutputStream().write(new String JavaDoc(str).getBytes());
202             // get a single byte response
203
int i = sock.getInputStream().read();
204         } catch (Exception JavaDoc ex) {
205             throw ex;
206         }/* finally {
207             if (sock != null) {
208                 try {
209                     sock.close();
210                 } catch (IOException ex) {
211                     // ignore
212                 }
213             }
214          }*/

215     }
216
217
218     /**
219      * Read all the contents that are to be read
220      */

221     static void readFully(HttpURLConnection JavaDoc connection) throws IOException JavaDoc
222     {
223         // finish reading it to prevent (harmless) server-side exceptions
224
BufferedInputStream JavaDoc is = new BufferedInputStream JavaDoc(connection.getInputStream());
225         byte[] buffer = new byte[256];
226         while((is.read(buffer)) > 0) {}
227         is.close();
228     }
229
230     /**
231      * Sets the target to call to start server 1.
232      *
233      * @param theStartTarget the Ant target to call
234      */

235     public void setTcpServerTarget(String JavaDoc theStartTarget)
236     {
237         tcpServerTarget = theStartTarget;
238     }
239
240     /**
241      * Sets the target to call to start server 2.
242      *
243      * @param theStartTarget the Ant target to call
244      */

245     public void setHttpServerTarget(String JavaDoc theStartTarget)
246     {
247         httpServerTarget = theStartTarget;
248     }
249
250     /**
251      * Sets the target to call to run the tests.
252      *
253      * @param theTestTarget the Ant target to call
254      */

255     public void setTestTarget(String JavaDoc theTestTarget)
256     {
257         testTarget = theTestTarget;
258     }
259
260     /**
261      * Sets the stop target. This is the target which does
262      * a HTTP admin shutdown on the simple server.
263      */

264     public void setHttpStopTarget (String JavaDoc theStopTarget)
265     {
266         httpStopTarget = theStopTarget;
267     }
268
269     /**
270      * Sets the target URL (just http://host:port)
271      */

272     public void setUrl (String JavaDoc theUrl) {
273         try {
274             url = new URL JavaDoc(theUrl);
275         } catch (MalformedURLException JavaDoc ex) {
276             System.err.println("Can't make URL from "+theUrl);
277         }
278     }
279
280
281     /**
282      * Helper class to execute a task in a thread.
283      */

284     public class TaskRunnable implements Runnable JavaDoc
285     {
286         String JavaDoc taskName;
287         public TaskRunnable (String JavaDoc taskName) {
288             this.taskName = taskName;
289         }
290         public void run () {
291             antcall(taskName);
292         }
293     }
294 }
295
296
297
Popular Tags