1 package org.andromda.maven.plugin.cartridge;
2
3 import java.io.File;
4 import java.util.List;
5 import junit.framework.TestCase;
6 import org.apache.commons.io.FileUtils;
7 import org.apache.commons.lang.StringUtils;
8
9
10
11
12
13
14
15
16 public class FileComparator
17 extends TestCase
18 {
19 private File expectedFile;
20 private File actualFile;
21 private boolean binary = false;
22 private boolean ignoreLineEndings = true;
23 private boolean ignoreWhitespace = false;
24 private List<String> ignoreLinesWithStrings = null;
25
26
27
28
29
30
31
32
33
34
35 public FileComparator(
36 String testName,
37 File expectedFile,
38 File actualFile,
39 boolean binary)
40 {
41 super();
42 this.setName(testName);
43 this.expectedFile = expectedFile;
44 this.actualFile = actualFile;
45 this.binary = binary;
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public FileComparator(
61 String testName,
62 File expectedFile,
63 File actualFile,
64 boolean binary,
65 boolean ignoreWhitespace,
66 boolean ignoreLineEndings,
67 List<String> ignoreLinesWithStrings)
68 {
69 this(testName, expectedFile, actualFile, binary);
70 this.ignoreWhitespace = ignoreWhitespace;
71 this.ignoreLineEndings = ignoreLineEndings;
72 this.ignoreLinesWithStrings = ignoreLinesWithStrings;
73 }
74
75
76
77
78 public void testEquals()
79 {
80 assertTrue(
81 "expected file <" + expectedFile.getPath() + "> doesn't exist",
82 expectedFile.exists());
83 assertTrue(
84 "actual file <" + actualFile.getPath() + "> doesn't exist",
85 actualFile.exists());
86 this.testContentsEqual();
87 }
88
89
90
91
92
93 protected void testContentsEqual()
94 {
95 try
96 {
97 String message = "actual file <" + actualFile + "> does not match\n";
98 if (this.binary)
99 {
100 assertTrue(
101 message,
102 FileUtils.contentEquals(
103 expectedFile,
104 actualFile));
105 }
106 else
107 {
108 String actualContents = FileUtils.readFileToString(actualFile);
109 String expectedContents = FileUtils.readFileToString(expectedFile);
110
111
112
113 if (this.ignoreLinesWithStrings != null && !this.ignoreLinesWithStrings.isEmpty())
114 {
115 expectedContents = removeLinesWithStrings(expectedContents, ignoreLinesWithStrings);
116 actualContents = removeLinesWithStrings(actualContents, ignoreLinesWithStrings);
117 }
118 if (this.ignoreWhitespace)
119 {
120 expectedContents = StringUtils.remove(expectedContents, ' ');
121 expectedContents = StringUtils.remove(expectedContents, '\t');
122 actualContents = StringUtils.remove(actualContents, ' ');
123 actualContents = StringUtils.remove(actualContents, '\t');
124 }
125 if (this.ignoreLineEndings)
126 {
127 expectedContents = StringUtils.remove(expectedContents, '\r');
128 actualContents = StringUtils.remove(actualContents, '\r');
129 }
130
131 assertEquals(
132 message,
133 expectedContents.trim(),
134 actualContents.trim());
135 }
136 }
137 catch (final Throwable throwable)
138 {
139 fail(throwable.toString());
140 }
141 }
142
143
144
145
146
147
148 public String removeLinesWithStrings(String input, List<String> patternList)
149 {
150 String tempString = input;
151 for (String pattern : patternList)
152 {
153 int patternIndex = tempString.indexOf(pattern);
154 while (patternIndex > 0)
155 {
156 String temp = tempString.substring(patternIndex, tempString.length());
157 temp = temp.substring(temp.indexOf('\n'));
158 tempString = tempString.substring(0, patternIndex) + temp;
159 patternIndex = tempString.indexOf(pattern);
160 }
161 }
162 return tempString;
163 }
164
165
166
167
168
169
170 public File getActualFile()
171 {
172 return this.actualFile;
173 }
174
175
176
177
178
179
180
181 public File getExpectedFile()
182 {
183 return this.expectedFile;
184 }
185 }