vmassol     2004/03/16 13:45:02

  Modified:    framework/src/java/share/org/apache/cactus
                        AbstractCactusTestCase.java
               framework/src/test/share/org/apache/cactus
                        AbstractTestAbstractTestCase.java TestAll.java
               framework/src/java/share/org/apache/cactus/internal/client
                        ClientTestCaseCaller.java
               framework/src/test/share/org/apache/cactus/util TestAll.java
  Added:       framework/src/test/share/org/apache/cactus
                        TestNoNameTestCase.java
               framework/src/java/share/org/apache/cactus/util
                        TestCaseImplementError.java
                        TestCaseImplementChecker.java
               framework/src/test/share/org/apache/cactus/util
                        TestTestCaseImplementChecker.java
  Log:
  Applied patch from Kazuhito:

  - refaactored code so that Cactus test case checks are all located in a single 
location

  - added check for verifying that the test name cannot be null
  
  Revision  Changes    Path
  1.8       +7 -2      
jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractCactusTestCase.java
  
  Index: AbstractCactusTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractCactusTestCase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AbstractCactusTestCase.java       29 Feb 2004 09:42:32 -0000      1.7
  +++ AbstractCactusTestCase.java       16 Mar 2004 21:45:02 -0000      1.8
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2003 The Apache Software Foundation.
  + * Copyright 2003-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -26,6 +26,7 @@
   import org.apache.cactus.configuration.ConfigurationInitializer;
   import org.apache.cactus.internal.client.ClientTestCaseCaller;
   import org.apache.cactus.internal.server.ServerTestCaseCaller;
  +import org.apache.cactus.util.TestCaseImplementChecker;
   
   /**
    * Base class for all Cactus test case extensions.
  @@ -120,6 +121,10 @@
        */
       public void runBare() throws Throwable
       {
  +        TestCaseImplementChecker.checkTestName(this);
  +        TestCaseImplementChecker.checkTestName(
  +            getServerCaller().getWrappedTest());
  +
           runBareClient();
       }
   
  
  
  
  1.8       +9 -10     
jakarta-cactus/framework/src/test/share/org/apache/cactus/AbstractTestAbstractTestCase.java
  
  Index: AbstractTestAbstractTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/test/share/org/apache/cactus/AbstractTestAbstractTestCase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AbstractTestAbstractTestCase.java 29 Feb 2004 09:37:36 -0000      1.7
  +++ AbstractTestAbstractTestCase.java 16 Mar 2004 21:45:02 -0000      1.8
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2001-2003 The Apache Software Foundation.
  + * Copyright 2001-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -159,9 +159,8 @@
           if (checkName("testBeginMethodBadParamType"))
           {
               assertEquals("The method "
  -                + "[beginBeginMethodBadParamType] must accept a single "
  -                + "parameter implementing interface "
  -                + "[org.apache.cactus.Request], but "
  +                + "[beginBeginMethodBadParamType] must accept "
  +                + "[org.apache.cactus.Request] as 1st parameter, but "
                   + "found a [java.lang.String] parameter instead", theMessage);
   
               return true;
  @@ -173,10 +172,9 @@
           if (checkName("testBeginMethodBadParamNumber"))
           {
               assertEquals("The method "
  -                + "[beginBeginMethodBadParamNumber] must accept a single "
  -                + "parameter implementing interface "
  -                + "[org.apache.cactus.Request], but 2 "
  -                + "parameters were found", theMessage);
  +                + "[beginBeginMethodBadParamNumber] must have "
  +                + "1 parameter(s), but 2 parameter(s) were found",
  +                theMessage);
   
               return true;
           }
  @@ -241,7 +239,8 @@
           if (checkName("testEndMethodBadParamNumber"))
           {
               assertEquals("The method [endEndMethodBadParamNumber] "
  -                + "must only have a single parameter", theMessage);
  +                + "must have 1 parameter(s), but 2 parameter(s) were found",
  +                theMessage);
   
               return true;
           }
  
  
  
  1.20      +2 -1      
