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
;
27 public ConversionTest(final String testUri
, final String expectedDir
,
28 final String resultDir
, String
[] args
) {
29 super("Conversion", args
);
31 this.testUri
= testUri
;
32 this.expectedDir
= expectedDir
;
33 this.resultDir
= resultDir
;
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
)) {
43 addTest(new TestCase("Read the test file") {
45 public void test() throws Exception
{
46 assertEquals("The test file \"" + testUri
47 + "\" cannot be found", true,
48 new File(testUri
).exists());
52 addTest(new TestCase("Assure directories exist") {
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());
66 for (BasicOutput
.OutputType type
: realTypes
) {
67 addTest(getTestFor(type
));
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:/"));
84 protected void stop() throws Exception
{
87 private TestCase
getTestFor(final BasicOutput
.OutputType type
) {
88 return new TestCase(type
+ " output mode") {
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));
96 compareFiles(this, new File(expectedDir
), new File(resultDir
),
97 type
, "Generate " + type
);
99 // LATEX not supported as input
100 if (BasicOutput
.OutputType
.LATEX
.equals(type
)) {
105 for (BasicOutput
.OutputType crossType
: realTypes
) {
106 File crossDir
= Test
.tempFiles
107 .createTempDir("cross-result");
108 generate(this, target
.getAbsolutePath(), crossDir
,
110 compareFiles(this, new File(resultDir
), crossDir
,
111 crossType
, "Cross compare " + crossType
112 + " generated from " + type
);
118 private File
generate(TestCase testCase
, String testUri
, File resultDir
,
119 BasicOutput
.OutputType type
) throws Exception
{
120 final List
<String
> errors
= new ArrayList
<String
>();
122 TraceHandler previousTraceHandler
= Instance
.getTraceHandler();
123 Instance
.setTraceHandler(new TraceHandler(true, true, 0) {
125 public void error(String message
) {
130 public void error(Exception e
) {
132 for (Throwable t
= e
; t
!= null; t
= t
.getCause()) {
133 error(((t
== e
) ?
"(" : "..caused by: (")
134 + t
.getClass().getSimpleName() + ") "
136 for (StackTraceElement s
: t
.getStackTrace()) {
137 error("\t" + s
.toString());
144 File target
= new File(resultDir
, type
.toString());
145 int code
= Main
.convert(testUri
, type
.toString(),
146 target
.getAbsolutePath(), false, null);
149 for (String err
: errors
) {
150 if (!error
.isEmpty())
154 testCase
.assertEquals("The conversion returned an error message: "
155 + error
, 0, errors
.size());
157 testCase
.fail("The conversion failed with return code: " + code
);
162 Instance
.setTraceHandler(previousTraceHandler
);
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() {
173 public boolean accept(File dir
, String name
) {
174 return name
.toLowerCase().startsWith(
175 limitTiFiles
.toString().toLowerCase());
180 List
<String
> resultFiles
;
181 List
<String
> expectedFiles
;
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
);
191 testCase
.assertEquals(errMess
, expectedFiles
, resultFiles
);
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
));
197 testCase
.assertEquals(errMess
+ ": type mismatch: expected a "
198 + (expected
.isDirectory() ?
"directory" : "file")
200 + (result
.isDirectory() ?
"directory" : "file"),
201 expected
.isDirectory(), result
.isDirectory());
203 if (expected
.isDirectory()) {
204 compareFiles(testCase
, expected
, result
, null, errMess
);
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()
214 IOUtils
.unzip(expected
, tmpExpected
);
215 IOUtils
.unzip(result
, tmpResult
);
216 compareFiles(testCase
, tmpExpected
, tmpResult
, null, errMess
);
218 List
<String
> expectedLines
= Arrays
.asList(IOUtils
219 .readSmallFile(expected
).split("\n"));
220 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
221 result
).split("\n"));
223 String name
= expected
.getAbsolutePath();
224 if (name
.startsWith(expectedDir
.getAbsolutePath())) {
225 name
= expectedDir
.getName()
226 + name
.substring(expectedDir
.getAbsolutePath()
230 testCase
.assertEquals(errMess
+ ": " + name
231 + ": the number of lines is not the same",
232 expectedLines
.size(), resultLines
.size());
234 for (int j
= 0; j
< expectedLines
.size(); j
++) {
235 String expectedLine
= expectedLines
.get(j
);
236 String resultLine
= resultLines
.get(j
);
238 boolean skip
= false;
239 for (Entry
<String
, List
<String
>> skipThose
: skipCompare
241 for (String skipStart
: skipThose
.getValue()) {
242 if (name
.endsWith(skipThose
.getKey())
243 && expectedLine
.startsWith(skipStart
)
244 && resultLine
.startsWith(skipStart
)) {
254 testCase
.assertEquals(errMess
+ ": line " + (j
+ 1)
255 + " is not the same in file " + name
, expectedLine
,