Class TestCase

java.lang.Object
junit.framework.Assert
junit.framework.TestCase
uk.ac.starlink.util.TestCase
All Implemented Interfaces:
junit.framework.Test

public class TestCase extends junit.framework.TestCase
This class extends 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

    Fields
    Modifier and Type
    Field
    Description
    static final short
    static final short
     
    static final short
     
    static final short
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    No-arg constructor.
    Constructs a test case with the given name.
  • Method Summary

    Modifier and Type
    Method
    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
    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 XML Source objects represent the same XML Infoset.
    void
    Asserts that the contents of a stream are valid XML.
    void
    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
    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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • TestCase

      public TestCase()
      No-arg constructor.
    • TestCase

      public TestCase(String name)
      Constructs a test case with the given name.
      Parameters:
      name - test case name
  • Method Details

    • assertArrayEquals

      public void assertArrayEquals(String message, Object expected, Object actual)
      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 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.
      Parameters:
      message - the message to output if the assertion fails
      expected - the expected array object
      actual - the actual array object
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertArrayNotEquals

      public void assertArrayNotEquals(String message, Object expected, Object actual)
      Asserts that two objects are not arrays with the same contents. This assertion has exactly the opposite sense to that of assertArrayEquals(String,Object,Object).
      Parameters:
      message - the message to output if the assertion fails
      expected - the expected array object
      actual - the actual array object, asserted not to match expected
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertArrayEquals

      public void assertArrayEquals(Object expected, Object actual)
      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 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.
      Parameters:
      expected - the expected array object
      actual - the actual array object
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertArrayNotEquals

      public void assertArrayNotEquals(Object expected, Object actual)
      Asserts that two objects are not arrays with the same contents. This assertion has exactly the opposite sense to that of assertArrayEquals(Object,Object).
      Parameters:
      expected - the expected array object
      actual - the actual array object, asserted not to match expected
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertArrayEquals

      public void assertArrayEquals(String message, Object expected, Object actual, double delta)
      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 fails
      expected - the array object containing the expected values
      actual - the array object containing the actual values
      delta - the tolerance acceptable between values
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertArrayNotEquals

      public 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. This assertion has exactly the opposite sense to that of assertArrayEquals(String,Object,Object,double).
      Parameters:
      message - the message to output if the assertion fails
      expected - the array object containing the expected values
      actual - the array object containing the actual values, asserted not to match expected
      delta - the tolerance acceptable between values
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertArrayEquals

      public void assertArrayEquals(Object expected, Object actual, double delta)
      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 values
      actual - the array object containing the actual values
      delta - the tolerance acceptable between values
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertArrayNotEquals

      public void assertArrayNotEquals(Object expected, Object actual, double delta)
      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 of assertArrayEquals(Object,Object,double).
      Parameters:
      expected - the array object containing the expected values
      actual - the array object containing the actual values, asserted not to match expected
      delta - the tolerance acceptable between values
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertEquals

      public void assertEquals(String message, float expected, float actual)
      Asserts that two floats are exactly equal.
      Parameters:
      message - the message to output if the assertion fails
      expected - the expected value
      actual - 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 value
      actual - the actual value
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
    • assertEquals

      public void assertEquals(String message, double expected, double actual)
      Asserts that two doubles are exactly equal.
      Parameters:
      message - the message to output if the assertion fails
      expected - the expected value
      actual - 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 value
      actual - 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 to 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.
      Parameters:
      url - pointing to an XML file -- the document element of this file is the expected value
      actual - the node which is being compared
      Throws:
      IOException - if the file cannot be found
      SAXException - if there is a problem parsing the XML
      ParserConfigurationException - if the XML parser cannot be initialised
      junit.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 to 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.
      Parameters:
      url - pointing to an XML file -- the document element of this file is the expected value
      actual - the node which is being compared
      context - 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)
      Throws:
      IOException - if the file cannot be found
      SAXException - if there is a problem parsing the XML
      ParserConfigurationException - if the XML parser cannot be initialised
      junit.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:
      IOException
      SAXException
      ParserConfigurationException
      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 value
      actual - the node which is compared
      context - 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)
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
      IOException
      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 value
      actual - the node which is compared
      context - a string indicating the context of this; may be null
      flags - a set of flags controlling the comparison; see assertDOMEquals(Node,Node,String,int)
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
      IOException
      SAXException
      ParserConfigurationException
      See Also:
    • assertDOMEquals

      public void assertDOMEquals(Node expected, Node actual)
      Asserts that two DOMs are equal.
      Parameters:
      expected - the Node containing the expected DOM
      actual - the Node to be tested
      Throws:
      junit.framework.AssertionFailedError - if the assertion is untrue
      See Also:
    • assertSourceEquals

      public void assertSourceEquals(Source expected, Source actual, String context, int flags)
      Asserts that two XML Source objects represent the same XML Infoset. Differences in whitespace and in comments may optionally be ignored.
      Parameters:
      expected - the Source object containing the expected infoset
      actual - the Source object containing the actual infoset, asserted to match expected
      context - a string indicating the context of this; may be null
      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

      public void assertDOMEquals(Node expected, Node actual, String context, int flags)
      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 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
      do not check whether attributes match
      TestCase.IGNORE_ATTRIBUTE_VALUE
      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 DOM
      actual - the Node to be tested
      context - a string indicating the context, which will be used in assertion failure reports. May be null
      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
    • assertValidXML

      public void assertValidXML(String message, InputSource isrc) throws IOException, SAXException
      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 failure
      isrc - input source containing an XML document
      Throws:
      IOException - if there is an error reading strm
      SAXException - if the document in strm is badly-formed or invalid
    • assertValidXML

      public void assertValidXML(InputSource isrc) throws IOException, SAXException
      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 strm
      SAXException - if the document in strm is badly-formed or invalid
    • fillRandom

      public void fillRandom(Object array, double min, double max)
      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 values
      min - 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

      public void fillRandom(Object array, int min, int max)
      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 values
      min - 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

      public void fillCycle(Object array, int min, int max)
      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 values
      min - the first value
      max - 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

      public void fillCycle(Object destArray, Object sourceArray)
      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 items
      sourceArray - 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 the 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.
      Returns:
      true if graphics type stuff will fail