| 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; |
| 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 | } |
| 185 | |
| 186 | testCase.assertEquals(errMess, expectedFiles, resultFiles); |
| 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 | |
| 192 | testCase.assertEquals(errMess + ": type mismatch: expected a " |
| 193 | + (expected.isDirectory() ? "directory" : "file") |
| 194 | + ", received a " |
| 195 | + (result.isDirectory() ? "directory" : "file"), |
| 196 | expected.isDirectory(), result.isDirectory()); |
| 197 | |
| 198 | if (expected.isDirectory()) { |
| 199 | compareFiles(testCase, expected, result, null, errMess); |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | if (expected.getName().endsWith(".cbz") |
| 204 | || expected.getName().endsWith(".epub")) { |
| 205 | File tmpExpected = Test.tempFiles.createTempDir(expected |
| 206 | .getName() + "[zip-content]"); |
| 207 | File tmpResult = Test.tempFiles.createTempDir(result.getName() |
| 208 | + "[zip-content]"); |
| 209 | IOUtils.unzip(expected, tmpExpected); |
| 210 | IOUtils.unzip(result, tmpResult); |
| 211 | compareFiles(testCase, tmpExpected, tmpResult, null, errMess); |
| 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 | } |
| 224 | |
| 225 | testCase.assertEquals(errMess + ": " + name |
| 226 | + ": the number of lines is not the same", |
| 227 | expectedLines.size(), resultLines.size()); |
| 228 | |
| 229 | for (int j = 0; j < expectedLines.size(); j++) { |
| 230 | String expectedLine = expectedLines.get(j); |
| 231 | String resultLine = resultLines.get(j); |
| 232 | |
| 233 | boolean skip = false; |
| 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 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (skip) { |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | testCase.assertEquals(errMess + ": line " + (j + 1) |
| 250 | + " is not the same in file " + name, expectedLine, |
| 251 | resultLine); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |