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