01: package abbot.tester;
02:
03: import java.io.*;
04:
05: import junit.extensions.abbot.TestHelper;
06: import junit.framework.TestCase;
07:
08: /** Test FileComparator. */
09:
10: public class FileComparatorTest extends TestCase {
11:
12: private FileComparator fc;
13:
14: private File generateFile(String contents, int length)
15: throws Throwable {
16: File file = File.createTempFile(getName(), null);
17: file.deleteOnExit();
18: FileOutputStream os = new FileOutputStream(file);
19: while (length > 0) {
20: if (length > contents.length()) {
21: os.write(contents.getBytes());
22: length -= contents.length();
23: } else {
24: os.write(contents.getBytes(), 0, length);
25: length = 0;
26: }
27: }
28: os.close();
29: return file;
30: }
31:
32: protected void setUp() {
33: fc = new FileComparator();
34: }
35:
36: public void testCompareWithNull() throws Throwable {
37: File f1 = generateFile("a", 10);
38: File f2 = null;
39: assertTrue("Shouldn't match null", fc.compare(f1, f2) > 0);
40: assertTrue("Shouldn't match null", fc.compare(f2, f1) < 0);
41: }
42:
43: public void testCompareSameFiles() throws Throwable {
44: File f1 = generateFile("a", 10);
45: File f2 = f1;
46: assertEquals("Same files, a==b", 0, fc.compare(f1, f2));
47: assertEquals("Same files, b==a", 0, fc.compare(f2, f1));
48: }
49:
50: public void testCompareIdenticalFiles() throws Throwable {
51: File f1 = generateFile("A", 10);
52: File f2 = generateFile("A", 10);
53: assertEquals("Identical files, a==b", 0, fc.compare(f1, f2));
54: assertEquals("Identical files, b==a", 0, fc.compare(f2, f1));
55: }
56:
57: public void testCompareDifferentFiles() throws Throwable {
58: File f1 = generateFile("A", 10);
59: File f2 = generateFile("A", 20);
60: assertTrue("Differing files, a < b", fc.compare(f1, f2) < 0);
61: assertTrue("Differing files, a > b", fc.compare(f2, f1) > 0);
62:
63: File f3 = generateFile("B", 10);
64: assertTrue("Differing files, a < b", fc.compare(f1, f3) < 0);
65: assertTrue("Differing files, a > b", fc.compare(f3, f1) > 0);
66: }
67:
68: public void testComparNonFiles() {
69: File f1 = new File("tmp.txt");
70: Object obj = new Object();
71: try {
72: fc.compare(f1, obj);
73: fail("Should have thrown an exception");
74: } catch (IllegalArgumentException iae) {
75: }
76: try {
77: fc.compare(obj, f1);
78: fail("Should have thrown an exception");
79: } catch (IllegalArgumentException iae) {
80: }
81: }
82:
83: /** Create a new test case with the given name. */
84: public FileComparatorTest(String name) {
85: super (name);
86: }
87:
88: /** Return the default test suite. */
89: public static void main(String[] args) {
90: TestHelper.runTests(args, FileComparatorTest.class);
91: }
92: }
|