You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ak...@apache.org on 2004/06/11 03:16:12 UTC

cvs commit: jakarta-commons-sandbox/test/src/test/org/apache/commons/test ExampleWithPrivates.java PrivateTestCaseTest.java

akarasulu    2004/06/10 18:16:12

  Modified:    test     .cvsignore project.xml
  Added:       test/src/java/org/apache/commons/test PrivateTestCase.java
                        TestException.java
               test/src/test/org/apache/commons/test
                        ExampleWithPrivates.java PrivateTestCaseTest.java
  Log:
  Commit changes ...
  
   o added test case base class with utility methods for accessing private
     members and methods
   o included test case
   o added runtime test exception for wrapping exceptions
   o the ExampleWithPrivates class has a private member, private method and
     private static method for testing
  
  To do ...
  
   o start writing a utility to generate test case methods for accessing private
     members and methods using reflection so the test writer does not have to
   o clean up and add the next batch of test code while factoring them out of
     other projects - do this slowly over time
  
  Revision  Changes    Path
  1.2       +1 -0      jakarta-commons-sandbox/test/.cvsignore
  
  Index: .cvsignore
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/test/.cvsignore,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- .cvsignore	10 Jun 2004 15:18:18 -0000	1.1
  +++ .cvsignore	11 Jun 2004 01:16:12 -0000	1.2
  @@ -1 +1,2 @@
   target
  +*.iml
  
  
  
  1.2       +0 -5      jakarta-commons-sandbox/test/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/test/project.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- project.xml	10 Jun 2004 15:18:18 -0000	1.1
  +++ project.xml	11 Jun 2004 01:16:12 -0000	1.2
  @@ -46,11 +46,6 @@
         <version>3.8.1</version>
         <url>http://www.junit.org/</url>
       </dependency>
  -    <dependency>
  -      <id>commons-logging</id>
  -      <version>1.0.3</version>
  -      <url>http://jakarta.apache.org/commons/logging.html</url>
  -    </dependency>
       </dependencies>
       <issueTrackingUrl>http://nagoya.apache.org/bugzilla/buglist.cgi?bug_status=NEW&amp;bug_status=ASSIGNED&amp;bug_status=REOPENED&amp;email1=&amp;emailtype1=substring&amp;emailassigned_to1=1&amp;email2=&amp;emailtype2=substring&amp;emailreporter2=1&amp;bugidtype=include&amp;bug_id=&amp;changedin=&amp;votes=&amp;chfieldfrom=&amp;chfieldto=Now&amp;chfieldvalue=&amp;product=Commons&amp;component=Codec&amp;short_desc=&amp;short_desc_type=allwordssubstr&amp;long_desc=&amp;long_desc_type=allwordssubstr&amp;bug_file_loc=&amp;bug_file_loc_type=allwordssubstr&amp;keywords=&amp;keywords_type=anywords&amp;field0-0-0=noop&amp;type0-0-0=noop&amp;value0-0-0=&amp;cmdtype=doit&amp;newqueryname=&amp;order=Reuse+same+sort+as+last+time</issueTrackingUrl>
       <build>
  
  
  
  1.1                  jakarta-commons-sandbox/test/src/java/org/apache/commons/test/PrivateTestCase.java
  
  Index: PrivateTestCase.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.commons.test;
  
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.lang.reflect.InvocationTargetException;
  
  import junit.framework.TestCase;
  
  
  /**
   * A test case with utility methods used to access private members and test
   * private methods.  Bill Venners discusses the technique of unit testing
   * private code without basturdizing your code in the following artical:
   * <a href="http://www.artima.com/suiterunner/private.html">Testing Private
   * Methods with JUnit and SuiteRunner</a>.   We have created this class to
   * follow his recommendation for unit testing private methods using Reflection.
   *
   * @todo look into the idea of using attributes to generate code that
   * calls methods based on reflection so the ugliness of reflection can go away.
   *
   * @version $Revision: 1.1 $
   */
  public class PrivateTestCase extends TestCase
  {
      public PrivateTestCase()
      {
          super();
      }
  
  
      public PrivateTestCase( String s )
      {
          super( s );
      }
  
  
      /**
       * Gets a member regardless of its visibility.
       *
       * @param name the name of the member
       * @param obj the Object whose member we access
       * @return the member Object
       */
      public static Object getMember( String name, Object obj )
      {
          try
          {
              Field field = obj.getClass().getDeclaredField( name ) ;
              field.setAccessible( true ) ;
              return field.get( obj ) ;
          }
          catch ( NoSuchFieldException e )
          {
              throw new TestException( e ) ;
          }
          catch ( IllegalAccessException e )
          {
              throw new TestException( e ) ;
          }
      }
  
  
      /**
       * Invokes a method on an object regardless of the method's visibility.
       *
       * @todo seriously add some code to this to reduce the number of args -
       * smart stuff to find the right method based on class heirarchy
       *
       * @param obj the object whose method is called
       * @param targetClass the class of the object (don't need it)
       * @param methodName the name of the method to invoke
       * @param argClasses the classes of the args
       * @param argObjects the arguments of the call
       * @return the result or null if the return value is void
       */
      public static Object invoke( Object obj, Class targetClass,
              String methodName, Class[] argClasses, Object[] argObjects )
      {
          try
          {
              Method method = targetClass.getDeclaredMethod( methodName,
                      argClasses );
              method.setAccessible( true );
              return method.invoke( obj, argObjects );
  
          }
          catch ( NoSuchMethodException e )
          {
              throw new TestException( e );
          }
          catch ( SecurityException e )
          {
              throw new TestException( e );
          }
          catch ( IllegalAccessException e )
          {
              throw new TestException( e );
          }
          catch ( IllegalArgumentException e )
          {
              throw new TestException( e );
          }
          catch ( InvocationTargetException e )
          {
              e.printStackTrace() ;
              throw new TestException( e.getTargetException() );
          }
      }
  
  
      /**
       * Invokes a static method on a class regardless of the method's visibility.
       *
       * @todo seriously add some code to this to reduce the number of args -
       * smart stuff to find the right method based on class heirarchy
       *
       * @param targetClass the class of the object (don't need it)
       * @param methodName the name of the method to invoke
       * @param argClasses the classes of the args
       * @param argObjects the arguments of the call
       * @return the result or null if the return value is void
       */
      public static Object invoke(Class targetClass,
              String methodName, Class[] argClasses, Object[] argObjects)
      {
          try
          {
              Method method = targetClass.getDeclaredMethod( methodName,
                      argClasses );
              method.setAccessible( true );
              return method.invoke( null, argObjects );
  
          }
          catch ( NoSuchMethodException e )
          {
              throw new TestException( e );
          }
          catch ( SecurityException e )
          {
              throw new TestException( e );
          }
          catch ( IllegalAccessException e )
          {
              throw new TestException( e );
          }
          catch ( IllegalArgumentException e )
          {
              e.printStackTrace() ;
              throw new TestException( e );
          }
          catch ( InvocationTargetException e )
          {
              e.printStackTrace();
              throw new TestException( e.getTargetException() );
          }
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/test/src/java/org/apache/commons/test/TestException.java
  
  Index: TestException.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.commons.test;
  
  
  /**
   * A test exception.
   * 
   * @version $Revision: 1.1 $
   */
  public class TestException extends RuntimeException
  {
      /**
       * Creates a test exception.
       */
      public TestException()
      {
          super();
      }
  
  
      /**
       * Creates a test exception with a message.
       *
       * @param message the exception message
       */
      public TestException( String message )
      {
          super( message );
      }
  
  
      /**
       * Creates a test exception with another nested exception that caused
       * this one.
       *
       * @param cause the nested exception that caused this one
       */
      public TestException( Throwable cause )
      {
          super( cause );
      }
  
  
      /**
       * Creates a test exception with a message and a nested exception.
       *
       * @param message the exception message
       * @param cause the nested exception that caused this one
       */
      public TestException( String message, Throwable cause )
      {
          super( message, cause );
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/test/src/test/org/apache/commons/test/ExampleWithPrivates.java
  
  Index: ExampleWithPrivates.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.commons.test;
  
  
  /**
   * Just a bogus example class with private members and methods used to test
   * access to private fields.
   * 
   * @version $Revision: 1.1 $
   */
  public class ExampleWithPrivates
  {
      /** test counter limit constant */
      static final int LIMIT = 100 ;
      /** the variable used to test private member access */
      private int counter = 0 ;
  
  
      /**
       * Gets the next counter value stopping when the limit is reached.
       *
       * @return the next value
       * @throws IllegalStateException if called when the limit is reached
       */
      private int next()
      {
          if ( counter == LIMIT )
          {
              throw new IllegalStateException( "Limit reached" ) ;
          }
          return counter++ ;
      }
  
  
      /**
       * Bogus private static method to test invokation.
       *
       * @return the private limit value
       */
      private static int getLimit()
      {
          return LIMIT ;
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/test/src/test/org/apache/commons/test/PrivateTestCaseTest.java
  
  Index: PrivateTestCaseTest.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.commons.test;
  
  
  /**
   * Tests PrivateTestCase.
   * 
   * @version $Revision: 1.1 $
   */
  public class PrivateTestCaseTest extends PrivateTestCase
  {
      /**
       * @see junit.framework.TestCase
       */
      public PrivateTestCaseTest()
      {
          super() ;
      }
  
  
      /**
       * Gets the private counter member of the example class.
       *
       * @param obj the instance of the example class
       * @return the counter value
       */
      public int getCounter( ExampleWithPrivates obj )
      {
          return ( ( Integer ) getMember( "counter", obj ) ).intValue();
      }
  
  
      /**
       * Invokes the private next() method on the example class instance.
       *
       * @param obj the example class instance
       * @return the next counter value
       */
      public int next( ExampleWithPrivates obj )
      {
          return ( ( Integer ) invoke( obj, ExampleWithPrivates.class,
                  "next", null, null ) ).intValue();
      }
  
  
      /**
       * Invokes the static private getLimit() method on the example class.
       *
       * @return the return value of the example class
       */
      public int getLimit()
      {
          return ( ( Integer ) invoke( ExampleWithPrivates.class,
                  "getLimit", null, null ) ).intValue();
      }
  
  
      /**
       * Tests the static method invokation with a private static method.
       */
      public void testInvokeStaticMethod()
      {
          assertEquals( ExampleWithPrivates.LIMIT, getLimit() );
      }
  
  
      /**
       * Tests the member method invokation with a private static method.
       */
      public void testInvokeMemberMethod()
      {
          ExampleWithPrivates obj = new ExampleWithPrivates();
          assertEquals( 0, next( obj ) ) ;
          assertEquals( 1, next( obj ) ) ;
          assertEquals( 2, next( obj ) ) ;
      }
  
  
      /**
       * Tests the getMember to access a private member.
       */
      public void testGetMember()
      {
          ExampleWithPrivates obj = new ExampleWithPrivates();
          assertEquals( 0, getCounter( obj ) );
          assertEquals( 0, next( obj ) );
          assertEquals( 1, getCounter( obj ) );
          assertEquals( 1, next( obj ) );
          assertEquals( 2, getCounter( obj ) );
          assertEquals( 2, next( obj ) );
          assertEquals( 3, getCounter( obj ) );
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org