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