1 package org.andromda.maven.plugin.cartridge;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.Iterator;
9 import java.util.List;
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.apache.commons.io.FileUtils;
14 import org.apache.commons.io.FilenameUtils;
15 import org.apache.commons.io.filefilter.TrueFileFilter;
16 import org.apache.commons.lang.StringUtils;
17 import org.apache.log4j.Logger;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class CartridgeTest
35 extends TestCase
36 {
37 private static final Logger logger = Logger.getLogger(CartridgeTest.class);
38
39
40
41
42
43 public static final String EXPECTED_DIRECTORY = "expected.dir";
44
45
46
47
48 public static final String ACTUAL_DIRECTORY = "actual.dir";
49
50
51
52
53
54 public static final String BINARY_SUFFIXES = "binary.suffixes";
55
56
57
58
59 private static CartridgeTest instance;
60
61
62
63
64
65
66 public static final CartridgeTest instance()
67 {
68 if (instance == null)
69 {
70 instance = new CartridgeTest();
71 }
72 return instance;
73 }
74
75 private CartridgeTest()
76 {
77 super();
78 }
79
80
81
82
83 private String binarySuffixes = StringUtils.trimToEmpty(System.getProperty(BINARY_SUFFIXES));
84
85
86
87
88
89
90
91 public void setBinarySuffixes(final String binarySuffixes)
92 {
93 this.binarySuffixes = binarySuffixes;
94 }
95
96
97
98
99 private String actualOutputPath = StringUtils.trimToEmpty(System.getProperty(ACTUAL_DIRECTORY));
100
101
102
103
104
105
106
107 public void setActualOutputPath(final String actualOutputPath)
108 {
109 this.actualOutputPath = actualOutputPath;
110 }
111
112
113
114
115 private String expectedOutputPath = StringUtils.trimToEmpty(System.getProperty(EXPECTED_DIRECTORY));
116
117
118
119
120
121
122
123 public void setExpectedOutputPath(final String expectedOutputPath)
124 {
125 this.expectedOutputPath = expectedOutputPath;
126 }
127
128
129
130
131 public CartridgeTest(final String name)
132 {
133 super(name);
134 }
135
136
137
138
139 public static Test suite()
140 {
141 TestSuite suite = new TestSuite();
142 instance().addTests(suite);
143 return suite;
144 }
145
146
147
148
149
150
151
152 private void addTests(final TestSuite suite)
153 {
154 Collection<File> expectedFiles = Collections.emptyList();
155
156 final File directory = this.getExpectedOutputDirectory();
157 if (null!=directory && directory.exists())
158 {
159 expectedFiles = FileUtils.listFiles(
160 directory.isDirectory()? directory : directory.getParentFile(),
161 TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
162 }
163
164 final Iterator<File> iterator = expectedFiles.iterator();
165 logger.info(" --- Expecting " + expectedFiles.size() + " Generated Files --- ");
166 logger.info("ExpectedOutputDirectory --> " + this.getExpectedOutputDirectory());
167 final List<File> missingFiles = new ArrayList<File>();
168 for (int ctr = 1; iterator.hasNext(); ctr++)
169 {
170 final File expectedFile = iterator.next();
171 final File actualFile = getActualFile(expectedFile);
172 if (!actualFile.exists())
173 {
174 missingFiles.add(actualFile);
175 }
176 final boolean binary = isBinary(actualFile);
177 if (logger.isDebugEnabled())
178 {
179 logger.debug(ctr + ") binary = " + binary);
180 logger.debug("expected --> '" + expectedFile + '\'');
181 logger.debug("actual --> '" + actualFile + '\'');
182 }
183
184
185
186
187 List<String> strings = new ArrayList<String>(2);
188 strings.add("enerated by ");
189 strings.add("enerated on ");
190 suite.addTest(new FileComparator(
191 "testEquals",
192 expectedFile,
193 actualFile,
194 binary,
195 false,
196 true,
197 strings));
198 }
199 if (!missingFiles.isEmpty())
200 {
201 Collections.sort(missingFiles);
202 StringBuilder failureMessage = new StringBuilder("\n--- The following ");
203 failureMessage.append(missingFiles.size());
204 failureMessage.append(" expected files do not exist ----\n");
205 Iterator<File> missingFileIterator = missingFiles.iterator();
206 for (int ctr = 1; missingFileIterator.hasNext(); ctr++)
207 {
208 failureMessage.append(ctr).append(") ");
209 failureMessage.append(missingFileIterator.next());
210 if (missingFileIterator.hasNext())
211 {
212 failureMessage.append('\n');
213 }
214 }
215 TestCase.fail(failureMessage.toString());
216 }
217 }
218
219
220
221
222
223
224
225
226 private File getActualFile(final File expectedFile)
227 {
228 String actualFile;
229 final File actualOutputDirectory = this.getActualOutputDirectory();
230 final File expectedOutputDirectory = this.getExpectedOutputDirectory();
231 final String path = expectedFile.getPath();
232 if (expectedFile.getPath().startsWith(actualOutputDirectory.getPath()))
233 {
234 actualFile = path.substring(
235 actualOutputDirectory.getPath().length(),
236 path.length());
237 actualFile = expectedOutputDirectory + expectedFile.toString();
238 }
239 else
240 {
241 actualFile = path.substring(
242 expectedOutputDirectory.getPath().length(),
243 path.length());
244 actualFile = actualOutputDirectory + actualFile;
245 }
246 return new File(actualFile);
247 }
248
249
250
251
252 private File expectedOutputDirectory;
253
254
255
256
257
258
259 private File getExpectedOutputDirectory()
260 {
261 if (this.expectedOutputDirectory == null)
262 {
263 this.expectedOutputDirectory = this.getDirectory(this.expectedOutputPath);
264 }
265 return this.expectedOutputDirectory;
266 }
267
268
269
270
271
272
273 protected void setExpectedOutputDirectory(File file)
274 {
275 if (file == null)
276 {
277 this.expectedOutputDirectory = this.getDirectory(this.expectedOutputPath);
278 }
279 }
280
281
282
283
284 private File actualOutputDirectory;
285
286 private File getActualOutputDirectory()
287 {
288 if (this.actualOutputDirectory == null)
289 {
290 this.actualOutputDirectory = this.getDirectory(this.actualOutputPath);
291 }
292 return this.actualOutputDirectory;
293 }
294
295
296
297
298
299 protected void setActualOutputDirectory(File file)
300 {
301 if (file == null)
302 {
303 this.actualOutputDirectory = this.getDirectory(this.actualOutputPath);
304 }
305 }
306
307
308
309
310
311
312
313 private File getDirectory(final String path)
314 {
315 File directory = new File(path);
316 if (!directory.exists() || !directory.isDirectory())
317 {
318 throw new RuntimeException("directory <" + path + "> doesn't exist");
319 }
320 return directory;
321 }
322
323
324
325
326
327
328
329
330 private boolean isBinary(final File file)
331 {
332 String suffix = FilenameUtils.getExtension(file.getName());
333 return this.getBinarySuffixes().contains(suffix);
334 }
335
336 private Collection<String> binarySuffixCollection;
337
338
339
340
341
342
343
344 private Collection<String> getBinarySuffixes()
345 {
346 if (this.binarySuffixCollection == null)
347 {
348 final String suffixes = this.binarySuffixes != null ? this.binarySuffixes.trim() : "";
349 final String[] suffixArray = suffixes.split("\\s*,\\s*");
350 this.binarySuffixCollection = Arrays.asList(suffixArray);
351 }
352 return this.binarySuffixCollection;
353 }
354
355
356
357
358 public void shutdown()
359 {
360 CartridgeTest.instance = null;
361 }
362 }