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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. // header - edit "Data/yourJavaHeader" to customize
  3. // contents - edit "EventHandlers/Java file/onCreate" to customize
  4. //
  5. */
  6. import java.util.*;
  7. import java.io.*;
  8. import org.apache.tools.ant.Task;
  9. import org.apache.tools.ant.BuildException;
  10. import java.text.DateFormat;
  11. public class Compare {
  12. private String referenceDirectory, testDirectory;
  13. private String [] filenameList;
  14. private String filenames;
  15. private static boolean IDENTICAL_FILES = true;
  16. private static boolean NOTIDENTICAL_FILES = false;
  17. private BufferedInputStream oldfileInput;
  18. private BufferedInputStream newfileInput;
  19. //sets directory for test files
  20. public void setTestDirectory(String testDirectory) {
  21. if (!(testDirectory.endsWith("/") | testDirectory.endsWith("\\"))) {
  22. testDirectory += File.separator;
  23. }
  24. this.testDirectory = testDirectory;
  25. }
  26. //sets directory for reference files
  27. public void setReferenceDirectory(String referenceDirectory) {
  28. if (!(referenceDirectory.endsWith("/") | referenceDirectory.endsWith("\\"))) {
  29. referenceDirectory += File.separator;
  30. }
  31. this.referenceDirectory = referenceDirectory;
  32. }
  33. public void setFilenames (String filenames) {
  34. StringTokenizer tokens = new StringTokenizer(filenames,",");
  35. Vector filenameListTmp = new Vector(20);
  36. while (tokens.hasMoreTokens()) {
  37. filenameListTmp.add(tokens.nextToken());
  38. }
  39. filenameList = new String [filenameListTmp.size()] ;
  40. filenameListTmp.copyInto((String[]) filenameList);
  41. }
  42. private boolean compareBytes (File oldFile, File newFile) {
  43. try {
  44. oldfileInput = new BufferedInputStream(new FileInputStream(oldFile));
  45. newfileInput = new BufferedInputStream(new FileInputStream(newFile));
  46. int charactO = 0;
  47. int charactN = 0;
  48. boolean identical = true;
  49. while (identical & (charactO != -1)) {
  50. if (charactO == charactN) {
  51. charactO = oldfileInput.read();
  52. charactN = newfileInput.read();
  53. } else {
  54. return NOTIDENTICAL_FILES;
  55. }
  56. }
  57. return IDENTICAL_FILES;
  58. } catch (IOException io) {
  59. System.err.println("Task Compare - Error: \n" + io.toString());
  60. }
  61. return NOTIDENTICAL_FILES;
  62. }
  63. private boolean compareFileSize(File oldFile, File newFile) {
  64. if (oldFile.length() != newFile.length()) {
  65. return NOTIDENTICAL_FILES;
  66. } else {
  67. return IDENTICAL_FILES;
  68. }
  69. } //end: compareBytes
  70. private boolean filesExist (File oldFile, File newFile) {
  71. if (!oldFile.exists()) {
  72. System.err.println("Task Compare - ERROR: File "
  73. + referenceDirectory + oldFile.getName()
  74. + " doesn't exist!");
  75. return false;
  76. } else if (!newFile.exists()) {
  77. System.err.println("Task Compare - ERROR: File "
  78. + testDirectory + newFile.getName() + " doesn't exist!");
  79. return false;
  80. } else {
  81. return true;
  82. }
  83. }
  84. public void writeHeader (PrintWriter results) {
  85. String dateTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM).format(new Date());
  86. results.println("<html><head><title>Test Results</title></head><body>\n");
  87. results.println("<h2>Compare Results<br>");
  88. results.println("<font size='1'>created " + dateTime + "</font></h2>");
  89. results.println("<table cellpadding='10' border='2'><thead><th align='center'>reference file</th><th align='center'>test file</th>"
  90. + "<th align='center'>identical?</th></thead>");
  91. }
  92. //main method of task compare
  93. public void execute () throws BuildException {
  94. boolean identical = false;
  95. File oldFile;
  96. File newFile;
  97. try {
  98. PrintWriter results = new PrintWriter (new FileWriter("results.html"),true);
  99. this.writeHeader(results);
  100. for (int i = 0; i < filenameList.length; i++) {
  101. oldFile = new File (referenceDirectory + filenameList[i]);
  102. newFile = new File (testDirectory + filenameList[i]);
  103. if (filesExist(oldFile, newFile)) {
  104. identical = compareFileSize(oldFile, newFile);
  105. if (identical) {
  106. identical = compareBytes(oldFile,newFile);
  107. }
  108. if (!identical) {
  109. System.out.println("Task Compare: \nFiles " + referenceDirectory
  110. + oldFile.getName()+ " - " + testDirectory
  111. + newFile.getName() + " are *not* identical.");
  112. results.println("<tr><td><a href='" + referenceDirectory + oldFile.getName() + "'>"
  113. + oldFile.getName() + "</a> </td><td> <a href='"
  114. + testDirectory + newFile.getName() +"'>"
  115. + newFile.getName() +"</a>"
  116. + " </td><td><font color='red'>No</font></td></tr>");
  117. } else {
  118. results.println("<tr><td><a href='" + referenceDirectory + oldFile.getName() + "'>"
  119. + oldFile.getName() + "</a> </td><td> <a href='"
  120. + testDirectory + newFile.getName() + "'>"
  121. + newFile.getName() + "</a>"
  122. + " </td><td>Yes</td></tr>");
  123. }
  124. }
  125. }
  126. results.println("</table></html>");
  127. } catch (IOException ioe) {
  128. System.err.println("ERROR: " + ioe);
  129. }
  130. } //end: execute()
  131. }