Merge branch 'master' into subtree
[nikiroo-utils.git] / test / Test.java
CommitLineData
92fb0719
NR
1package be.nikiroo.fanfix.test;
2
22848428 3import java.io.File;
22848428 4import java.io.IOException;
22848428 5
581d42c0 6import be.nikiroo.fanfix.Instance;
06befaee 7import be.nikiroo.fanfix.bundles.Config;
22848428
NR
8import be.nikiroo.fanfix.bundles.ConfigBundle;
9import be.nikiroo.utils.IOUtils;
2aac79c7 10import be.nikiroo.utils.TempFiles;
22848428 11import be.nikiroo.utils.resources.Bundles;
92fb0719
NR
12import be.nikiroo.utils.test.TestLauncher;
13
14/**
15 * Tests for Fanfix.
16 *
17 * @author niki
18 */
19public class Test extends TestLauncher {
dabc4b95 20 //
e64228f8 21 // 4 files can control the test:
dabc4b95
NR
22 // - test/VERBOSE: enable verbose mode
23 // - test/OFFLINE: to forbid any downloading
b0c0a80e 24 // - test/URLS: to allow testing URLs
dabc4b95
NR
25 // - test/FORCE_REFRESH: to force a clear of the cache
26 //
b0c0a80e
NR
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 //
dabc4b95
NR
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
2aac79c7
NR
36 /**
37 * The temporary files handler.
38 */
39 static TempFiles tempFiles;
40
2a25f781
NR
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
b0c0a80e
NR
47 * @param urlsAllowed
48 * allow testing URLs (<tt>.url</tt> files)
dabc4b95
NR
49 *
50 * @throws IOException
2a25f781 51 */
b0c0a80e 52 public Test(String[] args, boolean urlsAllowed) throws IOException {
68e370a4 53 super("Fanfix", args);
d66deb8d 54 Instance.getInstance().setTraceHandler(null);
8d59ce07
NR
55 addSeries(new BasicSupportUtilitiesTest(args));
56 addSeries(new BasicSupportDeprecatedTest(args));
6cac0e45 57 addSeries(new LibraryTest(args));
b0c0a80e 58
dabc4b95
NR
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;
b0c0a80e 72 if (urlsAllowed && file.getName().endsWith(".url")) {
dabc4b95
NR
73 uri = IOUtils.readSmallFile(file).trim();
74 } else if (file.getName().endsWith(".story")) {
75 uri = file.getAbsolutePath();
76 } else {
77 continue;
78 }
79
06befaee
NR
80 addSeries(new ConversionTest(file.getName(), uri, expectedDir,
81 resultDir, args));
dabc4b95
NR
82 }
83 }
92fb0719
NR
84 }
85
86 /**
87 * Main entry point of the program.
88 *
89 * @param args
90 * the arguments passed to the {@link TestLauncher}s.
22848428 91 * @throws IOException
2a25f781 92 * in case of I/O error
92fb0719 93 */
22848428 94 static public void main(String[] args) throws IOException {
8d59ce07 95 Instance.init();
dabc4b95
NR
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
b0c0a80e
NR
103 // Allow URLs:
104 boolean urlsAllowed = new File("test/URLS").exists();
105
106
dabc4b95
NR
107 // Only download files if allowed:
108 boolean offline = new File("test/OFFLINE").exists();
d66deb8d 109 Instance.getInstance().getCache().setOffline(offline);
dabc4b95 110
b0c0a80e
NR
111
112
2aac79c7
NR
113 int result = 0;
114 tempFiles = new TempFiles("fanfix-test");
9fe3f177 115 try {
2aac79c7 116 File tmpConfig = tempFiles.createTempDir("fanfix-config");
dabc4b95 117 File localCache = new File("test/CACHE");
b0c0a80e 118 prepareCache(localCache, forceRefresh);
2aac79c7 119
2aac79c7
NR
120 ConfigBundle config = new ConfigBundle();
121 Bundles.setDirectory(tmpConfig.getAbsolutePath());
06befaee
NR
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);
2aac79c7 125 config.updateFile(tmpConfig.getPath());
2aac79c7 126 System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath());
22848428 127
06befaee 128 Instance.init(true);
d66deb8d 129 Instance.getInstance().getCache().setOffline(offline);
06befaee 130
b0c0a80e 131 TestLauncher tests = new Test(args, urlsAllowed);
dabc4b95
NR
132 tests.setDetails(verbose);
133
134 result = tests.launch();
22848428 135
2aac79c7 136 IOUtils.deltree(tmpConfig);
b0c0a80e 137 prepareCache(localCache, forceRefresh);
2aac79c7
NR
138 } finally {
139 // Test temp files
140 tempFiles.close();
141
142 // This is usually done in Fanfix.Main:
d66deb8d 143 Instance.getInstance().getTempFiles().close();
2aac79c7 144 }
22848428
NR
145
146 System.exit(result);
92fb0719 147 }
b0c0a80e
NR
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 }
92fb0719 177}