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