open conversion test for external input
[fanfix.git] / src / be / nikiroo / fanfix / test / Test.java
1 package be.nikiroo.fanfix.test;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.util.Properties;
7
8 import be.nikiroo.fanfix.Instance;
9 import be.nikiroo.fanfix.bundles.ConfigBundle;
10 import be.nikiroo.utils.IOUtils;
11 import be.nikiroo.utils.TempFiles;
12 import be.nikiroo.utils.resources.Bundles;
13 import be.nikiroo.utils.test.TestLauncher;
14
15 /**
16 * Tests for Fanfix.
17 *
18 * @author niki
19 */
20 public class Test extends TestLauncher {
21 //
22 // 3 files can control the test:
23 // - test/VERBOSE: enable verbose mode
24 // - test/OFFLINE: to forbid any downloading
25 // - test/FORCE_REFRESH: to force a clear of the cache
26 //
27 // The test files will be:
28 // - test/*.url: URL to download in text format, content = URL
29 // - test/*.story: text mode story, content = story
30 //
31
32 /**
33 * The temporary files handler.
34 */
35 static TempFiles tempFiles;
36
37 /**
38 * Create the Fanfix {@link TestLauncher}.
39 *
40 * @param args
41 * the arguments to configure the number of columns and the ok/ko
42 * {@link String}s
43 *
44 * @throws IOException
45 */
46 public Test(String[] args) throws IOException {
47 super("Fanfix", args);
48 Instance.setTraceHandler(null);
49 addSeries(new BasicSupportUtilitiesTest(args));
50 addSeries(new BasicSupportDeprecatedTest(args));
51 addSeries(new LibraryTest(args));
52
53 File sources = new File("test/");
54 if (sources.isDirectory()) {
55 for (File file : sources.listFiles()) {
56 if (file.isDirectory()) {
57 continue;
58 }
59
60 String expectedDir = new File(file.getParentFile(), "expected_"
61 + file.getName()).getAbsolutePath();
62 String resultDir = new File(file.getParentFile(), "result_"
63 + file.getName()).getAbsolutePath();
64
65 String uri;
66 if (file.getName().endsWith(".url")) {
67 uri = IOUtils.readSmallFile(file).trim();
68 } else if (file.getName().endsWith(".story")) {
69 uri = file.getAbsolutePath();
70 } else {
71 continue;
72 }
73
74 addSeries(new ConversionTest(uri, expectedDir, resultDir, args));
75 }
76 }
77 }
78
79 /**
80 * Main entry point of the program.
81 *
82 * @param args
83 * the arguments passed to the {@link TestLauncher}s.
84 * @throws IOException
85 * in case of I/O error
86 */
87 static public void main(String[] args) throws IOException {
88 Instance.init();
89
90 // Verbose mode:
91 boolean verbose = new File("test/VERBOSE").exists();
92
93 // Can force refresh
94 boolean forceRefresh = new File("test/FORCE_REFRESH").exists();
95
96 // Only download files if allowed:
97 boolean offline = new File("test/OFFLINE").exists();
98 Instance.getCache().setOffline(offline);
99
100 int result = 0;
101 tempFiles = new TempFiles("fanfix-test");
102 try {
103 File tmpConfig = tempFiles.createTempDir("fanfix-config");
104 File localCache = new File("test/CACHE");
105 if (forceRefresh) {
106 IOUtils.deltree(localCache);
107 }
108 localCache.mkdirs();
109
110 FileOutputStream out = null;
111 try {
112 out = new FileOutputStream(new File(tmpConfig,
113 "config.properties"));
114 Properties props = new Properties();
115 props.setProperty("CACHE_DIR", localCache.getAbsolutePath());
116 props.store(out, null);
117 } finally {
118 if (out != null) {
119 out.close();
120 }
121 }
122
123 ConfigBundle config = new ConfigBundle();
124 Bundles.setDirectory(tmpConfig.getAbsolutePath());
125 config.updateFile(tmpConfig.getPath());
126
127 System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath());
128
129 TestLauncher tests = new Test(args);
130 tests.setDetails(verbose);
131
132 result = tests.launch();
133
134 IOUtils.deltree(tmpConfig);
135 IOUtils.deltree(localCache);
136 } finally {
137 // Test temp files
138 tempFiles.close();
139
140 // This is usually done in Fanfix.Main:
141 Instance.getTempFiles().close();
142 }
143
144 System.exit(result);
145 }
146 }