ccl.util
Class Test

java.lang.Object
  |
  +--ccl.util.Test
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
CCLLineBorderTest, ClassPathUtilTest, DateFieldTest, DoubleListTest, FileUtilTest, GraphicsUtilTest, IniFileTest, InitTest, InputCancelDialogTest, MainTest, NativeTest, OutputErrorStreamManagerTest, SingletonTest, SToLAndConcatTest, StreamCopierTest, SwingUtilTest, SysEnvTest, TestCase, TestCaseTest, TestTest, TestTest.FailingTest, TestTest.SuccessfullTest, UtilPackageTest, UtilTest, XMLImportHandlerTest, XMLUtilTest

public abstract class Test
extends java.lang.Object
implements java.lang.Runnable

Framework and base class for all tests. Inherit _doit in your test and use bugIf( boolean ) statements for your actual test comparisons.

Features:
- support for stdout and stderr redirecting

Stdout redirection

Sometimes you test code which prints something to the console while for the test you don't want to confuse the user, since the test wants to print status information as well.

Console output will be redirected into two byte streams when desired (use 'Test.redirectStandardStreams( true )'). The test output will still be printed to the normal standard output streams.

When redirection takes place, the test can get a string with the last output to these streams and use this information for testing purposes as well. This buffer can also be cleaned anytime on purpose.

For an example how to write your own tests take a look at the ccl.util.test.UtilTest class.

If you write your own test code, extend from this test class and make sure you have these two constructors defined as they are expected from the Test base class in case you want to build a hierarchy of test classes.

     public MyOwnTest() {
         super();
     }

     public MyOwnTest( Test test ) {
         super( test );
     }
 
To execute your test code add for example the following code into your tests static main method:
     public static void main( String[] args ) {
         Test test = new MyNewTest();
         test.setVerbose( true );
         test.setTiming ( true );
         test.run();
         test.printResult();

         System.exit( 0 );
     }
 
So far you have not written any of your own test code and checks. These should go into the protected method '_doIt'. The test base class will invoke that method. Then you put all kinds of statements there which use the 'assert( boolean [, String] )' and 'bugIf( boolean [, String] )' methods of the base test class. Those assertions will be checked and counted. If any assertions fails the test class will print out a message as well as a stack trace. If the succeed a simple '.' will be printed. In the end the total time and the number of successful and total checks will be printed.

A successful run of the whole ccl test suite produces the following output:

 Testing Main
 -> Testing Native . SUCCESS 00:00:00.023
 -> Testing Util
    -> Testing text center formatting ...... SUCCESS 00:00:00.007
    -> Testing text block formatting ........ SUCCESS 00:00:00.054
    -> Testing OutputErrorStreamManager ............ SUCCESS 00:00:00.015
    -> Testing SToLAndConcat ............................ SUCCESS 00:00:00.042
    -> Testing FileUtil
       -> Testing copyDir ... SUCCESS 00:00:00.590
       ........................................ SUCCESS 00:00:00.915
    -> Testing Init ....................... SUCCESS 00:00:00.591
    -> Testing IniFile .... SUCCESS 00:00:00.024
    -> Testing ClassPathUtil .. SUCCESS 00:00:00.003
    -> Testing SysEnv .... SUCCESS 00:00:00.026
    -> Testing Singleton .. SUCCESS 00:00:00.004
    ................
    -> Testing atoi ................. SUCCESS 00:00:00.003
    -> Testing Int/Bytes Conversion ..................................... SUCCESS 00:00:00.005
    ................
    -> Testing Date Validation .................. SUCCESS 00:00:00.016
    ....... SUCCESS 00:00:01.877
 -> Testing XMLUtil  No Xalan XSLT parser found, skipping test!  SUCCESS 00:00:00.008
 -> Testing XMLImportHandler  No PostgreSQL database found, skipping tests!  SUCCESS 00:00:00.014
 -> Testing SwingUtil ..... SUCCESS 00:00:10.969
 SUCCESS 00:00:13.139
 *   *   *   *   *   *   *   *   *   *   *   *   *   *
 ccl.test.MainTest has finished.
 Hey, kewl, all 251 tests succeeded! :-)
 
If you want to build a hierarchy of tests like in the example above, here is an example of how to do it. In your '_doIt' method you invoke another sub test like this:
     protected void _doIt()
         throws Exception
     {
         Test testA = new ASubTest( this );
         testA.run();
         setTests( testA );

         Test testB = new AnotherSubTest( this );
         testB.run();
         setTests( testB );

         // do normal tests below
         // ...
     }
 
In the output those two sub tests will then be indented and mentioned at least with their class name and the checks done inside will be added to the number of checks done in your current class.

