Merge branch 'master' into subtree
[nikiroo-utils.git] / test / ConversionTest.java
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.utils.IOUtils;
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 {
21 private String testUri;
22 private String expectedDir;
23 private String resultDir;
24 private List<BasicOutput.OutputType> realTypes;
25 private Map<String, List<String>> skipCompare;
26 private Map<String, List<String>> skipCompareCross;
27
28 public ConversionTest(String testName, final String testUri,
29 final String expectedDir, final String resultDir, String[] args) {
30 super("Conversion - " + testName, args);
31
32 this.testUri = testUri;
33 this.expectedDir = expectedDir;
34 this.resultDir = resultDir;
35
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
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 }
54
55 addTest(new TestCase("Assure directories exist") {
56 @Override
57 public void test() throws Exception {
58 new File(expectedDir).mkdirs();
59 new File(resultDir).mkdirs();
60 assertEquals("The Expected directory \"" + expectedDir
61 + "\" cannot be created", true,
62 new File(expectedDir).exists());
63 assertEquals("The Result directory \"" + resultDir
64 + "\" cannot be created", true,
65 new File(resultDir).exists());
66 }
67 });
68
69 for (BasicOutput.OutputType type : realTypes) {
70 addTest(getTestFor(type));
71 }
72 }
73
74 @Override
75 protected void start() throws Exception {
76 skipCompare = new HashMap<String, List<String>>();
77 skipCompareCross = new HashMap<String, List<String>>();
78
79 skipCompare.put("epb.ncx", Arrays.asList(
80 " <meta name=\"dtb:uid\" content=",
81 " <meta name=\"epub-creator\" content=\""));
82 skipCompare.put("epb.opf", Arrays.asList(" <dc:subject>",
83 " <dc:identifier id=\"BookId\" opf:scheme=\"URI\">"));
84 skipCompare.put(".info", Arrays.asList("CREATION_DATE=",
85 "URL=\"file:/", "UUID=EPUBCREATOR=\"", ""));
86 skipCompare.put("URL", Arrays.asList("file:/"));
87
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(""));
99 }
100
101 @Override
102 protected void stop() throws Exception {
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 {
109 File target = generate(this, testUri, new File(resultDir), type);
110 target = new File(target.getAbsolutePath()
111 + type.getDefaultExtension(false));
112
113 // Check conversion:
114 compareFiles(this, new File(expectedDir), new File(resultDir),
115 type, "Generate " + type);
116
117 // LATEX not supported as input
118 if (BasicOutput.OutputType.LATEX.equals(type)) {
119 return;
120 }
121
122 // Cross-checks:
123 for (BasicOutput.OutputType crossType : realTypes) {
124 File crossDir = Test.tempFiles
125 .createTempDir("cross-result");
126
127 generate(this, target.getAbsolutePath(), crossDir,
128 crossType);
129 compareFiles(this, new File(resultDir), crossDir,
130 crossType, "Cross compare " + crossType
131 + " generated from " + type);
132 }
133 }
134 };
135 }
136
137 private File generate(TestCase testCase, String testUri, File resultDir,
138 BasicOutput.OutputType type) throws Exception {
139 final List<String> errors = new ArrayList<String>();
140
141 TraceHandler previousTraceHandler = Instance.getInstance().getTraceHandler();
142 Instance.getInstance().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());
164 int code = Main.convert(testUri, type.toString(),
165 target.getAbsolutePath(), false, null);
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.getInstance().setTraceHandler(previousTraceHandler);
182 }
183 }
184
185 private void compareFiles(TestCase testCase, File expectedDir,
186 File resultDir, final BasicOutput.OutputType limitTiFiles,
187 final String errMess) throws Exception {
188
189 Map<String, List<String>> skipCompare = errMess.startsWith("Cross") ? this.skipCompareCross
190 : this.skipCompare;
191
192 FilenameFilter filter = null;
193 if (limitTiFiles != null) {
194 filter = new FilenameFilter() {
195 @Override
196 public boolean accept(File dir, String name) {
197 return name.toLowerCase().startsWith(
198 limitTiFiles.toString().toLowerCase());
199 }
200 };
201 }
202
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 }
213
214 testCase.assertEquals(errMess, expectedFiles, resultFiles);
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
220 testCase.assertEquals(errMess + ": type mismatch: expected a "
221 + (expected.isDirectory() ? "directory" : "file")
222 + ", received a "
223 + (result.isDirectory() ? "directory" : "file"),
224 expected.isDirectory(), result.isDirectory());
225
226 if (expected.isDirectory()) {
227 compareFiles(testCase, expected, result, null, errMess);
228 continue;
229 }
230
231 if (expected.getName().endsWith(".cbz")
232 || expected.getName().endsWith(".epub")) {
233 File tmpExpected = Test.tempFiles.createTempDir(expected
234 .getName() + "[zip-content]");
235 File tmpResult = Test.tempFiles.createTempDir(result.getName()
236 + "[zip-content]");
237 IOUtils.unzip(expected, tmpExpected);
238 IOUtils.unzip(result, tmpResult);
239 compareFiles(testCase, tmpExpected, tmpResult, null, errMess);
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 }
252
253 testCase.assertEquals(errMess + ": " + name
254 + ": the number of lines is not the same",
255 expectedLines.size(), resultLines.size());
256
257 for (int j = 0; j < expectedLines.size(); j++) {
258 String expectedLine = expectedLines.get(j);
259 String resultLine = resultLines.get(j);
260
261 boolean skip = false;
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 }
270 }
271 }
272
273 if (skip) {
274 continue;
275 }
276
277 testCase.assertEquals(errMess + ": line " + (j + 1)
278 + " is not the same in file " + name, expectedLine,
279 resultLine);
280 }
281 }
282 }
283 }
284 }