Package org.apache.batik.test
Class AbstractTest
- java.lang.Object
-
- org.apache.batik.test.AbstractTest
-
- All Implemented Interfaces:
Test
- Direct Known Subclasses:
AbstractRenderingAccuracyTest,DefaultTestSuite,DummyValidTest,ImageCompareTest,ParametrizedTest,PerformanceTest,PerformanceTestValidator,SelfContainedSVGOnLoadTest,SelfContainedSVGOnLoadTestValidator.DefaultErrorTest,SelfContainedSVGOnLoadTestValidator.ReportSuccess,SVGOnLoadExceptionTest,SVGRenderingAccuracyTestValidator.DefaultConfigTest,TestReportValidator,XMLTestSuiteRunnerValidator.XMLTestSuiteRunnerTest
public abstract class AbstractTest extends java.lang.Object implements Test
Base class containing convenience methods for writing tests.
There are at least three approaches to write new tests derived fromAbstractTest:- You can simply override the
runImplBasicmethod and return true or false depending on whether or not the test fails. - You can choose to report more complex test failure conditions
by overriding the
runImplmethod which returns aTestReport. In that case, you can use the convenience methods such asreportFailurereportSuccessorreportExceptionto help build aTestReport, and use theTestReport'saddDescriptionEntryto populate the report with relevant error description. - You can choose to use the various assertion methods such as
assertNull,assertEqualsorassertTrue. These methods throw exceptions which will be turned inTestReportsby theAbstractTest.
public class MyTestA extends AbstractTest { public boolean runImplBasic() { if(someConditionFails){ return false; } return true; } }public class MyTestB extends AbstractTest { public TestReport runImpl() { if(someConditionFails){ TestReport report = reportError(MY_ERROR_CODE); report.addDescriptionEntry(ENTRY_KEY_MY_ERROR_DESCRIPTION_KEY, myErrorDescriptionValue); return report; } return reportSuccess(); }public class MyTestC extends AbstractTest { public TestReport runImpl() throws Exception { assertTrue(somCondition); assertEquals(valueA, valueB); assertNull(shouldBeNullRef); if(someErrorCondition){ error(MY_ERROR_CODE); } return reportSuccess(); }- Version:
- $Id: AbstractTest.java 1805408 2017-08-18 12:21:52Z ssteiner $
-
-
Constructor Summary
Constructors Constructor Description AbstractTest()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidassertEquals(int ref, int cmp)voidassertEquals(java.lang.Object ref, java.lang.Object cmp)Convenience method to check for a specific condition.voidassertNull(java.lang.Object ref)Convenience method to check that a reference is nullvoidassertTrue(boolean b)Convenience method to check that a given boolean is true.voiderror(java.lang.String errorCode)Convenience method to report an error condition.java.lang.StringgetId()Return thisTest's id.java.lang.StringgetName()Returns thisTest's name.TestSuitegetParent()Returns thisTest's parent, in case thisTestis part of aTestSuite.java.lang.StringgetQualifiedId()Return thisTest's qualified id.TestReportreportError(java.lang.String errorCode)Convenience method to report a simple error code.TestReportreportException(java.lang.String errorCode, java.lang.Exception e)Convenience method to help implementations report errors.TestReportreportSuccess()Convenience method.TestReportrun()This default implementation of the run method catches any Exception thrown from the runImpl method and creates aTestReportindicating an internalTestfailure when that happens.TestReportrunImpl()Subclasses should implement this method with the content of the test case.booleanrunImplBasic()In the simplest test implementation, developers can simply implement the following method.voidsetId(java.lang.String id)Set thisTest's id.voidsetName(java.lang.String name)Sets this test's namevoidsetParent(TestSuite parent)Set thisTest's parent.
-
-
-
Field Detail
-
id
protected java.lang.String id
This test's id.
-
parent
protected TestSuite parent
This test's parent, in case this test is part of a suite.
-
name
protected java.lang.String name
This test's name. If null, the class' name is returned.
-
report
private DefaultTestReport report
TestReport
-
-
Method Detail
-
getName
public java.lang.String getName()
Returns thisTest's name.
-
setName
public void setName(java.lang.String name)
Sets this test's name
-
getQualifiedId
public java.lang.String getQualifiedId()
Return thisTest's qualified id.- Specified by:
getQualifiedIdin interfaceTest
-
setId
public void setId(java.lang.String id)
Set thisTest's id. Null is not allowed.
-
getParent
public TestSuite getParent()
Description copied from interface:TestReturns thisTest's parent, in case thisTestis part of aTestSuite. The returned value may be null.
-
setParent
public void setParent(TestSuite parent)
Description copied from interface:TestSet thisTest's parent.
-
run
public TestReport run()
This default implementation of the run method catches any Exception thrown from the runImpl method and creates aTestReportindicating an internalTestfailure when that happens. Otherwise, this method simply returns theTestReportgenerated by therunImplmethod.
-
runImpl
public TestReport runImpl() throws java.lang.Exception
Subclasses should implement this method with the content of the test case. Typically, implementations will choose to catch and process all exceptions and error conditions they are looking for in the code they exercise but will let exceptions due to their own processing propagate.- Throws:
java.lang.Exception
-
runImplBasic
public boolean runImplBasic() throws java.lang.ExceptionIn the simplest test implementation, developers can simply implement the following method.- Throws:
java.lang.Exception
-
reportSuccess
public TestReport reportSuccess()
Convenience method.
-
reportError
public TestReport reportError(java.lang.String errorCode)
Convenience method to report a simple error code.
-
error
public void error(java.lang.String errorCode) throws TestErrorConditionExceptionConvenience method to report an error condition.- Throws:
TestErrorConditionException
-
assertNull
public void assertNull(java.lang.Object ref) throws AssertNullExceptionConvenience method to check that a reference is null- Throws:
AssertNullException
-
assertTrue
public void assertTrue(boolean b) throws AssertTrueExceptionConvenience method to check that a given boolean is true.- Throws:
AssertTrueException
-
assertEquals
public void assertEquals(java.lang.Object ref, java.lang.Object cmp) throws AssertEqualsExceptionConvenience method to check for a specific condition. Returns true if both objects are null or if ref is not null and ref.equals(cmp) is true.- Throws:
AssertEqualsException
-
assertEquals
public void assertEquals(int ref, int cmp) throws AssertEqualsException- Throws:
AssertEqualsException
-
reportException
public TestReport reportException(java.lang.String errorCode, java.lang.Exception e)
Convenience method to help implementations report errors. AnAbstractTestextension will typically catch exceptions for specific error conditions it wants to point out. For example:public TestReport runImpl() throws Exception {
try{
.... something ....
catch(MySpecialException e){
return reportException(MY_SPECIAL_ERROR_CODE, e);
}
public static final String MY_SPECIAL_ERROR_CODE = "myNonQualifiedClassName.my.error.code"
Note that the implementor will also need to add an entry in its Messages.properties file. That file is expected to be in a resource file calledMessageshaving the same package name as theTestclass, appended with ".resources".
-
-