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 File testFile
;
22 private File expectedDir
;
23 private File resultDir
;
24 private List
<BasicOutput
.OutputType
> realTypes
;
25 private Map
<String
, List
<String
>> skipCompare
;
27 public ConversionTest(String
[] args
) {
28 super("Conversion", args
);
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
)) {
38 addTest(new TestCase("Read the test file") {
40 public void test() throws Exception
{
41 assertEquals("The test file \"" + testFile
42 + "\" cannot be found", true, testFile
.exists());
46 addTest(new TestCase("Assure directories exist") {
48 public void test() throws Exception
{
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());
58 for (BasicOutput
.OutputType type
: realTypes
) {
59 addTest(getTestFor(type
));
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/");
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:/"));
80 protected void stop() throws Exception
{
83 private TestCase
getTestFor(final BasicOutput
.OutputType type
) {
84 return new TestCase(type
+ " output mode") {
86 public void test() throws Exception
{
87 File target
= generate(this, testFile
, resultDir
, type
);
88 target
= new File(target
.getAbsolutePath()
89 + type
.getDefaultExtension(false));
92 compareFiles(this, expectedDir
, resultDir
, type
, "Generate "
95 // LATEX not supported as input
96 if (BasicOutput
.OutputType
.LATEX
.equals(type
)) {
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 "
113 private File
generate(TestCase testCase
, File testFile
, File resultDir
,
114 BasicOutput
.OutputType type
) throws Exception
{
115 final List
<String
> errors
= new ArrayList
<String
>();
117 TraceHandler previousTraceHandler
= Instance
.getTraceHandler();
118 Instance
.setTraceHandler(new TraceHandler(true, true, 0) {
120 public void error(String message
) {
125 public void error(Exception e
) {
127 for (Throwable t
= e
; t
!= null; t
= t
.getCause()) {
128 error(((t
== e
) ?
"(" : "..caused by: (")
129 + t
.getClass().getSimpleName() + ") "
131 for (StackTraceElement s
: t
.getStackTrace()) {
132 error("\t" + s
.toString());
139 File target
= new File(resultDir
, type
.toString());
140 int code
= Main
.convert(testFile
.getAbsolutePath(),
141 type
.toString(), target
.getAbsolutePath(), false, null);
144 for (String err
: errors
) {
145 if (!error
.isEmpty())
149 testCase
.assertEquals("The conversion returned an error message: "
150 + error
, 0, errors
.size());
152 testCase
.fail("The conversion failed with return code: " + code
);
157 Instance
.setTraceHandler(previousTraceHandler
);
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() {
168 public boolean accept(File dir
, String name
) {
169 return name
.toLowerCase().startsWith(
170 limitTiFiles
.toString().toLowerCase());
175 List
<String
> resultFiles
= Arrays
.asList(resultDir
.list(filter
));
176 resultFiles
.sort(null);
177 List
<String
> expectedFiles
= Arrays
.asList(expectedDir
.list(filter
));
178 expectedFiles
.sort(null);
180 testCase
.assertEquals(errMess
, expectedFiles
, resultFiles
);
182 for (int i
= 0; i
< resultFiles
.size(); i
++) {
183 File expected
= new File(expectedDir
, expectedFiles
.get(i
));
184 File result
= new File(resultDir
, resultFiles
.get(i
));
186 testCase
.assertEquals(errMess
+ ": type mismatch: expected a "
187 + (expected
.isDirectory() ?
"directory" : "file")
189 + (result
.isDirectory() ?
"directory" : "file"),
190 expected
.isDirectory(), result
.isDirectory());
192 if (expected
.isDirectory()) {
193 compareFiles(testCase
, expected
, result
, null, errMess
);
197 if (expected
.getName().endsWith(".cbz")
198 || expected
.getName().endsWith(".epub")) {
199 File tmpExpected
= Test
.tempFiles
.createTempDir(expected
200 .getName() + "[zip-content]");
201 File tmpResult
= Test
.tempFiles
.createTempDir(result
.getName()
203 IOUtils
.unzip(expected
, tmpExpected
);
204 IOUtils
.unzip(result
, tmpResult
);
205 compareFiles(testCase
, tmpExpected
, tmpResult
, null, errMess
);
207 List
<String
> expectedLines
= Arrays
.asList(IOUtils
208 .readSmallFile(expected
).split("\n"));
209 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
210 result
).split("\n"));
212 String name
= expected
.getAbsolutePath();
213 if (name
.startsWith(expectedDir
.getAbsolutePath())) {
214 name
= expectedDir
.getName()
215 + name
.substring(expectedDir
.getAbsolutePath()
219 testCase
.assertEquals(errMess
+ ": " + name
220 + ": the number of lines is not the same",
221 expectedLines
.size(), resultLines
.size());
223 for (int j
= 0; j
< expectedLines
.size(); j
++) {
224 String expectedLine
= expectedLines
.get(j
);
225 String resultLine
= resultLines
.get(j
);
227 boolean skip
= false;
228 for (Entry
<String
, List
<String
>> skipThose
: skipCompare
230 for (String skipStart
: skipThose
.getValue()) {
231 if (name
.endsWith(skipThose
.getKey())
232 && expectedLine
.startsWith(skipStart
)
233 && resultLine
.startsWith(skipStart
)) {
243 testCase
.assertEquals(errMess
+ ": line " + (j
+ 1)
244 + " is not the same in file " + name
, expectedLine
,