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
.utils
.IOUtils
;
16 import be
.nikiroo
.utils
.TraceHandler
;
17 import be
.nikiroo
.utils
.test
.TestCase
;
18 import be
.nikiroo
.utils
.test
.TestLauncher
;
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
;
28 public ConversionTest(String testName
, final String testUri
,
29 final String expectedDir
, final String resultDir
, String
[] args
) {
30 super("Conversion - " + testName
, args
);
32 this.testUri
= testUri
;
33 this.expectedDir
= expectedDir
;
34 this.resultDir
= resultDir
;
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
)) {
44 if (!testUri
.startsWith("http://") && !testUri
.startsWith("https://")) {
45 addTest(new TestCase("Read the test file") {
47 public void test() throws Exception
{
48 assertEquals("The test file \"" + testUri
49 + "\" cannot be found", true,
50 new File(testUri
).exists());
55 addTest(new TestCase("Assure directories exist") {
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());
69 for (BasicOutput
.OutputType type
: realTypes
) {
70 addTest(getTestFor(type
));
75 protected void start() throws Exception
{
76 skipCompare
= new HashMap
<String
, List
<String
>>();
77 skipCompareCross
= new HashMap
<String
, List
<String
>>();
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:/"));
87 for (String key
: skipCompare
.keySet()) {
88 skipCompareCross
.put(key
, skipCompare
.get(key
));
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(""));
101 protected void stop() throws Exception
{
104 private TestCase
getTestFor(final BasicOutput
.OutputType type
) {
105 return new TestCase(type
+ " output mode") {
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));
113 compareFiles(this, new File(expectedDir
), new File(resultDir
),
114 type
, "Generate " + type
);
116 // LATEX not supported as input
117 if (BasicOutput
.OutputType
.LATEX
.equals(type
)) {
122 for (BasicOutput
.OutputType crossType
: realTypes
) {
123 File crossDir
= Test
.tempFiles
124 .createTempDir("cross-result");
126 generate(this, target
.getAbsolutePath(), crossDir
,
128 compareFiles(this, new File(resultDir
), crossDir
,
129 crossType
, "Cross compare " + crossType
130 + " generated from " + type
);
136 private File
generate(TestCase testCase
, String testUri
, File resultDir
,
137 BasicOutput
.OutputType type
) throws Exception
{
138 final List
<String
> errors
= new ArrayList
<String
>();
140 TraceHandler previousTraceHandler
= Instance
.getTraceHandler();
141 Instance
.setTraceHandler(new TraceHandler(true, true, 0) {
143 public void error(String message
) {
148 public void error(Exception e
) {
150 for (Throwable t
= e
; t
!= null; t
= t
.getCause()) {
151 error(((t
== e
) ?
"(" : "..caused by: (")
152 + t
.getClass().getSimpleName() + ") "
154 for (StackTraceElement s
: t
.getStackTrace()) {
155 error("\t" + s
.toString());
162 File target
= new File(resultDir
, type
.toString());
163 int code
= Main
.convert(testUri
, type
.toString(),
164 target
.getAbsolutePath(), false, null);
167 for (String err
: errors
) {
168 if (!error
.isEmpty())
172 testCase
.assertEquals("The conversion returned an error message: "
173 + error
, 0, errors
.size());
175 testCase
.fail("The conversion failed with return code: " + code
);
180 Instance
.setTraceHandler(previousTraceHandler
);
184 private void compareFiles(TestCase testCase
, File expectedDir
,
185 File resultDir
, final BasicOutput
.OutputType limitTiFiles
,
186 final String errMess
) throws Exception
{
188 Map
<String
, List
<String
>> skipCompare
= errMess
.startsWith("Cross") ?
this.skipCompareCross
191 FilenameFilter filter
= null;
192 if (limitTiFiles
!= null) {
193 filter
= new FilenameFilter() {
195 public boolean accept(File dir
, String name
) {
196 return name
.toLowerCase().startsWith(
197 limitTiFiles
.toString().toLowerCase());
202 List
<String
> resultFiles
;
203 List
<String
> expectedFiles
;
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
);
213 testCase
.assertEquals(errMess
, expectedFiles
, resultFiles
);
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
));
219 testCase
.assertEquals(errMess
+ ": type mismatch: expected a "
220 + (expected
.isDirectory() ?
"directory" : "file")
222 + (result
.isDirectory() ?
"directory" : "file"),
223 expected
.isDirectory(), result
.isDirectory());
225 if (expected
.isDirectory()) {
226 compareFiles(testCase
, expected
, result
, null, errMess
);
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()
236 IOUtils
.unzip(expected
, tmpExpected
);
237 IOUtils
.unzip(result
, tmpResult
);
238 compareFiles(testCase
, tmpExpected
, tmpResult
, null, errMess
);
240 List
<String
> expectedLines
= Arrays
.asList(IOUtils
241 .readSmallFile(expected
).split("\n"));
242 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
243 result
).split("\n"));
245 String name
= expected
.getAbsolutePath();
246 if (name
.startsWith(expectedDir
.getAbsolutePath())) {
247 name
= expectedDir
.getName()
248 + name
.substring(expectedDir
.getAbsolutePath()
252 testCase
.assertEquals(errMess
+ ": " + name
253 + ": the number of lines is not the same",
254 expectedLines
.size(), resultLines
.size());
256 for (int j
= 0; j
< expectedLines
.size(); j
++) {
257 String expectedLine
= expectedLines
.get(j
);
258 String resultLine
= resultLines
.get(j
);
260 boolean skip
= false;
261 for (Entry
<String
, List
<String
>> skipThose
: skipCompare
263 for (String skipStart
: skipThose
.getValue()) {
264 if (name
.endsWith(skipThose
.getKey())
265 && expectedLine
.startsWith(skipStart
)
266 && resultLine
.startsWith(skipStart
)) {
276 testCase
.assertEquals(errMess
+ ": line " + (j
+ 1)
277 + " is not the same in file " + name
, expectedLine
,