2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Clover coverage report - Cactus 1.8dev for J2EE API 1.3
Coverage timestamp: Sun Mar 26 2006 18:50:18 BRT
file stats: LOC: 246   Methods: 9
NCLOC: 117   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
RunServerTestsTask.java 0% 0% 0% 0%
coverage
 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;
 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 239003 2004-05-31 20:05:27Z vmassol $
 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 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  0
     public void execute() throws BuildException
 88   
     {
 89  0
         if (!this.container.isStartUpSet())
 90   
         {
 91  0
             throw new BuildException("You must specify either a nested [start] "
 92   
                 + "element or the [starttarget] attribute");
 93   
         }
 94   
         
 95  0
         if (!this.container.isShutDownSet())
 96   
         {
 97  0
             throw new BuildException("You must specify either a nested [stop] "
 98   
                 + "element or the [stoptarget] attribute");
 99   
         }
 100   
 
 101  0
         if (this.testHook == null)
 102   
         {
 103  0
             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  0
         if (this.testURL == null)
 109   
         {
 110  0
             throw new BuildException(
 111   
                 "The [testurl] attribute must be specified");
 112   
         }
 113   
 
 114  0
         this.container.setAntTaskFactory(new DefaultAntTaskFactory(
 115   
             getProject(), getTaskName(), getLocation(), getOwningTarget())); 
 116   
 
 117  0
         ContainerRunner runner = new ContainerRunner(this.container);
 118  0
         runner.setLog(new AntLog(this));
 119  0
         runner.setURL(this.testURL);
 120  0
         runner.setTimeout(this.timeout);
 121  0
         runner.startUpContainer();
 122  0
         try
 123   
         {
 124  0
             this.testHook.execute();
 125   
         }
 126   
         finally
 127   
         {
 128  0
             runner.shutDownContainer();
 129   
         }
 130   
     }
 131   
 
 132   
     // Public Methods ----------------------------------------------------------
 133   
 
 134   
     /**
 135   
      * Creates a nested start element.
 136   
      * 
 137   
      * @return The start element
 138   
      */
 139  0
     public final GenericContainer.Hook createStart()
 140   
     {
 141  0
         if (this.container.isStartUpSet())
 142   
         {
 143  0
             throw new BuildException(
 144   
                 "This task supports only one nested [start] element");
 145   
         }
 146  0
         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  0
     public void setStartTarget(String theStartTarget)
 155   
     {
 156  0
         if (this.container.isStartUpSet())
 157   
         {
 158  0
             throw new BuildException("Either specify the [starttarget] "
 159   
                 + "attribute or the nested [start] element, but not both");
 160   
         }
 161  0
         this.container.setStartUpTarget(theStartTarget);
 162   
     }
 163   
 
 164   
     /**
 165   
      * Creates a nested stop element.
 166   
      * 
 167   
      * @return The stop element
 168   
      */
 169  0
     public final GenericContainer.Hook createStop()
 170   
     {
 171  0
         if (this.container.isShutDownSet())
 172   
         {
 173  0
             throw new BuildException(
 174   
                 "This task supports only one nested [stop] element");
 175   
         }
 176  0
         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  0
     public void setStopTarget(String theStopTarget)
 185   
     {
 186  0
         if (this.container.isShutDownSet())
 187   
         {
 188  0
             throw new BuildException("Either specify the [stoptarget] "
 189   
                 + "attribute or the nested [stop] element, but not both");
 190   
         }
 191  0
         this.container.setShutDownTarget(theStopTarget);
 192   
     }
 193   
 
 194   
     /**
 195   
      * Creates a nested test element.
 196   
      * 
 197   
      * @return The test element
 198   
      */
 199  0
     public final GenericContainer.Hook createTest()
 200   
     {
 201  0
         if (this.testHook != null)
 202   
         {
 203  0
             throw new BuildException(
 204   
                 "This task supports only one nested [test] element");
 205   
         }
 206  0
         this.testHook = container.new Hook();
 207  0
         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  0
     public void setTestTarget(String theTestTarget)
 216   
     {
 217  0
         if (this.testHook != null)
 218   
         {
 219  0
             throw new BuildException("Either specify the [testtarget] "
 220   
                 + "attribute or the nested [test] element, but not both");
 221   
         }
 222  0
         this.testHook = container.new Hook();
 223  0
         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  0
     public void setTestURL(URL theTestURL)
 232   
     {
 233  0
         this.testURL = theTestURL;
 234   
     }
 235   
 
 236   
     /**
 237   
      * @param theTimeout the timeout after which we stop trying to call the test
 238   
      *        URL.
 239   
      */
 240  0
     public void setTimeout(long theTimeout)
 241   
     {
 242  0
         this.timeout = theTimeout;
 243   
     }
 244   
 
 245   
 }
 246