Commit | Line | Data |
---|---|---|
9a553563 NR |
1 | package be.nikiroo.fanfix.test; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.FilenameFilter; | |
5 | import java.util.ArrayList; | |
6 | import java.util.Arrays; | |
7 | import java.util.HashMap; | |
8 | import java.util.List; | |
9 | import java.util.Map; | |
10 | import java.util.Map.Entry; | |
11 | ||
12 | import be.nikiroo.fanfix.Instance; | |
13 | import be.nikiroo.fanfix.Main; | |
14 | import be.nikiroo.fanfix.output.BasicOutput; | |
15 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
16 | import be.nikiroo.utils.IOUtils; | |
17 | import be.nikiroo.utils.Progress; | |
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 class MainWithConvert extends Main { | |
24 | @Override | |
25 | public int convert(String urlString, OutputType type, String target, | |
26 | boolean infoCover, Progress pg) { | |
27 | return super.convert(urlString, type, target, infoCover, pg); | |
28 | } | |
29 | } | |
30 | ||
31 | private String testUri; | |
32 | private String expectedDir; | |
33 | private String resultDir; | |
34 | private List<BasicOutput.OutputType> realTypes; | |
35 | private Map<String, List<String>> skipCompare; | |
36 | private Map<String, List<String>> skipCompareCross; | |
37 | ||
38 | public ConversionTest(String testName, final String testUri, | |
39 | final String expectedDir, final String resultDir, String[] args) { | |
40 | super("Conversion - " + testName, args); | |
41 | ||
42 | this.testUri = testUri; | |
43 | this.expectedDir = expectedDir; | |
44 | this.resultDir = resultDir; | |
45 | ||
46 | // Special mode SYSOUT is not a file type (System.out) | |
47 | realTypes = new ArrayList<BasicOutput.OutputType>(); | |
48 | for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) { | |
49 | if (!BasicOutput.OutputType.SYSOUT.equals(type)) { | |
50 | realTypes.add(type); | |
51 | } | |
52 | } | |
53 | ||
54 | if (!testUri.startsWith("http://") && !testUri.startsWith("https://")) { | |
55 | addTest(new TestCase("Read the test file") { | |
56 | @Override | |
57 | public void test() throws Exception { | |
58 | assertEquals("The test file \"" + testUri | |
59 | + "\" cannot be found", true, | |
60 | new File(testUri).exists()); | |
61 | } | |
62 | }); | |
63 | } | |
64 | ||
65 | addTest(new TestCase("Assure directories exist") { | |
66 | @Override | |
67 | public void test() throws Exception { | |
68 | new File(expectedDir).mkdirs(); | |
69 | new File(resultDir).mkdirs(); | |
70 | assertEquals("The Expected directory \"" + expectedDir | |
71 | + "\" cannot be created", true, | |
72 | new File(expectedDir).exists()); | |
73 | assertEquals("The Result directory \"" + resultDir | |
74 | + "\" cannot be created", true, | |
75 | new File(resultDir).exists()); | |
76 | } | |
77 | }); | |
78 | ||
79 | for (BasicOutput.OutputType type : realTypes) { | |
80 | addTest(getTestFor(type)); | |
81 | } | |
82 | } | |
83 | ||
84 | @Override | |
85 | protected void start() throws Exception { | |
86 | skipCompare = new HashMap<String, List<String>>(); | |
87 | skipCompareCross = new HashMap<String, List<String>>(); | |
88 | ||
89 | skipCompare.put("epb.ncx", Arrays.asList( | |
90 | " <meta name=\"dtb:uid\" content=", | |
91 | " <meta name=\"epub-creator\" content=\"")); | |
92 | skipCompare.put("epb.opf", Arrays.asList(" <dc:subject>", | |
93 | " <dc:identifier id=\"BookId\" opf:scheme=\"URI\">")); | |
94 | skipCompare.put(".info", Arrays.asList("CREATION_DATE=", | |
95 | "URL=\"file:/", "UUID=EPUBCREATOR=\"", "")); | |
96 | skipCompare.put("URL", Arrays.asList("file:/")); | |
97 | ||
98 | for (String key : skipCompare.keySet()) { | |
99 | skipCompareCross.put(key, skipCompare.get(key)); | |
100 | } | |
101 | ||
102 | skipCompareCross.put(".info", Arrays.asList("")); | |
103 | skipCompareCross.put("epb.opf", Arrays.asList(" <dc:")); | |
104 | skipCompareCross.put("title.xhtml", | |
105 | Arrays.asList(" <div class=\"type\">")); | |
106 | skipCompareCross.put("index.html", | |
107 | Arrays.asList(" <div class=\"type\">")); | |
108 | skipCompareCross.put("URL", Arrays.asList("")); | |
109 | } | |
110 | ||
111 | @Override | |
112 | protected void stop() throws Exception { | |
113 | } | |
114 | ||
115 | private TestCase getTestFor(final BasicOutput.OutputType type) { | |
116 | return new TestCase(type + " output mode") { | |
117 | @Override | |
118 | public void test() throws Exception { | |
119 | File target = generate(this, testUri, new File(resultDir), type); | |
120 | target = new File(target.getAbsolutePath() | |
121 | + type.getDefaultExtension(false)); | |
122 | ||
123 | // Check conversion: | |
124 | compareFiles(this, new File(expectedDir), new File(resultDir), | |
125 | type, "Generate " + type); | |
126 | ||
127 | // LATEX not supported as input | |
128 | if (BasicOutput.OutputType.LATEX.equals(type)) { | |
129 | return; | |
130 | } | |
131 | ||
132 | // Cross-checks: | |
133 | for (BasicOutput.OutputType crossType : realTypes) { | |
134 | File crossDir = Test.tempFiles | |
135 | .createTempDir("cross-result"); | |
136 | ||
137 | generate(this, target.getAbsolutePath(), crossDir, | |
138 | crossType); | |
139 | compareFiles(this, new File(resultDir), crossDir, | |
140 | crossType, "Cross compare " + crossType | |
141 | + " generated from " + type); | |
142 | } | |
143 | } | |
144 | }; | |
145 | } | |
146 | ||
147 | private File generate(TestCase testCase, String testUri, File resultDir, | |
148 | BasicOutput.OutputType type) throws Exception { | |
149 | final List<String> errors = new ArrayList<String>(); | |
150 | ||
151 | TraceHandler previousTraceHandler = Instance.getInstance().getTraceHandler(); | |
152 | Instance.getInstance().setTraceHandler(new TraceHandler(true, true, 0) { | |
153 | @Override | |
154 | public void error(String message) { | |
155 | errors.add(message); | |
156 | } | |
157 | ||
158 | @Override | |
159 | public void error(Exception e) { | |
160 | error(" "); | |
161 | for (Throwable t = e; t != null; t = t.getCause()) { | |
162 | error(((t == e) ? "(" : "..caused by: (") | |
163 | + t.getClass().getSimpleName() + ") " | |
164 | + t.getMessage()); | |
165 | for (StackTraceElement s : t.getStackTrace()) { | |
166 | error("\t" + s.toString()); | |
167 | } | |
168 | } | |
169 | } | |
170 | }); | |
171 | ||
172 | try { | |
173 | File target = new File(resultDir, type.toString()); | |
174 | int code = new MainWithConvert().convert(testUri, type, | |
175 | target.getAbsolutePath(), false, null); | |
176 | ||
177 | String error = ""; | |
178 | for (String err : errors) { | |
179 | if (!error.isEmpty()) | |
180 | error += "\n"; | |
181 | error += err; | |
182 | } | |
183 | testCase.assertEquals("The conversion returned an error message: " | |
184 | + error, 0, errors.size()); | |
185 | if (code != 0) { | |
186 | testCase.fail("The conversion failed with return code: " + code); | |
187 | } | |
188 | ||
189 | return target; | |
190 | } finally { | |
191 | Instance.getInstance().setTraceHandler(previousTraceHandler); | |
192 | } | |
193 | } | |
194 | ||
195 | private void compareFiles(TestCase testCase, File expectedDir, | |
196 | File resultDir, final BasicOutput.OutputType limitTiFiles, | |
197 | final String errMess) throws Exception { | |
198 | ||
199 | Map<String, List<String>> skipCompare = errMess.startsWith("Cross") ? this.skipCompareCross | |
200 | : this.skipCompare; | |
201 | ||
202 | FilenameFilter filter = null; | |
203 | if (limitTiFiles != null) { | |
204 | filter = new FilenameFilter() { | |
205 | @Override | |
206 | public boolean accept(File dir, String name) { | |
207 | return name.toLowerCase().startsWith( | |
208 | limitTiFiles.toString().toLowerCase()); | |
209 | } | |
210 | }; | |
211 | } | |
212 | ||
213 | List<String> resultFiles; | |
214 | List<String> expectedFiles; | |
215 | { | |
216 | String[] resultArr = resultDir.list(filter); | |
217 | Arrays.sort(resultArr); | |
218 | resultFiles = Arrays.asList(resultArr); | |
219 | String[] expectedArr = expectedDir.list(filter); | |
220 | Arrays.sort(expectedArr); | |
221 | expectedFiles = Arrays.asList(expectedArr); | |
222 | } | |
223 | ||
224 | testCase.assertEquals(errMess, expectedFiles, resultFiles); | |
225 | ||
226 | for (int i = 0; i < resultFiles.size(); i++) { | |
227 | File expected = new File(expectedDir, expectedFiles.get(i)); | |
228 | File result = new File(resultDir, resultFiles.get(i)); | |
229 | ||
230 | testCase.assertEquals(errMess + ": type mismatch: expected a " | |
231 | + (expected.isDirectory() ? "directory" : "file") | |
232 | + ", received a " | |
233 | + (result.isDirectory() ? "directory" : "file"), | |
234 | expected.isDirectory(), result.isDirectory()); | |
235 | ||
236 | if (expected.isDirectory()) { | |
237 | compareFiles(testCase, expected, result, null, errMess); | |
238 | continue; | |
239 | } | |
240 | ||
241 | if (expected.getName().endsWith(".cbz") | |
242 | || expected.getName().endsWith(".epub")) { | |
243 | File tmpExpected = Test.tempFiles.createTempDir(expected | |
244 | .getName() + "[zip-content]"); | |
245 | File tmpResult = Test.tempFiles.createTempDir(result.getName() | |
246 | + "[zip-content]"); | |
247 | IOUtils.unzip(expected, tmpExpected); | |
248 | IOUtils.unzip(result, tmpResult); | |
249 | compareFiles(testCase, tmpExpected, tmpResult, null, errMess); | |
250 | } else { | |
251 | List<String> expectedLines = Arrays.asList(IOUtils | |
252 | .readSmallFile(expected).split("\n")); | |
253 | List<String> resultLines = Arrays.asList(IOUtils.readSmallFile( | |
254 | result).split("\n")); | |
255 | ||
256 | String name = expected.getAbsolutePath(); | |
257 | if (name.startsWith(expectedDir.getAbsolutePath())) { | |
258 | name = expectedDir.getName() | |
259 | + name.substring(expectedDir.getAbsolutePath() | |
260 | .length()); | |
261 | } | |
262 | ||
263 | testCase.assertEquals(errMess + ": " + name | |
264 | + ": the number of lines is not the same", | |
265 | expectedLines.size(), resultLines.size()); | |
266 | ||
267 | for (int j = 0; j < expectedLines.size(); j++) { | |
268 | String expectedLine = expectedLines.get(j); | |
269 | String resultLine = resultLines.get(j); | |
270 | ||
271 | boolean skip = false; | |
272 | for (Entry<String, List<String>> skipThose : skipCompare | |
273 | .entrySet()) { | |
274 | for (String skipStart : skipThose.getValue()) { | |
275 | if (name.endsWith(skipThose.getKey()) | |
276 | && expectedLine.startsWith(skipStart) | |
277 | && resultLine.startsWith(skipStart)) { | |
278 | skip = true; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | if (skip) { | |
284 | continue; | |
285 | } | |
286 | ||
287 | testCase.assertEquals(errMess + ": line " + (j + 1) | |
288 | + " is not the same in file " + name, expectedLine, | |
289 | resultLine); | |
290 | } | |
291 | } | |
292 | } | |
293 | } | |
294 | } |