tests: allow testing live URLs
[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.IOException;
5
6 import be.nikiroo.fanfix.Instance;
7 import be.nikiroo.fanfix.bundles.Config;
8 import be.nikiroo.fanfix.bundles.ConfigBundle;
9 import be.nikiroo.utils.IOUtils;
10 import be.nikiroo.utils.TempFiles;
11 import be.nikiroo.utils.resources.Bundles;
12 import be.nikiroo.utils.test.TestLauncher;
13
14 /**
15 * Tests for Fanfix.
16 *
17 * @author niki
18 */
19 public class Test extends TestLauncher {
20 //
21 // 3 files can control the test:
22 // - test/VERBOSE: enable verbose mode
23 // - test/OFFLINE: to forbid any downloading
24 // - test/FORCE_REFRESH: to force a clear of the cache
25 //
26 // The test files will be:
27 // - test/*.url: URL to download in text format, content = URL
28 // - test/*.story: text mode story, content = story
29 //
30
31 /**
32 * The temporary files handler.
33 */
34 static TempFiles tempFiles;
35
36 /**
37 * Create the Fanfix {@link TestLauncher}.
38 *
39 * @param args
40 * the arguments to configure the number of columns and the ok/ko
41 * {@link String}s
42 *
43 * @throws IOException
44 */
45 public Test(String[] args) throws IOException {
46 super("Fanfix", args);
47 Instance.setTraceHandler(null);
48 addSeries(new BasicSupportUtilitiesTest(args));
49 addSeries(new BasicSupportDeprecatedTest(args));
50 addSeries(new LibraryTest(args));
51
52 File sources = new File("test/");
53 if (sources.isDirectory()) {
54 for (File file : sources.listFiles()) {
55 if (file.isDirectory()) {
56 continue;
57 }
58
59 String expectedDir = new File(file.getParentFile(), "expected_"
60 + file.getName()).getAbsolutePath();
61 String resultDir = new File(file.getParentFile(), "result_"
62 + file.getName()).getAbsolutePath();
63
64 String uri;
65 if (file.getName().endsWith(".url")) {
66 uri = IOUtils.readSmallFile(file).trim();
67 } else if (file.getName().endsWith(".story")) {
68 uri = file.getAbsolutePath();
69 } else {
70 continue;
71 }
72
73 addSeries(new ConversionTest(file.getName(), uri, expectedDir,
74 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 ConfigBundle config = new ConfigBundle();
111 Bundles.setDirectory(tmpConfig.getAbsolutePath());
112 config.setString(Config.CACHE_DIR, localCache.getAbsolutePath());
113 config.setInteger(Config.CACHE_MAX_TIME_STABLE, -1);
114 config.setInteger(Config.CACHE_MAX_TIME_CHANGING, -1);
115 config.updateFile(tmpConfig.getPath());
116 System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath());
117
118 Instance.init(true);
119 Instance.getCache().setOffline(offline);
120
121 TestLauncher tests = new Test(args);
122 tests.setDetails(verbose);
123
124 result = tests.launch();
125
126 IOUtils.deltree(tmpConfig);
127 } finally {
128 // Test temp files
129 tempFiles.close();
130
131 // This is usually done in Fanfix.Main:
132 Instance.getTempFiles().close();
133 }
134
135 System.exit(result);
136 }
137 }