You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileCompare.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.tools.anttasks;
  19. import java.io.BufferedInputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.PrintWriter;
  23. import java.text.DateFormat;
  24. import java.util.Date;
  25. import java.util.List;
  26. import java.util.StringTokenizer;
  27. import org.apache.commons.io.IOUtils;
  28. import org.apache.tools.ant.BuildException;
  29. /**
  30. * This class is an extension of Ant, a script utility from
  31. * http://ant.apache.org.
  32. * It provides methods to compare two files.
  33. */
  34. public class FileCompare {
  35. private String referenceDirectory;
  36. private String testDirectory;
  37. private String[] filenameList;
  38. /**
  39. * Sets directory for test files.
  40. * @param testDirectory the test directory
  41. */
  42. public void setTestDirectory(String testDirectory) {
  43. if (!(testDirectory.endsWith("/") || testDirectory.endsWith("\\"))) {
  44. testDirectory += File.separator;
  45. }
  46. this.testDirectory = testDirectory;
  47. }
  48. /**
  49. * Sets directory for reference files.
  50. * @param referenceDirectory the reference directory
  51. */
  52. public void setReferenceDirectory(String referenceDirectory) {
  53. if (!(referenceDirectory.endsWith("/")
  54. || referenceDirectory.endsWith("\\"))) {
  55. referenceDirectory += File.separator;
  56. }
  57. this.referenceDirectory = referenceDirectory;
  58. }
  59. /**
  60. * Sets the comma-separated list of files to process.
  61. * @param filenames list of files, comma-separated
  62. */
  63. public void setFilenames(String filenames) {
  64. StringTokenizer tokens = new StringTokenizer(filenames, ",");
  65. List filenameListTmp = new java.util.ArrayList(20);
  66. while (tokens.hasMoreTokens()) {
  67. filenameListTmp.add(tokens.nextToken());
  68. }
  69. filenameList = new String[filenameListTmp.size()];
  70. filenameList = (String[])filenameListTmp.toArray(new String[filenameListTmp.size()]);
  71. }
  72. /**
  73. * Compares two files to see if they are equal
  74. * @param f1 first file to compare
  75. * @param f2 second file to compare
  76. * @return true if files are same, false otherwise
  77. * @throws IOException if not caught
  78. */
  79. public static boolean compareFiles(File f1, File f2) throws IOException {
  80. return (compareFileSize(f1, f2) && compareBytes(f1, f2));
  81. }
  82. /**
  83. * Compare the contents of two files.
  84. * @param file1 the first file to compare
  85. * @param file2 the second file to compare
  86. * @return true if files are same byte-by-byte, false otherwise
  87. */
  88. private static boolean compareBytes(File file1, File file2) throws IOException {
  89. boolean same = true;
  90. BufferedInputStream file1Input
  91. = new BufferedInputStream(new java.io.FileInputStream(file1));
  92. BufferedInputStream file2Input
  93. = new BufferedInputStream(new java.io.FileInputStream(file2));
  94. int charact1 = 0;
  95. int charact2 = 0;
  96. while (charact1 != -1) {
  97. if (charact1 == charact2) {
  98. charact1 = file1Input.read();
  99. charact2 = file2Input.read();
  100. } else {
  101. same = false;
  102. break;
  103. }
  104. }
  105. IOUtils.closeQuietly(file1Input);
  106. IOUtils.closeQuietly(file2Input);
  107. return same;
  108. }
  109. /**
  110. * Does a file size compare of two files
  111. * @param oldFile the first file to compare
  112. * @param newFile the second file to compare
  113. * @return true if files are same length, false otherwise
  114. */
  115. private static boolean compareFileSize(File oldFile, File newFile) {
  116. return oldFile.length() == newFile.length();
  117. }
  118. private boolean filesExist(File oldFile, File newFile) {
  119. if (!oldFile.exists()) {
  120. System.err.println("Task Compare - ERROR: File "
  121. + referenceDirectory + oldFile.getName()
  122. + " doesn't exist!");
  123. return false;
  124. } else if (!newFile.exists()) {
  125. System.err.println("Task Compare - ERROR: File " + testDirectory
  126. + newFile.getName() + " doesn't exist!");
  127. return false;
  128. } else {
  129. return true;
  130. }
  131. }
  132. private void writeHeader(PrintWriter results) {
  133. String dateTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
  134. DateFormat.MEDIUM).format(new Date());
  135. results.println("<html><head><title>Test Results</title></head><body>\n");
  136. results.println("<h2>Compare Results<br>");
  137. results.println("<font size='1'>created " + dateTime
  138. + "</font></h2>");
  139. results.println("<table cellpadding='10' border='2'><thead>"
  140. + "<th align='center'>reference file</th>"
  141. + "<th align='center'>test file</th>"
  142. + "<th align='center'>identical?</th></thead>");
  143. }
  144. /**
  145. * Main method of task compare
  146. * @throws BuildException If the execution fails.
  147. */
  148. public void execute() throws BuildException {
  149. boolean identical = false;
  150. File oldFile;
  151. File newFile;
  152. try {
  153. PrintWriter results
  154. = new PrintWriter(new java.io.FileWriter("results.html"), true);
  155. this.writeHeader(results);
  156. for (String aFilenameList : filenameList) {
  157. oldFile = new File(referenceDirectory + aFilenameList);
  158. newFile = new File(testDirectory + aFilenameList);
  159. if (filesExist(oldFile, newFile)) {
  160. identical = compareFileSize(oldFile, newFile);
  161. if (identical) {
  162. identical = compareBytes(oldFile, newFile);
  163. }
  164. if (!identical) {
  165. System.out.println("Task Compare: \nFiles "
  166. + referenceDirectory
  167. + oldFile.getName() + " - "
  168. + testDirectory
  169. + newFile.getName()
  170. + " are *not* identical.");
  171. results.println("<tr><td><a href='"
  172. + referenceDirectory
  173. + oldFile.getName() + "'>"
  174. + oldFile.getName()
  175. + "</a> </td><td> <a href='"
  176. + testDirectory + newFile.getName()
  177. + "'>" + newFile.getName() + "</a>"
  178. + " </td><td><font color='red'>No</font></td></tr>");
  179. } else {
  180. results.println("<tr><td><a href='"
  181. + referenceDirectory
  182. + oldFile.getName() + "'>"
  183. + oldFile.getName()
  184. + "</a> </td><td> <a href='"
  185. + testDirectory + newFile.getName()
  186. + "'>" + newFile.getName() + "</a>"
  187. + " </td><td>Yes</td></tr>");
  188. }
  189. }
  190. }
  191. results.println("</table></html>");
  192. } catch (IOException ioe) {
  193. System.err.println("ERROR: " + ioe);
  194. }
  195. } // end: execute()
  196. }