01: package org.acm.seguin.junit;
02:
03: import junit.framework.AssertionFailedError;
04: import java.io.File;
05: import java.io.FileInputStream;
06: import java.io.IOException;
07: import org.acm.seguin.io.FileCopy;
08:
09: /**
10: * Unit test tool that compares two files and throws an
11: * exception if they are different.
12: *
13: *@author Chris Seguin
14: */
15: public class FileCompare {
16: private static int count = 0;
17:
18: /**
19: * Determines if they are equal
20: *
21: *@param message the message to be printed if they are different
22: *@param one the first file
23: *@param two the second file
24: */
25: public static void assertEquals(String message, File one, File two) {
26: assertEquals(message, one, two, true);
27: }
28:
29: /**
30: * Determines if they are equal
31: *
32: *@param message the message to be printed if they are different
33: *@param one the first file
34: *@param two the second file
35: *@param copy should the file be copied
36: */
37: public static void assertEquals(String message, File one, File two,
38: boolean copy) {
39: try {
40: if (one.length() == two.length()) {
41: boolean same = true;
42: FileInputStream oneInput = new FileInputStream(one);
43: FileInputStream twoInput = new FileInputStream(two);
44:
45: for (long index = one.length(); same && (index > 0); index--) {
46: same = (oneInput.read() == twoInput.read());
47: }
48:
49: oneInput.close();
50: twoInput.close();
51:
52: if (same) {
53: return;
54: }
55: }
56: } catch (IOException ioe) {
57: System.out
58: .println("FileCompare.assertEquals Problem comparing files");
59: ioe.printStackTrace(System.out);
60: }
61:
62: File destFile = new File("c:\\temp\\file" + count + ".java");
63: if (copy) {
64: count++;
65: (new FileCopy(two, destFile, false)).run();
66: }
67:
68: String formatted = "";
69: if (message != null) {
70: formatted = message + " ";
71: }
72: throw new AssertionFailedError(formatted
73: + "expected same, copied to " + destFile.getPath()
74: + " (len=" + two.length() + ") compared against "
75: + one.getPath() + " (len=" + one.length() + ")");
76: }
77: }
|