See Also:
UtilTest

Constructor Summary
Test()
          Default constructor which should be overwritten by an implementation of a custom test class.
Test(Test tstParent_)
          If you build a hierarchy of test use this constructor to create a new sub test.
 
Method Summary
protected abstract  void _doIt()
          Inherit this method to do your test inside of it.
protected  void _enterSubTest(java.lang.String sName_)
           
protected  void _exitSubTest()
           
protected  java.lang.Object _getValue()
           
protected  void _increment()
           
protected  void _setTests(Test pTest_)
          Deprecated. Use setTests(..) instead.
protected  void _showLiveSignals(boolean bShowLiveSignals_)
          Deprecated. Use setVerbose(..) instead.
 void assert(boolean bCheck_)
          Deprecated. use 'Assert' instead since as of jdk release 1.4 'assert' is a keyword.
 void Assert(boolean bCheck_)
          Conducts a check that a given object fullfills a given condition.
 void assert(boolean bCheck_, java.lang.String sMessage_)
          Deprecated. use 'Assert' instead since as of jdk release 1.4 'assert' is a keyword.
 void Assert(boolean bCheck_, java.lang.String sMessage_)
          Conducts a check that a given object fullfills a given condition.
 void assertEquals(boolean expected, boolean actual)
          Conducts a check that a given boolean equals another expected boolean value.
 void assertEquals(double actual, double expected, double delta)
          Conducts a check that a given double valud equals another expected double value inside or equal to a given delta value.
 void assertEquals(double actual, double expected, double delta, java.lang.String message)
          Conducts a check that a given double valud equals another expected double value inside or equal to a given delta value.
 void assertEquals(java.lang.String message, boolean expected, boolean actual)
          Conducts a check that a given boolean equals another expected boolean value.
 void assertEquals(java.lang.String message, int expected, int actual)
          Conducts a check that a given integer equals another expected integer value.
 void assertEquals(java.lang.String expected, java.lang.String actual)
          Conducts a check that a given string equals another expected string value.
 void assertEquals(java.lang.String message, java.lang.String expected, java.lang.String actual)
          Conducts a check that a given string equals another expected string value.
 void assertNotNull(java.lang.Object object_)
          Conduct a check that a given object is not null.
 void assertNotNull(java.lang.Object object_, java.lang.String sMessage_)
          Conduct a check that a given object is not null.
 void assertTrue(boolean bCheck_)
          Conducts a check that a given object fullfills a given condition.
 void assertTrue(boolean bCheck_, java.lang.String sMessage_)
          Conducts a check that a given object fullfills a given condition.
 void assertTrue(java.lang.String sMessage_, boolean bCheck_)
          Conducts a check that a given object fullfills a given condition.
 void bug(java.lang.String message)
          Increases the test counter as well as the bug counter by one and prints the given message.
 boolean bugIf(boolean bCondition)
          The opposite of assert.
 boolean bugIf(boolean bCondition, java.lang.String sDescription)
          The opposite of assert.
 boolean bugIf(boolean bCondition, java.lang.String sDescription, java.lang.Throwable pThrowable)
          The opposite of assert.
 void fail()
          Increases the test counter as well as the bug counter by one.
 void fail(java.lang.String message)
          Increases the test counter as well as the bug counter by one and prints the given message.
 long getBugs()
          The total number of failures found so far.
 java.lang.String getComment()
          Return a one line comment for this test.
 long getGlobalTests()
          The total number of tests done so far.
 long getLocalTests()
           
 java.lang.String getName()
          The application name of this test object.
 java.lang.String getTestClassDirectory()
          Deprecated. Use getTestDirectory instead as this method will not work well together with jar files.
 java.lang.String getTestDirectory()
          This method returns the full path of a dedicated test directory under the application directory.
static java.lang.String getTestName(java.lang.Object testObject)
          Given a test object it returns a string containing a name representing that test.
static java.lang.String getTestName(java.lang.String sTestName)
          Given a test object it returns a string containing a name representing that test.
static java.lang.Object getValue()
           
 void initialize(java.lang.String[] asArg_)
          Process the arguments from the main class here.
static boolean isTest()
           
 boolean isTiming()
          True if timing information gets printed in addition to the standard test output.
 boolean isVerbose()
          Checks if each test condition handled will be visually presented with a dot on the standard output.
 void printResult()
          Prints a summary of all tests to standard output.
static void printResult(Test pTest_)
          Prints a summary of all tests to standard output.
