Improve new conversion tests
[fanfix.git] / src / be / nikiroo / fanfix / test / ConversionTest.java
1 package be.nikiroo.fanfix.test;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FilenameFilter;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.zip.ZipEntry;
11 import java.util.zip.ZipInputStream;
12
13 import be.nikiroo.fanfix.Instance;
14 import be.nikiroo.fanfix.Main;
15 import be.nikiroo.fanfix.output.BasicOutput;
16 import be.nikiroo.utils.IOUtils;
17 import be.nikiroo.utils.TempFiles;
18 import be.nikiroo.utils.TraceHandler;
19 import be.nikiroo.utils.test.TestCase;
20 import be.nikiroo.utils.test.TestLauncher;
21
22 class ConversionTest extends TestLauncher {
23 private TempFiles tempFiles;
24 private File testFile;
25 private File expectedDir;
26 private File resultDir;
27
28 public ConversionTest(String[] args) {
29 super("Conversion", args);
30
31 addTest(new TestCase("Read the test file") {
32 @Override
33 public void test() throws Exception {
34 assertEquals("The test file \"" + testFile
35 + "\" cannot be found", true, testFile.exists());
36 }
37 });
38
39 addTest(new TestCase("Assure directories exist") {
40 @Override
41 public void test() throws Exception {
42 expectedDir.mkdirs();
43 resultDir.mkdirs();
44 assertEquals("The Expected directory \"" + expectedDir
45 + "\" cannot be created", true, expectedDir.exists());
46 assertEquals("The Result directory \"" + resultDir
47 + "\" cannot be created", true, resultDir.exists());
48 }
49 });
50
51 for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) {
52 // NOT for special mode SYSOUT
53 if (!BasicOutput.OutputType.SYSOUT.equals(type)) {
54 addTest(getTestFor(type));
55 }
56 }
57 }
58
59 @Override
60 protected void start() throws Exception {
61 testFile = new File("test/test.story");
62 expectedDir = new File("test/expected/");
63 resultDir = new File("test/result/");
64
65 tempFiles = new TempFiles("Fanfix-ConversionTest");
66 }
67
68 @Override
69 protected void stop() throws Exception {
70 tempFiles.close();
71 }
72
73 private TestCase getTestFor(final BasicOutput.OutputType type) {
74 return new TestCase(type + " output mode") {
75 @Override
76 public void test() throws Exception {
77 File target = generate(this, testFile, resultDir, type);
78 target = new File(target.getAbsolutePath()
79 + type.getDefaultExtension(false));
80
81 // Check conversion:
82 compareFiles(this, expectedDir, resultDir, type, null);
83
84 // Cross-checks:
85
86 // LATEX not supported as input
87 if (!BasicOutput.OutputType.LATEX.equals(type)) {
88 for (BasicOutput.OutputType crossType : BasicOutput.OutputType
89 .values()) {
90 // NOT for special mode SYSOUT
91 if (!BasicOutput.OutputType.SYSOUT.equals(crossType)) {
92 File crossDir = tempFiles
93 .createTempDir("cross-result");
94 generate(this, target, crossDir, crossType);
95 compareFiles(this, crossDir, resultDir, crossType,
96 crossType);
97 }
98 }
99 }
100 }
101 };
102 }
103
104 private File generate(TestCase testCase, File testFile, File resultDir,
105 BasicOutput.OutputType type) throws Exception {
106 final List<String> errors = new ArrayList<String>();
107
108 TraceHandler previousTraceHandler = Instance.getTraceHandler();
109 Instance.setTraceHandler(new TraceHandler(true, true, 0) {
110 @Override
111 public void error(String message) {
112 errors.add(message);
113 }
114
115 @Override
116 public void error(Exception e) {
117 error(" ");
118 for (Throwable t = e; t != null; t = t.getCause()) {
119 error(((t == e) ? "(" : "..caused by: (")
120 + t.getClass().getSimpleName() + ") "
121 + t.getMessage());
122 for (StackTraceElement s : t.getStackTrace()) {
123 error("\t" + s.toString());
124 }
125 }
126 }
127 });
128
129 try {
130 File target = new File(resultDir, type.toString());
131 int code = Main.convert(testFile.getAbsolutePath(),
132 type.toString(), target.getAbsolutePath(), false, null);
133
134 String error = "";
135 for (String err : errors) {
136 if (!error.isEmpty())
137 error += "\n";
138 error += err;
139 }
140 testCase.assertEquals("The conversion returned an error message: "
141 + error, 0, errors.size());
142 if (code != 0) {
143 testCase.fail("The conversion failed with return code: " + code);
144 }
145
146 return target;
147 } finally {
148 Instance.setTraceHandler(previousTraceHandler);
149 }
150 }
151
152 private void compareFiles(TestCase testCase, File expectedDir,
153 File resultDir, final BasicOutput.OutputType typeToCompare,
154 final BasicOutput.OutputType sourceType) throws Exception {
155
156 FilenameFilter filter = null;
157 if (typeToCompare != null) {
158 filter = new FilenameFilter() {
159 @Override
160 public boolean accept(File dir, String name) {
161 return name.startsWith(typeToCompare.toString());
162 }
163 };
164 }
165
166 List<String> resultFiles = Arrays.asList(resultDir.list(filter));
167 resultFiles.sort(null);
168 List<String> expectedFiles = Arrays.asList(expectedDir.list(filter));
169 expectedFiles.sort(null);
170
171 testCase.assertEquals("The resulting file names are not expected",
172 expectedFiles, resultFiles);
173
174 for (int i = 0; i < resultFiles.size(); i++) {
175 File expected = new File(expectedDir, expectedFiles.get(i));
176 File result = new File(resultDir, resultFiles.get(i));
177
178 testCase.assertEquals(
179 "Type mismatch: expected a "
180 + (expected.isDirectory() ? "directory" : "file")
181 + ", received a "
182 + (result.isDirectory() ? "directory" : "file"),
183 expected.isDirectory(), result.isDirectory());
184
185 if (expected.isDirectory()) {
186 compareFiles(testCase, expected, result, null, sourceType);
187 continue;
188 }
189
190 if (expected.getName().endsWith(".cbz")
191 || expected.getName().endsWith(".epub")) {
192 File tmpExpected = tempFiles.createTempDir(expected.getName()
193 + "[zip-content]");
194 File tmpResult = tempFiles.createTempDir(result.getName()
195 + "[zip-content]");
196 unzip(expected, tmpExpected);
197 unzip(result, tmpResult);
198 compareFiles(testCase, tmpExpected, tmpResult, null, sourceType);
199 } else {
200 List<String> expectedLines = Arrays.asList(IOUtils
201 .readSmallFile(expected).split("\n"));
202 List<String> resultLines = Arrays.asList(IOUtils.readSmallFile(
203 result).split("\n"));
204
205 String name = expected.getAbsolutePath();
206 if (name.startsWith(expectedDir.getAbsolutePath())) {
207 name = expectedDir.getName()
208 + name.substring(expectedDir.getAbsolutePath()
209 .length());
210 }
211 for (int j = 0; j < expectedLines.size(); j++) {
212 String expectedLine = expectedLines.get(j);
213 String resultLine = resultLines.get(j);
214
215 boolean skip = false;
216 for (String skipStart : new String[] { "CREATION_DATE=",
217 "SUBJECT=", "URL=", "UUID=" }) {
218 if (name.endsWith(".info")
219 && expectedLine.startsWith(skipStart)
220 && resultLine.startsWith(skipStart)) {
221 // TODO: check the format?
222 skip = true;
223 }
224 }
225
226 if (skip) {
227 continue;
228 }
229
230 testCase.assertEquals("Line " + (j + 1) + " (" + sourceType
231 + ") is not the same in file " + name,
232 expectedLine, resultLine);
233 }
234 }
235 }
236 }
237
238 private static void unzip(File zipFile, File targetDirectory)
239 throws IOException {
240 if (targetDirectory.exists() && targetDirectory.isFile()) {
241 throw new IOException("Cannot unzip " + zipFile + " into "
242 + targetDirectory + ": it is not a directory");
243 }
244
245 targetDirectory.mkdir();
246 if (!targetDirectory.exists()) {
247 throw new IOException("Cannot create target directory "
248 + targetDirectory);
249 }
250
251 FileInputStream in = new FileInputStream(zipFile);
252 try {
253 ZipInputStream zipStream = new ZipInputStream(in);
254 try {
255 for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream
256 .getNextEntry()) {
257 File file = new File(targetDirectory, entry.getName());
258 if (entry.isDirectory()) {
259 file.mkdirs();
260 } else {
261 IOUtils.write(zipStream, file);
262 }
263 }
264 } finally {
265 zipStream.close();
266 }
267 } finally {
268 in.close();
269 }
270 }
271 }