1 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 ; 23 import java.io.IOException ; 24 import java.net.HttpURLConnection ; 25 import java.net.MalformedURLException ; 26 import java.net.Socket ; 27 import java.net.URL ; 28 29 39 public class RunAxisFunctionalTestsTask extends Task 40 { 41 private String tcpServerTarget = null; 42 private String httpServerTarget = null; 43 private String testTarget; 44 private String httpStopTarget = null; 45 private URL url = null; 46 47 50 public void execute() throws BuildException 51 { 52 try { 53 callStart(tcpServerTarget); 54 callStart(httpServerTarget); 55 callTests(); 56 } finally { 57 callStop(); 59 } 60 } 61 62 65 private void callStart(String startTarget) 66 { 67 if (startTarget == null) { 68 return; 69 } 70 71 new Thread (new TaskRunnable(startTarget)).start(); 73 74 if (! startTarget.equals(tcpServerTarget)) 75 return; 76 77 while (true) { 79 try { 80 Thread.sleep(500); 81 } catch (InterruptedException ex) { 82 } 83 try { 84 sendOnSocket("ping\r\n"); 85 System.out.println("RunAxisFunctionalTestsTask.callStart successfully pinged server."); 87 return; 88 } catch (Exception ex) { 89 } 91 } 92 93 } 95 96 99 private void callTests() 100 { 101 antcall(testTarget); 102 } 103 104 107 private void callStop() 108 { 109 try { 110 if (tcpServerTarget != null) { 112 sendOnSocket("quit\r\n"); 113 } 114 115 116 if (httpServerTarget != null) { 119 URL url = new URL ("http://localhost:8080/"); 120 try { 121 HttpURLConnection connection = (HttpURLConnection )url.openConnection(); 122 connection.connect(); 123 readFully(connection); 124 connection.disconnect(); 125 } catch (IOException e) { 126 System.out.println("Error from HTTP read: " + e); 128 return; 129 } 130 } 131 132 antcall(httpStopTarget); 134 135 try { 137 Thread.sleep(500); 138 } catch (InterruptedException e) { 139 throw new BuildException("Interruption during sleep", e); 140 } 141 142 170 System.out.println("RunAxisFunctionalTestsTask.callStop successfully sent quit message."); 171 } catch (Exception ex) { 172 } 174 } 175 176 177 180 private void antcall (String 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 194 private void sendOnSocket (String str) throws Exception { 195 if (url == null) 196 return; 197 198 Socket sock = null; 199 try { 200 sock = new Socket (url.getHost(), url.getPort()); 201 sock.getOutputStream().write(new String (str).getBytes()); 202 int i = sock.getInputStream().read(); 204 } catch (Exception ex) { 205 throw ex; 206 } 215 } 216 217 218 221 static void readFully(HttpURLConnection connection) throws IOException 222 { 223 BufferedInputStream is = new BufferedInputStream (connection.getInputStream()); 225 byte[] buffer = new byte[256]; 226 while((is.read(buffer)) > 0) {} 227 is.close(); 228 } 229 230 235 public void setTcpServerTarget(String theStartTarget) 236 { 237 tcpServerTarget = theStartTarget; 238 } 239 240 245 public void setHttpServerTarget(String theStartTarget) 246 { 247 httpServerTarget = theStartTarget; 248 } 249 250 255 public void setTestTarget(String theTestTarget) 256 { 257 testTarget = theTestTarget; 258 } 259 260 264 public void setHttpStopTarget (String theStopTarget) 265 { 266 httpStopTarget = theStopTarget; 267 } 268 269 272 public void setUrl (String theUrl) { 273 try { 274 url = new URL (theUrl); 275 } catch (MalformedURLException ex) { 276 System.err.println("Can't make URL from "+theUrl); 277 } 278 } 279 280 281 284 public class TaskRunnable implements Runnable 285 { 286 String taskName; 287 public TaskRunnable (String taskName) { 288 this.taskName = taskName; 289 } 290 public void run () { 291 antcall(taskName); 292 } 293 } 294 } 295 296 297 | Popular Tags |