Merge branch 'master' of github.com:nikiroo/fanfix
[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 {
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
11758a0f
N
175 List<String> resultFiles;
176 List<String> expectedFiles;
177 {
178 String[] resultArr = resultDir.list(filter);
179 Arrays.sort(resultArr);
180 resultFiles = Arrays.asList(resultArr);
181 String[] expectedArr = expectedDir.list(filter);
182 Arrays.sort(expectedArr);
183 expectedFiles = Arrays.asList(expectedArr);
184 }
8958119f 185
3cc0bacc 186 testCase.assertEquals(errMess, expectedFiles, resultFiles);
f7460e4c
NR
187
188 for (int i = 0; i < resultFiles.size(); i++) {
189 File expected = new File(expectedDir, expectedFiles.get(i));
190 File result = new File(resultDir, resultFiles.get(i));
191
3cc0bacc
NR
192 testCase.assertEquals(errMess + ": type mismatch: expected a "
193 + (expected.isDirectory() ? "directory" : "file")
194 + ", received a "
195 + (result.isDirectory() ? "directory" : "file"),
f7460e4c
NR
196 expected.isDirectory(), result.isDirectory());
197
198 if (expected.isDirectory()) {
3cc0bacc 199 compareFiles(testCase, expected, result, null, errMess);
f7460e4c
NR
200 continue;
201 }
202
203 if (expected.getName().endsWith(".cbz")
204 || expected.getName().endsWith(".epub")) {
2aac79c7
NR
205 File tmpExpected = Test.tempFiles.createTempDir(expected
206 .getName() + "[zip-content]");
207 File tmpResult = Test.tempFiles.createTempDir(result.getName()
f7460e4c 208 + "[zip-content]");
2aac79c7
NR
209 IOUtils.unzip(expected, tmpExpected);
210 IOUtils.unzip(result, tmpResult);
3cc0bacc 211 compareFiles(testCase, tmpExpected, tmpResult, null, errMess);
f7460e4c
NR
212 } else {
213 List<String> expectedLines = Arrays.asList(IOUtils
214 .readSmallFile(expected).split("\n"));
215 List<String> resultLines = Arrays.asList(IOUtils.readSmallFile(
216 result).split("\n"));
217
218 String name = expected.getAbsolutePath();
219 if (name.startsWith(expectedDir.getAbsolutePath())) {
220 name = expectedDir.getName()
221 + name.substring(expectedDir.getAbsolutePath()
222 .length());
223 }
2aac79c7
NR
224
225 testCase.assertEquals(errMess + ": " + name
226 + ": the number of lines is not the same",
227 expectedLines.size(), resultLines.size());
228
f7460e4c
NR
229 for (int j = 0; j < expectedLines.size(); j++) {
230 String expectedLine = expectedLines.get(j);
231 String resultLine = resultLines.get(j);
da5f6fc9
NR
232
233 boolean skip = false;
c2f96f37
NR
234 for (Entry<String, List<String>> skipThose : skipCompare
235 .entrySet()) {
236 for (String skipStart : skipThose.getValue()) {
237 if (name.endsWith(skipThose.getKey())
238 && expectedLine.startsWith(skipStart)
239 && resultLine.startsWith(skipStart)) {
240 skip = true;
241 }
da5f6fc9
NR
242 }
243 }
244
245 if (skip) {
f7460e4c
NR
246 continue;
247 }
da5f6fc9 248
3cc0bacc
NR
249 testCase.assertEquals(errMess + ": line " + (j + 1)
250 + " is not the same in file " + name, expectedLine,
251 resultLine);
f7460e4c
NR
252 }
253 }
254 }
255 }
f7460e4c 256}