Class DiffTestCase


public abstract class DiffTestCase extends Object
DiffTestCase is an abstract base for JUnit tests which produce multi-line output to be verified by diffing against a pre-existing reference file.
  • Field Details

    • logFile

      protected File logFile
      Name of current .log file.
    • refFile

      protected File refFile
      Name of current .ref file.
    • logOutputStream

      protected OutputStream logOutputStream
      OutputStream for current test log.
  • Constructor Details

    • DiffTestCase

      protected DiffTestCase(String testCaseName) throws Exception
      Initializes a new DiffTestCase.
      testCaseName - Test case name
  • Method Details

    • setUp

      @BeforeEach protected void setUp()
    • tearDown

      @AfterEach protected void tearDown() throws IOException
    • openTestLog

      protected Writer openTestLog() throws Exception
      Initializes a diff-based test. Any existing .log and .dif files corresponding to this test case are deleted, and a new, empty .log file is created. The default log file location is a subdirectory under the result getTestlogRoot(), where the subdirectory name is based on the unqualified name of the test class. The generated log file name will be testMethodName.log, and the expected reference file will be testMethodName.ref.
      Writer for log file, which caller should use as a destination for test output to be diffed
    • getTestlogRoot

      protected abstract File getTestlogRoot() throws Exception
      Returns the root directory under which testlogs should be written.
    • openTestLogOutputStream

      protected OutputStream openTestLogOutputStream(File testFileSansExt) throws IOException
      Initializes a diff-based test, overriding the default log file naming scheme altogether.
      testFileSansExt - full path to log filename, without .log/.ref extension
    • diffTestLog

      protected void diffTestLog() throws IOException
      Finishes a diff-based test. Output that was written to the Writer returned by openTestLog is diffed against a .ref file, and if any differences are detected, the test case fails. Note that the diff used is just a boolean test, and does not create any .dif ouput.

      NOTE: if you wrap the Writer returned by openTestLog() (e.g. with a PrintWriter), be sure to flush the wrapping Writer before calling this method.

      See Also:
    • diffFile

      protected void diffFile(File logFile, File refFile) throws IOException
      Compares a log file with its reference log.

      Usually, the log file and the reference log are in the same directory, one ending with '.log' and the other with '.ref'.

      If the files are identical, removes logFile.

      logFile - Log file
      refFile - Reference log
    • addDiffMask

      protected void addDiffMask(String mask)
      Adds a diff mask. Strings matching the given regular expression will be masked before diffing. This can be used to suppress spurious diffs on a case-by-case basis.
      mask - a regular expression, as per String.replaceAll
    • addIgnorePattern

      protected void addIgnorePattern(String javaPattern)
    • diff

      public static String diff(File file1, File file2)
      Returns a string containing the difference between the contents of two files. The string has a similar format to the UNIX 'diff' utility.
    • diffLines

      public static String diffLines(List<String> lines1, List<String> lines2)
      Returns a string containing the difference between the two sets of lines.
    • fileContents

      protected static String fileContents(File file)
      Returns the contents of a file as a string.
      file - File
      Contents of the file
    • setVerbose

      protected void setVerbose(boolean verbose)
      Sets whether to give verbose message if diff fails.
    • setRefFileDiffMasks

      protected void setRefFileDiffMasks()
      Sets the diff masks that are common to .REF files