Class FileComparisonUtils


  • public class FileComparisonUtils
    extends java.lang.Object
    A utility class to compare two text files and assert that they have the same contents.
    Author:
    Fabrizio Giudici
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertSameContents​(java.io.File expectedFile, java.io.File actualFile)
      Asserts that two files have the same contents.
      static void assertSameContents​(java.nio.file.Path expectedPath, java.nio.file.Path actualPath)
      Asserts that two files have the same contents.
      static void assertSameContents​(java.util.List<java.lang.String> expected, java.util.List<java.lang.String> actual)
      Asserts that two collections of strings have the same contents.
      static boolean checkSameContents​(java.nio.file.Path expectedPath, java.nio.file.Path actualPath)
      Checks whether two files have the same contents.
      static java.lang.String commonPrefix​(java.lang.String s1, java.lang.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.
      static java.util.List<java.lang.String> fileToStrings​(java.nio.file.Path file)
      Reads a file into a list of strings.
      static java.util.List<java.lang.String> resourceToStrings​(java.io.InputStream is)
      Reads an input stream into a list of strings.
      static java.util.List<java.lang.String> resourceToStrings​(java.lang.String path)
      Reads a classpath resource (not a regular file) into a list of strings.
      static java.util.List<java.lang.String> stringToStrings​(java.lang.String string)
      Converts a string which contains newlines into a list of strings.
      • Methods inherited from class java.lang.Object

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

      • P_TABULAR_OUTPUT

        public static final java.lang.String P_TABULAR_OUTPUT
      • P_TABULAR_LIMIT

        public static final java.lang.String P_TABULAR_LIMIT
    • Constructor Detail

      • FileComparisonUtils

        public FileComparisonUtils()
    • Method Detail

      • assertSameContents

        public static void assertSameContents​(@Nonnull
                                              java.io.File expectedFile,
                                              @Nonnull
                                              java.io.File actualFile)
                                       throws java.io.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:
        java.io.IOException - in case of error
      • assertSameContents

        public static void assertSameContents​(@Nonnull
                                              java.nio.file.Path expectedPath,
                                              @Nonnull
                                              java.nio.file.Path actualPath)
                                       throws java.io.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:
        java.io.IOException - in case of error
      • assertSameContents

        public static void assertSameContents​(@Nonnull
                                              java.util.List<java.lang.String> expected,
                                              @Nonnull
                                              java.util.List<java.lang.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
                                                java.nio.file.Path expectedPath,
                                                @Nonnull
                                                java.nio.file.Path actualPath)
                                         throws java.io.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
        Throws:
        java.io.IOException - in case of error
        Since:
        1.2-ALPHA-15
      • stringToStrings

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

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

        @Nonnull
        public static java.util.List<java.lang.String> resourceToStrings​(@Nonnull
                                                                         java.lang.String path)
                                                                  throws java.io.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:
        java.io.IOException - in case of error
      • resourceToStrings

        @Nonnull
        public static java.util.List<java.lang.String> resourceToStrings​(@Nonnull
                                                                         java.io.InputStream is)
                                                                  throws java.io.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:
        java.io.IOException - in case of error
      • commonPrefix

        @Nonnull
        public static java.lang.String commonPrefix​(@Nonnull
                                                    java.lang.String s1,
                                                    @Nonnull
                                                    java.lang.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