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