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: 368   Methods: 21
NCLOC: 224   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
AbstractTestSuite.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.internal;
 21   
 
 22   
 import java.io.PrintWriter;
 23   
 import java.io.StringWriter;
 24   
 import java.lang.reflect.Constructor;
 25   
 import java.lang.reflect.InvocationTargetException;
 26   
 import java.lang.reflect.Method;
 27   
 import java.lang.reflect.Modifier;
 28   
 import java.util.Enumeration;
 29   
 import java.util.Vector;
 30   
 
 31   
 import org.apache.cactus.ServletTestCase;
 32   
 
 33   
 import junit.framework.Test;
 34   
 import junit.framework.TestCase;
 35   
 import junit.framework.TestResult;
 36   
 
 37   
 /**
 38   
  * Test Suite that wraps all the tests of the suite in Cactus Test Case 
 39   
  * objects so that pure JUnit tests can be run on the server side.
 40   
  *
 41   
  * @version $Id: AbstractTestSuite.java 238991 2004-05-22 11:34:50Z vmassol $
 42   
  * @since 1.5
 43   
  */
 44   
 public abstract class AbstractTestSuite implements Test
 45   
 {
 46   
     /**
 47   
      * Lists of tests to execute (Test objects)
 48   
      */
 49   
     private Vector tests = new Vector(10);
 50   
 
 51   
     /**
 52   
      * Name of the current test suite
 53   
      */
 54   
     private String name;
 55   
     
 56   
     /**
 57   
      * @see junit.framework.TestSuite#TestSuite()
 58   
      */
 59  0
     public AbstractTestSuite()
 60   
     {
 61   
     }
 62   
 
 63   
     /**
 64   
      * @see junit.framework.TestSuite#TestSuite(Class)
 65   
      */
 66  0
     public AbstractTestSuite(final Class theClass)
 67   
     {
 68  0
         setName(theClass.getName());
 69  0
         Constructor constructor;
 70  0
         try
 71   
         {
 72   
             // Avoid generating multiple error messages
 73  0
             constructor = getTestConstructor(theClass); 
 74   
         }
 75   
         catch (NoSuchMethodException e)
 76   
         {
 77  0
             addTest(warning("Class " + theClass.getName()
 78   
                 + " has no public constructor TestCase(String name)"));
 79  0
             return;
 80   
         }
 81   
 
 82  0
         if (!Modifier.isPublic(theClass.getModifiers()))
 83   
         {
 84  0
             addTest(warning("Class " + theClass.getName() + " is not public"));
 85  0
             return;
 86   
         }
 87   
 
 88  0
         Class superClass = theClass;
 89  0
         Vector names = new Vector();
 90  0
         while (Test.class.isAssignableFrom(superClass))
 91   
         {
 92  0
             Method[] methods = superClass.getDeclaredMethods();
 93  0
             for (int i = 0; i < methods.length; i++)
 94   
             {
 95  0
                 addTestMethod(methods[i], names, constructor);
 96   
             }
 97  0
             superClass = superClass.getSuperclass();
 98   
         }
 99  0
         if (this.tests.size() == 0)
 100   
         {
 101  0
             addTest(warning("No tests found in " + theClass.getName()));
 102   
         }
 103   
     }
 104   
 
 105   
     /**
 106   
      * @see junit.framework.TestSuite#TestSuite(String)
 107   
      */
 108  0
     public AbstractTestSuite(String theName)
 109   
     {
 110  0
         setName(theName);
 111   
     }
 112   
 
 113   
     /**
 114   
      * @see junit.framework.TestSuite#addTest(Test)
 115   
      */
 116  0
     protected void addTest(Test theTest)
 117   
     {
 118  0
         this.tests.addElement(theTest);
 119   
     }
 120   
 
 121   
     /**
 122   
      * @see junit.framework.TestSuite#addTestSuite(Class)
 123   
      */
 124  0
     protected void addTestSuite(Class theTestClass)
 125   
     {
 126  0
         addTest(createTestSuite(theTestClass));
 127   
     }
 128   
 
 129   
     /**
 130   
      * @see junit.framework.TestSuite#addTestMethod(Method, Vector, Constructor)
 131   
      */
 132  0
     private void addTestMethod(Method theMethod, Vector theNames, 
 133   
         Constructor theConstructor)
 134   
     {
 135  0
         String name = theMethod.getName();
 136  0
         if (theNames.contains(name))
 137   
         {
 138  0
             return;
 139   
         }
 140  0
         if (isPublicTestMethod(theMethod))
 141   
         {
 142  0
             theNames.addElement(name);
 143   
 
 144  0
             try
 145   
             {
 146   
                 // Note: We wrap the Test in a Cactus Test Case
 147  0
                 Object constructorInstance;
 148  0
                 if (theConstructor.getParameterTypes().length == 0)
 149   
                 {
 150  0
                     constructorInstance = theConstructor.newInstance(
 151   
                         new Object[0]);
 152  0
                     if (constructorInstance instanceof TestCase)
 153   
                     {
 154  0
                         ((TestCase) constructorInstance).setName(name);
 155   
                     }
 156   
                 }
 157   
                 else
 158   
                 {
 159  0
                     constructorInstance = theConstructor.newInstance(
 160   
                         new Object[] {name});
 161   
                 }
 162  0
                 addTest(new ServletTestCase(name, (Test) constructorInstance));
 163   
             }
 164   
             catch (InstantiationException e)
 165   
             {
 166  0
                 addTest(warning("Cannot instantiate test case: " + name
 167   
                     + " (" + exceptionToString(e) + ")"));
 168   
             }
 169   
             catch (InvocationTargetException e)
 170   
             {
 171  0
                 addTest(warning("Exception in constructor: " + name + " (" 
 172   
                     + exceptionToString(e.getTargetException()) + ")"));
 173   
             }
 174   
             catch (IllegalAccessException e)
 175   
             {
 176  0
                 addTest(warning("Cannot access test case: " + name + " ("
 177   
                     + exceptionToString(e) + ")"));
 178   
             }
 179   
         }
 180   
         else
 181   
         { 
 182   
             // Almost a test method
 183  0
             if (isTestMethod(theMethod))
 184   
             {
 185  0
                 addTest(warning("Test method isn't public: " 
 186   
                     + theMethod.getName()));
 187   
             }
 188   
         }
 189   
     }
 190   
 
 191   
     /**
 192   
      * @see junit.framework.TestSuite#exceptionToString(Throwable)
 193   
      */
 194  0
     private String exceptionToString(Throwable theThrowable)
 195   
     {
 196  0
         StringWriter stringWriter = new StringWriter();
 197  0
         PrintWriter writer = new PrintWriter(stringWriter);
 198  0
         theThrowable.printStackTrace(writer);
 199  0
         return stringWriter.toString();
 200   
     }
 201   
 
 202   
     /**
 203   
      * @see junit.framework.TestSuite#countTestCases()
 204   
      */
 205  0
     public int countTestCases()
 206   
     {
 207  0
         int count = 0;
 208  0
         for (Enumeration e = tests(); e.hasMoreElements();)
 209   
         {
 210  0
             Test test = (Test) e.nextElement();
 211  0
             count = count + test.countTestCases();
 212   
         }
 213  0
         return count;
 214   
     }
 215   
 
 216   
     /**
 217   
      * @see junit.framework.TestSuite#isPublicTestMethod(Method)
 218   
      */
 219  0
     private boolean isPublicTestMethod(Method theMethod)
 220   
     {
 221  0
         return isTestMethod(theMethod) 
 222   
             && Modifier.isPublic(theMethod.getModifiers());
 223   
     }
 224   
 
 225   
     /**
 226   
      * @see junit.framework.TestSuite#isTestMethod(Method)
 227   
      */
 228  0
     private boolean isTestMethod(Method theMethod)
 229   
     {
 230  0
         String name = theMethod.getName();
 231  0
         Class[] parameters = theMethod.getParameterTypes();
 232  0
         Class returnType = theMethod.getReturnType();
 233  0
         return parameters.length == 0
 234   
             && name.startsWith("test")
 235   
             && returnType.equals(Void.TYPE);
 236   
     }
 237   
 
 238   
     /**
 239   
      * @see junit.framework.TestSuite#run(TestResult)
 240   
      */
 241  0
     public void run(TestResult theResult)
 242   
     {
 243  0
         for (Enumeration e = tests(); e.hasMoreElements();)
 244   
         {
 245  0
             if (theResult.shouldStop())
 246   
             {
 247  0
                 break;
 248   
             }
 249  0
             Test test = (Test) e.nextElement();
 250  0
             runTest(test, theResult);
 251   
         }
 252   
     }
 253   
     
 254   
     /**
 255   
      * @see junit.framework.TestSuite#runTest(Test, TestResult)
 256   
      */
 257  0
     protected void runTest(Test theTest, TestResult theResult)
 258   
     {
 259  0
         theTest.run(theResult);
 260   
     }
 261   
     
 262   
     /**
 263   
      * @see junit.framework.TestSuite#testAt(int)
 264   
      */
 265  0
     protected Test testAt(int theIndex)
 266   
     {
 267  0
         return (Test) this.tests.elementAt(theIndex);
 268   
     }
 269   
 
 270   
     /**
 271   
      * Gets a constructor which takes a single String as
 272   
      * its argument or a no arg constructor.
 273   
      * 
 274   
      * @param theClass the class for which to find the constructor
 275   
      * @return the valid constructor found
 276   
      * @exception NoSuchMethodException if no valid constructor is
 277   
      *            found
 278   
      */
 279  0
     protected static Constructor getTestConstructor(Class theClass) 
 280   
         throws NoSuchMethodException
 281   
     {
 282  0
         Constructor result;
 283  0
         try
 284   
         {
 285  0
             result = theClass.getConstructor(new Class[] {String.class});
 286   
         }
 287   
         catch (NoSuchMethodException e)
 288   
         {
 289  0
             result = theClass.getConstructor(new Class[0]);
 290   
         }
 291  0
         return result; 
 292   
     }
 293   
 
 294   
     /**
 295   
      * @see junit.framework.TestSuite#testCount()
 296   
      */
 297  0
     protected int testCount()
 298   
     {
 299  0
         return this.tests.size();
 300   
     }
 301   
 
 302   
     /**
 303   
      * @see junit.framework.TestSuite#tests()
 304   
      */
 305  0
     protected Enumeration tests()
 306   
     {
 307  0
         return this.tests.elements();
 308   
     }
 309   
 
 310   
     /**
 311   
      * @see junit.framework.TestSuite#toString()
 312   
      */
 313  0
     public String toString()
 314   
     {
 315  0
         if (getName() != null)
 316   
         {
 317  0
             return getName();
 318   
         }
 319  0
         return super.toString();
 320   
     }
 321   
 
 322   
     /**
 323   
      * @see junit.framework.TestSuite#setName(String)
 324   
      */
 325  0
     protected void setName(String theName)
 326   
     {
 327  0
         this.name = theName;
 328   
     }
 329   
 
 330   
     /**
 331   
      * @see junit.framework.TestSuite#getName()
 332   
      */
 333  0
     protected String getName()
 334   
     {
 335  0
         return this.name;
 336   
     }
 337   
 
 338   
     /**
 339   
      * @see junit.framework.TestSuite#warning(String)
 340   
      */
 341  0
     private static Test warning(final String theMessage)
 342   
     {
 343  0
         return new TestCase("warning")
 344   
         {
 345  0
             protected void runTest()
 346   
             {
 347  0
                 fail(theMessage);
 348   
             }
 349   
         };
 350   
     }
 351   
 
 352   
     /**
 353   
      * @param theTestClass the test class containing the tests to be included
 354   
      *        in the Cactus Test Suite
 355   
      * @return a Cactus Test Suite (ex: ServletTestSuite) initialized with a
 356   
      *         test class
 357   
      */
 358   
     protected abstract Test createTestSuite(Class theTestClass);
 359   
 
 360   
     /**
 361   
      * @param theName the name of the Cactus Test Case
 362   
      * @param theTest the wrapped test
 363   
      * @return a Cactus Test Case object initialized with the give name and
 364   
      *         wrapped test
 365   
      */
 366   
     protected abstract Test createCactusTestCase(String theName, Test theTest);
 367   
 }
 368