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