Small fixes:
[nikiroo-utils.git] / src / be / nikiroo / fanfix / 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 File testFile;
22 private File expectedDir;
23 private File resultDir;
24 private List<BasicOutput.OutputType> realTypes;
25 private Map<String, List<String>> skipCompare;
26
27 public ConversionTest(String[] args) {
28 super("Conversion", args);
29
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
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
58 for (BasicOutput.OutputType type : realTypes) {
59 addTest(getTestFor(type));
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
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:/"));
77 }
78
79 @Override
80 protected void stop() throws Exception {
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:
92 compareFiles(this, expectedDir, resultDir, type, "Generate "
93 + type);
94
95 // LATEX not supported as input
96 if (BasicOutput.OutputType.LATEX.equals(type)) {
97 return;
98 }
99
100 // Cross-checks:
101 for (BasicOutput.OutputType crossType : realTypes) {
102 File crossDir = Test.tempFiles
103 .createTempDir("cross-result");
104 generate(this, target, crossDir, crossType);
105 compareFiles(this, resultDir, crossDir, crossType,
106 "Cross compare " + crossType + " generated from "
107 + type);
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,
162 File resultDir, final BasicOutput.OutputType limitTiFiles,
163 final String errMess) throws Exception {
164 FilenameFilter filter = null;
165 if (limitTiFiles != null) {
166 filter = new FilenameFilter() {
167 @Override
168 public boolean accept(File dir, String name) {
169 return name.toLowerCase().startsWith(
170 limitTiFiles.toString().toLowerCase());
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
180 testCase.assertEquals(errMess, expectedFiles, resultFiles);
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
186 testCase.assertEquals(errMess + ": type mismatch: expected a "
187 + (expected.isDirectory() ? "directory" : "file")
188 + ", received a "
189 + (result.isDirectory() ? "directory" : "file"),
190 expected.isDirectory(), result.isDirectory());
191
192 if (expected.isDirectory()) {
193 compareFiles(testCase, expected, result, null, errMess);
194 continue;
195 }
196
197 if (expected.getName().endsWith(".cbz")
198 || expected.getName().endsWith(".epub")) {
199 File tmpExpected = Test.tempFiles.createTempDir(expected
200 .getName() + "[zip-content]");
201 File tmpResult = Test.tempFiles.createTempDir(result.getName()
202 + "[zip-content]");
203 IOUtils.unzip(expected, tmpExpected);
204 IOUtils.unzip(result, tmpResult);
205 compareFiles(testCase, tmpExpected, tmpResult, null, errMess);
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 }
218
219 testCase.assertEquals(errMess + ": " + name
220 + ": the number of lines is not the same",
221 expectedLines.size(), resultLines.size());
222
223 for (int j = 0; j < expectedLines.size(); j++) {
224 String expectedLine = expectedLines.get(j);
225 String resultLine = resultLines.get(j);
226
227 boolean skip = false;
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 }
236 }
237 }
238
239 if (skip) {
240 continue;
241 }
242
243 testCase.assertEquals(errMess + ": line " + (j + 1)
244 + " is not the same in file " + name, expectedLine,
245 resultLine);
246 }
247 }
248 }
249 }
250 }