1 package be
.nikiroo
.fanfix
.test
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.FilenameFilter
;
6 import java
.io
.IOException
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Arrays
;
9 import java
.util
.HashMap
;
10 import java
.util
.List
;
12 import java
.util
.Map
.Entry
;
13 import java
.util
.zip
.ZipEntry
;
14 import java
.util
.zip
.ZipInputStream
;
16 import be
.nikiroo
.fanfix
.Instance
;
17 import be
.nikiroo
.fanfix
.Main
;
18 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
19 import be
.nikiroo
.utils
.IOUtils
;
20 import be
.nikiroo
.utils
.TempFiles
;
21 import be
.nikiroo
.utils
.TraceHandler
;
22 import be
.nikiroo
.utils
.test
.TestCase
;
23 import be
.nikiroo
.utils
.test
.TestLauncher
;
25 class ConversionTest
extends TestLauncher
{
26 private TempFiles tempFiles
;
27 private File testFile
;
28 private File expectedDir
;
29 private File resultDir
;
30 private List
<BasicOutput
.OutputType
> realTypes
;
31 private Map
<String
, List
<String
>> skipCompare
;
33 public ConversionTest(String
[] args
) {
34 super("Conversion", args
);
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 addTest(new TestCase("Read the test file") {
46 public void test() throws Exception
{
47 assertEquals("The test file \"" + testFile
48 + "\" cannot be found", true, testFile
.exists());
52 addTest(new TestCase("Assure directories exist") {
54 public void test() throws Exception
{
57 assertEquals("The Expected directory \"" + expectedDir
58 + "\" cannot be created", true, expectedDir
.exists());
59 assertEquals("The Result directory \"" + resultDir
60 + "\" cannot be created", true, resultDir
.exists());
64 for (BasicOutput
.OutputType type
: realTypes
) {
65 addTest(getTestFor(type
));
70 protected void start() throws Exception
{
71 testFile
= new File("test/test.story");
72 expectedDir
= new File("test/expected/");
73 resultDir
= new File("test/result/");
75 tempFiles
= new TempFiles("Fanfix-ConversionTest");
77 skipCompare
= new HashMap
<String
, List
<String
>>();
78 skipCompare
.put("epb.ncx",
79 Arrays
.asList(" <meta name=\"dtb:uid\" content="));
80 skipCompare
.put("epb.opf", Arrays
.asList(" <dc:subject>",
81 " <dc:identifier id=\"BookId\" opf:scheme=\"URI\">"));
82 skipCompare
.put(".info",
83 Arrays
.asList("CREATION_DATE=", "SUBJECT=", "URL=", "UUID="));
84 skipCompare
.put("URL", Arrays
.asList("file:/"));
88 protected void stop() throws Exception
{
92 private TestCase
getTestFor(final BasicOutput
.OutputType type
) {
93 return new TestCase(type
+ " output mode") {
95 public void test() throws Exception
{
96 File target
= generate(this, testFile
, resultDir
, type
);
97 target
= new File(target
.getAbsolutePath()
98 + type
.getDefaultExtension(false));
101 compareFiles(this, expectedDir
, resultDir
, type
, "Generate "
104 // LATEX not supported as input
105 if (BasicOutput
.OutputType
.LATEX
.equals(type
)) {
110 for (BasicOutput
.OutputType crossType
: realTypes
) {
111 File crossDir
= tempFiles
.createTempDir("cross-result");
112 generate(this, target
, crossDir
, crossType
);
113 compareFiles(this, resultDir
, crossDir
, crossType
,
114 "Cross compare " + crossType
+ " generated from "
121 private File
generate(TestCase testCase
, File testFile
, File resultDir
,
122 BasicOutput
.OutputType type
) throws Exception
{
123 final List
<String
> errors
= new ArrayList
<String
>();
125 TraceHandler previousTraceHandler
= Instance
.getTraceHandler();
126 Instance
.setTraceHandler(new TraceHandler(true, true, 0) {
128 public void error(String message
) {
133 public void error(Exception e
) {
135 for (Throwable t
= e
; t
!= null; t
= t
.getCause()) {
136 error(((t
== e
) ?
"(" : "..caused by: (")
137 + t
.getClass().getSimpleName() + ") "
139 for (StackTraceElement s
: t
.getStackTrace()) {
140 error("\t" + s
.toString());
147 File target
= new File(resultDir
, type
.toString());
148 int code
= Main
.convert(testFile
.getAbsolutePath(),
149 type
.toString(), target
.getAbsolutePath(), false, null);
152 for (String err
: errors
) {
153 if (!error
.isEmpty())
157 testCase
.assertEquals("The conversion returned an error message: "
158 + error
, 0, errors
.size());
160 testCase
.fail("The conversion failed with return code: " + code
);
165 Instance
.setTraceHandler(previousTraceHandler
);
169 private void compareFiles(TestCase testCase
, File expectedDir
,
170 File resultDir
, final BasicOutput
.OutputType limitTiFiles
,
171 final String errMess
) throws Exception
{
172 FilenameFilter filter
= null;
173 if (limitTiFiles
!= null) {
174 filter
= new FilenameFilter() {
176 public boolean accept(File dir
, String name
) {
177 return name
.toLowerCase().startsWith(
178 limitTiFiles
.toString().toLowerCase());
183 List
<String
> resultFiles
= Arrays
.asList(resultDir
.list(filter
));
184 resultFiles
.sort(null);
185 List
<String
> expectedFiles
= Arrays
.asList(expectedDir
.list(filter
));
186 expectedFiles
.sort(null);
188 testCase
.assertEquals(errMess
, expectedFiles
, resultFiles
);
190 for (int i
= 0; i
< resultFiles
.size(); i
++) {
191 File expected
= new File(expectedDir
, expectedFiles
.get(i
));
192 File result
= new File(resultDir
, resultFiles
.get(i
));
194 testCase
.assertEquals(errMess
+ ": type mismatch: expected a "
195 + (expected
.isDirectory() ?
"directory" : "file")
197 + (result
.isDirectory() ?
"directory" : "file"),
198 expected
.isDirectory(), result
.isDirectory());
200 if (expected
.isDirectory()) {
201 compareFiles(testCase
, expected
, result
, null, errMess
);
205 if (expected
.getName().endsWith(".cbz")
206 || expected
.getName().endsWith(".epub")) {
207 File tmpExpected
= tempFiles
.createTempDir(expected
.getName()
209 File tmpResult
= tempFiles
.createTempDir(result
.getName()
211 unzip(expected
, tmpExpected
);
212 unzip(result
, tmpResult
);
213 compareFiles(testCase
, tmpExpected
, tmpResult
, null, errMess
);
215 List
<String
> expectedLines
= Arrays
.asList(IOUtils
216 .readSmallFile(expected
).split("\n"));
217 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
218 result
).split("\n"));
220 String name
= expected
.getAbsolutePath();
221 if (name
.startsWith(expectedDir
.getAbsolutePath())) {
222 name
= expectedDir
.getName()
223 + name
.substring(expectedDir
.getAbsolutePath()
226 for (int j
= 0; j
< expectedLines
.size(); j
++) {
227 String expectedLine
= expectedLines
.get(j
);
228 String resultLine
= resultLines
.get(j
);
230 boolean skip
= false;
231 for (Entry
<String
, List
<String
>> skipThose
: skipCompare
233 for (String skipStart
: skipThose
.getValue()) {
234 if (name
.endsWith(skipThose
.getKey())
235 && expectedLine
.startsWith(skipStart
)
236 && resultLine
.startsWith(skipStart
)) {
246 testCase
.assertEquals(errMess
+ ": line " + (j
+ 1)
247 + " is not the same in file " + name
, expectedLine
,
254 // TODO: remove and use IOUtils when updated
255 private static void unzip(File zipFile
, File targetDirectory
)
257 if (targetDirectory
.exists() && targetDirectory
.isFile()) {
258 throw new IOException("Cannot unzip " + zipFile
+ " into "
259 + targetDirectory
+ ": it is not a directory");
262 targetDirectory
.mkdir();
263 if (!targetDirectory
.exists()) {
264 throw new IOException("Cannot create target directory "
268 FileInputStream in
= new FileInputStream(zipFile
);
270 ZipInputStream zipStream
= new ZipInputStream(in
);
272 for (ZipEntry entry
= zipStream
.getNextEntry(); entry
!= null; entry
= zipStream
274 File file
= new File(targetDirectory
, entry
.getName());
275 if (entry
.isDirectory()) {
278 IOUtils
.write(zipStream
, file
);