src.jptools.testing.TestSuite Maven / Gradle / Ivy
/*
* TestSuite.java
*
* Copyright by jptools, all rights reserved.
*/
package jptools.testing;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import jptools.logger.LogInformation;
import jptools.util.StackTrace;
import jptools.util.StackTraceElement;
import jptools.util.profile.IProfileMarker;
import jptools.util.profile.ProfileMarkerFactory;
import jptools.util.profile.ProfileResult;
import jptools.util.profile.ProfileStack;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;
import junit.framework.TestResult;
/**
* This class wraps the TestSuite of the junit package
* @author Patrick Meier
* @version $Revision: 1.20 $
*/
public class TestSuite
extends junit.framework.TestSuite
{
static long failureCounter = 0;
private static boolean listenerAdded = false;
private static int numberOfTestSuites;
private static int numberOfTestCases;
private TestFramework testFramework;
private int timeProfileTolerance;
List failures = new ArrayList();
int numberOfErrors;
int numberOfFailres;
/**
* Constructor
*/
public TestSuite()
{
super();
testFramework = null;
timeProfileTolerance = -1;
}
/**
* Constructor
* @param testFramework the test framework instance
* @param timeProfileTolerance the time tolerance
*/
public TestSuite( TestFramework testFramework,
int timeProfileTolerance )
{
super();
this.testFramework = testFramework;
this.timeProfileTolerance = timeProfileTolerance;
}
/**
* Constructor
* @param name the name of the test suite
* @param s TestSuite
*/
public TestSuite( String name, TestSuite s )
{
super( name );
this.testFramework = s.testFramework;
this.timeProfileTolerance = s.timeProfileTolerance;
}
/**
* Constructor
* @param theClass the class
* @param name the name
*/
public TestSuite( Class extends TestCase> theClass, String name )
{
super( theClass, name );
testFramework = null;
timeProfileTolerance = -1;
}
/**
* Constructor
* @param theClass the class
*/
public TestSuite( Class extends TestCase> theClass )
{
super( theClass );
testFramework = null;
timeProfileTolerance = -1;
}
/**
* Constructor
* @param theClass the class
* @param timeProfileTolerance the time tolerance
*/
public TestSuite( Class extends TestCase> theClass,
int timeProfileTolerance )
{
super( theClass );
this.testFramework = null;
this.timeProfileTolerance = timeProfileTolerance;
}
/**
* Constructor
* @param theClass the class
* @param name the name of the test suite
* @param timeProfileTolerance the time tolerance
*/
public TestSuite( Class extends TestCase> theClass,
String name,
int timeProfileTolerance )
{
super( theClass, name );
this.testFramework = null;
this.timeProfileTolerance = timeProfileTolerance;
}
/**
* Constructor
* @param name the name
*/
public TestSuite( String name )
{
super( name );
testFramework = null;
timeProfileTolerance = -1;
}
/**
* Gets the time profile tolerance
*
* @return the time tolerance
*/
public int getTimeProfileTolerance()
{
return timeProfileTolerance;
}
/**
* @see junit.framework.TestSuite#addTest(junit.framework.Test)
*/
@Override
public void addTest( Test test )
{
if( test.toString().equals( "warning(junit.framework.TestSuite$1)" ))
{
; // ignore internal warning
}
else
{
super.addTest( test );
}
}
/**
* @see junit.framework.TestSuite#addTestSuite(java.lang.Class)
*/
@SuppressWarnings("unchecked")
@Override
public void addTestSuite( Class testClass )
{
super.addTestSuite( testClass );
}
/**
* @see junit.framework.Test#countTestCases()
*/
@Override
public int countTestCases()
{
return super.countTestCases();
}
/**
* @see junit.framework.Test#run(junit.framework.TestResult)
*/
@Override
public void run( TestResult result )
{
if( testFramework!=null )
testFramework.startTestSuite();
TestCaseLogger.getInstance();
boolean isTestSuite = false;
Test test;
String name = "";
String printName = "";
String lastName = "";
LogInformation logInfo;
int sameMethodNameCounter = 1;
if( !listenerAdded )
addFailureListener( result );
for( Enumeration e = tests(); e.hasMoreElements(); )
{
if( result.shouldStop() )
break;
isTestSuite = false;
logInfo = null;
lastName = printName;
test = (Test)e.nextElement();
name = test.getClass().getName();
printName = name;
if( test instanceof LoggerTestCase )
{
name += "." + ((LoggerTestCase)test).getName();
name = name.trim();
printName = name;
logInfo = ((LoggerTestCase)test).getLogInformation();
numberOfTestCases++;
}
else if( test instanceof junit.framework.TestCase )
{
name += "." + ((junit.framework.TestCase)test).getName();
name = name.trim();
printName = name;
numberOfTestCases++;
}
else if( test instanceof junit.framework.TestSuite )
{
if( ((junit.framework.TestSuite)test).getName()!=null )
name = ((junit.framework.TestSuite)test).getName();
name = name.trim();
printName = name;
isTestSuite = true;
numberOfTestSuites++;
}
else
{
if( lastName.equals( printName ) )
{
name += sameMethodNameCounter;
sameMethodNameCounter++;
}
else
{
sameMethodNameCounter = 1;
}
}
callTestCase( result,
test,
printName,
name,
isTestSuite,
logInfo,
timeProfileTolerance );
}
if( testFramework!=null )
testFramework.endTestSuite( timeProfileTolerance );
}
/**
*
* @param result
*/
private void addFailureListener( TestResult result )
{
listenerAdded = true;
result.addListener( new TestListener()
{
/**
* @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable)
*/
@Override
public void addError( Test currentTest, Throwable t )
{
if( currentTest.getClass().getName().startsWith( "junit.framework" ) )
return;
numberOfErrors++;
processThrowable( currentTest, t );
}
/**
* @see junit.framework.TestListener#addFailure(junit.framework.Test, junit.framework.AssertionFailedError)
*/
@Override
public void addFailure( Test currentTest, AssertionFailedError f )
{
if( currentTest.getClass().getName().startsWith( "junit.framework" ) )
return;
numberOfFailres++;
processThrowable( currentTest, f );
}
/**
* The test
* @param currentTest the current test
* @param t the throwable
*/
private void processThrowable( Test currentTest,
Throwable t )
{
StringWriter stringWriter=new StringWriter();
PrintWriter printWriter=new PrintWriter( stringWriter );
// output the stack trace to the print writer
t.printStackTrace( printWriter );
StackTraceElement[] elements;
try
{
elements = StackTrace.parseStackTraceElements( stringWriter.toString(), 0, -1 );
int pos = 0;
List stackTrace = new ArrayList();
StackTraceElement element = elements[ pos ];
if( element!=null && element.getClassName()!=null )
{
// ignore trailing statements
for( ; element.getClassName().startsWith( "junit." ) && pos