Clover coverage report - Cactus 1.4 for J2EE API 12
Coverage timestamp: Sun Aug 25 2002 18:00:03 BST
file stats: LOC: 237   Methods: 11
NCLOC: 121   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractTestCase.java 77.8% 81.1% 100% 82.9%
 1   
 /*   Generated by AspectJ version 1.0.5 */
 2   
 package org.apache.cactus;
 3   
 import java.lang.reflect.InvocationTargetException;
 4   
 import java.lang.reflect.Method;
 5   
 import java.lang.reflect.Modifier;
 6   
 import junit.framework.TestCase;
 7   
 import org.apache.cactus.util.JUnitVersionHelper;
 8   
 import org.apache.commons.logging.Log;
 9   
 import org.apache.commons.logging.LogFactory;
 10   
 
 11   
 /** 
 12   
  * Abstract class that is a thin layer on top of JUnit and that knows about 
 13   
  * test cases that are executed on the server side. This class is independent 
 14   
  * of the protocol used to communicate from the Cactus client side and the 
 15   
  * Cactus server side; it can be HTTP, JMS, etc. Subclasses will define 
 16   
  * additional behaviour that depends on the protocol. 
 17   
  * 
 18   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 19   
  * 
 20   
  * @version $Id: AbstractTestCase.java,v 1.9 2002/07/22 12:26:04 vmassol Exp $ 
 21   
  */
 22   
 public abstract class AbstractTestCase extends TestCase {
 23   
   /** 
 24   
        * The prefix of a test method. 
 25   
        */
 26   
   protected static final String TEST_METHOD_PREFIX = "test";
 27   
   /** 
 28   
        * The prefix of a begin test method. 
 29   
        */
 30   
   protected static final String BEGIN_METHOD_PREFIX = "begin";
 31   
   /** 
 32   
        * The prefix of an end test method. 
 33   
        */
 34   
   protected static final String END_METHOD_PREFIX = "end";
 35   
   /** 
 36   
        * Name of properties file to initialize logging subsystem 
 37   
        */
 38   
   public static final String LOG_CLIENT_CONFIG = "log_client.properties";
 39   
   /** 
 40   
        * The name of the current test method being executed. This name is valid 
 41   
        * both on the client side and on the server side, meaning you can call it 
 42   
        * from a <code>testXXX()</code>, <code>setUp()</code> or 
 43   
        * <code>tearDown()</code> method, as well as from <code>beginXXX()</code> 
 44   
        * and <code>endXXX()</code> methods. 
 45   
        */
 46   
   private String currentTestMethod;
 47   
   /** 
 48   
        * The logger (only used on the client side). 
 49   
        */
 50   
   private Log logger;
 51   
   /** 
 52   
        * Constructs a JUnit test case with the given name. 
 53   
        * 
 54   
        * @param theName the name of the test case 
 55   
        */
 56  366
   public AbstractTestCase(String theName) {
 57  366
     super(theName);
 58   
     ;
 59  366
     this.setCurrentTestMethod(JUnitVersionHelper.getTestCaseName(this));
 60   
   } 
 61   
   /** 
 62   
        * @return The logger used by the <code>TestCase</code> class and 
 63   
        *         subclasses to perform logging. 
 64   
        */
 65  354
   protected final Log getLogger() {
 66  354
     return this.logger;
 67   
   } 
 68   
 
 69   
   /** 
 70   
        * @return the name of the test method to call without the 
 71   
        *         TEST_METHOD_PREFIX prefix 
 72   
        */
 73  24827
   private String getBaseMethodName() {
 74  24827
     if (!this.getCurrentTestMethod().startsWith("test")) {
 75  0
       throw new RuntimeException("bad name [" + this.getCurrentTestMethod() + 
 76   
           "]. It should start with [" + "test" + "].");
 77   
     } 
 78  24827
     return this.getCurrentTestMethod().substring("test".length());
 79   
   } 
 80   
 
 81   
   /** 
 82   
        * @return the name of the test begin method to call that initialize the 
 83   
        *         test by initializing the <code>WebRequest</code> object 
 84   
        *         for the test case. 
 85   
        */
 86  12423
   protected String getBeginMethodName() {
 87  12423
     return "begin" + this.getBaseMethodName();
 88   
   } 
 89   
 
 90   
   /** 
 91   
        * @return the name of the test end method to call when the test has been 
 92   
        *         run on the server. It can be used to verify returned headers, 
 93   
        *         cookies, ... 
 94   
        */
 95  12404
   protected String getEndMethodName() {
 96  12404
     return "end" + this.getBaseMethodName();
 97   
   } 
 98   
 
 99   
   /** 
 100   
        * Runs the bare test sequence. This method is overridden from the 
 101   
        * JUnit <code>TestCase</code> class in order to prevent the latter 
 102   
        * to call the <code>setUp()</code> and <code>tearDown()</code> methods 
 103   
        * which, in our case, need to be ran in the servlet engine by the 
 104   
        * servlet redirector class. 
 105   
        * 
 106   
        * @exception Throwable if any exception is thrown during the test. Any 
 107   
        *            exception will be displayed by the JUnit Test Runner 
 108   
        */
 109  177
   public void runBare() throws Throwable {
 110  177
     this.logger = LogFactory.getLog(this.getClass());
 111  177
     this.getLogger().debug("------------- Test: " + this.getCurrentTestMethod());
 112  177
     try {
 113  177
       this.runTest();
 114   
     } catch (Throwable t) {
 115  0
       this.logger.debug("Exception in test", t);
 116  0
       throw t;
 117   
     } 
 118   
   } 
 119   
 
 120   
   /** 
 121   
        * Runs a test case. This method is overriden from the JUnit 
 122   
        * <code>TestCase</code> class in order to seamlessly call the 
 123   
        * Cactus redirection servlet. 
 124   
        * 
 125   
        * @exception Throwable any error that occurred when calling the test method 
 126   
        *            for the current test case. 
 127   
        */
 128   
   protected abstract void runTest() throws Throwable;
 129   
 
 130   
   /** 
 131   
        * Run the test that was specified in the constructor on the server side, 
 132   
        * calling <code>setUp()</code> and <code>tearDown()</code>. 
 133   
        * 
 134   
        * @exception Throwable any error that occurred when calling the test method 
 135   
        *         for the current test case, on the server side. 
 136   
        */
 137  177
   public void runBareServerTest() throws Throwable {
 138  177
     if (this.getLogger() == null) {
 139  177
       this.logger = LogFactory.getLog(this.getClass());
 140   
     } 
 141  177
     this.setUp();
 142  177
     try {
 143  177
       this.runServerTest();
 144   
     } finally {
 145  177
       this.tearDown();
 146   
     } 
 147   
   } 
 148   
 
 149   
   /** 
 150   
        * Call the test case begin method. 
 151   
        * 
 152   
        * @param theRequest the request object to pass to the begin method. 
 153   
        * @exception Throwable any error that occurred when calling the begin 
 154   
        *            method for the current test case. 
 155   
        */
 156  189
   protected void callBeginMethod(Request theRequest) throws Throwable {
 157  189
     Method[] methods = this.getClass().getMethods();
 158  189
     for (int i = 0; i < methods.length; i++) {
 159  12423
       if (methods[i].getName().equals(this.getBeginMethodName())) {
 160  76
         if (!methods[i].getReturnType().getName().equals("void")) {
 161  1
           AbstractTestCase.fail("The begin method [" + methods[i].getName() + 
 162   
               "] should return void and not [" + methods[i].getReturnType().getName() + "]");
 163   
         } 
 164  75
         if (!Modifier.isPublic(methods[i].getModifiers())) {
 165  0
           AbstractTestCase.fail("Method [" + methods[i].getName() + "] should be declared public");
 166   
         } 
 167  75
         Class[] parameters = methods[i].getParameterTypes();
 168  75
         if (parameters.length != 1) {
 169  1
           AbstractTestCase.fail("The begin method [" + methods[i].getName() + 
 170   
               "] must accept a single parameter derived from " + "class [" + 
 171   
               WebRequest.class.getName() + "], " + "but " + parameters.length + 
 172   
               " parameters were found");
 173  74
         } else if (!theRequest.getClass().isAssignableFrom(parameters[0])) {
 174  1
           AbstractTestCase.fail("The begin method [" + methods[i].getName() + 
 175   
               "] must accept a single parameter derived from " + "class [" + theRequest.getClass(
 176   
               ).getName() + "], " + "but found a [" + parameters[0].getName() + "] " + 
 177   
               "parameter instead");
 178   
         } 
 179  73
         try {
 180  73
           methods[i].invoke(this, new java.lang.Object[] {theRequest});
 181  72
           break;
 182   
         } catch (InvocationTargetException e) {
 183  1
           e.fillInStackTrace();
 184  1
           throw e.getTargetException();
 185   
         } catch (IllegalAccessException e) {
 186  0
           e.fillInStackTrace();
 187  0
           throw e;
 188   
         } 
 189   
       } 
 190   
     } 
 191   
   } 
 192   
 
 193   
   /** 
 194   
        * Run the test that was specified in the constructor on the server side. 
 195   
        * 
 196   
        * @exception Throwable any error that occurred when calling the test method 
 197   
        *         for the current test case, on the server side. 
 198   
        */
 199  177
   protected void runServerTest() throws Throwable {
 200  177
     Method runMethod = null;
 201  177
     try {
 202  177
       runMethod = this.getClass().getMethod(this.getCurrentTestMethod(), new java.lang.Class[0]);
 203   
     } catch (NoSuchMethodException e) {
 204  0
       AbstractTestCase.fail("Method [" + this.getCurrentTestMethod() + 
 205   
           "()] does not exist for class [" + this.getClass().getName() + "].");
 206   
     } 
 207  177
     if (runMethod != null && !Modifier.isPublic(runMethod.getModifiers())) {
 208  0
       AbstractTestCase.fail("Method [" + this.getCurrentTestMethod() + "()] should be public");
 209   
     } 
 210  177
     try {
 211  177
       runMethod.invoke(this, new java.lang.Class[0]);
 212   
     } catch (InvocationTargetException e) {
 213  12
       e.fillInStackTrace();
 214  12
       throw e.getTargetException();
 215   
     } catch (IllegalAccessException e) {
 216  0
       e.fillInStackTrace();
 217  0
       throw e;
 218   
     } 
 219   
   } 
 220   
 
 221   
   /** 
 222   
        * @return the name of the current test case being executed (it corresponds 
 223   
        *         to the name of the test method with the "test" prefix removed. 
 224   
        *         For example, for "testSomeTestOk" would return "someTestOk". 
 225   
        */
 226  50266
   protected String getCurrentTestMethod() {
 227  50266
     return this.currentTestMethod;
 228   
   } 
 229   
 
 230   
   /** 
 231   
        * @param theCurrentTestMethod the name of the current test case. 
 232   
        */
 233  366
   private void setCurrentTestMethod(String theCurrentTestMethod) {
 234  366
     this.currentTestMethod = theCurrentTestMethod;
 235   
   } 
 236   
 
 237   
 }