Class FileCompare

java.lang.Object
org.apache.uima.test.junit_extension.FileCompare

public class FileCompare extends Object
FileCompare class provides a several methods, which compare two files or input streams. Most methods are static. It has a facility to incorporate a regex ignore-differences filter
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    compare(File file1, File file2)
    compares two files and return true if the files have the same content.
    static boolean
     
    static boolean
    compare(String filename1, InputStream in)
    compares two files and return true if the files have the same content.
    static boolean
    compare(String filename1, String filename2)
    compares two files and return true if the files have the same content.
    static boolean
    Compare 2 strings, ignoring whitespace characters
    static boolean
    Compare 2 strings, showing where they differ in output to system.out, after doing filtering: normalize cr nl to nl normalize <xmltag:> </xmltag> to <xmltag/> normalize by applying supplied Pattern and deleting anything it matches normalize by converting all 2 or more spaces/tabs to just 1 space
    static boolean
    Compare two strings, give message indicating where they miscompare, including approx 10 chars before and after the first miscompare, for context
    static boolean
    compareWithFilter(String filename1, String filename2, Pattern pattern)
    Compares two files and returns true, if both have the same content, after filtering using the supplied Pattern.
    static boolean
    compareXML(String filename1, String filename2)
    Compares two XML files and returns true, if both have the same content.
    static String
    Read the contents of a file into a string, using the default platform encoding.
    static String
    reader2String(Reader reader, int bufSize)
    Read a bufferedReader into a string, using the default platform encoding.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileCompare

      public FileCompare()
  • Method Details

    • compare

      public static boolean compare(String filename1, String filename2) throws IOException
      compares two files and return true if the files have the same content.
      Parameters:
      filename1 - filename of the first file
      filename2 - filename of the second file
      Returns:
      - true if the files have the same content
      Throws:
      IOException - -
    • compare

      public static boolean compare(File file1, File file2) throws IOException
      compares two files and return true if the files have the same content.
      Parameters:
      file1 - first file
      file2 - second file
      Returns:
      - true if the files have the same content
      Throws:
      IOException - -
    • compare

      public static boolean compare(String filename1, InputStream in) throws IOException
      compares two files and return true if the files have the same content.
      Parameters:
      filename1 - filename of the first file
      in - an input Sream
      Returns:
      - true if the content is the same
      Throws:
      IOException - -
    • compare

      public static boolean compare(InputStream in1, InputStream in2) throws IOException
      Throws:
      IOException
    • compareStrings

      public static boolean compareStrings(String in1, String in2)
      Compare 2 strings, ignoring whitespace characters
      Parameters:
      in1 - -
      in2 - -
      Returns:
      -
    • compareXML

      public static boolean compareXML(String filename1, String filename2) throws IOException
      Compares two XML files and returns true, if both have the same content. Different notations for empty tags are considered equal.
      Parameters:
      filename1 - Filename of the first XML file.
      filename2 - Filename of the second XML file.
      Returns:
      -
      Throws:
      IOException - -
    • compareWithFilter

      public static boolean compareWithFilter(String filename1, String filename2, Pattern pattern) throws IOException
      Compares two files and returns true, if both have the same content, after filtering using the supplied Pattern. In addition,
      • \r\n is normalized to \n,
      • multiple spaces and tabs are normalized to a single space
      Parameters:
      filename1 - Filename of the first XML file.
      filename2 - Filename of the second XML file.
      pattern - an instance of Pattern which matches all substrings which should be filtered out of the match
      Returns:
      true, if both have the same content, after filtering using the supplied Pattern.
      Throws:
      IOException - -
    • compareStringsWithFilter

      public static boolean compareStringsWithFilter(String s1, String s2, Pattern pattern)
      Compare 2 strings, showing where they differ in output to system.out, after doing filtering:
      • normalize cr nl to nl
      • normalize <xmltag:> </xmltag> to <xmltag/>
      • normalize by applying supplied Pattern and deleting anything it matches
      • normalize by converting all 2 or more spaces/tabs to just 1 space
      Parameters:
      s1 - -
      s2 - -
      pattern - -
      Returns:
      -
    • compareStringsWithMsg

      public static boolean compareStringsWithMsg(String s1, String s2)
      Compare two strings, give message indicating where they miscompare, including approx 10 chars before and after the first miscompare, for context
      Parameters:
      s1 - first string to compare
      s2 - second string to compare
      Returns:
      true if strings have the same charactersS
    • file2String

      public static String file2String(File file) throws IOException
      Read the contents of a file into a string, using the default platform encoding.
      Parameters:
      file - The file to be read in.
      Returns:
      String The contents of the file.
      Throws:
      IOException - Various I/O errors. ' TODO: This is duplicated from org.apache.uima.internal.util.FileUtils in the uimaj-core package. We can't have a compile dependency on uimaj-core since that introduces a cycle. Not sure what the best way of handling this is.
    • reader2String

      public static String reader2String(Reader reader, int bufSize) throws IOException
      Read a bufferedReader into a string, using the default platform encoding.
      Parameters:
      reader - to be read in
      bufSize - - size of stream, in bytes. Size in chars is ≤ size in bytes, because chars take 1 or more bytes to encode.
      Returns:
      String The contents of the stream.
      Throws:
      IOException - Various I/O errors. TODO: This is duplicated from org.apache.uima.internal.util.FileUtils in the uimaj-core package. We can't have a compile dependency on uimaj-core since that introduces a cycle. Not sure what the best way of handling this is.