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.

Compare.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. import java.util.*;
  55. import java.io.*;
  56. import org.apache.tools.ant.Task;
  57. import org.apache.tools.ant.BuildException;
  58. import java.text.DateFormat;
  59. /** This class is an extension of Ant, a script utility from
  60. * jakarta.apache.org.
  61. * It provides methods to compare two files
  62. */
  63. public class Compare {
  64. private String referenceDirectory, testDirectory;
  65. private String [] filenameList;
  66. private String filenames;
  67. private static boolean IDENTICAL_FILES = true;
  68. private static boolean NOTIDENTICAL_FILES = false;
  69. private BufferedInputStream oldfileInput;
  70. private BufferedInputStream newfileInput;
  71. //sets directory for test files
  72. public void setTestDirectory(String testDirectory) {
  73. if (!(testDirectory.endsWith("/") | testDirectory.endsWith("\\"))) {
  74. testDirectory += File.separator;
  75. }
  76. this.testDirectory = testDirectory;
  77. }
  78. //sets directory for reference files
  79. public void setReferenceDirectory(String referenceDirectory) {
  80. if (!(referenceDirectory.endsWith("/") | referenceDirectory.endsWith("\\"))) {
  81. referenceDirectory += File.separator;
  82. }
  83. this.referenceDirectory = referenceDirectory;
  84. }
  85. public void setFilenames (String filenames) {
  86. StringTokenizer tokens = new StringTokenizer(filenames,",");
  87. Vector filenameListTmp = new Vector(20);
  88. while (tokens.hasMoreTokens()) {
  89. filenameListTmp.addElement(tokens.nextToken());
  90. }
  91. filenameList = new String [filenameListTmp.size()] ;
  92. filenameListTmp.copyInto((String[]) filenameList);
  93. }
  94. private boolean compareBytes (File oldFile, File newFile) {
  95. try {
  96. oldfileInput = new BufferedInputStream(new FileInputStream(oldFile));
  97. newfileInput = new BufferedInputStream(new FileInputStream(newFile));
  98. int charactO = 0;
  99. int charactN = 0;
  100. boolean identical = true;
  101. while (identical & (charactO != -1)) {
  102. if (charactO == charactN) {
  103. charactO = oldfileInput.read();
  104. charactN = newfileInput.read();
  105. } else {
  106. return NOTIDENTICAL_FILES;
  107. }
  108. }
  109. return IDENTICAL_FILES;
  110. } catch (IOException io) {
  111. System.err.println("Task Compare - Error: \n" + io.toString());
  112. }
  113. return NOTIDENTICAL_FILES;
  114. }
  115. private boolean compareFileSize(File oldFile, File newFile) {
  116. if (oldFile.length() != newFile.length()) {
  117. return NOTIDENTICAL_FILES;
  118. } else {
  119. return IDENTICAL_FILES;
  120. }
  121. } //end: compareBytes
  122. private boolean filesExist (File oldFile, File newFile) {
  123. if (!oldFile.exists()) {
  124. System.err.println("Task Compare - ERROR: File "
  125. + referenceDirectory + oldFile.getName()
  126. + " doesn't exist!");
  127. return false;
  128. } else if (!newFile.exists()) {
  129. System.err.println("Task Compare - ERROR: File "
  130. + testDirectory + newFile.getName() + " doesn't exist!");
  131. return false;
  132. } else {
  133. return true;
  134. }
  135. }
  136. public void writeHeader (PrintWriter results) {
  137. String dateTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM).format(new Date());
  138. results.println("<html><head><title>Test Results</title></head><body>\n");
  139. results.println("<h2>Compare Results<br>");
  140. results.println("<font size='1'>created " + dateTime + "</font></h2>");
  141. results.println("<table cellpadding='10' border='2'><thead><th align='center'>reference file</th><th align='center'>test file</th>"
  142. + "<th align='center'>identical?</th></thead>");
  143. }
  144. //main method of task compare
  145. public void execute () throws BuildException {
  146. boolean identical = false;
  147. File oldFile;
  148. File newFile;
  149. try {
  150. PrintWriter results = new PrintWriter (new FileWriter("results.html"),true);
  151. this.writeHeader(results);
  152. for (int i = 0; i < filenameList.length; i++) {
  153. oldFile = new File (referenceDirectory + filenameList[i]);
  154. newFile = new File (testDirectory + filenameList[i]);
  155. if (filesExist(oldFile, newFile)) {
  156. identical = compareFileSize(oldFile, newFile);
  157. if (identical) {
  158. identical = compareBytes(oldFile,newFile);
  159. }
  160. if (!identical) {
  161. System.out.println("Task Compare: \nFiles " + referenceDirectory
  162. + oldFile.getName()+ " - " + testDirectory
  163. + newFile.getName() + " are *not* identical.");
  164. results.println("<tr><td><a href='" + referenceDirectory + oldFile.getName() + "'>"
  165. + oldFile.getName() + "</a> </td><td> <a href='"
  166. + testDirectory + newFile.getName() +"'>"
  167. + newFile.getName() +"</a>"
  168. + " </td><td><font color='red'>No</font></td></tr>");
  169. } else {
  170. results.println("<tr><td><a href='" + referenceDirectory + oldFile.getName() + "'>"
  171. + oldFile.getName() + "</a> </td><td> <a href='"
  172. + testDirectory + newFile.getName() + "'>"
  173. + newFile.getName() + "</a>"
  174. + " </td><td>Yes</td></tr>");
  175. }
  176. }
  177. }
  178. results.println("</table></html>");
  179. } catch (IOException ioe) {
  180. System.err.println("ERROR: " + ioe);
  181. }
  182. } //end: execute()
  183. }