Class TestResult

java.lang.Object
com.sun.javatest.TestResult

public class TestResult extends Object
The TestResult object encapsulates the results from a test. Test results are formatted in sections of command output, comments and sometimes "streams" of output (stdout for example). Each of these sections is represented by a (@link TestResult.Section Section). Instances of this class are mutable until the result of the section is set or until the result of the test itself is set.

Test results are stored in a structured text files. The TestResult class serves as the API for accessing the various components that make up the result of running a test. The status is cached as its size is small and it is accessed often.

This class and inner classes will throw IllegalStateExceptions if an attempt is made to modify the any part of the object that has been marked immutable.

  • Field Details

    • NO_CHECKSUM

      public static final int NO_CHECKSUM
      A code indicating that no checksum was found in a .jtr file.
      See Also:
    • BAD_CHECKSUM

      public static final int BAD_CHECKSUM
      A code indicating that an invalid checksum was found in a .jtr file.
      See Also:
    • GOOD_CHECKSUM

      public static final int GOOD_CHECKSUM
      A code indicating that a good checksum was found in a .jtr file.
      See Also:
    • NUM_CHECKSUM_STATES

      public static final int NUM_CHECKSUM_STATES
      The number of different checksum states (none, good, bad).
      See Also:
    • MESSAGE_OUTPUT_NAME

      public static final String MESSAGE_OUTPUT_NAME
      The name of the default output that all Sections are equipped with.
      See Also:
    • MSG_SECTION_NAME

      public static final String MSG_SECTION_NAME
      The name of the default section that all TestResult objects are equipped with.
      See Also:
    • DESCRIPTION

      public static final String DESCRIPTION
      The name of the property that defines the test description file.
      See Also:
    • END

      public static final String END
      The name of the property that defines the time at which the test execution finished.
      See Also:
    • ENVIRONMENT

      public static final String ENVIRONMENT
      The name of the property that defines the environment name.
      See Also:
    • EXEC_STATUS

      public static final String EXEC_STATUS
      The name of the property that defines the test execution status.
      See Also:
    • JAVATEST_OS

      public static final String JAVATEST_OS
      The name of the property that defines the OS on which JT Harness was running when the test was run.
      See Also:
    • SCRIPT

      public static final String SCRIPT
      The name of the property that defines the script that ran the test.
      See Also:
    • SECTIONS

      public static final String SECTIONS
      The name of the property that defines the test output sections that were recorded when the test ran.
      See Also:
    • START

      public static final String START
      The name of the property that defines the time at which the test execution started.
      See Also:
    • TEST

      public static final String TEST
      The name of the property that defines the test for which this is the result object.
      See Also:
    • VERSION

      public static final String VERSION
      The name of the property that defines which version of JT Harness was used to run the test.
      See Also:
    • WORK

      public static final String WORK
      The name of the property that defines the work directory for the test.
      See Also:
    • VARIETY

      public static final String VARIETY
      The name of the property that defines the variety of harness in use. Generally the full harness or the lite version.
      See Also:
    • LOADER

      public static final String LOADER
      The name of the property that defines the type of class loader used when running the harness (classpath mode or module mode generally).
      See Also:
    • dateFormat

      public static final DateFormat dateFormat
      DateFormat, that is used to store date into TestResult
  • Constructor Details

    • TestResult

      public TestResult(TestDescription td)
      Construct a test result object that will be built as the test runs. The status string will be "running..." rather than "not run".
      Parameters:
      td - The test description to base this new object on. Cannot be null.
    • TestResult

      public TestResult(TestDescription td, WorkDirectory workDir) throws TestResult.Fault
      Reconstruct the results of a previously run test.
      Parameters:
      td - Description of the test that was run
      workDir - Work directory in which the tests were run
      Throws:
      TestResult.Fault - if there is a problem recreating the results from the appropriate file in the work directory
    • TestResult

      Reconstruct the results of a previously run test.
      Parameters:
      file - File that the results have been stored into.
      Throws:
      TestResult.ReloadFault - if there is a problem recreating the results from the given file
      TestResult.ResultFileNotFoundFault - if there is a problem locating the given file
    • TestResult

      public TestResult(WorkDirectory workDir, String workRelativePath) throws TestResult.Fault
      Reconstruct the results of a previously run test.
      Parameters:
      workDir - The work directory where previous results for the guven test can be found.
      workRelativePath - The path to the JTR to reload, relative to the workdir.
      Throws:
      TestResult.Fault - if there is a problem recreating the results from the given file
    • TestResult

      public TestResult(TestDescription td, Status s)
      Create a temporary test result for which can be handed around in situations where a reasonable test result can't be created.
      Parameters:
      td - Description of the test
      s - Status to associate with running the test... presumed to be of the Status.FAILED type.
  • Method Details

    • notRun

      public static TestResult notRun(TestDescription td)
      Create a placeholder TestResult for a test that has not yet been run.
      Parameters:
      td - The test description for the test
      Returns:
      A test result that indicates that the test has not yet been run
    • getWorkRelativePath

      public static String getWorkRelativePath(TestDescription td)
      Get the path name for the results file for a test, relative to the work directory. The internal separator is '/'.
      Parameters:
      td - the test description for the test in question
      Returns:
      the path name for the results file for a test, relative to the work directory
    • getWorkRelativePath

      public static String getWorkRelativePath(String testURL)
      Get the path name for the results file for a test, relative to the work directory. The internal separator is '/'.
      Parameters:
      testURL - May not be null;
      Returns:
      The work relative path of the JTR for this test. Null if the given URL is null.
    • getWorkRelativePath

      public static String getWorkRelativePath(String baseURL, String testId)
      Get the path name for the results file for a test, relative to the work directory. The internal separator is '/'.
      Parameters:
      baseURL - May not be null;
      testId - The test identifier that goes with the URL. This may be null.
      Returns:
      The work relative path of the JTR for this test. Null if the given URL is null.
    • isResultFile

      public static boolean isResultFile(File f)
      Check if this file is or appears to be a result (.jtr) file, according to its filename extension.
      Parameters:
      f - the file to be checked
      Returns:
      true if this file is or appears to be a result (.jtr) file.
    • parseDate

      public static Date parseDate(String s) throws ParseException
      Parse the date format used for timestamps, such as the start/stop timestamp.
      Parameters:
      s - The string containing the date to be restored.
      Throws:
      ParseException
      See Also:
    • formatDate

      public static String formatDate(Date d)
      Format the date format used for timestamps, such as the start/stop timestamp.
      Parameters:
      d - The date object to be formatted into a string.
      See Also:
    • createSection

      public TestResult.Section createSection(String name)
      Create a new section inside this test result.
      Parameters:
      name - The symbolic name for this new section.
      Returns:
      The new section that was created.
    • putProperty

      public void putProperty(String name, String value)
      Add a new property value to this TestResult.
      Parameters:
      name - The name of the property to be updated.
      value - The new value of the specified property.
    • setMaxOutputSize

      public void setMaxOutputSize(int size)
      Sets the maximum output size for the current TestResult. The value will be used instead of the value specified by the system property javatest.maxOutputSize.
      Parameters:
      size - the maximum number of characters.
    • reloadFromWorkDir

      public void reloadFromWorkDir(WorkDirectory workDir) throws TestResult.Fault
      Reconstruct the results of a previously run test.
      Parameters:
      workDir - Work directory in which the tests were run
      Throws:
      TestResult.Fault - if an error occurs while reloading the results
    • getChecksumState

      public byte getChecksumState()
      Get info about the checksum in this object.
      Returns:
      a value indicating the validity or otherwise of the checksum found while reading this result object.
      See Also:
    • getTestCommentWriter

      public PrintWriter getTestCommentWriter()
      A way to write comments about the test execution into the results.
      Returns:
      If this is null, then the object is in a state in which it does not accept new messages.
    • getTestName

      public String getTestName()
      Get the test name, as given by the test URL defined by TestDescription.getRootRelativeURL(). This method always returns a useful string, representing the test name.
      Returns:
      the name of the test for which this is the result object
      See Also:
    • isReloadable

      public boolean isReloadable()
      Check whether this test result can be reloaded from a file. This method does not validate the contents of the file.
      Returns:
      true if the result file for this object can be read
    • isShrunk

      public boolean isShrunk()
      Check whether this object has been "shrunk" to reduce its memory footprint. If it has, some or all of the data will have to be reloaded. This method is somewhat orthogonal to isReloadable() and should not be used as a substitute.
      Returns:
      True if this object is currently incomplete, false otherwise.
      See Also:
    • getDescription

      public TestDescription getDescription() throws TestResult.Fault
      Get the description of the test from which this result was created. Depending on how the test result was created, this information may not be immediately available, and may be recreated from the test result file.
      Returns:
      the test description for this test result object
      Throws:
      TestResult.Fault - if there is a problem recreating the description from the results file.
    • getWorkRelativePath

      public String getWorkRelativePath()
      Get the path name for the results file for this test, relative to the work directory. The internal separator is '/'.
      Returns:
      the path name for the results file for this test, relative to the work directory
    • getFile

      public File getFile()
      Get the name, if any, for the result file for this object. The path information contains platform specific path separators.
      Returns:
      the name, if any, for the result file for this object
    • resetFile

      public void resetFile()
    • getPropertyNames

      public Enumeration<String> getPropertyNames()
      Get the keys of the properties that this object has stored.
      Returns:
      the keys of the properties that this object has stored
    • getProperty

      public String getProperty(String name) throws TestResult.Fault
      Get the value of a property of this test result.
      Parameters:
      name - The name of the property to be retrieved.
      Returns:
      The value corresponding to the property name, null if not found.
      Throws:
      TestResult.Fault - if there is a problem recreating data from the results file.
    • getEnvironment

      public Map<String,String> getEnvironment() throws TestResult.Fault
      Get a copy of the environment that this object has stored.
      Returns:
      a copy of the environment that this object has stored
      Throws:
      TestResult.Fault - if there is a problem recreating data from the results file.
      See Also:
    • setEnvironment

      public void setEnvironment(TestEnvironment environment)
      Set the environment used by this test. When the test is run, those entries in the environment that are referenced are noted; those entries will be recorded here in the test result object.
      Parameters:
      environment - the test environment used by this test.
      See Also:
    • getParent

      public TestResultTable.TreeNode getParent()
      Get the parent node in the test result table that contains this test result object.
      Returns:
      the parent node in the test result table that contains this test result object.
    • isMutable

      public boolean isMutable()
      Determine if the test result object is still mutable. Test results are only mutable while they are being created, up to the point that the final status is set.
      Returns:
      true if the test result object is still mutable, and false otherwise
    • getStatus

      public Status getStatus()
      Get the status for this test.
      Returns:
      the status for this test
      See Also:
    • setStatus

      public void setStatus(Status stat)
      Set the result of this test. This action makes this object immutable. If a result comparison is needed, it will be done in here.
      Parameters:
      stat - A status object representing the outcome of the test
      See Also:
    • getSectionCount

      public int getSectionCount()
      Find out how many sections this test result contains.
      Returns:
      The number of sections in this result.
    • getSection

      public TestResult.Section getSection(int index) throws TestResult.ReloadFault
      Get the section specified by index. Remember that index 0 is the default message section.
      Parameters:
      index - The index of the section to be retrieved.
      Returns:
      The requested section. Will be null if the section does not exist.
      Throws:
      TestResult.ReloadFault - Will occur if an error is encountered when reloading JTR data. This may be the result of a corrupt or missing JTR file.
      See Also:
    • getSectionTitles

      public String[] getSectionTitles()
      Get the titles of all sections in this test result. A null result probably indicates that there are no sections. This is improbable since most test result object automatically have one section.
      Returns:
      The titles, one at a time in the array. Null if the titles do not exist or could not be determined.
    • writeResults

      public void writeResults(WorkDirectory workDir, BackupPolicy backupPolicy) throws IOException
      Writes the TestResult into a version 2 jtr file.
      Parameters:
      workDir - The work directory in which to write the results
      backupPolicy - a policy object defining what to do if a file already exists with the same name as that which is about to be written.
      Throws:
      IllegalStateException - This will occur if you attempt to write a result which is still mutable.
      IOException - Occurs when the output file cannot be created or written to. Under this condition, this object will change it status to an error.
    • addObserver

      public void addObserver(TestResult.Observer obs)
      Add an observer to watch this test result for changes.
      Parameters:
      obs - the observer to be added
    • removeObserver

      public void removeObserver(TestResult.Observer obs)
      Remove an observer that was previously added.
      Parameters:
      obs - the observer to be removed
    • getEndTime

      public long getEndTime()
      Gets the time when the test was completed, or at least the time when it's final status was set. Be aware that if the information is not available in memory, it will be loaded from disk.
      Returns:
      Time when this test acquired its final status setting.
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object