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