1 package be
.nikiroo
.fanfix
.test
;
4 import java
.io
.FilenameFilter
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
7 import java
.util
.HashMap
;
10 import java
.util
.Map
.Entry
;
12 import be
.nikiroo
.fanfix
.Instance
;
13 import be
.nikiroo
.fanfix
.Main
;
14 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
16 import be
.nikiroo
.utils
.IOUtils
;
17 import be
.nikiroo
.utils
.Progress
;
18 import be
.nikiroo
.utils
.TraceHandler
;
19 import be
.nikiroo
.utils
.test
.TestCase
;
20 import be
.nikiroo
.utils
.test
.TestLauncher
;
22 class ConversionTest
extends TestLauncher
{
23 private class MainWithConvert
extends Main
{
25 public int convert(String urlString
, OutputType type
, String target
,
26 boolean infoCover
, Progress pg
) {
27 return super.convert(urlString
, type
, target
, infoCover
, pg
);
31 private String testUri
;
32 private String expectedDir
;
33 private String resultDir
;
34 private List
<BasicOutput
.OutputType
> realTypes
;
35 private Map
<String
, List
<String
>> skipCompare
;
36 private Map
<String
, List
<String
>> skipCompareCross
;
38 public ConversionTest(String testName
, final String testUri
,
39 final String expectedDir
, final String resultDir
, String
[] args
) {
40 super("Conversion - " + testName
, args
);
42 this.testUri
= testUri
;
43 this.expectedDir
= expectedDir
;
44 this.resultDir
= resultDir
;
46 // Special mode SYSOUT is not a file type (System.out)
47 realTypes
= new ArrayList
<BasicOutput
.OutputType
>();
48 for (BasicOutput
.OutputType type
: BasicOutput
.OutputType
.values()) {
49 if (!BasicOutput
.OutputType
.SYSOUT
.equals(type
)) {
54 if (!testUri
.startsWith("http://") && !testUri
.startsWith("https://")) {
55 addTest(new TestCase("Read the test file") {
57 public void test() throws Exception
{
58 assertEquals("The test file \"" + testUri
59 + "\" cannot be found", true,
60 new File(testUri
).exists());
65 addTest(new TestCase("Assure directories exist") {
67 public void test() throws Exception
{
68 new File(expectedDir
).mkdirs();
69 new File(resultDir
).mkdirs();
70 assertEquals("The Expected directory \"" + expectedDir
71 + "\" cannot be created", true,
72 new File(expectedDir
).exists());
73 assertEquals("The Result directory \"" + resultDir
74 + "\" cannot be created", true,
75 new File(resultDir
).exists());
79 for (BasicOutput
.OutputType type
: realTypes
) {
80 addTest(getTestFor(type
));
85 protected void start() throws Exception
{
86 skipCompare
= new HashMap
<String
, List
<String
>>();
87 skipCompareCross
= new HashMap
<String
, List
<String
>>();
89 skipCompare
.put("epb.ncx", Arrays
.asList(
90 " <meta name=\"dtb:uid\" content=",
91 " <meta name=\"epub-creator\" content=\""));
92 skipCompare
.put("epb.opf", Arrays
.asList(" <dc:subject>",
93 " <dc:identifier id=\"BookId\" opf:scheme=\"URI\">"));
94 skipCompare
.put(".info", Arrays
.asList("CREATION_DATE=",
95 "URL=\"file:/", "UUID=EPUBCREATOR=\"", ""));
96 skipCompare
.put("URL", Arrays
.asList("file:/"));
98 for (String key
: skipCompare
.keySet()) {
99 skipCompareCross
.put(key
, skipCompare
.get(key
));
102 skipCompareCross
.put(".info", Arrays
.asList(""));
103 skipCompareCross
.put("epb.opf", Arrays
.asList(" <dc:"));
104 skipCompareCross
.put("title.xhtml",
105 Arrays
.asList(" <div class=\"type\">"));
106 skipCompareCross
.put("index.html",
107 Arrays
.asList(" <div class=\"type\">"));
108 skipCompareCross
.put("URL", Arrays
.asList(""));
112 protected void stop() throws Exception
{
115 private TestCase
getTestFor(final BasicOutput
.OutputType type
) {
116 return new TestCase(type
+ " output mode") {
118 public void test() throws Exception
{
119 File target
= generate(this, testUri
, new File(resultDir
), type
);
120 target
= new File(target
.getAbsolutePath()
121 + type
.getDefaultExtension(false));
124 compareFiles(this, new File(expectedDir
), new File(resultDir
),
125 type
, "Generate " + type
);
127 // LATEX not supported as input
128 if (BasicOutput
.OutputType
.LATEX
.equals(type
)) {
133 for (BasicOutput
.OutputType crossType
: realTypes
) {
134 File crossDir
= Test
.tempFiles
135 .createTempDir("cross-result");
137 generate(this, target
.getAbsolutePath(), crossDir
,
139 compareFiles(this, new File(resultDir
), crossDir
,
140 crossType
, "Cross compare " + crossType
141 + " generated from " + type
);
147 private File
generate(TestCase testCase
, String testUri
, File resultDir
,
148 BasicOutput
.OutputType type
) throws Exception
{
149 final List
<String
> errors
= new ArrayList
<String
>();
151 TraceHandler previousTraceHandler
= Instance
.getInstance().getTraceHandler();
152 Instance
.getInstance().setTraceHandler(new TraceHandler(true, true, 0) {
154 public void error(String message
) {
159 public void error(Exception e
) {
161 for (Throwable t
= e
; t
!= null; t
= t
.getCause()) {
162 error(((t
== e
) ?
"(" : "..caused by: (")
163 + t
.getClass().getSimpleName() + ") "
165 for (StackTraceElement s
: t
.getStackTrace()) {
166 error("\t" + s
.toString());
173 File target
= new File(resultDir
, type
.toString());
174 int code
= new MainWithConvert().convert(testUri
, type
,
175 target
.getAbsolutePath(), false, null);
178 for (String err
: errors
) {
179 if (!error
.isEmpty())
183 testCase
.assertEquals("The conversion returned an error message: "
184 + error
, 0, errors
.size());
186 testCase
.fail("The conversion failed with return code: " + code
);
191 Instance
.getInstance().setTraceHandler(previousTraceHandler
);
195 private void compareFiles(TestCase testCase
, File expectedDir
,
196 File resultDir
, final BasicOutput
.OutputType limitTiFiles
,
197 final String errMess
) throws Exception
{
199 Map
<String
, List
<String
>> skipCompare
= errMess
.startsWith("Cross") ?
this.skipCompareCross
202 FilenameFilter filter
= null;
203 if (limitTiFiles
!= null) {
204 filter
= new FilenameFilter() {
206 public boolean accept(File dir
, String name
) {
207 return name
.toLowerCase().startsWith(
208 limitTiFiles
.toString().toLowerCase());
213 List
<String
> resultFiles
;
214 List
<String
> expectedFiles
;
216 String
[] resultArr
= resultDir
.list(filter
);
217 Arrays
.sort(resultArr
);
218 resultFiles
= Arrays
.asList(resultArr
);
219 String
[] expectedArr
= expectedDir
.list(filter
);
220 Arrays
.sort(expectedArr
);
221 expectedFiles
= Arrays
.asList(expectedArr
);
224 testCase
.assertEquals(errMess
, expectedFiles
, resultFiles
);
226 for (int i
= 0; i
< resultFiles
.size(); i
++) {
227 File expected
= new File(expectedDir
, expectedFiles
.get(i
));
228 File result
= new File(resultDir
, resultFiles
.get(i
));
230 testCase
.assertEquals(errMess
+ ": type mismatch: expected a "
231 + (expected
.isDirectory() ?
"directory" : "file")
233 + (result
.isDirectory() ?
"directory" : "file"),
234 expected
.isDirectory(), result
.isDirectory());
236 if (expected
.isDirectory()) {
237 compareFiles(testCase
, expected
, result
, null, errMess
);
241 if (expected
.getName().endsWith(".cbz")
242 || expected
.getName().endsWith(".epub")) {
243 File tmpExpected
= Test
.tempFiles
.createTempDir(expected
244 .getName() + "[zip-content]");
245 File tmpResult
= Test
.tempFiles
.createTempDir(result
.getName()
247 IOUtils
.unzip(expected
, tmpExpected
);
248 IOUtils
.unzip(result
, tmpResult
);
249 compareFiles(testCase
, tmpExpected
, tmpResult
, null, errMess
);
251 List
<String
> expectedLines
= Arrays
.asList(IOUtils
252 .readSmallFile(expected
).split("\n"));
253 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
254 result
).split("\n"));
256 String name
= expected
.getAbsolutePath();
257 if (name
.startsWith(expectedDir
.getAbsolutePath())) {
258 name
= expectedDir
.getName()
259 + name
.substring(expectedDir
.getAbsolutePath()
263 testCase
.assertEquals(errMess
+ ": " + name
264 + ": the number of lines is not the same",
265 expectedLines
.size(), resultLines
.size());
267 for (int j
= 0; j
< expectedLines
.size(); j
++) {
268 String expectedLine
= expectedLines
.get(j
);
269 String resultLine
= resultLines
.get(j
);
271 boolean skip
= false;
272 for (Entry
<String
, List
<String
>> skipThose
: skipCompare
274 for (String skipStart
: skipThose
.getValue()) {
275 if (name
.endsWith(skipThose
.getKey())
276 && expectedLine
.startsWith(skipStart
)
277 && resultLine
.startsWith(skipStart
)) {
287 testCase
.assertEquals(errMess
+ ": line " + (j
+ 1)
288 + " is not the same in file " + name
, expectedLine
,