Commit | Line | Data |
---|---|---|
f7460e4c NR |
1 | package be.nikiroo.fanfix.test; |
2 | ||
3 | import java.io.File; | |
f7460e4c | 4 | import java.io.FilenameFilter; |
f7460e4c NR |
5 | import java.util.ArrayList; |
6 | import java.util.Arrays; | |
c2f96f37 | 7 | import java.util.HashMap; |
f7460e4c | 8 | import java.util.List; |
c2f96f37 NR |
9 | import java.util.Map; |
10 | import java.util.Map.Entry; | |
f7460e4c NR |
11 | |
12 | import be.nikiroo.fanfix.Instance; | |
13 | import be.nikiroo.fanfix.Main; | |
14 | import be.nikiroo.fanfix.output.BasicOutput; | |
2ef95142 | 15 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
f7460e4c | 16 | import be.nikiroo.utils.IOUtils; |
2ef95142 | 17 | import be.nikiroo.utils.Progress; |
f7460e4c NR |
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 { | |
2ef95142 NR |
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 | ||
dabc4b95 NR |
31 | private String testUri; |
32 | private String expectedDir; | |
33 | private String resultDir; | |
86d49dbc | 34 | private List<BasicOutput.OutputType> realTypes; |
c2f96f37 | 35 | private Map<String, List<String>> skipCompare; |
06befaee | 36 | private Map<String, List<String>> skipCompareCross; |
f7460e4c | 37 | |
06befaee NR |
38 | public ConversionTest(String testName, final String testUri, |
39 | final String expectedDir, final String resultDir, String[] args) { | |
40 | super("Conversion - " + testName, args); | |
f7460e4c | 41 | |
dabc4b95 NR |
42 | this.testUri = testUri; |
43 | this.expectedDir = expectedDir; | |
44 | this.resultDir = resultDir; | |
45 | ||
86d49dbc NR |
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 | ||
06befaee NR |
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 | } | |
f7460e4c NR |
64 | |
65 | addTest(new TestCase("Assure directories exist") { | |
66 | @Override | |
67 | public void test() throws Exception { | |
dabc4b95 NR |
68 | new File(expectedDir).mkdirs(); |
69 | new File(resultDir).mkdirs(); | |
f7460e4c | 70 | assertEquals("The Expected directory \"" + expectedDir |
dabc4b95 NR |
71 | + "\" cannot be created", true, |
72 | new File(expectedDir).exists()); | |
f7460e4c | 73 | assertEquals("The Result directory \"" + resultDir |
dabc4b95 NR |
74 | + "\" cannot be created", true, |
75 | new File(resultDir).exists()); | |
f7460e4c NR |
76 | } |
77 | }); | |
78 | ||
86d49dbc NR |
79 | for (BasicOutput.OutputType type : realTypes) { |
80 | addTest(getTestFor(type)); | |
f7460e4c NR |
81 | } |
82 | } | |
83 | ||
84 | @Override | |
85 | protected void start() throws Exception { | |
c2f96f37 | 86 | skipCompare = new HashMap<String, List<String>>(); |
06befaee | 87 | skipCompareCross = new HashMap<String, List<String>>(); |
32bcfff4 NR |
88 | |
89 | skipCompare.put("epb.ncx", Arrays.asList( | |
90 | " <meta name=\"dtb:uid\" content=", | |
91 | " <meta name=\"epub-creator\" content=\"")); | |
c2f96f37 NR |
92 | skipCompare.put("epb.opf", Arrays.asList(" <dc:subject>", |
93 | " <dc:identifier id=\"BookId\" opf:scheme=\"URI\">")); | |
32bcfff4 NR |
94 | skipCompare.put(".info", Arrays.asList("CREATION_DATE=", |
95 | "URL=\"file:/", "UUID=EPUBCREATOR=\"", "")); | |
c2f96f37 | 96 | skipCompare.put("URL", Arrays.asList("file:/")); |
32bcfff4 | 97 | |
06befaee NR |
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("")); | |
f7460e4c NR |
109 | } |
110 | ||
111 | @Override | |
112 | protected void stop() throws Exception { | |
f7460e4c NR |
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 { | |
dabc4b95 | 119 | File target = generate(this, testUri, new File(resultDir), type); |
f7460e4c NR |
120 | target = new File(target.getAbsolutePath() |
121 | + type.getDefaultExtension(false)); | |
122 | ||
123 | // Check conversion: | |
dabc4b95 NR |
124 | compareFiles(this, new File(expectedDir), new File(resultDir), |
125 | type, "Generate " + type); | |
f7460e4c | 126 | |
da5f6fc9 | 127 | // LATEX not supported as input |
86d49dbc NR |
128 | if (BasicOutput.OutputType.LATEX.equals(type)) { |
129 | return; | |
130 | } | |
131 | ||
132 | // Cross-checks: | |
133 | for (BasicOutput.OutputType crossType : realTypes) { | |
2aac79c7 NR |
134 | File crossDir = Test.tempFiles |
135 | .createTempDir("cross-result"); | |
06befaee | 136 | |
dabc4b95 NR |
137 | generate(this, target.getAbsolutePath(), crossDir, |
138 | crossType); | |
139 | compareFiles(this, new File(resultDir), crossDir, | |
140 | crossType, "Cross compare " + crossType | |
141 | + " generated from " + type); | |
f7460e4c NR |
142 | } |
143 | } | |
144 | }; | |
145 | } | |
146 | ||
dabc4b95 | 147 | private File generate(TestCase testCase, String testUri, File resultDir, |
f7460e4c NR |
148 | BasicOutput.OutputType type) throws Exception { |
149 | final List<String> errors = new ArrayList<String>(); | |
150 | ||
d66deb8d NR |
151 | TraceHandler previousTraceHandler = Instance.getInstance().getTraceHandler(); |
152 | Instance.getInstance().setTraceHandler(new TraceHandler(true, true, 0) { | |
f7460e4c NR |
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()); | |
2ef95142 | 174 | int code = new MainWithConvert().convert(testUri, type, |
dabc4b95 | 175 | target.getAbsolutePath(), false, null); |
f7460e4c NR |
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 { | |
d66deb8d | 191 | Instance.getInstance().setTraceHandler(previousTraceHandler); |
f7460e4c NR |
192 | } |
193 | } | |
194 | ||
195 | private void compareFiles(TestCase testCase, File expectedDir, | |
3cc0bacc NR |
196 | File resultDir, final BasicOutput.OutputType limitTiFiles, |
197 | final String errMess) throws Exception { | |
06befaee NR |
198 | |
199 | Map<String, List<String>> skipCompare = errMess.startsWith("Cross") ? this.skipCompareCross | |
200 | : this.skipCompare; | |
201 | ||
f7460e4c | 202 | FilenameFilter filter = null; |
3cc0bacc | 203 | if (limitTiFiles != null) { |
f7460e4c NR |
204 | filter = new FilenameFilter() { |
205 | @Override | |
206 | public boolean accept(File dir, String name) { | |
3cc0bacc NR |
207 | return name.toLowerCase().startsWith( |
208 | limitTiFiles.toString().toLowerCase()); | |
f7460e4c NR |
209 | } |
210 | }; | |
211 | } | |
212 | ||
11758a0f N |
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 | } | |
8958119f | 223 | |
3cc0bacc | 224 | testCase.assertEquals(errMess, expectedFiles, resultFiles); |
f7460e4c NR |
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 | ||
3cc0bacc NR |
230 | testCase.assertEquals(errMess + ": type mismatch: expected a " |
231 | + (expected.isDirectory() ? "directory" : "file") | |
232 | + ", received a " | |
233 | + (result.isDirectory() ? "directory" : "file"), | |
f7460e4c NR |
234 | expected.isDirectory(), result.isDirectory()); |
235 | ||
236 | if (expected.isDirectory()) { | |
3cc0bacc | 237 | compareFiles(testCase, expected, result, null, errMess); |
f7460e4c NR |
238 | continue; |
239 | } | |
240 | ||
241 | if (expected.getName().endsWith(".cbz") | |
242 | || expected.getName().endsWith(".epub")) { | |
2aac79c7 NR |
243 | File tmpExpected = Test.tempFiles.createTempDir(expected |
244 | .getName() + "[zip-content]"); | |
245 | File tmpResult = Test.tempFiles.createTempDir(result.getName() | |
f7460e4c | 246 | + "[zip-content]"); |
2aac79c7 NR |
247 | IOUtils.unzip(expected, tmpExpected); |
248 | IOUtils.unzip(result, tmpResult); | |
3cc0bacc | 249 | compareFiles(testCase, tmpExpected, tmpResult, null, errMess); |
f7460e4c NR |
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 | } | |
2aac79c7 NR |
262 | |
263 | testCase.assertEquals(errMess + ": " + name | |
264 | + ": the number of lines is not the same", | |
265 | expectedLines.size(), resultLines.size()); | |
266 | ||
f7460e4c NR |
267 | for (int j = 0; j < expectedLines.size(); j++) { |
268 | String expectedLine = expectedLines.get(j); | |
269 | String resultLine = resultLines.get(j); | |
da5f6fc9 NR |
270 | |
271 | boolean skip = false; | |
c2f96f37 NR |
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 | } | |
da5f6fc9 NR |
280 | } |
281 | } | |
282 | ||
283 | if (skip) { | |
f7460e4c NR |
284 | continue; |
285 | } | |
da5f6fc9 | 286 | |
3cc0bacc NR |
287 | testCase.assertEquals(errMess + ": line " + (j + 1) |
288 | + " is not the same in file " + name, expectedLine, | |
289 | resultLine); | |
f7460e4c NR |
290 | } |
291 | } | |
292 | } | |
293 | } | |
f7460e4c | 294 | } |