static void redirectStandardStreams(boolean bRedirect_)
          Redirect stdout and stderr into private streams so the do not confuse the user with test output.
 void run()
           
 void setBug()
          Count one more failure.
 void setParentTest(Test tstParent_)
          If you build a hierarchy of test use this method to set a parent test object before running the checks of the sub test.
 void setTests(Test test_)
           
 void setTiming(boolean bTiming_)
          You want timing information? Set this to true!
 void setValue(java.lang.Object oValue_)
          Deprecated.  
 void setVerbose(boolean bVerbose_)
          Defines if each test condition handled will be visually presented with a dot on the standard output.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Test

public Test()
Default constructor which should be overwritten by an implementation of a custom test class.


Test

public Test(Test tstParent_)
If you build a hierarchy of test use this constructor to create a new sub test.

Method Detail

_getValue

protected java.lang.Object _getValue()

_enterSubTest

protected void _enterSubTest(java.lang.String sName_)
Parameters:
sName_ - e.g. "jacob".

_exitSubTest

protected void _exitSubTest()

_setTests

protected void _setTests(Test pTest_)
Deprecated. Use setTests(..) instead.


_increment

protected void _increment()

_showLiveSignals

protected void _showLiveSignals(boolean bShowLiveSignals_)
Deprecated. Use setVerbose(..) instead.


getLocalTests

public long getLocalTests()

getGlobalTests

public long getGlobalTests()
The total number of tests done so far.


getBugs

public long getBugs()
The total number of failures found so far.


setBug

public void setBug()
Count one more failure.


setValue

public void setValue(java.lang.Object oValue_)
Deprecated.  


isVerbose

public boolean isVerbose()
Checks if each test condition handled will be visually presented with a dot on the standard output.


setVerbose

public void setVerbose(boolean bVerbose_)
Defines if each test condition handled will be visually presented with a dot on the standard output.


setTiming

public void setTiming(boolean bTiming_)
You want timing information? Set this to true!


isTiming

public boolean isTiming()
True if timing information gets printed in addition to the standard test output.


printResult

public void printResult()
Prints a summary of all tests to standard output.


printResult

public static void printResult(Test pTest_)
Prints a summary of all tests to standard output.


setParentTest

public void setParentTest(Test tstParent_)
If you build a hierarchy of test use this method to set a parent test object before running the checks of the sub test.


bugIf

public boolean bugIf(boolean bCondition)
The opposite of assert. If the condition is true, it gets counted as a failure.


bugIf

public boolean bugIf(boolean bCondition,
                     java.lang.String sDescription)
The opposite of assert. If the condition is true, it gets counted as a failure.


bugIf

public boolean bugIf(boolean bCondition,
                     java.lang.String sDescription,
                     java.lang.Throwable pThrowable)
The opposite of assert. If the condition is true, it gets counted as a failure.


assertNotNull

public void assertNotNull(java.lang.Object object_)
Conduct a check that a given object is not null. Otherwise count as a failed test.


assertNotNull

public void assertNotNull(java.lang.Object object_,
                          java.lang.String sMessage_)
Conduct a check that a given object is not null. Otherwise count as a failed test.

Parameters:
sMessage_ - The message gets printed when the assertion fails.

assert

public void assert(boolean bCheck_)
Deprecated. use 'Assert' instead since as of jdk release 1.4 'assert' is a keyword.

Conducts a check that a given object fullfills a given condition. If this condition is not fullfilled (boolean is false) this check gets counted as a failure.


assert

public void assert(boolean bCheck_,
                   java.lang.String sMessage_)
Deprecated. use 'Assert' instead since as of jdk release 1.4 'assert' is a keyword.

Conducts a check that a given object fullfills a given condition. If this condition is not fullfilled (boolean is false) this check gets counted as a failure.

Parameters:
sMessage_ - The message gets printed when the assertion fails.

Assert

public void Assert(boolean bCheck_)
Conducts a check that a given object fullfills a given condition. If this condition is not fullfilled (boolean is false) this check gets counted as a failure.


Assert

public void Assert(boolean bCheck_,
                   java.lang.String sMessage_)
Conducts a check that a given object fullfills a given condition. If this condition is not fullfilled (boolean is false) this check gets counted as a failure.

Parameters:
sMessage_ - The message gets printed when the assertion fails.

assertTrue

public void assertTrue(boolean bCheck_)
Conducts a check that a given object fullfills a given condition. If this condition is not fullfilled (boolean is false) this check gets counted as a failure.


assertTrue

public void assertTrue(boolean bCheck_,
                       java.lang.String sMessage_)
Conducts a check that a given object fullfills a given condition. If this condition is not fullfilled (boolean is false) this check gets counted as a failure.

Parameters:
sMessage_ - The message gets printed when the assertion fails.

assertTrue

public void assertTrue(java.lang.String sMessage_,
                       boolean bCheck_)
