FileDocCategorySizeDatePackage
RunAxisFunctionalTestsTask.javaAPI DocApache Axis 1.48626Sat Apr 22 18:57:28 BST 2006org.apache.axis.tools.ant.axis

RunAxisFunctionalTestsTask

public class RunAxisFunctionalTestsTask extends org.apache.tools.ant.Task
Ant task for starting / stopping servers and running junit in the middle. Based on the Cactus org.apache.commons.cactus.ant package, heavily munged and cruftily dumped into one file.

For Axis development; there is no support or stability associated with this task

ant.task
category="axis"
author
Rob Jellinghaus (robj@unrealities.com)

Fields Summary
private String
tcpServerTarget
private String
httpServerTarget
private String
testTarget
private String
httpStopTarget
private URL
url
Constructors Summary
Methods Summary
private voidantcall(java.lang.String taskName)
Call the selected ant task.

        CallTarget callee;
        callee = (CallTarget) getProject().createTask("antcall");
        callee.setOwningTarget(getOwningTarget());
        callee.setTaskName(getTaskName());
        callee.setLocation(getLocation());
        callee.init();
        callee.setTarget(taskName);
        callee.execute();
    
private voidcallStart(java.lang.String startTarget)
Call the start server task

        if (startTarget == null) {
            return;
        }

        // Execute the ant target
        new Thread(new TaskRunnable(startTarget)).start();

        if (! startTarget.equals(tcpServerTarget))
            return;
        
        // try a ping for the TCP server
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
            }
            try {
                sendOnSocket("ping\r\n");
                // if no exception, return
                System.out.println("RunAxisFunctionalTestsTask.callStart successfully pinged server.");
                return;
            } catch (Exception ex) {
                // loop & try again
            }
        }

        // NOTREACHED since the while loop returns if it successfully pings
    
private voidcallStop()
Call the stop server task

        try {
            // first, stop the tcp server
            if (tcpServerTarget != null) {
                sendOnSocket("quit\r\n");
            }
            
            
            // second, and more involvedly, stop the http server
            // Try connecting in case the server is already stopped.
            if (httpServerTarget != null) {
                URL url = new URL("http://localhost:8080/");
                try {
                    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                    connection.connect();
                    readFully(connection);
                    connection.disconnect();
                } catch (IOException e) {
                    // Server is not running. Make this task a no-op.
                    System.out.println("Error from HTTP read: " + e);
                    return;
                }
            }

            // Call the target that stops the server
            antcall(httpStopTarget);

            // Wait a few ms more (just to make sure)
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new BuildException("Interruption during sleep", e);
            }

            /*
             // Continuously try calling the test URL until it fails
            while (true) {
System.out.println("Trying localhost:8080...");
                try {
                    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                    connection.connect();
                    this.readFully(connection);
                    connection.disconnect();
                } catch (IOException e) {
                    break;
                }

                try {
                    Thread.sleep(500);
                } catch (InterruptedException ee) {
                    throw new BuildException("Interruption during sleep", ee);
                }

            }

            // Wait a few ms more (just to be sure !)
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new BuildException("Interruption during sleep", e);
            }
             */
            System.out.println("RunAxisFunctionalTestsTask.callStop successfully sent quit message.");
        } catch (Exception ex) {
            // ignore; if socket not there, presume dead already
        }
    
private voidcallTests()
Call the run tests target

        antcall(testTarget);
    
public voidexecute()
Executes the task.


            
        
    
        try {
            callStart(tcpServerTarget);
            callStart(httpServerTarget);
            callTests();
        } finally {
            // Make sure we stop the server
            callStop();
        }
    
static voidreadFully(java.net.HttpURLConnection connection)
Read all the contents that are to be read

        // finish reading it to prevent (harmless) server-side exceptions
        BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
        byte[] buffer = new byte[256];
        while((is.read(buffer)) > 0) {}
        is.close();
    
private voidsendOnSocket(java.lang.String str)
Make a socket to the url, and send the given string

        if (url == null)
            return;
        
        Socket sock = null;
        try {
            sock = new Socket(url.getHost(), url.getPort());
            sock.getOutputStream().write(new String(str).getBytes());
            // get a single byte response
            int i = sock.getInputStream().read();
        } catch (Exception ex) {
            throw ex;
        }/* finally {
            if (sock != null) {
                try {
                    sock.close();
                } catch (IOException ex) {
                    // ignore
                }
            }
         }*/
    
public voidsetHttpServerTarget(java.lang.String theStartTarget)
Sets the target to call to start server 2.

param
theStartTarget the Ant target to call

        httpServerTarget = theStartTarget;
    
public voidsetHttpStopTarget(java.lang.String theStopTarget)
Sets the stop target. This is the target which does a HTTP admin shutdown on the simple server.

        httpStopTarget = theStopTarget;
    
public voidsetTcpServerTarget(java.lang.String theStartTarget)
Sets the target to call to start server 1.

param
theStartTarget the Ant target to call

        tcpServerTarget = theStartTarget;
    
public voidsetTestTarget(java.lang.String theTestTarget)
Sets the target to call to run the tests.

param
theTestTarget the Ant target to call

        testTarget = theTestTarget;
    
public voidsetUrl(java.lang.String theUrl)
Sets the target URL (just http://host:port)

        try {
            url = new URL(theUrl);
        } catch (MalformedURLException ex) {
            System.err.println("Can't make URL from "+theUrl);
        }