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 shortFlags forassertDOMEquals(URL, Node)static final shortstatic final shortstatic final short -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidassertArrayEquals(Object expected, Object actual) Asserts that two arrays have exactly the same contents.voidassertArrayEquals(Object expected, Object actual, double delta) Asserts that two numeric arrays have the same contents within a given tolerance.voidassertArrayEquals(String message, Object expected, Object actual) Asserts that two arrays have exactly the same contents.voidassertArrayEquals(String message, Object expected, Object actual, double delta) Asserts that two numeric arrays have the same contents within a given tolerance.voidassertArrayNotEquals(Object expected, Object actual) Asserts that two objects are not arrays with the same contents.voidassertArrayNotEquals(Object expected, Object actual, double delta) Asserts that two objects are not numeric arrays having the same contents within a given tolerance.voidassertArrayNotEquals(String message, Object expected, Object actual) Asserts that two objects are not arrays with the same contents.voidassertArrayNotEquals(String message, Object expected, Object actual, double delta) Asserts that two objects are not numeric arrays having the same contents within a given tolerance.voidassertDOMEquals(InputStream s, Node actual, String context, int flags) Asserts that a DOM is equivalent to the DOM read from a given stream.voidassertDOMEquals(String s, Node n) Asserts that a DOM is equivalent to the DOM implied by the XML in a given string.voidassertDOMEquals(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.voidassertDOMEquals(URL url, Node actual) Asserts that a DOM is equivalent to the XML in a given URL.voidassertDOMEquals(URL url, Node actual, String context, int flags) Asserts that a DOM is equivalent to the XML in a given URL.voidassertDOMEquals(Node expected, Node actual) Asserts that two DOMs are equal.voidassertDOMEquals(Node expected, Node actual, String context, int flags) Asserts that two DOMs are equal.voidassertEquals(double expected, double actual) Asserts that twodoubles are exactly equal.voidassertEquals(float expected, float actual) Asserts that twofloats are exactly equal.voidassertEquals(String message, double expected, double actual) Asserts that twodoubles are exactly equal.voidassertEquals(String message, float expected, float actual) Asserts that twofloats are exactly equal.voidassertSourceEquals(Source expected, Source actual, String context, int flags) Asserts that two XMLSourceobjects represent the same XML Infoset.voidassertValidXML(String message, InputSource isrc) Asserts that the contents of a stream are valid XML.voidassertValidXML(InputSource isrc) Asserts that the contents of a stream are valid XML.voidFills a given array with a regular pattern of integer values.voidFills a given array with a pattern of values taken from another one.voidfillRandom(Object array, double min, double max) Fills a given array with random numbers between two floating point values.voidfillRandom(Object array, int min, int max) Fills a given array with random numbers between two integer values.static booleanTests 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, toStringMethods 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
-
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,expectedandactualmust both be array objects whose component types are of the same class; they must have the same number of elements; and the ith element ofexpectedmust be `equal' to the ith element ofactual. `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(Object)method. In the case offloat[]anddouble[]arrays, elements which both have the valueNaNare 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 matchexpected- 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,expectedandactualmust both be array objects whose component types are of the same class; they must have the same number of elements; and the ith element ofexpectedmust be `equal' to the ith element ofactual. `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(Object)method. In the case offloat[]anddouble[]arrays, elements which both have the valueNaNare 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 matchexpected- 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,expectedandactualmust 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 ofexpectedmust be `equal' to the ith element ofactual. Equality in this sense normally means(abs(expected[i]-actual[i]<delta), but in the case offloat[]anddouble[]arrays, elements which both have the valueNaNare taken to match, and if the expected element is infinite, thendeltais ignored.If
delta<1and the arrays are of integer type, this method does the same asassertArrayEquals(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 matchexpecteddelta- 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,expectedandactualmust 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 ofexpectedmust be `equal' to the ith element ofactual. Equality in this sense normally means(abs(expected[i]-actual[i]<delta), but in the case offloat[]anddouble[]arrays, elements which both have the valueNaNare taken to match, and if the expected element is infinite, thendeltais ignored.If
delta<1and the arrays are of integer type, this method does the same asassertArrayEquals(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 matchexpecteddelta- the tolerance acceptable between values- Throws:
junit.framework.AssertionFailedError- if the assertion is untrue
-
assertEquals
Asserts that twofloats 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 twofloats are exactly equal.- Parameters:
expected- the expected valueactual- the actual value- Throws:
junit.framework.AssertionFailedError- if the assertion is untrue
-
assertEquals
Asserts that twodoubles 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 twodoubles 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.- Throws:
IOExceptionSAXExceptionParserConfigurationException- See Also:
-
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 untrueIOExceptionSAXExceptionParserConfigurationException- 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 benullflags- a set of flags controlling the comparison; seeassertDOMEquals(Node,Node,String,int)- Throws:
junit.framework.AssertionFailedError- if the assertion is untrueIOExceptionSAXExceptionParserConfigurationException- See Also:
-
assertDOMEquals
-
assertSourceEquals
Asserts that two XMLSourceobjects 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 matchexpectedcontext- a string indicating the context of this; may benullflags- 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.xmlIf the
flagsargument 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 readingstrmSAXException- if the document instrmis 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 readingstrmSAXException- if the document instrmis 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- ifarrayis 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- ifarrayis 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 valuesmin, min+1, min+2 .. max-1, min, min+1, min+2..and so on. If themax<minthen the values will start atminand keep increasing.The results might not be as expected if you use a
minandmaxvalues 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 thanminan indication that there is no maximum- Throws:
IllegalArgumentException- ifarrayis not an array of a suitable primitive type
-
fillCycle
Fills a given array with a pattern of values taken from another one.destArrayis filled up with copies ofsourceArray.destArrayandsourceArraymust 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 theGraphicsEnvironmentcall returns true in practice is if you start java with the propertyjava.awt.headless=true.- Returns:
trueif graphics type stuff will fail
-