Conducts a check that a given object fullfills a given condition. If this condition is not fullfilled (boolean is false) this check gets counted as a failure. This message is for (local :-) conformance with the JUnit Assert API.

Parameters:
sMessage_ - The message gets printed when the assertion fails.

assertEquals

public void assertEquals(java.lang.String message,
                         boolean expected,
                         boolean actual)
Conducts a check that a given boolean equals another expected boolean value. If this condition is not fullfilled (boolean is false) this check gets counted as a failure. This message is for (local :-) conformance with the JUnit Assert API.


assertEquals

public void assertEquals(boolean expected,
                         boolean actual)
Conducts a check that a given boolean equals another expected boolean value. If this condition is not fullfilled (boolean is false) this check gets counted as a failure. This message is for (local :-) conformance with the JUnit Assert API.


assertEquals

public void assertEquals(java.lang.String message,
                         int expected,
                         int actual)
Conducts a check that a given integer equals another expected integer value. If this condition is not fullfilled (boolean is false) this check gets counted as a failure. This message is for (local :-) conformance with the JUnit Assert API.


assertEquals

public void assertEquals(java.lang.String message,
                         java.lang.String expected,
                         java.lang.String actual)
Conducts a check that a given string equals another expected string value. If this condition is not fullfilled (boolean is false) this check gets counted as a failure. This message is for (local :-) conformance with the JUnit Assert API.


assertEquals

public void assertEquals(java.lang.String expected,
                         java.lang.String actual)
Conducts a check that a given string equals another expected string value. If this condition is not fullfilled (boolean is false) this check gets counted as a failure. This message is for (local :-) conformance with the JUnit Assert API.


assertEquals

public void assertEquals(double actual,
                         double expected,
                         double delta)
Conducts a check that a given double valud equals another expected double value inside or equal to a given delta value. If this condition is not fullfilled this check gets counted as a failure.

This message does not conform to the JUnit API as it has the order of the expected and actual value object just the other way around, thought that should not make a difference.


assertEquals

public void assertEquals(double actual,
                         double expected,
                         double delta,
                         java.lang.String message)
Conducts a check that a given double valud equals another expected double value inside or equal to a given delta value. If this condition is not fullfilled this check gets counted as a failure.

This message does not conform to the JUnit API as the message string is the last parameter and not the first.


fail

public void fail(java.lang.String message)
Increases the test counter as well as the bug counter by one and prints the given message. Methods 'bug' and 'fail' are equivalent.

See Also:
bug(java.lang.String)

fail

public void fail()
Increases the test counter as well as the bug counter by one. Methods 'bug' and 'fail' are equivalent.

See Also:
bug(java.lang.String)

bug

public void bug(java.lang.String message)
Increases the test counter as well as the bug counter by one and prints the given message. Methods 'bug' and 'fail' are equivalent.

See Also:
fail(java.lang.String)

_doIt

protected abstract void _doIt()
                       throws java.lang.Exception
Inherit this method to do your test inside of it.

Throws:
java.lang.Exception - Whatever can go wrong.

run

public void run()
Specified by:
run in interface java.lang.Runnable

getName

public java.lang.String getName()
The application name of this test object. By default it is the class name without any Test' part in it.


getTestName

public static java.lang.String getTestName(java.lang.Object testObject)
Given a test object it returns a string containing a name representing that test. For example class ccl.util.UtilTest will result in "Util".


getTestName

public static java.lang.String getTestName(java.lang.String sTestName)
Given a test object it returns a string containing a name representing that test. For example class ccl.util.UtilTest will result in "Util".

Parameters:
sTestName - name of a test class, e.g. "UtilTest".

initialize

public void initialize(java.lang.String[] asArg_)
Process the arguments from the main class here. This way you can pass arguments to this test also from the test center, which does not use the main method to invoke a test.


isTest

public static boolean isTest()

getValue

public static java.lang.Object getValue()

getTestClassDirectory

public java.lang.String getTestClassDirectory()
Deprecated. Use getTestDirectory instead as this method will not work well together with jar files.

Returns the directory of the class on which this method is invoked.

See Also:
getTestDirectory()

getTestDirectory

public java.lang.String getTestDirectory()
This method returns the full path of a dedicated test directory under the application directory.

Returns:
a string for a director with test data for the current application, e.g. "/home/clemens/src/java/jacob/test".

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setTests

public void setTests(Test test_)

getComment

public java.lang.String getComment()
Return a one line comment for this test. Should be overwritten be test classes inheriting from this class.


redirectStandardStreams

public static void redirectStandardStreams(boolean bRedirect_)
Redirect stdout and stderr into private streams so the do not confuse the user with test output. Look at the class documentation at the top for detailed stdout redirection documentation.