Merge from master
[nikiroo-utils.git] / 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 // 4 files can control the test:
22 // - test/VERBOSE: enable verbose mode
23 // - test/OFFLINE: to forbid any downloading
24 // - test/URLS: to allow testing URLs
25 // - test/FORCE_REFRESH: to force a clear of the cache
26 //
27 // Note that test/CACHE can be kept, as it will contain all internet related
28 // files you need (if you allow URLs, run the test once which will populate
29 // the CACHE then go OFFLINE, it will still work).
30 //
31 // The test files will be:
32 // - test/*.url: URL to download in text format, content = URL
33 // - test/*.story: text mode story, content = story
34 //
35
36 /**
37 * The temporary files handler.
38 */
39 static TempFiles tempFiles;
40
41 /**
42 * Create the Fanfix {@link TestLauncher}.
43 *
44 * @param args
45 * the arguments to configure the number of columns and the ok/ko
46 * {@link String}s
47 * @param urlsAllowed
48 * allow testing URLs (<tt>.url</tt> files)
49 *
50 * @throws IOException
51 */
52 public Test(String[] args, boolean urlsAllowed) throws IOException {
53 super("Fanfix", args);
54 Instance.getInstance().setTraceHandler(null);
55 addSeries(new BasicSupportUtilitiesTest(args));
56 addSeries(new BasicSupportDeprecatedTest(args));
57 addSeries(new LibraryTest(args));
58
59 File sources = new File("test/");
60 if (sources.isDirectory()) {
61 for (File file : sources.listFiles()) {
62 if (file.isDirectory()) {
63 continue;
64 }
65
66 String expectedDir = new File(file.getParentFile(), "expected_"
67 + file.getName()).getAbsolutePath();
68 String resultDir = new File(file.getParentFile(), "result_"
69 + file.getName()).getAbsolutePath();
70
71 String uri;
72 if (urlsAllowed && file.getName().endsWith(".url")) {
73 uri = IOUtils.readSmallFile(file).trim();
74 } else if (file.getName().endsWith(".story")) {
75 uri = file.getAbsolutePath();
76 } else {
77 continue;
78 }
79
80 addSeries(new ConversionTest(file.getName(), uri, expectedDir,
81 resultDir, args));
82 }
83 }
84 }
85
86 /**
87 * Main entry point of the program.
88 *
89 * @param args
90 * the arguments passed to the {@link TestLauncher}s.
91 * @throws IOException
92 * in case of I/O error
93 */
94 static public void main(String[] args) throws IOException {
95 Instance.init();
96
97 // Verbose mode:
98 boolean verbose = new File("test/VERBOSE").exists();
99
100 // Can force refresh
101 boolean forceRefresh = new File("test/FORCE_REFRESH").exists();
102
103 // Allow URLs:
104 boolean urlsAllowed = new File("test/URLS").exists();
105
106
107 // Only download files if allowed:
108 boolean offline = new File("test/OFFLINE").exists();
109 Instance.getInstance().getCache().setOffline(offline);
110
111
112
113 int result = 0;
114 tempFiles = new TempFiles("fanfix-test");
115 try {
116 File tmpConfig = tempFiles.createTempDir("fanfix-config");
117 File localCache = new File("test/CACHE");
118 prepareCache(localCache, forceRefresh);
119
120 ConfigBundle config = new ConfigBundle();
121 Bundles.setDirectory(tmpConfig.getAbsolutePath());
122 config.setString(Config.CACHE_DIR, localCache.getAbsolutePath());
123 config.setInteger(Config.CACHE_MAX_TIME_STABLE, -1);
124 config.setInteger(Config.CACHE_MAX_TIME_CHANGING, -1);
125 config.updateFile(tmpConfig.getPath());
126 System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath());
127
128 Instance.init(true);
129 Instance.getInstance().getCache().setOffline(offline);
130
131 TestLauncher tests = new Test(args, urlsAllowed);
132 tests.setDetails(verbose);
133
134 result = tests.launch();
135
136 IOUtils.deltree(tmpConfig);
137 prepareCache(localCache, forceRefresh);
138 } finally {
139 // Test temp files
140 tempFiles.close();
141
142 // This is usually done in Fanfix.Main:
143 Instance.getInstance().getTempFiles().close();
144 }
145
146 System.exit(result);
147 }
148
149 /**
150 * Prepare the cache (or clean it up).
151 * <p>
152 * The cache directory will always exist if this method succeed
153 *
154 * @param localCache
155 * the cache directory
156 * @param forceRefresh
157 * TRUE to force acache refresh (delete all files)
158 *
159 * @throw IOException if the cache cannot be created
160 */
161 private static void prepareCache(File localCache, boolean forceRefresh)
162 throws IOException {
163 // if needed
164 localCache.mkdirs();
165
166 if (!localCache.isDirectory()) {
167 throw new IOException("Cannot get a cache");
168 }
169
170 // delete local cached files (_*) or all files if forceRefresh
171 for (File f : localCache.listFiles()) {
172 if (forceRefresh || f.getName().startsWith("_")) {
173 IOUtils.deltree(f);
174 }
175 }
176 }
177 }