jakarta-cactus/framework/src/test/share/org/apache/cactus/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/test/share/org/apache/cactus/TestAll.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- TestAll.java      29 Feb 2004 09:37:36 -0000      1.19
  +++ TestAll.java      16 Mar 2004 21:45:02 -0000      1.20
  @@ -50,6 +50,7 @@
           ConfigurationInitializer.initialize();
           
           suite.addTestSuite(TestAbstractTestCase.class);
  +        suite.addTestSuite(TestNoNameTestCase.class);
           suite.addTestSuite(TestServletURL.class);
           suite.addTestSuite(TestServletUtil.class);
           suite.addTestSuite(TestWebTestResult.class);
  
  
  
  1.1                  
jakarta-cactus/framework/src/test/share/org/apache/cactus/TestNoNameTestCase.java
  
  Index: TestNoNameTestCase.java
  ===================================================================
  /* 
   * ========================================================================
   * 
   * Copyright 2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
   * ========================================================================
   */
  package org.apache.cactus;
  
  import junit.framework.TestCase;
  import org.apache.cactus.client.connector.ProtocolHandler;
  import org.apache.cactus.client.connector.http.HttpProtocolHandler;
  import org.apache.cactus.configuration.BaseConfiguration;
  import org.apache.cactus.configuration.ServletConfiguration;
  import org.apache.cactus.util.TestCaseImplementError;
  
  /**
   * Unit tests of the <code>AbstractTestCase</code> class and its subclasses.
   *
   * @version $Id: TestNoNameTestCase.java,v 1.1 2004/03/16 21:45:02 vmassol Exp $
   */
  public class TestNoNameTestCase extends TestCase
  {
      /**
       * Sample subclass of AbstractCactusTestCase of which the constructor
       * and setName() don't set test name.
       */
      class NoNameTestCase extends AbstractCactusTestCase
      {
          /**
           * default constructor
           */
          public NoNameTestCase()
          {
          }
  
          /**
           * Construct without super(theName)
           * @param theName name of the test
           */
          public NoNameTestCase(String theName)
          {
          }
  
          /**
           * override junit.framework.TestCase#setName(String).
           * @param theName name of the test
           */
          public void setName(String theName)
          {
          }
  
          /**
           * dummy implementation
           * @return ProtocolHandler always return null
           */
          protected ProtocolHandler createProtocolHandler()
          {
              return new HttpProtocolHandler(new ServletConfiguration());
          }
  
          /**
           * dummy test entry
           */
          public void testNoName()
          {
          }
      }
  
      /**
       * Sample subclass of ServletTestCase of which the constructor
       * and setName() don't set test name.
       */
      class NoNameServletTestCase extends ServletTestCase
      {
          /**
           * default constructor
           */
          public NoNameServletTestCase()
          {
          }
  
          /**
           * Construct without super(theName)
           * @param theName name of the test
           */
          public NoNameServletTestCase(String theName)
          {
          }
  
          /**
           * override junit.framework.TestCase#setName(String).
           * @param theName name of the test
           */
          public void setName(String theName)
          {
          }
  
          /**
           * dummy test entry
           */
          public void testNoName()
          {
          }
      }
  
      /**
       * Sample subclass of JspTestCase of which the constructor
       * and setName() don't set test name.
       */
      class NoNameJspTestCase extends JspTestCase
      {
          /**
           * default constructor
           */
          public NoNameJspTestCase()
          {
          }
  
          /**
           * Construct without super(theName)
           * @param theName name of the test
           */
          public NoNameJspTestCase(String theName)
          {
          }
  
          /**
           * override junit.framework.TestCase#setName(String).
           * @param theName name of the test
           */
          public void setName(String theName)
          {
          }
  
          /**
           * dummy test entry
           */
          public void testNoName()
          {
          }
      }
  
      /**
       * set cactus.contextURL as a system property.
       */
      public void setUp()
      {
          System.setProperty(BaseConfiguration.CACTUS_CONTEXT_URL_PROPERTY,
              "http://localhost/dummy";);
      }
  
      /**
       * @param theName name of the test
       */
      public TestNoNameTestCase(String theName)
      {
          super(theName);
      }
  
      /**
       * @param theTest the test to test
       */
      private void executeRunBare(TestCase theTest)
      {
          try
          {
              theTest.runBare();
              fail("test should fail");
          }
          catch (Throwable t)
          {
              assertEquals(TestCaseImplementError.class.getName(),
                  t.getClass().getName());
              String message = t.getMessage();
              assertNotNull("no message", message);
              assertEquals("No test name found. "
                  + "The test [" + theTest.getClass().getName()
                  + "] is not properly implemented.", message);
          }
      }
  
      /**
       * Test subclass of AbstractCactusTestCase.
       * Set the test name by constructor NoNameTestCase(String).
       */
      public void testNoNameTestCase()
      {
          TestCase test; 
          test = new NoNameTestCase("testNoName");
          executeRunBare(test);
      }
  
      /**
       * Test subclass of AbstractCactusTestCase.
       * Set the test name by TestCase#setName(String).
       */
      public void testNoNameTestCaseWithSetName()
      {
          TestCase test; 
          test = new NoNameTestCase();
          test.setName("testNoName");
          executeRunBare(test);
      }
  
      /**
       * Test subclass of ServletTestCase.
       * Set the test name by constructor NoNameTestCase(String).
       */
      public void testNoNameServletTestCase()
      {
          TestCase test; 
          test = new NoNameServletTestCase("testNoName");
          executeRunBare(test);
      }
  
      /**
       * Test subclass of ServletTestCase.
       * Set the test name by TestCase#setName(String).
       */
      public void testNoNameServletTestCaseWithSetName()
      {
          TestCase test; 
          test = new NoNameServletTestCase();
          test.setName("testNoName");
          executeRunBare(test);
      }
  
      /**
       * Test subclass of JspTestCase.
       * Set the test name by constructor NoNameTestCase(String).
       */
      public void testNoNameJspTestCase()
      {
          TestCase test; 
          test = new NoNameJspTestCase("testNoName");
          executeRunBare(test);
      }
  
      /**
       * Test subclass of JspTestCase.
       * Set the test name by TestCase#setName(String).
       */
      public void testNoNameJspTestCaseWithSetName()
      {
          TestCase test; 
          test = new NoNameJspTestCase();
          test.setName("testNoName");
          executeRunBare(test);
      }
  }
  
  
  
  1.1                  
