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