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: 562   Methods: 33
NCLOC: 249   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
AbstractContainer.java 4.2% 25% 15.2% 19%
coverage coverage
 1   
 /* 
 2   
  * ========================================================================
 3   
  * 
 4   
  * Copyright 2003-2005 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.container;
 21   
 
 22   
 import java.io.File;
 23   
 import java.io.IOException;
 24   
 
 25   
 import org.apache.cactus.integration.ant.deployment.DeployableFile;
 26   
 import org.apache.cactus.integration.ant.util.AntLog;
 27   
 import org.apache.cactus.integration.ant.util.AntTaskFactory;
 28   
 import org.apache.commons.logging.Log;
 29   
 import org.apache.tools.ant.BuildException;
 30   
 import org.apache.tools.ant.ProjectComponent;
 31   
 import org.apache.tools.ant.Task;
 32   
 import org.apache.tools.ant.filters.ReplaceTokens;
 33   
 import org.apache.tools.ant.taskdefs.Delete;
 34   
 import org.apache.tools.ant.types.FileSet;
 35   
 import org.apache.tools.ant.types.FilterChain;
 36   
 import org.apache.tools.ant.types.Path;
 37   
 import org.apache.tools.ant.types.PatternSet;
 38   
 import org.apache.tools.ant.types.Environment.Variable;
 39   
 import org.apache.tools.ant.types.selectors.SelectorUtils;
 40   
 
 41   
 /**
 42   
  * Abstract base class for supporting specific containers as nested elements in
 43   
  * the {@link org.apache.cactus.integration.ant.CactusTask}.
 44   
  * 
 45   
  * @version $Id: AbstractContainer.java 239130 2005-01-29 15:49:18Z vmassol $
 46   
  */
 47   
 public abstract class AbstractContainer extends ProjectComponent
 48   
     implements Container
 49   
 {
 50   
     // Constants ---------------------------------------------------------------
 51   
 
 52   
     /**
 53   
      * The path under which the container resources are stored in the JAR.
 54   
      */
 55   
     protected static final String RESOURCE_PATH =
 56   
         "/org/apache/cactus/integration/ant/container/resources/";
 57   
 
 58   
     // Instance Variables ------------------------------------------------------
 59   
 
 60   
     /**
 61   
      * The WAR or EAR that should be deployed to the container.
 62   
      */
 63   
     private DeployableFile deployableFile;
 64   
 
 65   
     /**
 66   
      * A pattern set which lists patterns for names of test cases that are to be
 67   
      * excluded from a specific container.
 68   
      */
 69   
     private PatternSet patternSet = new PatternSet();
 70   
 
 71   
     /**
 72   
      * The directory to which the test reports should be written.
 73   
      */
 74   
     private File toDir;
 75   
 
 76   
     /**
 77   
      * Name of a property that must exist in the project if tests are to be run
 78   
      * on the container. 
 79   
      */
 80   
     private String ifCondition;
 81   
 
 82   
     /**
 83   
      * Name of a property that must not exist in the project if tests are to be
 84   
      * run on the container. 
 85   
      */
 86   
     private String unlessCondition;
 87   
 
 88   
     /**
 89   
      * The factory for creating ant tasks.
 90   
      */
 91   
     private AntTaskFactory antTaskFactory;
 92   
 
 93   
     /**
 94   
      * The log to use.
 95   
      */
 96   
     private Log log = AntLog.NULL;
 97   
 
 98   
     /**
 99   
      * List of system properties to set in the container JVM. 
 100   
      */
 101   
     private Variable[] systemProperties;
 102   
 
 103   
     /**
 104   
      * The time to sleep after the container has started up. 
 105   
      */
 106   
     private long startUpWait = 1000;
 107   
 
 108   
     /**
 109   
      * Additional classpath entries for the classpath that will be used to 
 110   
      * start the containers.
 111   
      */
 112   
     private Path containerClasspath;    
 113   
 
 114   
     /**
 115   
      * The server (name or ip) the container is living on
 116   
      */
 117   
     private String server = "localhost";
 118   
 
 119   
     /**
 120   
      * The protocol the container is using
 121   
      */
 122   
     private String protocol = "http";
 123   
     
 124   
     // Public Methods ----------------------------------------------------------
 125   
 
 126   
     /**
 127   
      * @see Container#getTestContext()
 128   
      */
 129  0
     public String getTestContext()
 130   
     {
 131  0
         return null;
 132   
     }
 133   
     
 134   
     /**
 135   
      * Sets the time to wait after the container has been started up.
 136   
      * 
 137   
      * The default time is 1 second.
 138   
      * 
 139   
      * Note: This is a hack while waiting for container specific solutions
 140   
      * that tell exactly when the server is started or not. ATM, the only known
 141   
      * issue is with JBoss, where the servlet engine is started before the full
 142   
      * JBoss is started and thus it may happen that we try to shutdown JBoss 
 143   
      * before it has finished starting, leading to an exception.
 144   
      * 
 145   
      * @param theStartUpWait The time to wait in milliseconds
 146   
      */
 147  0
     public void setStartUpWait(long theStartUpWait)
 148   
     {
 149  0
         this.startUpWait = theStartUpWait;
 150   
     }
 151   
 
 152   
     /**
 153   
      * Creates a nested exclude element that is added to the pattern set.
 154   
      * 
 155   
      * @return The created exclude element
 156   
      */
 157  0
     public final PatternSet.NameEntry createExclude()
 158   
     {
 159  0
         return this.patternSet.createExclude();
 160   
     }
 161   
 
 162   
     /**
 163   
      * Returns the exclude patterns.
 164   
      * 
 165   
      * @return The exclude patterns 
 166   
      */
 167  0
     public final String[] getExcludePatterns()
 168   
     {
 169  0
         return this.patternSet.getExcludePatterns(getProject());
 170   
     }
 171   
 
 172   
     /**
 173   
      * Sets the name of a property that must exist in the project if tests are 
 174   
      * to be run on the container.
 175   
      * 
 176   
      * @param theIfCondition The property name to set
 177   
      */
 178  0
     public final void setIf(String theIfCondition)
 179   
     {
 180  0
         this.ifCondition = theIfCondition;
 181   
     }
 182   
 
 183   
     /**
 184   
      * Sets the directory to which the test reports should be written.
 185   
      * 
 186   
      * @param theToDir The output directory to set
 187   
      */
 188  0
     public final void setToDir(File theToDir)
 189   
     {
 190  0
         this.toDir = theToDir;
 191   
     }
 192   
 
 193   
     /**
 194   
      * Sets the name of a property that must not exist in the project if tests
 195   
      * are to be run on the container.
 196   
      * 
 197   
      * @param theUnlessCondition The property name to set
 198   
      */
 199  0
     public final void setUnless(String theUnlessCondition)
 200   
     {
 201  0
         this.unlessCondition = theUnlessCondition;
 202   
     }
 203   
 
 204   
     /**
 205   
      * Sets the server (name or ip) to which the container is living.
 206   
      * 
 207   
      * @param theServer The server to set
 208   
      */
 209  0
     public final void setServer(String theServer)
 210   
     {
 211  0
         this.server = theServer;
 212   
     }
 213   
 
 214   
     /**
 215   
      * Sets the protocol the container should use
 216   
      * 
 217   
      * @param theProtocol The protocol to set
 218   
      */
 219  0
     public final void setProtocol(String theProtocol)
 220   
     {
 221  0
        this.protocol = theProtocol;
 222   
     }
 223   
     
 224   
     // Container Implementation ------------------------------------------------
 225   
 
 226   
     /**
 227   
      * @see Container#getStartUpWait 
 228   
      */
 229  0
     public long getStartUpWait()
 230   
     {
 231  0
         return this.startUpWait;
 232   
     }
 233   
 
 234   
     /**
 235   
      * @see Container#getToDir
 236   
      */
 237  0
     public final File getToDir()
 238   
     {
 239  0
         return this.toDir;
 240   
     }
 241   
 
 242   
     /**
 243   
      * The default implementation does nothing.
 244   
      * 
 245   
      * @see Container#init
 246   
      */
 247  0
     public void init()
 248   
     {
 249   
         // The default implementation doesn nothing
 250   
     }
 251   
 
 252   
     /**
 253   
      * @see Container#isEnabled
 254   
      */
 255  0
     public final boolean isEnabled()
 256   
     {
 257  0
         return (testIfCondition() && testUnlessCondition());
 258   
     }
 259   
 
 260   
     /**
 261   
      * @see Container#isExcluded
 262   
      */
 263  0
     public final boolean isExcluded(String theTestName)
 264   
     {
 265  0
         String[] excludePatterns =
 266   
             this.patternSet.getExcludePatterns(getProject());
 267  0
         if (excludePatterns != null)
 268   
         {
 269  0
             String testPath = theTestName.replace('.', '/');
 270  0
             for (int i = 0; i < excludePatterns.length; i++)
 271   
             {
 272  0
                 String excludePattern = excludePatterns[i];
 273  0
                 if (excludePattern.endsWith(".java")
 274   
                  || excludePattern.endsWith(".class"))
 275   
                 {
 276  0
                     excludePattern = excludePattern.substring(
 277   
                         0, excludePattern.lastIndexOf('.'));
 278   
                 }
 279  0
                 if (SelectorUtils.matchPath(excludePattern, testPath))
 280   
                 {
 281  0
                     return true;
 282   
                 }
 283   
             }
 284   
         }
 285  0
         return false;
 286   
     }
 287   
 
 288   
     /**
 289   
      * @see Container#setAntTaskFactory
 290   
      */
 291  1
     public final void setAntTaskFactory(AntTaskFactory theFactory)
 292   
     {
 293  1
         this.antTaskFactory = theFactory;
 294   
     }
 295   
 
 296   
     /**
 297   
      * @see Container#setDeployableFile
 298   
      */
 299  1
     public final void setDeployableFile(DeployableFile theDeployableFile)
 300   
     {
 301  1
         this.deployableFile = theDeployableFile;
 302   
     }
 303   
 
 304   
     /**
 305   
      * @see Container#setLog
 306   
      */
 307  0
     public final void setLog(Log theLog)
 308   
     {
 309  0
         this.log = theLog;
 310   
     }
 311   
 
 312   
     /**
 313   
      * @see Container#setSystemProperties
 314   
      */
 315  0
     public void setSystemProperties(Variable[] theProperties)
 316   
     {
 317  0
         this.systemProperties = theProperties;
 318   
     }
 319   
 
 320   
     /**
 321   
      * @see Container#getSystemProperties
 322   
      */
 323  0
     public Variable[] getSystemProperties()
 324   
     {
 325  0
         return this.systemProperties;
 326   
     }
 327   
 
 328   
     /**
 329   
      * @see Container#setContainerClasspath(Path)
 330   
      * @since Cactus 1.6
 331   
      */
 332  1
     public void setContainerClasspath(Path theClasspath)
 333   
     {
 334  1
         this.containerClasspath = theClasspath;
 335   
     }    
 336   
 
 337   
     /**
 338   
      * @see Container#getContainerClasspath()
 339   
      * @since Cactus 1.6
 340   
      */
 341  0
     public Path getContainerClasspath()
 342   
     {
 343  0
         return this.containerClasspath;
 344   
     }
 345   
 
 346   
     /**
 347   
      * @see Container#getServer()
 348   
      */
 349  0
     public final String getServer()
 350   
     {
 351  0
         return this.server;
 352   
     }
 353   
     
 354   
     /**
 355   
      * @see Container#getProtocol()
 356   
      */
 357  0
     public final String getProtocol()
 358   
     {
 359  0
         return this.protocol;
 360   
     }
 361   
 
 362   
     /**
 363   
      * @see Container#getBaseURL()
 364   
      */
 365  0
     public final String getBaseURL()
 366   
     {
 367  0
         return this.getProtocol() + "://" + this.getServer() + ":" 
 368   
             + this.getPort();
 369   
     }
 370   
     
 371   
     // Protected Methods -------------------------------------------------------
 372   
 
 373   
     /**
 374   
      * Creates and returns a new instance of the Ant task mapped to the
 375   
      * specified logical name using the
 376   
      * {@link org.apache.cactus.integration.ant.util.AntTaskFactory} set.
 377   
      * 
 378   
      * @param theName The logical name of the task to create
 379   
      * @return A new isntance of the task
 380   
      * @see AntTaskFactory#createTask
 381   
      */
 382  0
     protected final Task createAntTask(String theName)
 383   
     {
 384  0
         return this.antTaskFactory.createTask(theName);
 385   
     }
 386   
 
 387   
     /**
 388   
      * Convenience method for creating a new directory inside another one.
 389   
      * 
 390   
      * @param theParentDir The directory in which the new directory should be
 391   
      *        created
 392   
      * @param theName The name of the directory to create
 393   
      * @return The new directory
 394   
      * @throws IOException If the directory could not be created
 395   
      */
 396  0
     protected final File createDirectory(File theParentDir, String theName)
 397   
         throws IOException
 398   
     {
 399  0
         File dir = new File(theParentDir, theName);
 400  0
         dir.mkdirs();
 401  0
         if (!dir.isDirectory())
 402   
         {
 403  0
             throw new IOException(
 404   
                 "Couldn't create directory " + dir.getAbsolutePath());
 405   
         }
 406  0
         return dir;
 407   
     }
 408   
 
 409   
     /**
 410   
      * Creates the default filter chain that should be applied while copying
 411   
      * container configuration files to the temporary directory from which the
 412   
      * container is started. The default filter chain replaces all occurences
 413   
      * of @cactus.port@ with the TCP port of the container, and all occurences
 414   
      * of @cactus.context@ with the web-application's context path (if the
 415   
      * deployable file is a web-app).
 416   
      * 
 417   
      * @return The default filter chain
 418   
      */
 419  1
     protected final FilterChain createFilterChain()
 420   
     {
 421  1
         ReplaceTokens.Token token = null;
 422  1
         FilterChain filterChain = new FilterChain();
 423   
 
 424   
         // Token for the cactus port
 425  1
         ReplaceTokens replacePort = new ReplaceTokens();
 426  1
         token = new ReplaceTokens.Token();
 427  1
         token.setKey("cactus.port");
 428  1
         token.setValue(String.valueOf(getPort()));
 429  1
         replacePort.addConfiguredToken(token);
 430  1
         filterChain.addReplaceTokens(replacePort);
 431   
 
 432   
         // Token for the cactus webapp context.
 433  1
         if (getDeployableFile() != null)
 434   
         {
 435  1
             ReplaceTokens replaceContext = new ReplaceTokens();
 436  1
             token = new ReplaceTokens.Token();
 437  1
             token.setKey("cactus.context");
 438  1
             token.setValue(getDeployableFile().getTestContext());
 439  1
             replaceContext.addConfiguredToken(token);
 440  1
             filterChain.addReplaceTokens(replaceContext);
 441   
         }
 442   
         
 443  1
         return filterChain;
 444   
     }
 445   
 
 446   
     /**
 447   
      * Clean the temporary directory.
 448   
      * 
 449   
      * @param theTmpDir the temp directory to clean
 450   
      */
 451  0
     protected void cleanTempDirectory(File theTmpDir)
 452   
     {
 453   
         // Clean up stuff previously put in the temporary directory
 454  0
         Delete delete = (Delete) createAntTask("delete");
 455  0
         FileSet fileSet = new FileSet();
 456  0
         fileSet.setDir(theTmpDir);
 457  0
         fileSet.createInclude().setName("**/*");
 458  0
         delete.addFileset(fileSet);
 459  0
         delete.setIncludeEmptyDirs(true);
 460  0
         delete.setFailOnError(false);
 461  0
         delete.execute();
 462   
     }
 463   
     
 464   
     /**
 465   
      * Convenience method that creates a temporary directory or
 466   
      * prepares the one passed by the user.
 467   
      * 
 468   
      * @return The temporary directory
 469   
      * @param theCustomTmpDir The user specified custom dir or null if none has
 470   
      *        been specified (ie we'll create default one).
 471   
      * @param theName The name of the directory relative to the system specific
 472   
      *        temporary directory
 473   
      */
 474  0
     protected File setupTempDirectory(File theCustomTmpDir, String theName)
 475   
     {
 476  0
         File tmpDir;
 477   
         
 478  0
         if (theCustomTmpDir == null)
 479   
         {
 480  0
             tmpDir = new File(System.getProperty("java.io.tmpdir"), theName);
 481   
         }
 482   
         else
 483   
         {
 484  0
             tmpDir = theCustomTmpDir;
 485   
         }
 486   
         
 487  0
         if (!tmpDir.exists())
 488   
         {
 489  0
             if (!tmpDir.mkdirs())
 490   
             {
 491  0
                 throw new BuildException("Could not create temporary "
 492   
                     + "directory [" + tmpDir + "]");
 493   
             }
 494   
         }
 495   
 
 496   
         // make sure we're returning a directory
 497  0
         if (!tmpDir.isDirectory())
 498   
         {
 499  0
             throw new BuildException("[" + tmpDir + "] is not a directory");
 500   
         }
 501   
 
 502  0
         return tmpDir;
 503   
     }
 504   
     
 505   
     /**
 506   
      * Returns the log to use.
 507   
      * 
 508   
      * @return The log
 509   
      */
 510  0
     protected final Log getLog()
 511   
     {
 512  0
         return this.log;
 513   
     }
 514   
 
 515   
     /**
 516   
      * Returns the web-application archive that is to be deployed to the
 517   
      * container.
 518   
      * 
 519   
      * @return The WAR file  
 520   
      */
 521  2
     protected final DeployableFile getDeployableFile()
 522   
     {
 523  2
         return this.deployableFile;
 524   
     }
 525   
 
 526   
     // Private Methods ---------------------------------------------------------
 527   
         
 528   
     /**
 529   
      * Tests whether the property necessary to run the tests in the container 
 530   
      * has been set.
 531   
      * 
 532   
      * @return <code>true</code> if the tests should be run in the container,
 533   
      *         <code>false</code> otherwise
 534   
      */
 535  0
     private boolean testIfCondition()
 536   
     {
 537  0
         if (ifCondition == null || ifCondition.length() == 0)
 538   
         {
 539  0
             return true;
 540   
         }
 541   
         
 542  0
         return (getProject().getProperty(ifCondition) != null);
 543   
     }
 544   
 
 545   
     /**
 546   
      * Tests whether the property specified as the 'unless' condition has not
 547   
      * been set.
 548   
      * 
 549   
      * @return <code>true</code> if the tests should be run in the container,
 550   
      *         <code>false</code> otherwise
 551   
      */
 552  0
     private boolean testUnlessCondition()
 553   
     {
 554  0
         if (unlessCondition == null || unlessCondition.length() == 0)
 555   
         {
 556  0
             return true;
 557   
         }
 558  0
         return (getProject().getProperty(unlessCondition) == null);
 559   
     }
 560   
 
 561   
 }
 562