jakarta-cactus/framework/src/java/share/org/apache/cactus/util/TestCaseImplementError.java
  
  Index: TestCaseImplementError.java
  ===================================================================
  /* 
   * ========================================================================
   * 
   * Copyright 2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
   * ========================================================================
   */
  package org.apache.cactus.util;
  
  import junit.framework.AssertionFailedError;
  
  /**
   * Represent an error around TestCase implementation.
   *
   * @version $Id: TestCaseImplementError.java,v 1.1 2004/03/16 21:45:02 vmassol Exp $
   */
  public class TestCaseImplementError extends AssertionFailedError
  {
      /**
       * Create a <code>TestCaseImplementError</code> and set the error message
       *
       * @param theMessage the message of the error
       */
      public TestCaseImplementError(String theMessage)
      {
          super(theMessage);
      }
  
      /**
       * Print the full stack trace, including the original exception.
       */
      public void printStackTrace()
      {
          super.printStackTrace(System.err);
      }
  }
  
  
  
  1.1                  
jakarta-cactus/framework/src/java/share/org/apache/cactus/util/TestCaseImplementChecker.java
  
  Index: TestCaseImplementChecker.java
  ===================================================================
  /* 
   * ========================================================================
   * 
   * Copyright 2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
   * ========================================================================
   */
  package org.apache.cactus.util;
  
  import java.lang.reflect.Method;
  import java.lang.reflect.Modifier;
  
  import junit.framework.Test;
  
  import org.apache.cactus.Request;
  
  /**
   * Utilities to check TestCase implementation.
   * @version $Id: TestCaseImplementChecker.java,v 1.1 2004/03/16 21:45:02 vmassol Exp 
$
   */
  public abstract class TestCaseImplementChecker
  {
      /**
       * Default constructor that requires that 
       * [EMAIL PROTECTED] #setConfiguration(Configuration)} be called before the 
methods
       * requiring a configuration object.
       * 
       */
      private TestCaseImplementChecker()
      {
      }
  
      /**
       * Check if the Test to run is properly implemented or not.
       * @param theTest the test to check
       * @throws TestCaseImplementError if has no name
       */
      public static void checkTestName(Test theTest)
          throws TestCaseImplementError
      {
          if (theTest == null)
          {
              return;
          }
  
          if (JUnitVersionHelper.getTestCaseName(theTest) == null)
          {
              throw new TestCaseImplementError("No test name found. The test ["
                  + theTest.getClass().getName()
                  + "] is not properly implemented.");
          }
      }
  
      /**
       * @param theNum the number
       * @return a numeric expresion of theNum
       */
      private static String numeric(int theNum)
      {
          switch(theNum)
          {
          case 1: return "1st";
          case 2: return "2nd";
          case 3: return "3rd";
          default: return (theNum + "th");
          }
      }
  
      /**
       * @param theMethod the method to check
       * @param theType the expected return type
       * @throws TestCaseImplementError if the return-type is not the one expected
       */
      private static void checkReturnType(Method theMethod, Class theType)
          throws TestCaseImplementError
      {
          if (!theMethod.getReturnType().equals(theType))
          {
              throw new TestCaseImplementError("The method ["
                  + theMethod.getName()
                  + "] should return " + theType
                  + " and not [" + theMethod.getReturnType().getName()
                  + "]");
          }
      }
  
      /**
       * @param theMethod the method to test
       * @throws TestCaseImplementError if the method is not public
       */
      private static void isPublic(Method theMethod)
          throws TestCaseImplementError
      {
          if (!Modifier.isPublic(theMethod.getModifiers()))
          {
              throw new TestCaseImplementError("The method ["
                  + theMethod.getName()
                  + "] should be declared public");
          }
      }
  
      /**
       * @param theMethod the method to check
       * @param theParams the expected parameters for the method
       * @throws TestCaseImplementError if the number of parameter is not same as
       *                                that of expected
       */
      private static void checkParameterCount(Method theMethod, Class[] theParams)
          throws TestCaseImplementError
      {
          Class[] parameters = theMethod.getParameterTypes();
          if (parameters.length != theParams.length)
          {
              throw new TestCaseImplementError("The method ["
                  + theMethod.getName()
                  + "] must have " + theParams.length + " parameter(s), "
                  + "but " + parameters.length + " parameter(s) were found");
          }
      }
  
      /**
       * @param theMethod the method to check
       * @param theParams the expected parameters for the method
       * @throws TestCaseImplementError if the number and type of parameter
       *                                 are not same as those of expected
       */
      private static void checkParameterTypes(Method theMethod, Class[] theParams)
          throws TestCaseImplementError
      {
          checkParameterCount(theMethod, theParams);
  
          Class[] parameters = theMethod.getParameterTypes();
          for (int i = 0; i < parameters.length; i++)
          {
              Class expected = theParams[i];
              Class actual = parameters[i];
              if (!expected.isAssignableFrom(actual))
              {
                  throw new TestCaseImplementError("The method ["
                      + theMethod.getName()
                      + "] must accept [" + expected.getName() + "] as "
                      + numeric(i + 1)
                      + " parameter, but found a ["
                      + actual.getName() + "] parameter instead");
              }
          }
      }
  
      /**
       * Check if the method is suitable for a test/begin/end method.
       * @param theMethod the method to check
       * @throws TestCaseImplementError if the method is not suitable
       *                                for Cactus test method
       */
      private static void checkAsCactusMethod(Method theMethod)
          throws TestCaseImplementError
      {
          checkReturnType(theMethod, Void.TYPE);
          isPublic(theMethod);
      }
  
      /**
       * Check if the method is suitable for a begin method.
       * Throws <code>AssertionFailedError</code> if at least one of following
       * conditions is failed:
       * <ul>
       *   <li>return type of the method is void</li>
       *   <li>the method is public</li>
       *   <li>the method accept a parameter of type <code>Request</code></li>
       * </ul>
       * @param theMethod the method to check
       * @throws TestCaseImplementError if the method is not suitable
       *                                for Cactus begin method
       */
      public static void checkAsBeginMethod(Method theMethod)
          throws TestCaseImplementError
      {
          checkAsCactusMethod(theMethod);
          checkParameterTypes(theMethod, new Class[]{Request.class});
      }
  
      /**
       * Check if the method is suitable for a end method.
       * Throws <code>AssertionFailedError</code> if at least one of following
       * conditions is failed:
       * <ul>
       *   <li>return type of the method is void</li>
       *   <li>the method is public</li>
       *   <li>the method accept one parameter</li>
       * </ul>
       * @param theMethod the method to check
       * @throws TestCaseImplementError if the method is not suitable
       *                                for Cactus end method
       */
      public static void checkAsEndMethod(Method theMethod)
          throws TestCaseImplementError
      {
          checkAsCactusMethod(theMethod);
          checkParameterCount(theMethod, new Class[]{Object.class});
      }
  }
  
  
  
  1.4       +6 -61     
jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/client/ClientTestCaseCaller.java
  
  Index: ClientTestCaseCaller.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/client/ClientTestCaseCaller.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ClientTestCaseCaller.java 29 Feb 2004 09:42:31 -0000      1.3
  +++ ClientTestCaseCaller.java 16 Mar 2004 21:45:02 -0000      1.4
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2001-2003 The Apache Software Foundation.
  + * Copyright 2001-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -21,7 +21,6 @@
   
   import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
  -import java.lang.reflect.Modifier;
   
   import junit.framework.Assert;
   import junit.framework.Test;
  @@ -32,6 +31,7 @@
   import org.apache.cactus.client.connector.ProtocolState;
   import org.apache.cactus.configuration.Configuration;
   import org.apache.cactus.util.JUnitVersionHelper;
  +import org.apache.cactus.util.TestCaseImplementChecker;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -357,40 +357,7 @@
           {
               if (methods[i].getName().equals(theMethodName))
               {
  -                // Check return type
  -                if (!methods[i].getReturnType().getName().equals("void"))
  -                {
  -                    fail("The method [" + methods[i].getName()
  -                        + "] should return void and not ["
  -                        + methods[i].getReturnType().getName() + "]");
  -                }
  -
  -                // Check if method is public
  -                if (!Modifier.isPublic(methods[i].getModifiers()))
  -                {
  -                    fail("Method [" + methods[i].getName()
  -                        + "] should be declared public");
  -                }
  -
  -                // Check parameters
  -                Class[] parameters = methods[i].getParameterTypes();
  -
  -                if (parameters.length != 1)
  -                {
  -                    fail("The method [" + methods[i].getName()
  -                        + "] must accept a single parameter implementing "
  -                        + "interface [" + Request.class.getName() + "], "
  -                        + "but " + parameters.length
  -                        + " parameters were found");
  -                }
  -                else if (!Request.class.isAssignableFrom(parameters[0]))
  -                {
  -                    fail("The method [" + methods[i].getName()
  -                        + "] must accept a single parameter implementing "
  -                        + "interface [" + Request.class.getName() + "], "
  -                        + "but found a [" + parameters[0].getName() + "] "
  -                        + "parameter instead");
  -                }
  +                TestCaseImplementChecker.checkAsBeginMethod(methods[i]);
   
                   try
                   {
  @@ -439,35 +406,13 @@
           {
               if (methods[i].getName().equals(theMethodName))
               {
  -                // Check return type
  -                if (!methods[i].getReturnType().getName().equals("void"))
  -                {
  -                    fail("The method [" + methods[i].getName()
  -                       + "] should return void and not ["
  -                       + methods[i].getReturnType().getName() + "]");
  -                }
  -
  -                // Check if method is public
  -                if (!Modifier.isPublic(methods[i].getModifiers()))
  -                {
  -                    fail("Method [" + methods[i].getName()
  -                       + "] should be declared public");
  -                }
  -
  -                // Check parameters
  -                Class[] parameters = methods[i].getParameterTypes();
  -
  -                // Verify only one parameter is defined
  -                if (parameters.length != 1)
  -                {
  -                    fail("The method [" + methods[i].getName()
  -                       + "] must only have a single parameter");
  -                }
  +                TestCaseImplementChecker.checkAsEndMethod(methods[i]);
   
                   paramObject = theResponse;
   
                   if (paramObject == null)
                   {
  +                    Class[] parameters = methods[i].getParameterTypes();
                       try
                       {
                           paramObject = theResponseFactory.getResponseObject(
  
  
  
  1.5       +3 -2      
jakarta-cactus/framework/src/test/share/org/apache/cactus/util/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/test/share/org/apache/cactus/util/TestAll.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestAll.java      29 Feb 2004 09:37:35 -0000      1.4
  +++ TestAll.java      16 Mar 2004 21:45:02 -0000      1.5
  @@ -1,7 +1,7 @@
   /* 
    * ========================================================================
    * 
  - * Copyright 2003 The Apache Software Foundation.
  + * Copyright 2003-2004 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -42,6 +42,7 @@
           suite.addTestSuite(TestStringUtil.class);
           suite.addTestSuite(TestCookieUtil.class);
           suite.addTestSuite(TestUniqueGenerator.class);
  +        suite.addTestSuite(TestTestCaseImplementChecker.class);
   
           return suite;
       }
  
  
  
  1.1                  
jakarta-cactus/framework/src/test/share/org/apache/cactus/util/TestTestCaseImplementChecker.java
  
  Index: TestTestCaseImplementChecker.java
  ===================================================================
  /* 
   * ========================================================================
   * 
   * Copyright 2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
   * ========================================================================
   */
  package org.apache.cactus.util;
  
  import java.lang.reflect.Method;
  import junit.framework.AssertionFailedError;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import org.apache.cactus.Request;
  import org.apache.cactus.WebResponse;
  
  /**
   * Unit tests for the [EMAIL PROTECTED] TestCaseImplementChecker} class.
   *
   * @version $Id: TestTestCaseImplementChecker.java,v 1.1 2004/03/16 21:45:02 vmassol 
Exp $
   */
  public class TestTestCaseImplementChecker extends TestCase
  {
      /**
       * normal test class for [EMAIL PROTECTED] #testCheckTestName}
       */
      class NormalTest extends TestCase
      {
          /**
           * @param theName the name of the test
           */
          public NormalTest(String theName)
          {
              super(theName);
          }
  
          /**
           * dummy test entry
           */
          public void testDummy()
          {
          }
      }
  
      /**
       * illegal test class for [EMAIL PROTECTED] #testCheckTestName}
       */
      class NoNameTest extends TestCase
      {
          /**
           * @param theName the name of the test
           */
          public NoNameTest(String theName)
          {
          }
  
          /**
           * dummy test entry
           */
          public void testDummy()
          {
          }
      }
  
      /**
       * declare methods to be used by [EMAIL PROTECTED] #testCheckAsBeginMethod} and
       * [EMAIL PROTECTED] #testCheckAsEndMethod}
       */
      class MethodHolder
      {
          /**
           */
          public MethodHolder()
          {
          }
          
          // ---- Begin Method ----
          
          /**
           * @param theRequest a Request
           */
          public void beginNormal(Request theRequest)
          {
          }
          
          /**
           * @param theRequest a Request
           * @return a dummy String
           */
          public String beginReturnsString(Request theRequest)
          {
              return "a string";
          }
          
          /**
           * @param theRequest a Request
           */
          protected void beginProtected(Request theRequest)
          {
          }
          
          /**
           * @param theRequest a Request
           */
          private void beginPrivate(Request theRequest)
          {
          }
          
          /**
           */
          public void beginNoParam()
          {
          }
          
          /**
           * @param theRequest a Request
           * @param theObject a Object
           */
          public void beginWithTwoParams(Request theRequest, Object theObject)
          {
          }
          
          /**
           * @param theString a String
           */
          public void beginWithStringParam(String theString)
          {
          }
  
          // ---- End Method ----
          
          /**
           * @param theResponse a WebResponse
           */
          public void endNormal(WebResponse theResponse)
          {
          }
          
          /**
           * @param theResponse a WebResponse
           * @return a dummy String
           */
          public String endReturnsString(WebResponse theResponse)
          {
              return "a string";
          }
          
          /**
           * @param theResponse a WebResponse
           */
          protected void endProtected(WebResponse theResponse)
          {
          }
          
          /**
           * @param theResponse a WebResponse
           */
          private void endPrivate(WebResponse theResponse)
          {
          }
          
          /**
           */
          public void endNoParam()
          {
          }
          
          /**
           * @param theResponse a WebResponse
           * @param theObject a Object
           */
          public void endWithTwoParams(WebResponse theResponse, Object theObject)
          {
          }
          
          /**
           * @param theString a String
           */
          public void endWithStringParam(String theString)
          {
          }
      }
  
      /**
       * @return a "Should not be here" message
       */
      private String shouldNotHere()
      {
          return "shold not be here";
      }
  
      /**
       * @param theThrowable a throwable
       * @return a "Should not be here" message with throwable information
       */
      private String shouldNotHere(Throwable theThrowable)
      {
          return shouldNotHere() + ": " + theThrowable.getClass().getName()
              + "[" + theThrowable.getMessage() + "]";
      }
  
      /**
       * @see {TestCaseImplementChecker#checkTestName(junit.framework.Test)}
       */
      public void testCheckTestName()
      {
          Test test;
          try
          {
              test = new NormalTest("testDummy");
              TestCaseImplementChecker.checkTestName(test);
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          try
          {
              test = new NoNameTest("testDummy");
              TestCaseImplementChecker.checkTestName(test);
              fail(shouldNotHere());
          }
          catch (TestCaseImplementError e)
          {
              assertEquals("No test name found. The test ["
                  + "org.apache.cactus.util"
                  + ".TestTestCaseImplementChecker$NoNameTest"
                  + "] is not properly implemented.", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
      }
  
      /**
       * @see {TestCaseImplementChecker#checkAsBeginMethod}
       */
      public void testCheckAsBeginMethod()
      {
          //--------------------------------------------------------------------
          
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "beginNormal", new Class[]{Request.class});
              TestCaseImplementChecker.checkAsBeginMethod(method);
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "beginReturnsString", new Class[]{Request.class});
              TestCaseImplementChecker.checkAsBeginMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [beginReturnsString] "
                  + "should return void and not [java.lang.String]",
                  e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getDeclaredMethod(
                  "beginProtected", new Class[]{Request.class});
              TestCaseImplementChecker.checkAsBeginMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [beginProtected] "
                  + "should be declared public", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getDeclaredMethod(
                  "beginPrivate", new Class[]{Request.class});
              TestCaseImplementChecker.checkAsBeginMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [beginPrivate] "
                  + "should be declared public", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "beginNoParam", new Class[]{});
              TestCaseImplementChecker.checkAsBeginMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [beginNoParam] must have 1 parameter(s), "
                  + "but 0 parameter(s) were found", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "beginWithTwoParams",
                  new Class[]{Request.class, Object.class});
              TestCaseImplementChecker.checkAsBeginMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [beginWithTwoParams] "
                  + "must have 1 parameter(s), "
                  + "but 2 parameter(s) were found", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "beginWithStringParam", new Class[]{String.class});
              TestCaseImplementChecker.checkAsBeginMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [beginWithStringParam] "
                  + "must accept [org.apache.cactus.Request] "
                  + "as 1st parameter, but found a "
                  + "[java.lang.String] parameter instead",
                  e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
      }
  
  
      /**
       * @see {TestCaseImplementChecker#checkAsEndMethod
       *       (java.lang.reflect.Method)}
       */
      public void testCheckAsEndMethod()
      {
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "endNormal", new Class[]{WebResponse.class});
              TestCaseImplementChecker.checkAsEndMethod(method);
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "endReturnsString", new Class[]{WebResponse.class});
              TestCaseImplementChecker.checkAsEndMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [endReturnsString] "
                  + "should return void and not [java.lang.String]",
                  e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getDeclaredMethod(
                  "endProtected", new Class[]{WebResponse.class});
              TestCaseImplementChecker.checkAsEndMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [endProtected] "
                  + "should be declared public", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getDeclaredMethod(
                  "endPrivate", new Class[]{WebResponse.class});
              TestCaseImplementChecker.checkAsEndMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [endPrivate] "
                  + "should be declared public", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "endNoParam", new Class[]{});
              TestCaseImplementChecker.checkAsEndMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [endNoParam] must have 1 parameter(s), "
                  + "but 0 parameter(s) were found", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
  
          //---------------------------------------------------------------------
  
          try
          {
              Method method = MethodHolder.class.getMethod(
                  "endWithTwoParams",
                  new Class[]{WebResponse.class, Object.class});
              TestCaseImplementChecker.checkAsEndMethod(method);
              fail(shouldNotHere());
          }
          catch (AssertionFailedError e)
          {
              assertEquals("The method [endWithTwoParams] "
                  + "must have 1 parameter(s), "
                  + "but 2 parameter(s) were found", e.getMessage());
          }
          catch (Throwable t)
          {
              fail(shouldNotHere(t));
          }
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to