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", Arrays
.asList(
80 " <meta name=\"dtb:uid\" content=",
81 " <meta name=\"epub-creator\" content=\""));
82 skipCompare
.put("epb.opf", Arrays
.asList(" <dc:subject>",
83 " <dc:identifier id=\"BookId\" opf:scheme=\"URI\">"));
84 skipCompare
.put(".info", Arrays
.asList("CREATION_DATE=",
85 "URL=\"file:/", "UUID=EPUBCREATOR=\"", ""));
86 skipCompare
.put("URL", Arrays
.asList("file:/"));
88 for (String key
: skipCompare
.keySet()) {
89 skipCompareCross
.put(key
, skipCompare
.get(key
));
92 skipCompareCross
.put(".info", Arrays
.asList(""));
93 skipCompareCross
.put("epb.opf", Arrays
.asList(" <dc:"));
94 skipCompareCross
.put("title.xhtml",
95 Arrays
.asList(" <div class=\"type\">"));
96 skipCompareCross
.put("index.html",
97 Arrays
.asList(" <div class=\"type\">"));
98 skipCompareCross
.put("URL", Arrays
.asList(""));
102 protected void stop() throws Exception
{
105 private TestCase
getTestFor(final BasicOutput
.OutputType type
) {
106 return new TestCase(type
+ " output mode") {
108 public void test() throws Exception
{
109 File target
= generate(this, testUri
, new File(resultDir
), type
);
110 target
= new File(target
.getAbsolutePath()
111 + type
.getDefaultExtension(false));
114 compareFiles(this, new File(expectedDir
), new File(resultDir
),
115 type
, "Generate " + type
);
117 // LATEX not supported as input
118 if (BasicOutput
.OutputType
.LATEX
.equals(type
)) {
123 for (BasicOutput
.OutputType crossType
: realTypes
) {
124 File crossDir
= Test
.tempFiles
125 .createTempDir("cross-result");
127 generate(this, target
.getAbsolutePath(), crossDir
,
129 compareFiles(this, new File(resultDir
), crossDir
,
130 crossType
, "Cross compare " + crossType
131 + " generated from " + type
);
137 private File
generate(TestCase testCase
, String testUri
, File resultDir
,
138 BasicOutput
.OutputType type
) throws Exception
{
139 final List
<String
> errors
= new ArrayList
<String
>();
141 TraceHandler previousTraceHandler
= Instance
.getInstance().getTraceHandler();
142 Instance
.getInstance().setTraceHandler(new TraceHandler(true, true, 0) {
144 public void error(String message
) {
149 public void error(Exception e
) {
151 for (Throwable t
= e
; t
!= null; t
= t
.getCause()) {
152 error(((t
== e
) ?
"(" : "..caused by: (")
153 + t
.getClass().getSimpleName() + ") "
155 for (StackTraceElement s
: t
.getStackTrace()) {
156 error("\t" + s
.toString());
163 File target
= new File(resultDir
, type
.toString());
164 int code
= Main
.convert(testUri
, type
.toString(),
165 target
.getAbsolutePath(), false, null);
168 for (String err
: errors
) {
169 if (!error
.isEmpty())
173 testCase
.assertEquals("The conversion returned an error message: "
174 + error
, 0, errors
.size());
176 testCase
.fail("The conversion failed with return code: " + code
);
181 Instance
.getInstance().setTraceHandler(previousTraceHandler
);
185 private void compareFiles(TestCase testCase
, File expectedDir
,
186 File resultDir
, final BasicOutput
.OutputType limitTiFiles
,
187 final String errMess
) throws Exception
{
189 Map
<String
, List
<String
>> skipCompare
= errMess
.startsWith("Cross") ?
this.skipCompareCross
192 FilenameFilter filter
= null;
193 if (limitTiFiles
!= null) {
194 filter
= new FilenameFilter() {
196 public boolean accept(File dir
, String name
) {
197 return name
.toLowerCase().startsWith(
198 limitTiFiles
.toString().toLowerCase());
203 List
<String
> resultFiles
;
204 List
<String
> expectedFiles
;
206 String
[] resultArr
= resultDir
.list(filter
);
207 Arrays
.sort(resultArr
);
208 resultFiles
= Arrays
.asList(resultArr
);
209 String
[] expectedArr
= expectedDir
.list(filter
);
210 Arrays
.sort(expectedArr
);
211 expectedFiles
= Arrays
.asList(expectedArr
);
214 testCase
.assertEquals(errMess
, expectedFiles
, resultFiles
);
216 for (int i
= 0; i
< resultFiles
.size(); i
++) {
217 File expected
= new File(expectedDir
, expectedFiles
.get(i
));
218 File result
= new File(resultDir
, resultFiles
.get(i
));
220 testCase
.assertEquals(errMess
+ ": type mismatch: expected a "
221 + (expected
.isDirectory() ?
"directory" : "file")
223 + (result
.isDirectory() ?
"directory" : "file"),
224 expected
.isDirectory(), result
.isDirectory());
226 if (expected
.isDirectory()) {
227 compareFiles(testCase
, expected
, result
, null, errMess
);
231 if (expected
.getName().endsWith(".cbz")
232 || expected
.getName().endsWith(".epub")) {
233 File tmpExpected
= Test
.tempFiles
.createTempDir(expected
234 .getName() + "[zip-content]");
235 File tmpResult
= Test
.tempFiles
.createTempDir(result
.getName()
237 IOUtils
.unzip(expected
, tmpExpected
);
238 IOUtils
.unzip(result
, tmpResult
);
239 compareFiles(testCase
, tmpExpected
, tmpResult
, null, errMess
);
241 List
<String
> expectedLines
= Arrays
.asList(IOUtils
242 .readSmallFile(expected
).split("\n"));
243 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
244 result
).split("\n"));
246 String name
= expected
.getAbsolutePath();
247 if (name
.startsWith(expectedDir
.getAbsolutePath())) {
248 name
= expectedDir
.getName()
249 + name
.substring(expectedDir
.getAbsolutePath()
253 testCase
.assertEquals(errMess
+ ": " + name
254 + ": the number of lines is not the same",
255 expectedLines
.size(), resultLines
.size());
257 for (int j
= 0; j
< expectedLines
.size(); j
++) {
258 String expectedLine
= expectedLines
.get(j
);
259 String resultLine
= resultLines
.get(j
);
261 boolean skip
= false;
262 for (Entry
<String
, List
<String
>> skipThose
: skipCompare
264 for (String skipStart
: skipThose
.getValue()) {
265 if (name
.endsWith(skipThose
.getKey())
266 && expectedLine
.startsWith(skipStart
)
267 && resultLine
.startsWith(skipStart
)) {
277 testCase
.assertEquals(errMess
+ ": line " + (j
+ 1)
278 + " is not the same in file " + name
, expectedLine
,