Clover coverage report - Cactus 1.5 for J2EE API 1.3
Coverage timestamp: Wed Feb 18 2004 09:09:13 EST
file stats: LOC: 303   Methods: 10
NCLOC: 130   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RunServerTestsTask.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * ====================================================================
 3   
  *
 4   
  * The Apache Software License, Version 1.1
 5   
  *
 6   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 7   
  * reserved.
 8   
  *
 9   
  * Redistribution and use in source and binary forms, with or without
 10   
  * modification, are permitted provided that the following conditions
 11   
  * are met:
 12   
  *
 13   
  * 1. Redistributions of source code must retain the above copyright
 14   
  *    notice, this list of conditions and the following disclaimer.
 15   
  *
 16   
  * 2. Redistributions in binary form must reproduce the above copyright
 17   
  *    notice, this list of conditions and the following disclaimer in
 18   
  *    the documentation and/or other materials provided with the
 19   
  *    distribution.
 20   
  *
 21   
  * 3. The end-user documentation included with the redistribution, if
 22   
  *    any, must include the following acknowlegement:
 23   
  *       "This product includes software developed by the
 24   
  *        Apache Software Foundation (http://www.apache.org/)."
 25   
  *    Alternately, this acknowlegement may appear in the software itself,
 26   
  *    if and wherever such third-party acknowlegements normally appear.
 27   
  *
 28   
  * 4. The names "The Jakarta Project", "Cactus" and "Apache Software
 29   
  *    Foundation" must not be used to endorse or promote products
 30   
  *    derived from this software without prior written permission. For
 31   
  *    written permission, please contact apache@apache.org.
 32   
  *
 33   
  * 5. Products derived from this software may not be called "Apache"
 34   
  *    nor may "Apache" appear in their names without prior written
 35   
  *    permission of the Apache Group.
 36   
  *
 37   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 38   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 39   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 40   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 41   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 42   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 43   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 44   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 45   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 46   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 47   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 48   
  * SUCH DAMAGE.
 49   
  * ====================================================================
 50   
  *
 51   
  * This software consists of voluntary contributions made by many
 52   
  * individuals on behalf of the Apache Software Foundation.  For more
 53   
  * information on the Apache Software Foundation, please see
 54   
  * <http://www.apache.org/>.
 55   
  *
 56   
  */
 57   
 package org.apache.cactus.integration.ant;
 58   
 
 59   
 import java.net.URL;
 60   
 
 61   
 import org.apache.cactus.integration.ant.container.ContainerRunner;
 62   
 import org.apache.cactus.integration.ant.container.GenericContainer;
 63   
 import org.apache.cactus.integration.ant.util.AntLog;
 64   
 import org.apache.cactus.integration.ant.util.AntTaskFactory;
 65   
 import org.apache.tools.ant.BuildException;
 66   
 import org.apache.tools.ant.Task;
 67   
 
 68   
 /**
 69   
  * Task to automate running in-container unit test. It has the following
 70   
  * syntax when used in Ant :
 71   
  * <code><pre>
 72   
  *   &lt;runservertests testURL="&t;url&gt;"
 73   
  *          starttarget="&lt;start target name&gt;"
 74   
  *          stoptarget="&lt;stop target name&gt;"
 75   
  *          testtarget="&lt;test target name&gt;"/>
 76   
  * </pre></code>
 77   
  * where <code>&lt;url&gt;</code> is the URL that is used by this task to
 78   
  * ensure that the server is running. Indeed, the algorithm is as follow :
 79   
  * <ul>
 80   
  *  <li>Checks if server is running by trying to open an HTTP connection to
 81   
  *  the URL,</li>
 82   
  *  <li>If it fails, call the start target and loop until the HTTP connection
 83   
  *  the URL can be established,</li>
 84   
  *  <li>Call the test target. This target is supposed to start the test,
 85   
  *  usually by running the junit Ant task,</li>
 86   
  *  <li>When the tests are finished, call the stop target to stop the server.
 87   
  *  Note: The stop target is called only if the server was not already running
 88   
  *  when this task was executed.</li>
 89   
  * </ul>
 90   
  *
 91   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
 92   
  * @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
 93   
  * 
 94   
  * @since Cactus 1.5
 95   
  * @version $Id: RunServerTestsTask.java,v 1.10 2003/06/11 16:21:51 cmlenz Exp $
 96   
  */
 97   
 public class RunServerTestsTask extends Task
 98   
 {
 99   
 
 100   
     // Instance Variables ------------------------------------------------------
 101   
 
 102   
     /**
 103   
      * The generic container.
 104   
      */
 105   
     private GenericContainer container = new GenericContainer();
 106   
 
 107   
     /**
 108   
      * The hook that is called when the tests should be run.
 109   
      */
 110   
     private GenericContainer.Hook testHook;
 111   
 
 112   
     /**
 113   
      * The URL that is continuously pinged to verify if the server is running.
 114   
      */
 115   
     private URL testUrl;
 116   
 
 117   
     /**
 118   
      * Timeout after which we stop trying to connect to the test URL (in ms).
 119   
      */
 120   
     private long timeout = 180000;
 121   
     
 122   
     /**
 123   
      * The factory for creating ant tasks that is passed to the containers.
 124   
      */
 125   
     private AntTaskFactory antTaskFactory = new AntTaskFactory()
 126   
     {
 127  0
         public Task createTask(String theName)
 128   
         {
 129  0
             Task retVal = getProject().createTask(theName);
 130  0
             if (retVal != null)
 131   
             {
 132  0
                 retVal.setTaskName(getTaskName());
 133  0
                 retVal.setLocation(getLocation());
 134  0
                 retVal.setOwningTarget(getOwningTarget());
 135   
             }
 136  0
             return retVal;
 137   
         }
 138   
     };
 139   
 
 140   
     // Task Implementation -----------------------------------------------------
 141   
 
 142   
     /**
 143   
      * @see Task#execute()
 144   
      */
 145  0
     public void execute() throws BuildException
 146   
     {
 147  0
         if (!this.container.isStartUpSet())
 148   
         {
 149  0
             throw new BuildException("You must specify either a nested [start] "
 150   
                 + "element or the [starttarget] attribute");
 151   
         }
 152   
         
 153  0
         if (!this.container.isShutDownSet())
 154   
         {
 155  0
             throw new BuildException("You must specify either a nested [stop] "
 156   
                 + "element or the [stoptarget] attribute");
 157   
         }
 158   
 
 159  0
         if (this.testHook == null)
 160   
         {
 161  0
             throw new BuildException("You must specify either a nested [test] "
 162   
                 + "element or the [testtarget] attribute");
 163   
         }
 164   
 
 165   
         // Verify that a test URL has been specified
 166  0
         if (this.testUrl == null)
 167   
         {
 168  0
             throw new BuildException(
 169   
                 "The [testurl] attribute must be specified");
 170   
         }
 171   
 
 172  0
         this.container.setAntTaskFactory(antTaskFactory);
 173   
 
 174  0
         ContainerRunner runner = new ContainerRunner(this.container);
 175  0
         runner.setLog(new AntLog(this));
 176  0
         runner.setUrl(this.testUrl);
 177  0
         runner.setTimeout(this.timeout);
 178  0
         runner.startUpContainer();
 179  0
         try
 180   
         {
 181  0
             this.testHook.execute();
 182   
         }
 183   
         finally
 184   
         {
 185  0
             runner.shutDownContainer();
 186   
         }
 187   
     }
 188   
 
 189   
     // Public Methods ----------------------------------------------------------
 190   
 
 191   
     /**
 192   
      * Creates a nested start element.
 193   
      * 
 194   
      * @return The start element
 195   
      */
 196  0
     public final GenericContainer.Hook createStart()
 197   
     {
 198  0
         if (this.container.isStartUpSet())
 199   
         {
 200  0
             throw new BuildException(
 201   
                 "This task supports only one nested [start] element");
 202   
         }
 203  0
         return this.container.createStartUp();
 204   
     }
 205   
 
 206   
     /**
 207   
      * Sets the target to call to start the server.
 208   
      *
 209   
      * @param theStartTarget the Ant target to call
 210   
      */
 211  0
     public void setStartTarget(String theStartTarget)
 212   
     {
 213  0
         if (this.container.isStartUpSet())
 214   
         {
 215  0
             throw new BuildException("Either specify the [starttarget] "
 216   
                 + "attribute or the nested [start] element, but not both");
 217   
         }
 218  0
         this.container.setStartUpTarget(theStartTarget);
 219   
     }
 220   
 
 221   
     /**
 222   
      * Creates a nested stop element.
 223   
      * 
 224   
      * @return The stop element
 225   
      */
 226  0
     public final GenericContainer.Hook createStop()
 227   
     {
 228  0
         if (this.container.isShutDownSet())
 229   
         {
 230  0
             throw new BuildException(
 231   
                 "This task supports only one nested [stop] element");
 232   
         }
 233  0
         return this.container.createShutDown();
 234   
     }
 235   
 
 236   
     /**
 237   
      * Sets the target to call to stop the server.
 238   
      *
 239   
      * @param theStopTarget the Ant target to call
 240   
      */
 241  0
     public void setStopTarget(String theStopTarget)
 242   
     {
 243  0
         if (this.container.isShutDownSet())
 244   
         {
 245  0
             throw new BuildException("Either specify the [stoptarget] "
 246   
                 + "attribute or the nested [stop] element, but not both");
 247   
         }
 248  0
         this.container.setShutDownTarget(theStopTarget);
 249   
     }
 250   
 
 251   
     /**
 252   
      * Creates a nested test element.
 253   
      * 
 254   
      * @return The test element
 255   
      */
 256  0
     public final GenericContainer.Hook createTest()
 257   
     {
 258  0
         if (this.testHook != null)
 259   
         {
 260  0
             throw new BuildException(
 261   
                 "This task supports only one nested [test] element");
 262   
         }
 263  0
         this.testHook = container.new Hook();
 264  0
         return this.testHook;
 265   
     }
 266   
 
 267   
     /**
 268   
      * Sets the target to call to run the tests.
 269   
      *
 270   
      * @param theTestTarget the Ant target to call
 271   
      */
 272  0
     public void setTestTarget(String theTestTarget)
 273   
     {
 274  0
         if (this.testHook != null)
 275   
         {
 276  0
             throw new BuildException("Either specify the [testtarget] "
 277   
                 + "attribute or the nested [test] element, but not both");
 278   
         }
 279  0
         this.testHook = container.new Hook();
 280  0
         this.testHook.setTarget(theTestTarget);
 281   
     }
 282   
 
 283   
     /**
 284   
      * Sets the URL to call for testing if the server is running.
 285   
      *
 286   
      * @param theTestUrl the test URL to ping
 287   
      */
 288  0
     public void setTestUrl(URL theTestUrl)
 289   
     {
 290  0
         this.testUrl = theTestUrl;
 291   
     }
 292   
 
 293   
     /**
 294   
      * @param theTimeout the timeout after which we stop trying to call the test
 295   
      *        URL.
 296   
      */
 297  0
     public void setTimeout(long theTimeout)
 298   
     {
 299  0
         this.timeout = theTimeout;
 300   
     }
 301   
 
 302   
 }
 303