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
;
10 import java
.util
.zip
.ZipEntry
;
11 import java
.util
.zip
.ZipInputStream
;
13 import be
.nikiroo
.fanfix
.Instance
;
14 import be
.nikiroo
.fanfix
.Main
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
;
16 import be
.nikiroo
.utils
.IOUtils
;
17 import be
.nikiroo
.utils
.TempFiles
;
18 import be
.nikiroo
.utils
.TraceHandler
;
19 import be
.nikiroo
.utils
.test
.TestCase
;
20 import be
.nikiroo
.utils
.test
.TestLauncher
;
22 class ConversionTest
extends TestLauncher
{
23 private TempFiles tempFiles
;
24 private File testFile
;
25 private File expectedDir
;
26 private File resultDir
;
27 private List
<BasicOutput
.OutputType
> realTypes
;
29 public ConversionTest(String
[] args
) {
30 super("Conversion", args
);
32 // Special mode SYSOUT is not a file type (System.out)
33 realTypes
= new ArrayList
<BasicOutput
.OutputType
>();
34 for (BasicOutput
.OutputType type
: BasicOutput
.OutputType
.values()) {
35 if (!BasicOutput
.OutputType
.SYSOUT
.equals(type
)) {
40 addTest(new TestCase("Read the test file") {
42 public void test() throws Exception
{
43 assertEquals("The test file \"" + testFile
44 + "\" cannot be found", true, testFile
.exists());
48 addTest(new TestCase("Assure directories exist") {
50 public void test() throws Exception
{
53 assertEquals("The Expected directory \"" + expectedDir
54 + "\" cannot be created", true, expectedDir
.exists());
55 assertEquals("The Result directory \"" + resultDir
56 + "\" cannot be created", true, resultDir
.exists());
60 for (BasicOutput
.OutputType type
: realTypes
) {
61 addTest(getTestFor(type
));
66 protected void start() throws Exception
{
67 testFile
= new File("test/test.story");
68 expectedDir
= new File("test/expected/");
69 resultDir
= new File("test/result/");
71 tempFiles
= new TempFiles("Fanfix-ConversionTest");
75 protected void stop() throws Exception
{
79 private TestCase
getTestFor(final BasicOutput
.OutputType type
) {
80 return new TestCase(type
+ " output mode") {
82 public void test() throws Exception
{
83 File target
= generate(this, testFile
, resultDir
, type
);
84 target
= new File(target
.getAbsolutePath()
85 + type
.getDefaultExtension(false));
88 compareFiles(this, expectedDir
, resultDir
, type
, null);
90 // LATEX not supported as input
91 if (BasicOutput
.OutputType
.LATEX
.equals(type
)) {
96 for (BasicOutput
.OutputType crossType
: realTypes
) {
97 File crossDir
= tempFiles
.createTempDir("cross-result");
98 generate(this, target
, crossDir
, crossType
);
99 compareFiles(this, resultDir
, crossDir
, crossType
,
106 private File
generate(TestCase testCase
, File testFile
, File resultDir
,
107 BasicOutput
.OutputType type
) throws Exception
{
108 final List
<String
> errors
= new ArrayList
<String
>();
110 TraceHandler previousTraceHandler
= Instance
.getTraceHandler();
111 Instance
.setTraceHandler(new TraceHandler(true, true, 0) {
113 public void error(String message
) {
118 public void error(Exception e
) {
120 for (Throwable t
= e
; t
!= null; t
= t
.getCause()) {
121 error(((t
== e
) ?
"(" : "..caused by: (")
122 + t
.getClass().getSimpleName() + ") "
124 for (StackTraceElement s
: t
.getStackTrace()) {
125 error("\t" + s
.toString());
132 File target
= new File(resultDir
, type
.toString());
133 int code
= Main
.convert(testFile
.getAbsolutePath(),
134 type
.toString(), target
.getAbsolutePath(), false, null);
137 for (String err
: errors
) {
138 if (!error
.isEmpty())
142 testCase
.assertEquals("The conversion returned an error message: "
143 + error
, 0, errors
.size());
145 testCase
.fail("The conversion failed with return code: " + code
);
150 Instance
.setTraceHandler(previousTraceHandler
);
154 private void compareFiles(TestCase testCase
, File expectedDir
,
155 File resultDir
, final BasicOutput
.OutputType typeToCompare
,
156 final BasicOutput
.OutputType sourceType
) throws Exception
{
158 FilenameFilter filter
= null;
159 if (typeToCompare
!= null) {
160 filter
= new FilenameFilter() {
162 public boolean accept(File dir
, String name
) {
163 return name
.startsWith(typeToCompare
.toString());
168 List
<String
> resultFiles
= Arrays
.asList(resultDir
.list(filter
));
169 resultFiles
.sort(null);
170 List
<String
> expectedFiles
= Arrays
.asList(expectedDir
.list(filter
));
171 expectedFiles
.sort(null);
173 testCase
.assertEquals("The resulting file names are not expected",
174 expectedFiles
, resultFiles
);
176 for (int i
= 0; i
< resultFiles
.size(); i
++) {
177 File expected
= new File(expectedDir
, expectedFiles
.get(i
));
178 File result
= new File(resultDir
, resultFiles
.get(i
));
180 testCase
.assertEquals(
181 "Type mismatch: expected a "
182 + (expected
.isDirectory() ?
"directory" : "file")
184 + (result
.isDirectory() ?
"directory" : "file"),
185 expected
.isDirectory(), result
.isDirectory());
187 if (expected
.isDirectory()) {
188 compareFiles(testCase
, expected
, result
, null, sourceType
);
192 if (expected
.getName().endsWith(".cbz")
193 || expected
.getName().endsWith(".epub")) {
194 File tmpExpected
= tempFiles
.createTempDir(expected
.getName()
196 File tmpResult
= tempFiles
.createTempDir(result
.getName()
198 unzip(expected
, tmpExpected
);
199 unzip(result
, tmpResult
);
200 compareFiles(testCase
, tmpExpected
, tmpResult
, null, sourceType
);
202 List
<String
> expectedLines
= Arrays
.asList(IOUtils
203 .readSmallFile(expected
).split("\n"));
204 List
<String
> resultLines
= Arrays
.asList(IOUtils
.readSmallFile(
205 result
).split("\n"));
207 String name
= expected
.getAbsolutePath();
208 if (name
.startsWith(expectedDir
.getAbsolutePath())) {
209 name
= expectedDir
.getName()
210 + name
.substring(expectedDir
.getAbsolutePath()
213 for (int j
= 0; j
< expectedLines
.size(); j
++) {
214 String expectedLine
= expectedLines
.get(j
);
215 String resultLine
= resultLines
.get(j
);
217 boolean skip
= false;
218 for (String skipStart
: new String
[] { "CREATION_DATE=",
219 "SUBJECT=", "URL=", "UUID=" }) {
220 if (name
.endsWith(".info")
221 && expectedLine
.startsWith(skipStart
)
222 && resultLine
.startsWith(skipStart
)) {
223 // TODO: check the format?
232 testCase
.assertEquals("Line " + (j
+ 1) + " (" + sourceType
233 + ") is not the same in file " + name
,
234 expectedLine
, resultLine
);
240 // TODO: remove and use IOUtils when updated
241 private static void unzip(File zipFile
, File targetDirectory
)
243 if (targetDirectory
.exists() && targetDirectory
.isFile()) {
244 throw new IOException("Cannot unzip " + zipFile
+ " into "
245 + targetDirectory
+ ": it is not a directory");
248 targetDirectory
.mkdir();
249 if (!targetDirectory
.exists()) {
250 throw new IOException("Cannot create target directory "
254 FileInputStream in
= new FileInputStream(zipFile
);
256 ZipInputStream zipStream
= new ZipInputStream(in
);
258 for (ZipEntry entry
= zipStream
.getNextEntry(); entry
!= null; entry
= zipStream
260 File file
= new File(targetDirectory
, entry
.getName());
261 if (entry
.isDirectory()) {
264 IOUtils
.write(zipStream
, file
);