01: package org.andromda.cartridges.testsuite;
02:
03: import java.io.File;
04:
05: import junit.framework.TestCase;
06:
07: import org.andromda.core.common.ResourceUtils;
08: import org.apache.commons.io.FileUtils;
09:
10: /**
11: * Compares two files. It checks if both file do exist and if the contents of
12: * both files are equal.
13: *
14: * @author Chad Brandon
15: */
16: public class FileComparator extends TestCase {
17: private File expectedFile;
18: private File actualFile;
19: private boolean binary;
20:
21: /**
22: * Constructs a new instance of the FileComparator.
23: *
24: * @param testName the name of the test to run
25: * @param expectedFile the location of the expected file
26: * @param actualFile the location of the actual file.
27: * @param binary whether or not the file is binary, if it is binary contents
28: * of the binary are not compared as Strings but as binary files.
29: */
30: public FileComparator(String testName, File expectedFile,
31: File actualFile, boolean binary) {
32: super ();
33: this .setName(testName);
34: this .expectedFile = expectedFile;
35: this .actualFile = actualFile;
36: this .binary = binary;
37: }
38:
39: public void testEquals() {
40: assertTrue("expected file <" + expectedFile.getPath()
41: + "> doesn't exist", expectedFile.exists());
42: assertTrue("actual file <" + actualFile.getPath()
43: + "> doesn't exist", actualFile.exists());
44: this .testContentsEqual();
45: }
46:
47: /**
48: * Loads both the <code>actual</code> and <code>expected</code> files
49: * and tests the contents for equality.
50: */
51: protected void testContentsEqual() {
52: try {
53: String actualContents = ResourceUtils
54: .getContents(actualFile.toURL());
55: String expectedContents = ResourceUtils
56: .getContents(expectedFile.toURL());
57: String message = "actual file <" + actualFile
58: + "> does not match\n";
59: if (this .binary) {
60: assertTrue(message, FileUtils.contentEquals(
61: expectedFile, actualFile));
62: } else {
63: assertEquals(message, expectedContents.trim(),
64: actualContents.trim());
65: }
66: } catch (final Throwable throwable) {
67: fail(throwable.toString());
68: }
69: }
70:
71: /**
72: * Gets the actual file being compared.
73: *
74: * @return the file being compared.
75: */
76: public File getActualFile() {
77: return this .actualFile;
78: }
79:
80: /**
81: * Gets the file expected file (i.e. the file that
82: * the actual file is compared against).
83: *
84: * @return the expected file.
85: */
86: public File getExpectedFile() {
87: return this.expectedFile;
88: }
89: }
|