Class DiffTestCase


  • public abstract class DiffTestCase
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      (package private) java.util.regex.Matcher compiledDiffMatcher  
      (package private) java.util.regex.Matcher compiledIgnoreMatcher  
      private java.lang.String diffMasks
      Diff masks defined so far
      private java.lang.String ignorePatterns  
      protected java.io.File logFile
      Name of current .log file.
      protected java.io.OutputStream logOutputStream
      OutputStream for current test log.
      protected java.io.File refFile
      Name of current .ref file.
      private java.lang.String testCaseName  
      private boolean verbose
      Whether to give verbose message if diff fails.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected DiffTestCase​(java.lang.String testCaseName)
      Initializes a new DiffTestCase.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addDiffMask​(java.lang.String mask)
      Adds a diff mask.
      protected void addIgnorePattern​(java.lang.String javaPattern)  
      private java.lang.String applyDiffMask​(java.lang.String s)  
      static java.lang.String diff​(java.io.File file1, java.io.File file2)
      Returns a string containing the difference between the contents of two files.
      private void diffFail​(java.io.File logFile, int lineNumber)  
      protected void diffFile​(java.io.File logFile, java.io.File refFile)
      Compares a log file with its reference log.
      static java.lang.String diffLines​(java.util.List<java.lang.String> lines1, java.util.List<java.lang.String> lines2)
      Returns a string containing the difference between the two sets of lines.
      protected void diffTestLog()
      Finishes a diff-based test.
      protected static java.lang.String fileContents​(java.io.File file)
      Returns the contents of a file as a string.
      private static java.util.List<java.lang.String> fileLines​(java.io.File file)
      Returns a list of the lines in a given file.
      protected abstract java.io.File getTestlogRoot()  
      private static boolean inIde()
      Returns whether this test is running inside the IntelliJ IDE.
      private boolean matchIgnorePatterns​(java.lang.String s)  
      protected java.io.Writer openTestLog()
      Initializes a diff-based test.
      protected java.io.OutputStream openTestLogOutputStream​(java.io.File testFileSansExt)
      Initializes a diff-based test, overriding the default log file naming scheme altogether.
      protected void setRefFileDiffMasks()
      Sets the diff masks that are common to .REF files
      protected void setUp()  
      protected void setVerbose​(boolean verbose)
      Sets whether to give verbose message if diff fails.
      protected void tearDown()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • testCaseName

        private final java.lang.String testCaseName
      • logFile

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

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

        protected java.io.OutputStream logOutputStream
        OutputStream for current test log.
      • diffMasks

        private java.lang.String diffMasks
        Diff masks defined so far
      • compiledDiffMatcher

        java.util.regex.Matcher compiledDiffMatcher
      • ignorePatterns

        private java.lang.String ignorePatterns
      • compiledIgnoreMatcher

        java.util.regex.Matcher compiledIgnoreMatcher
      • verbose

        private boolean verbose
        Whether to give verbose message if diff fails.
    • Constructor Detail

      • DiffTestCase

        protected DiffTestCase​(java.lang.String testCaseName)
                        throws java.lang.Exception
        Initializes a new DiffTestCase.
        Parameters:
        testCaseName - Test case name
        Throws:
        java.lang.Exception
    • Method Detail

      • setUp

        protected void setUp()
      • tearDown

        protected void tearDown()
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • openTestLog

        protected java.io.Writer openTestLog()
                                      throws java.lang.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.
        Returns:
        Writer for log file, which caller should use as a destination for test output to be diffed
        Throws:
        java.lang.Exception
      • getTestlogRoot

        protected abstract java.io.File getTestlogRoot()
                                                throws java.lang.Exception
        Returns:
        the root under which testlogs should be written
        Throws:
        java.lang.Exception
      • openTestLogOutputStream

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

        protected void diffTestLog()
                            throws java.io.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.

        Throws:
        java.io.IOException
        See Also:
        diffFile(File, File)
      • diffFile

        protected void diffFile​(java.io.File logFile,
                                java.io.File refFile)
                         throws java.io.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.

        Parameters:
        logFile - Log file
        refFile - Reference log
        Throws:
        java.io.IOException
      • addDiffMask

        protected void addDiffMask​(java.lang.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.
        Parameters:
        mask - a regular expression, as per String.replaceAll
      • addIgnorePattern

        protected void addIgnorePattern​(java.lang.String javaPattern)
      • applyDiffMask

        private java.lang.String applyDiffMask​(java.lang.String s)
      • matchIgnorePatterns

        private boolean matchIgnorePatterns​(java.lang.String s)
      • diffFail

        private void diffFail​(java.io.File logFile,
                              int lineNumber)
      • inIde

        private static boolean inIde()
        Returns whether this test is running inside the IntelliJ IDE.
        Returns:
        whether we're running in IntelliJ.
      • diff

        public static java.lang.String diff​(java.io.File file1,
                                            java.io.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 java.lang.String diffLines​(java.util.List<java.lang.String> lines1,
                                                 java.util.List<java.lang.String> lines2)
        Returns a string containing the difference between the two sets of lines.
      • fileLines

        private static java.util.List<java.lang.String> fileLines​(java.io.File file)
        Returns a list of the lines in a given file.
        Parameters:
        file - File
        Returns:
        List of lines
      • fileContents

        protected static java.lang.String fileContents​(java.io.File file)
        Returns the contents of a file as a string.
        Parameters:
        file - File
        Returns:
        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