Class TestCase
- All Implemented Interfaces:
junit.framework.Test
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.
- Author:
- Mark Taylor (Starlink)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final short
static final short
static final short
static final short
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
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
(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
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
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 XMLSource
objects represent the same XML Infoset.void
assertValidXML
(String message, InputSource isrc) Asserts that the contents of a stream are valid XML.void
assertValidXML
(InputSource isrc) Asserts that the contents of a stream are valid XML.void
Fills a given array with a regular pattern of integer values.void
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
Tests whether or not a display, keyboard and mouse can in fact be supported in this environment.Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, setUp, tearDown, toString
Methods inherited from class junit.framework.Assert
assertEquals, 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, failSame
-
Field Details
-
IGNORE_ATTRIBUTE_PRESENCE
public static final short IGNORE_ATTRIBUTE_PRESENCE- See Also:
-
IGNORE_ATTRIBUTE_VALUE
public static final short IGNORE_ATTRIBUTE_VALUE- See Also:
-
IGNORE_WHITESPACE
public static final short IGNORE_WHITESPACE- See Also:
-
IGNORE_COMMENTS
public static final short IGNORE_COMMENTS- See Also:
-
-
Constructor Details
-
TestCase
public TestCase()No-arg constructor. -
TestCase
Constructs a test case with the given name.- Parameters:
name
- test case name
-
-
Method Details
-
assertArrayEquals
Asserts that two arrays have exactly the same contents. For the assertion to be true, 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 theObject.equals(java.lang.Object)
method. In the case of float[] and double[] arrays, elements which both have the value NaN are taken to match.- Parameters:
message
- the message to output if the assertion failsexpected
- the expected array objectactual
- the actual array object- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertArrayNotEquals
Asserts that two objects are not arrays with the same contents. This assertion has exactly the opposite sense to that ofassertArrayEquals(String,Object,Object)
.- Parameters:
message
- the message to output if the assertion failsexpected
- the expected array objectactual
- the actual array object, asserted not to match expected- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertArrayEquals
Asserts that two arrays have exactly the same contents. For the assertion to be true, 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 theObject.equals(java.lang.Object)
method. In the case of float[] and double[] arrays, elements which both have the value NaN are taken to match.- Parameters:
expected
- the expected array objectactual
- the actual array object- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertArrayNotEquals
Asserts that two objects are not arrays with the same contents. This assertion has exactly the opposite sense to that ofassertArrayEquals(Object,Object)
.- Parameters:
expected
- the expected array objectactual
- the actual array object, asserted not to match expected- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertArrayEquals
Asserts that two numeric arrays have the same contents within a given tolerance. For the assertion to be true, 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)
.- Parameters:
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 values- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertArrayNotEquals
Asserts that two objects are not numeric arrays having the same contents within a given tolerance. This assertion has exactly the opposite sense to that ofassertArrayEquals(String,Object,Object,double)
.- Parameters:
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 values- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertArrayEquals
Asserts that two numeric arrays have the same contents within a given tolerance. For the assertion to be true, 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)
.- Parameters:
expected
- the array object containing the expected valuesactual
- the array object containing the actual valuesdelta
- the tolerance acceptable between values- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertArrayNotEquals
Asserts that two objects are not numeric arrays having the same contents within a given tolerance. This assertion has exactly the opposite sense to that ofassertArrayEquals(Object,Object,double)
.- Parameters:
expected
- the array object containing the expected valuesactual
- the array object containing the actual values, asserted not to match expecteddelta
- the tolerance acceptable between values- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertEquals
Asserts that two floats are exactly equal.- Parameters:
message
- the message to output if the assertion failsexpected
- the expected valueactual
- the actual value- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertEquals
public void assertEquals(float expected, float actual) Asserts that two floats are exactly equal.- Parameters:
expected
- the expected valueactual
- the actual value- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertEquals
Asserts that two doubles are exactly equal.- Parameters:
message
- the message to output if the assertion failsexpected
- the expected valueactual
- the actual value- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertEquals
public void assertEquals(double expected, double actual) Asserts that two doubles are exactly equal.- Parameters:
expected
- the expected valueactual
- the actual value- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertDOMEquals
public void assertDOMEquals(URL url, Node actual) throws IOException, SAXException, ParserConfigurationException Asserts that a DOM is equivalent to the XML in a given URL. Equivalent toassertDOMEquals(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.- Parameters:
url
- pointing to an XML file -- the document element of this file is the expected valueactual
- the node which is being compared- Throws:
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 untrue- See Also:
-
assertDOMEquals
public void assertDOMEquals(URL url, Node actual, String context, int flags) throws IOException, SAXException, ParserConfigurationException Asserts that a DOM is equivalent to the XML in a given URL. Equivalent toassertDOMEquals(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.- Parameters:
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; ifnull
, it defaults to `string:'flags
- a set of flags controlling the comparison; seeassertDOMEquals(Node,Node,String,int)
- Throws:
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 untrue- See Also:
-
assertDOMEquals
public void assertDOMEquals(String s, Node n) throws IOException, SAXException, ParserConfigurationException Asserts that a DOM is equivalent to the DOM implied by the XML in a given string. -
assertDOMEquals
public void assertDOMEquals(String expected, Node actual, String context, int flags) throws IOException, SAXException, ParserConfigurationException Asserts that a DOM is equivalent to the DOM implied by the XML in a given string.- Parameters:
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; ifnull
, it defaults to `string:'flags
- a set of flags controlling the comparison; seeassertDOMEquals(Node,Node,String,int)
- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrueIOException
SAXException
ParserConfigurationException
- See Also:
-
assertDOMEquals
public void assertDOMEquals(InputStream s, Node actual, String context, int flags) throws IOException, SAXException, ParserConfigurationException Asserts that a DOM is equivalent to the DOM read from a given stream.- Parameters:
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 benull
flags
- a set of flags controlling the comparison; seeassertDOMEquals(Node,Node,String,int)
- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrueIOException
SAXException
ParserConfigurationException
- See Also:
-
assertDOMEquals
Asserts that two DOMs are equal.- Parameters:
expected
- the Node containing the expected DOMactual
- the Node to be tested- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue- See Also:
-
assertSourceEquals
Asserts that two XMLSource
objects represent the same XML Infoset. Differences in whitespace and in comments may optionally be ignored.- Parameters:
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 benull
flags
- a set of flags indicating which node tests to omit. Passing as zero includes all tests.- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue- See Also:
-
assertDOMEquals
Asserts that two DOMs are equal.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 filetest.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
- do not check whether attributes match
TestCase.IGNORE_ATTRIBUTE_VALUE
- do not check whether attributes match
- check that the same attributes are present on the corresponding elements in the tree, but do not check their values
TestCase.IGNORE_WHITESPACE
- skip whitespace-only text nodes
TestCase.IGNORE_COMMENTS
- skip comment nodes
- Parameters:
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.- Throws:
junit.framework.AssertionFailedError
- if the assertion is untrue
-
assertValidXML
Asserts that the contents of a stream are valid XML. The stream is passed through a validating XML parser. Badly-formed XML, or failure to conform to any DTD or schema referenced in the document's declaration will result in a SAXParseException.Entity resolution is done using an instance of
StarEntityResolver
.- Parameters:
message
- message associated with assertion failureisrc
- input source containing an XML document- Throws:
IOException
- if there is an error reading strmSAXException
- if the document in strm is badly-formed or invalid
-
assertValidXML
Asserts that the contents of a stream are valid XML. The stream is passed through a validating XML parser. Badly-formed XML, or failure to conform to any DTD or schema referenced in the document's declaration will result in a SAXParseException.- Parameters:
isrc
- input stream containing an XML document- Throws:
IOException
- if there is an error reading strmSAXException
- if the document in strm is badly-formed or invalid
-
fillRandom
Fills a given array with random numbers between two floating point values. If the supplied minimum and maximum values are outside the range appropriate for the primitive type in question the range will be suitably clipped.- Parameters:
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)- Throws:
IllegalArgumentException
- if array is not an array of a suitable primitive type
-
fillRandom
Fills a given array with random numbers between two integer values. If the supplied minimum and maximum values are outside the range appropriate for the primitive type in question the range will be suitably clipped.- Parameters:
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)- Throws:
IllegalArgumentException
- if array is not an array of a suitable primitive type
-
fillCycle
Fills a given array with a regular pattern of integer values. The elements of the array will take the values 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.
- Parameters:
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 maximum- Throws:
IllegalArgumentException
- if array is not an array of a suitable primitive type
-
fillCycle
Fills a given array with a pattern of values taken from another one. 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).- Parameters:
destArray
- array to be filled with itemssourceArray
- array containing source items
-
isHeadless
public static boolean isHeadless()Tests whether or not a display, keyboard and mouse can in fact be supported in this environment. This differs from theGraphicsEnvironment.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.- Returns:
- true if graphics type stuff will fail
-