J avolution v5.4 (J2SE 1.6+)

javolution.testing
Class TestCase

java.lang.Object
  extended by javolution.testing.TestCase

public abstract class TestCase
extends java.lang.Object

This class represents a test case which can be used for validation, performance and regression tests.

The structure of a test case is as follow:

     class MyTestCase extends TestCase {

         // Prepares data/state in which to run the test.
         public void setUp() { ... } // Optional
         
         // Executes the test possibly exercising the function tested multiple times.
         public void execute() throws Throwable { ... } // Mandatory.
         
         // Returns the number of times the function tested has been exercised (default 1).
         public int count() { ... } // Optional
     
         // Validates the test results and possibly check for limit cases or exceptions.
         public void validate() throws Throwable { ... } // Mandatory.
         
         // Cleanups after execution (e.g. to release resources).
         public void tearDown() { ... } // Optional

     }
It should be noted that some testing contexts (e.g. TimeContext) may run the sequence: setUp, execute and tearDown multiple times to calculate for example the average execution time, validation in that case is performed only once after the last execution.
   public class TypeFormatParseInt extends TestCase {

       static final int N = 1000; // Number of random samples.
       int[] _expected = new int[N];
       int[] _actual = new int[N];
       String[] _strings = new String[N];

       public void setUp() {
           for (int i = 0; i < _expected.length; i++) {
               _expected[i] = MathLib.random(Integer.MIN_VALUE, Integer.MAX_VALUE);
               _strings[i] = String.valueOf(_expected[i]);
           }
       }

       public void execute() {
           for (int i = 0; i < N; i++) {
               _actual[i] = TypeFormat.parseInt(_strings[i]);
           }
       }

       public int count() {
           return N;
       }

       public void validate() {

           // Compares expected versus actual (for random values).
           TestContext.assertArrayEquals(_expected, _actual);

           // Supplementary tests to check for limit cases.
           TestContext.assertEquals(Integer.MIN_VALUE, TypeFormat.parseInt("-2147483648"));
           TestContext.assertEquals(0, TypeFormat.parseInt("0"));
           TestContext.assertEquals(Integer.MAX_VALUE, TypeFormat.parseInt("2147483647"));

           // Checks exceptions raised.
           TestContext.assertException(NumberFormatException.class, new Runnable() {
               public void run() {
                   TypeFormat.parseInt("2147483648"); // Overflow
               }
           });
           TestContext.assertException(NumberFormatException.class, new Runnable() {
               public void run() {
                   TypeFormat.parseInt("123E4"); // Invalid Character
               }
           });
       }
   }

Test cases may be run individually or as part of a TestSuite. If an error occurs the location of the assert failing is usually available (a hyperlink in Netbeans and Eclipse).

     ...
    > [test] TypeFormat.parseDouble(CharSequence)
    > [error] Array element at 840, expected 2.078139623637765E-308 but found 2.0781396236377647E-308
        at javolution.TypeFormatTest$ParseDouble.validate(TypeFormatTest.java:419)
     

Version:
5.3, February 27, 2009
Author:
Jean-Marie Dautelle
See Also:
TestContext

Constructor Summary
protected TestCase()
          Default constructor.
 
Method Summary
 int count()
          The number of times the test case is exercised (default 1).
abstract  void execute()
          Executes this test case (possibly multiple times in which case the count() method should be overriden).
 java.lang.String getName()
          Returns the name of this test case.
 TestCase ignore(boolean isIgnored)
          Selects whether or not this test case should be ignored.
 boolean isIgnored()
          Indicates whether or not this test case should be ignored.
 void setUp()
          Prepares the test case execution (the default implementation does nothing).
 void tearDown()
          Cleanup once test is complete (the default implementation does nothing).
 java.lang.String toString()
          Returns the String representation of this test case.
abstract  void validate()
          Validates the test results and possibly checks for limit cases or exceptions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TestCase

protected TestCase()
Default constructor.

Method Detail

getName

public java.lang.String getName()
Returns the name of this test case. The default implementation returns the class name.

Returns:
the test case name.

ignore

public TestCase ignore(boolean isIgnored)
Selects whether or not this test case should be ignored. If the test case is ignored it is not executed, but the test context will usually indicate that the test is being ignored.

Parameters:
isIgnored - true if test case is ignored; false otherwise.
Returns:
this test case.

isIgnored

public boolean isIgnored()
Indicates whether or not this test case should be ignored.

Returns:
true if this test case is ignored; false otherwise.

setUp

public void setUp()
Prepares the test case execution (the default implementation does nothing).


execute

public abstract void execute()
                      throws java.lang.Exception
Executes this test case (possibly multiple times in which case the count() method should be overriden).

Throws:
java.lang.Exception

count

public int count()
The number of times the test case is exercised (default 1).

Returns:
the number of test case occurences in execute().

validate

public abstract void validate()
                       throws java.lang.Exception
Validates the test results and possibly checks for limit cases or exceptions.

Throws:
java.lang.Exception

tearDown

public void tearDown()
Cleanup once test is complete (the default implementation does nothing).


toString

public java.lang.String toString()
Returns the String representation of this test case.

Overrides:
toString in class java.lang.Object
Returns:
this.getName()

J avolution v5.4 (J2SE 1.6+)

Copyright © 2005 - 2009 Javolution.