J avolution v5.2 (J2SE 1.5+)

javolution.testing
Class TestCase

java.lang.Object
  extended by javolution.testing.TestCase
Direct Known Subclasses:
ContextTestSuite.ArrayAllocation, ContextTestSuite.Concurrency, ContextTestSuite.SmallObjectAllocation, UtilTestSuite.ArrayListDirectIteration, UtilTestSuite.CollectionAdd, UtilTestSuite.CollectionIteration, UtilTestSuite.FastListDirectIteration, UtilTestSuite.FastMapDirectIteration, UtilTestSuite.FastTableDirectIteration, UtilTestSuite.MapGet, UtilTestSuite.MapIteration, UtilTestSuite.MapPut, UtilTestSuite.MapRemove

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 prepare() { ... } // Optional 
         
         // Executes the test (it may exercise the test case several times).
         public void execute() { ... } // Mandatory.
         
         // Returns the number of times the test case has been exercised (default 1).  
         public int count() { ... } // Optional
     
         // Validates the results. 
         public void validate() { ... } // Optional.
         
         // Cleanups after execution (e.g. release resources).  
         public void cleanup() { ... } // Optional

     }
It should be noted that some testing contexts (e.g. TimeContext) may run the sequence (prepare, execute, validate, cleanup) multiple times to calculate for example the average execution time ( validation in that case is performed only once after the last run). Here is an example of test case implementation for the HashMap.put(key, value) method:
     class HashMap_put extends TestCase {
         private HashMap _map;
         private int _size;
                   
         public HashMap_put(int n) { 
             _size = 0; 
             Index.setMinimumRange(0, n); // Preallocates.
         }               
         
         public void prepare() {
             _map = new HashMap();
         }
         
         public void execute() {
             for (int i=0; i < _size;) {
                 _map.put(Index.valueOf(i), Index.valueOf(i++));
             }
         }
         
         public int count() { // Number of put operations performed. 
             return _size;
         }
         
         public void validate() {
             TestContext.assertTrue("Wrong size", _size == _map.size());
             for (int i=0; i < _size;) {
                 if (!TestContext.assertEquals(_map.get(Index.valueOf(i)), Index.valueOf(i++)))
                      break; // Error, no need to continue.
             }
              
             // Asserts performance.
             long avgTime = TimeContext.getAverageTime("ns");
             TestContext.assertTrue(avgTime + "ns too slow!", avgTime < 100);
         }
         
         public CharSequence getDescription() {
            return "java.util.HashMap.put(key, value) - " + n + " entries added";
         }
    };
Test cases are typically grouped into a TestSuite:
    public HashMapTests extends TestSuite {
        public void run() {
            TestContext.info("Test put(key, value) for various size");
            TestContext.test(new HashMap_put(10));
            TestContext.test(new HashMap_put(100));
            TestContext.test(new HashMap_put(1000));
            ...
        }
    }
    TimeContext.enter(); // To measure execution time.
    try {
        new HashMapTests().run();
    } finally {
        TimeContext.exit();
    }

Version:
5.2, August 5, 2007
Author:
Jean-Marie Dautelle
See Also:
TestContext

Constructor Summary
protected TestCase()
          Default constructor.
 
Method Summary
 void cleanup()
          Cleanup once test is complete (the default implementation does nothing).
 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.CharSequence getDescription()
          Returns the description of this test case or null if none.
 void prepare()
          Prepares the test case execution (the default implementation does nothing).
 java.lang.String toString()
          Returns the String representation of this test case (the description or the class name by default).
 void validate()
          Validates the test results (the default implementation does nothing).
 
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

prepare

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


execute

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


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 void validate()
Validates the test results (the default implementation does nothing).


cleanup

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


getDescription

public java.lang.CharSequence getDescription()
Returns the description of this test case or null if none.

Returns:
the description or null

toString

public java.lang.String toString()
Returns the String representation of this test case (the description or the class name by default).

Overrides:
toString in class java.lang.Object
Returns:
the string representation of this test case.

J avolution v5.2 (J2SE 1.5+)

Copyright © 2005 - 2007 Javolution.