public class TestCase
extends junit.framework.TestCase
TestCase, providing some
additional assertions and methods for providing test data for convenience.
All the existing methods of TestCase are simply delegated
to the superclass.
Some of the methods are concerned with providing random values; these are deterministic in that the random seed is set to a fixed value when the test case is initialised, so a given test should always be working with the same data, though the same call twice in a given test will provide different random data.
| Modifier and Type | Field and Description |
|---|---|
static short |
IGNORE_ATTRIBUTE_PRESENCE
|
static short |
IGNORE_ATTRIBUTE_VALUE |
static short |
IGNORE_COMMENTS |
static short |
IGNORE_WHITESPACE |
| Constructor and Description |
|---|
TestCase()
No-arg constructor.
|
TestCase(String name)
Constructs a test case with the given name.
|
| Modifier and Type | Method and Description |
|---|---|
void |
assertArrayEquals(Object expected,
Object actual)
Asserts that two arrays have exactly the same contents.
|
void |
assertArrayEquals(Object expected,
Object actual,
double delta)
Asserts that two numeric arrays have the same contents
within a given tolerance.
|
void |
assertArrayEquals(String message,
Object expected,
Object actual)
Asserts that two arrays have exactly the same contents.
|
void |
assertArrayEquals(String message,
Object expected,
Object actual,
double delta)
Asserts that two numeric arrays have the same contents
within a given tolerance.
|
void |
assertArrayNotEquals(Object expected,
Object actual)
Asserts that two objects are not arrays with the same contents.
|
void |
assertArrayNotEquals(Object expected,
Object actual,
double delta)
Asserts that two objects are not numeric arrays having the same
contents within a given tolerance.
|
void |
assertArrayNotEquals(String message,
Object expected,
Object actual)
Asserts that two objects are not arrays with the same contents.
|
void |
assertArrayNotEquals(String message,
Object expected,
Object actual,
double delta)
Asserts that two objects are not numeric arrays having the same
contents within a given tolerance.
|
void |
assertDOMEquals(InputStream s,
Node actual,
String context,
int flags)
Asserts that a DOM is equivalent to the DOM read from a given stream.
|
void |
assertDOMEquals(Node expected,
Node actual)
Asserts that two DOMs are equal.
|
void |
assertDOMEquals(Node expected,
Node actual,
String context,
int flags)
Asserts that two DOMs are equal.
|
void |
assertDOMEquals(String s,
Node n)
Asserts that a DOM is equivalent to the DOM implied by the XML
in a given string.
|
void |
assertDOMEquals(String expected,
Node actual,
String context,
int flags)
Asserts that a DOM is equivalent to the DOM implied by the XML
in a given string.
|
void |
assertDOMEquals(URL url,
Node actual)
Asserts that a DOM is equivalent to the XML in a given URL.
|
void |
assertDOMEquals(URL url,
Node actual,
String context,
int flags)
Asserts that a DOM is equivalent to the XML in a given URL.
|
void |
assertEquals(double expected,
double actual)
Asserts that two
doubles are exactly equal. |
void |
assertEquals(float expected,
float actual)
Asserts that two
floats are exactly equal. |
void |
assertEquals(String message,
double expected,
double actual)
Asserts that two
doubles are exactly equal. |
void |
assertEquals(String message,
float expected,
float actual)
Asserts that two
floats are exactly equal. |
void |
assertSourceEquals(Source expected,
Source actual,
String context,
int flags)
Asserts that two XML
Source objects
represent the same XML Infoset. |
void |
assertValidXML(InputSource isrc)
Asserts that the contents of a stream are valid XML.
|
void |
assertValidXML(String message,
InputSource isrc)
Asserts that the contents of a stream are valid XML.
|
void |
fillCycle(Object array,
int min,
int max)
Fills a given array with a regular pattern of integer values.
|
void |
fillCycle(Object destArray,
Object sourceArray)
Fills a given array with a pattern of values taken from another one.
|
void |
fillRandom(Object array,
double min,
double max)
Fills a given array with random numbers between two floating point
values.
|
void |
fillRandom(Object array,
int min,
int max)
Fills a given array with random numbers between two integer values.
|
static boolean |
isHeadless()
Tests whether or not a display, keyboard and mouse can in fact
be supported in this environment.
|
countTestCases, createResult, getName, run, run, runBare, runTest, setName, setUp, tearDown, toStringassertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSamepublic static final short IGNORE_ATTRIBUTE_PRESENCE
public static final short IGNORE_ATTRIBUTE_VALUE
public static final short IGNORE_WHITESPACE
public static final short IGNORE_COMMENTS
public TestCase()
public TestCase(String name)
name - test case namepublic void assertArrayEquals(String message, Object expected, Object actual)
expected and
actual
must both be array objects whose component types are of the same
class; they must have the same number of elements; and the
ith element of expected must be `equal' to the
ith element of actual. `Equality' in this sense
depends on the types of the arrays: if they are arrays of a primitive
type, the primitive values must be equal, and if they are
object arrays equality is assessed using the
Object.equals(java.lang.Object) method.
In the case of float[] and double[] arrays,
elements which both have the value NaN are taken to match.message - the message to output if the assertion failsexpected - the expected array objectactual - the actual array objectjunit.framework.AssertionFailedError - if the assertion is untruepublic void assertArrayNotEquals(String message, Object expected, Object actual)
assertArrayEquals(String,Object,Object).message - the message to output if the assertion failsexpected - the expected array objectactual - the actual array object,
asserted not to match expectedjunit.framework.AssertionFailedError - if the assertion is untruepublic void assertArrayEquals(Object expected, Object actual)
expected and actual
must both be array objects whose component types are of the same
class; they must have the same number of elements; and the
ith element of expected must be `equal' to the
ith element of actual. `Equality' in this sense
depends on the types of the arrays: if they are arrays of a primitive
type, the primitive values must be equal, and if they are
object arrays equality is assessed using the
Object.equals(java.lang.Object) method.
In the case of float[] and double[] arrays,
elements which both have the value NaN are taken to match.expected - the expected array objectactual - the actual array objectjunit.framework.AssertionFailedError - if the assertion is untruepublic void assertArrayNotEquals(Object expected, Object actual)
assertArrayEquals(Object,Object).expected - the expected array objectactual - the actual array object,
asserted not to match expectedjunit.framework.AssertionFailedError - if the assertion is untruepublic void assertArrayEquals(String message, Object expected, Object actual, double delta)
expected and actual
must both be primitive numeric array objects whose
component types are of the same
class; they must have the same number of elements; and the
ith element of expected must be `equal' to the
ith element of actual.
Equality in this sense normally means
(abs(expected[i]-actual[i]<delta),
but in the case of float[] and double[] arrays,
elements which both have the value NaN are taken to match,
and if the expected element is infinite, then delta
is ignored.
If delta<1 and the arrays are of integer type,
this method does the same as
assertArrayEquals(String,Object,Object).
message - the message to output if the assertion failsexpected - the array object containing the expected valuesactual - the array object containing the actual valuesdelta - the tolerance acceptable between valuesjunit.framework.AssertionFailedError - if the assertion is untruepublic void assertArrayNotEquals(String message, Object expected, Object actual, double delta)
assertArrayEquals(String,Object,Object,double).message - the message to output if the assertion failsexpected - the array object containing the expected valuesactual - the array object containing the actual values,
asserted not to match expecteddelta - the tolerance acceptable between valuesjunit.framework.AssertionFailedError - if the assertion is untruepublic void assertArrayEquals(Object expected, Object actual, double delta)
expected and actual
must both be primitive numeric array objects whose component
types are of the same
class; they must have the same number of elements; and the
ith element of expected must be `equal' to the
ith element of actual.
Equality in this sense normally means
(abs(expected[i]-actual[i]<delta),
but in the case of float[] and double[] arrays,
elements which both have the value NaN are taken to match,
and if the expected element is infinite, then delta
is ignored.
If delta<1 and the arrays are of integer type,
this method does the same as
assertArrayEquals(Object,Object).
expected - the array object containing the expected valuesactual - the array object containing the actual valuesdelta - the tolerance acceptable between valuesjunit.framework.AssertionFailedError - if the assertion is untruepublic void assertArrayNotEquals(Object expected, Object actual, double delta)
assertArrayEquals(Object,Object,double).expected - the array object containing the expected valuesactual - the array object containing the actual values,
asserted not to match expecteddelta - the tolerance acceptable between valuesjunit.framework.AssertionFailedError - if the assertion is untruepublic void assertEquals(String message, float expected, float actual)
floats are exactly equal.message - the message to output if the assertion failsexpected - the expected valueactual - the actual valuejunit.framework.AssertionFailedError - if the assertion is untruepublic void assertEquals(float expected,
float actual)
floats are exactly equal.expected - the expected valueactual - the actual valuejunit.framework.AssertionFailedError - if the assertion is untruepublic void assertEquals(String message, double expected, double actual)
doubles are exactly equal.message - the message to output if the assertion failsexpected - the expected valueactual - the actual valuejunit.framework.AssertionFailedError - if the assertion is untruepublic void assertEquals(double expected,
double actual)
doubles are exactly equal.expected - the expected valueactual - the actual valuejunit.framework.AssertionFailedError - if the assertion is untruepublic void assertDOMEquals(URL url, Node actual) throws IOException, SAXException, ParserConfigurationException
assertDOMEquals(dom, actual, filename, 0) with
the first argument being the document element of the DOM read
from the URL, and the third argument being the last part
(the `file name') of the URL.url - pointing to an XML file -- the document element of
this file is the expected valueactual - the node which is being comparedIOException - if the file cannot be foundSAXException - if there is a problem parsing the XMLParserConfigurationException - if the
XML parser cannot be initialisedjunit.framework.AssertionFailedError - if the assertion is untrueassertDOMEquals(Node,Node,String,int)public void assertDOMEquals(URL url, Node actual, String context, int flags) throws IOException, SAXException, ParserConfigurationException
assertDOMEquals(dom, actual, context, flags) with
the first argument being the document element of the DOM read
from the URL, and the third argument being the last part
(the `file name') of the URL.url - pointing to an XML file -- the document element of
this file is the expected valueactual - the node which is being comparedcontext - a string indicating the context of this; if
null, it defaults to `string:'flags - a set of flags controlling the comparison; see
assertDOMEquals(Node,Node,String,int)IOException - if the file cannot be foundSAXException - if there is a problem parsing the XMLParserConfigurationException - if the
XML parser cannot be initialisedjunit.framework.AssertionFailedError - if the assertion is untrueassertDOMEquals(Node,Node,String,int)public void assertDOMEquals(String s, Node n) throws IOException, SAXException, ParserConfigurationException
public void assertDOMEquals(String expected, Node actual, String context, int flags) throws IOException, SAXException, ParserConfigurationException
expected - a string containing XML -- the document element of
this file is the expected valueactual - the node which is comparedcontext - a string indicating the context of this; if
null, it defaults to `string:'flags - a set of flags controlling the comparison; see
assertDOMEquals(Node,Node,String,int)junit.framework.AssertionFailedError - if the assertion is untrueIOExceptionSAXExceptionParserConfigurationExceptionassertDOMEquals(Node,Node,String,int)public void assertDOMEquals(InputStream s, Node actual, String context, int flags) throws IOException, SAXException, ParserConfigurationException
s - a stream from which XML may be read -- the document element of
the resulting DOM is the expected valueactual - the node which is comparedcontext - a string indicating the context of this; may be
nullflags - a set of flags controlling the comparison; see
assertDOMEquals(Node,Node,String,int)junit.framework.AssertionFailedError - if the assertion is untrueIOExceptionSAXExceptionParserConfigurationExceptionassertDOMEquals(Node,Node,String,int)public void assertDOMEquals(Node expected, Node actual)
expected - the Node containing the expected DOMactual - the Node to be testedjunit.framework.AssertionFailedError - if the assertion is untrueassertDOMEquals(Node,Node,String,int)public void assertSourceEquals(Source expected, Source actual, String context, int flags)
Source objects
represent the same XML Infoset. Differences in whitespace and
in comments may optionally be ignored.expected - the Source object containing the expected infosetactual - the Source object containing the actual infoset,
asserted to match expectedcontext - a string indicating the context of this; may be
nullflags - a set of flags indicating which node tests to
omit. Passing as zero includes all tests.junit.framework.AssertionFailedError - if the assertion is untrueassertDOMEquals(Node,Node,String,int)public void assertDOMEquals(Node expected, Node actual, String context, int flags)
If an assertion fails, the method indicates the location by showing in the failure message the location of the mismatched node, so that
AssertionFailedError: .../test.xml:/[1]ndx/[2]data expected: ...indicates that the assertion failed when examining the second child node (which was a
<data> element) of the first
child of the file test.xml
If the flags argument is non-zero, it indicates
a set of tests on the DOM to omit. The value is ORed together
from the following constants:
TestCase.IGNORE_ATTRIBUTE_PRESENCE
TestCase.IGNORE_ATTRIBUTE_VALUE
TestCase.IGNORE_WHITESPACE
TestCase.IGNORE_COMMENTS
expected - the Node containing the expected DOMactual - the Node to be testedcontext - a string indicating the context, which will be
used in assertion failure reports. May be nullflags - a set of flags indicating which node tests to
omit. Passing as zero includes all tests.junit.framework.AssertionFailedError - if the assertion is untruepublic void assertValidXML(String message, InputSource isrc) throws IOException, SAXException
Entity resolution is done using an instance of
StarEntityResolver.
message - message associated with assertion failureisrc - input source containing an XML documentIOException - if there is an error reading strmSAXException - if the document in strm
is badly-formed or invalidpublic void assertValidXML(InputSource isrc) throws IOException, SAXException
isrc - input stream containing an XML documentIOException - if there is an error reading strmSAXException - if the document in strm
is badly-formed or invalidpublic void fillRandom(Object array, double min, double max)
array - an array of primitives to be filled with
random valuesmin - the smallest value which will be used
(will be converted to the appropriate primitive type)max - the largest value which will be used
(will be converted to the appropriate primitive type)IllegalArgumentException - if array is not an array
of a suitable primitive typepublic void fillRandom(Object array, int min, int max)
array - an array of primitives to be filled with
random valuesmin - the smallest value which will be used
(will be converted to the appropriate primitive type)max - the largest value which will be used
(will be converted to the appropriate primitive type)IllegalArgumentException - if array is not an array
of a suitable primitive typepublic void fillCycle(Object array, int min, int max)
min, min+1, min+2 .. max-1, min, min+1, min+2.. and so on.
If the max<min then the values will start at
min and keep increasing.
The results might not be as expected if you use a min and
max values outside the range
of the numeric type in question.
array - an array of primitives to be filled with cycling valuesmin - the first valuemax - the highest value, or if less than min an
indication that there is no maximumIllegalArgumentException - if array is not an array
of a suitable primitive typepublic void fillCycle(Object destArray, Object sourceArray)
destArray is filled up with copies of
sourceArray.
destArray and sourceArray must be arrays of the
same class (but can be different lengths of course).destArray - array to be filled with itemssourceArray - array containing source itemspublic static boolean isHeadless()
GraphicsEnvironment.isHeadless() method in that
this one tries to do some graphics and if it catches a throwable
as a consequence it will return true. The only time that
the GraphicsEnvironment call returns true in practice
is if you start java with the property
java.awt.headless=true.true if graphics type stuff will failCopyright © 2025 Central Laboratory of the Research Councils. All Rights Reserved.