org.eigenbase.test
Class DiffTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.eigenbase.test.DiffTestCase
All Implemented Interfaces:
Test
Direct Known Subclasses:
DiffSanityTest, ResultSetTestCase

public abstract class DiffTestCase
extends TestCase

DiffTestCase is an abstract base for JUnit tests which produce multi-line output to be verified by diffing against a pre-existing reference file.

Version:
$Id: //open/dev/farrago/src/org/eigenbase/test/DiffTestCase.java#25 $
Author:
John V. Sichi

Field Summary
(package private)  Matcher compiledDiffMatcher
           
(package private)  Matcher compiledIgnoreMatcher
           
private  String diffMasks
          Diff masks defined so far
(package private)  int gcInterval
           
private  String ignorePatterns
           
protected  File logFile
          Name of current .log file.
protected  OutputStream logOutputStream
          OutputStream for current test log.
protected  File refFile
          Name of current .ref file.
private  boolean verbose
          Whether to give verbose message if diff fails.
 
Constructor Summary
protected DiffTestCase(String testCaseName)
          Initializes a new DiffTestCase.
 
Method Summary
protected  void addDiffMask(String mask)
          Adds a diff mask.
protected  void addIgnorePattern(String javaPattern)
           
private  String applyDiffMask(String s)
           
private static String diff(File file1, File file2)
          Returns a string containing the difference between the contents of two files.
private  void diffFail(File logFile, int lineNumber)
           
protected  void diffFile(File logFile, File refFile)
          Compares a log file with its reference log.
static String diffLines(List<String> lines1, List<String> lines2)
          Returns a string containing the difference between the two sets of lines.
protected  void diffTestLog()
          Finishes a diff-based test.
protected static String fileContents(File file)
          Returns the contents of a file as a string.
private static List<String> fileLines(File file)
          Returns a list of the lines in a given file.
protected abstract  File getTestlogRoot()
           
private static boolean inIde()
          Returns whether this test is running inside the IntelliJ IDE.
private  boolean matchIgnorePatterns(String s)
           
protected  Writer openTestLog()
          Initializes a diff-based test.
protected  OutputStream openTestLogOutputStream(File testFileSansExt)
          Initializes a diff-based test, overriding the default log file naming scheme altogether.
protected  void setGC(int n)
          set the number of lines for garbage collection.
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 junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, 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, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

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.


diffMasks

private String diffMasks
Diff masks defined so far


compiledDiffMatcher

Matcher compiledDiffMatcher

ignorePatterns

private String ignorePatterns

compiledIgnoreMatcher

Matcher compiledIgnoreMatcher

gcInterval

int gcInterval

verbose

private boolean verbose
Whether to give verbose message if diff fails.

Constructor Detail

DiffTestCase

protected DiffTestCase(String testCaseName)
                throws Exception
Initializes a new DiffTestCase.

Parameters:
testCaseName - JUnit test case name
Throws:
Exception
Method Detail

setUp

protected void setUp()
              throws Exception
Overrides:
setUp in class TestCase
Throws:
Exception

tearDown

protected void tearDown()
                 throws Exception
Overrides:
tearDown in class TestCase
Throws:
Exception

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.

Returns:
Writer for log file, which caller should use as a destination for test output to be diffed
Throws:
Exception

getTestlogRoot

protected abstract File getTestlogRoot()
                                throws Exception
Returns:
the root under which testlogs should be written
Throws:
Exception

openTestLogOutputStream

protected OutputStream openTestLogOutputStream(File testFileSansExt)
                                        throws 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:
IOException

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.

Throws:
IOException
See Also:
diffFile(File, File)

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.

Parameters:
logFile - Log file
refFile - Reference log
Throws:
IOException

setGC

protected void setGC(int n)
set the number of lines for garbage collection.

Parameters:
n - an integer, the number of line for garbage collection, 0 means no garbage collection.

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.

Parameters:
mask - a regular expression, as per String.replaceAll

addIgnorePattern

protected void addIgnorePattern(String javaPattern)

applyDiffMask

private String applyDiffMask(String s)

matchIgnorePatterns

private boolean matchIgnorePatterns(String s)

diffFail

private void diffFail(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

private 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.


fileLines

private static List<String> fileLines(File file)
Returns a list of the lines in a given file.

Parameters:
file - File
Returns:
List of lines

fileContents

protected static String fileContents(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