Class FileComparisonUtils

java.lang.Object
it.tidalwave.util.test.FileComparisonUtils

public class FileComparisonUtils extends Object
A utility class to compare two text files, or two lists of strings, and assert that they have the same contents. The comparison is performed line by line, by means of java-diff-utils.

When the contents differ, the assertSameContents() methods throw an AssertionError, while checkSameContents(Path, Path) returns false. In both cases the differences are logged at the ERROR level, with each line prefixed by TEST FAILED, so they can be easily found in the test log, for instance with grep. Line numbers start from 1, and lines are enclosed between asterisks, so leading and trailing spaces are visible:

  • exp[n] *...* followed by act[m] *...*: line n of the expected contents has been replaced by line m of the actual contents;
  • -act[n] *...*: line n of the expected contents is missing from the actual contents;
  • +act[n] *...*: line n of the actual contents is not present in the expected contents.

When files are compared, their paths are logged at the INFO level, relative to their common directory.

The output can be customised by means of the following system properties:

  • it.tidalwave.util.test.FileComparisonUtils.tabularOutput (see P_TABULAR_OUTPUT): if true, the differences are also logged as a table, with the expected lines on the left and the actual lines on the right (lines are trimmed). The default is false;
  • it.tidalwave.util.test.FileComparisonUtils.tabularLimit (see P_TABULAR_LIMIT): the maximum number of rows of the table. The default is 500.

The properties are read when this class is loaded, so they must be set before: for instance, on the command line (-D...), or in the configuration of the test runner (such as the systemPropertyVariables of the Maven Surefire plugin). When the tabular output is disabled, a reminder about these properties is logged after the differences.

Author:
Fabrizio Giudici
  • Field Details

    • P_TABULAR_OUTPUT

      public static final String P_TABULAR_OUTPUT
    • P_TABULAR_LIMIT

      public static final String P_TABULAR_LIMIT
  • Constructor Details

    • FileComparisonUtils

      public FileComparisonUtils()
  • Method Details

    • assertSameContents

      public static void assertSameContents(@Nonnull File expectedFile, @Nonnull File actualFile) throws IOException
      Asserts that two files have the same contents.
      Parameters:
      expectedFile - the file with the expected contents
      actualFile - the file with the contents to probe
      Throws:
      IOException - in case of error
    • assertSameContents

      public static void assertSameContents(@Nonnull Path expectedPath, @Nonnull Path actualPath) throws IOException
      Asserts that two files have the same contents.
      Parameters:
      expectedPath - the file with the expected contents
      actualPath - the file with the contents to probe
      Throws:
      IOException - in case of error
    • assertSameContents

      public static void assertSameContents(@Nonnull List<String> expected, @Nonnull List<String> actual)
      Asserts that two collections of strings have the same contents.
      Parameters:
      expected - the expected values
      actual - the actual values
    • checkSameContents

      public static boolean checkSameContents(@Nonnull Path expectedPath, @Nonnull Path actualPath) throws IOException
      Checks whether two files have the same contents.
      Parameters:
      expectedPath - the file with the expected contents
      actualPath - the file with the contents to probe
      Returns:
      whether the two files have the same contents
      Throws:
      IOException - in case of error
      Since:
      1.2-ALPHA-15
    • stringToStrings

      @Nonnull public static List<String> stringToStrings(@Nonnull String string) throws IOException
      Converts a string which contains newlines into a list of strings.
      Parameters:
      string - the source
      Returns:
      the strings
      Throws:
      IOException - in case of error
    • fileToStrings

      @Nonnull public static List<String> fileToStrings(@Nonnull Path file) throws IOException
      Reads a file into a list of strings.
      Parameters:
      file - the file
      Returns:
      the strings
      Throws:
      IOException - in case of error
    • resourceToStrings

      @Nonnull public static List<String> resourceToStrings(@Nonnull String path) throws IOException
      Reads a classpath resource (not a regular file) into a list of strings.
      Parameters:
      path - the path of the classpath resource
      Returns:
      the strings
      Throws:
      IOException - in case of error
    • resourceToStrings

      @Nonnull public static List<String> resourceToStrings(@Nonnull InputStream is) throws IOException
      Reads an input stream into a list of strings. The stream is closed at the end.
      Parameters:
      is - the input stream
      Returns:
      the strings
      Throws:
      IOException - in case of error
    • commonPrefix

      @Nonnull public static String commonPrefix(@Nonnull String s1, @Nonnull String s2)
      Given a string that represents a path whose segments are separated by the standard separator of the platform, returns the common prefix - which means the common directory parents.
      Parameters:
      s1 - the former string
      s2 - the latter string
      Returns:
      the common prefix