1 package be
.nikiroo
.fanfix
.test
;
4 import java
.io
.IOException
;
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
;
19 public class Test
extends TestLauncher
{
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
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).
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
37 * The temporary files handler.
39 static TempFiles tempFiles
;
42 * Create the Fanfix {@link TestLauncher}.
45 * the arguments to configure the number of columns and the ok/ko
48 * allow testing URLs (<tt>.url</tt> files)
52 public Test(String
[] args
, boolean urlsAllowed
) throws IOException
{
53 super("Fanfix", args
);
54 Instance
.setTraceHandler(null);
55 addSeries(new BasicSupportUtilitiesTest(args
));
56 addSeries(new BasicSupportDeprecatedTest(args
));
57 addSeries(new LibraryTest(args
));
59 File sources
= new File("test/");
60 if (sources
.isDirectory()) {
61 for (File file
: sources
.listFiles()) {
62 if (file
.isDirectory()) {
66 String expectedDir
= new File(file
.getParentFile(), "expected_"
67 + file
.getName()).getAbsolutePath();
68 String resultDir
= new File(file
.getParentFile(), "result_"
69 + file
.getName()).getAbsolutePath();
72 if (urlsAllowed
&& file
.getName().endsWith(".url")) {
73 uri
= IOUtils
.readSmallFile(file
).trim();
74 } else if (file
.getName().endsWith(".story")) {
75 uri
= file
.getAbsolutePath();
80 addSeries(new ConversionTest(file
.getName(), uri
, expectedDir
,
87 * Main entry point of the program.
90 * the arguments passed to the {@link TestLauncher}s.
92 * in case of I/O error
94 static public void main(String
[] args
) throws IOException
{
98 boolean verbose
= new File("test/VERBOSE").exists();
101 boolean forceRefresh
= new File("test/FORCE_REFRESH").exists();
104 boolean urlsAllowed
= new File("test/URLS").exists();
107 // Only download files if allowed:
108 boolean offline
= new File("test/OFFLINE").exists();
109 Instance
.getCache().setOffline(offline
);
114 tempFiles
= new TempFiles("fanfix-test");
116 File tmpConfig
= tempFiles
.createTempDir("fanfix-config");
117 File localCache
= new File("test/CACHE");
118 prepareCache(localCache
, forceRefresh
);
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());
129 Instance
.getCache().setOffline(offline
);
131 TestLauncher tests
= new Test(args
, urlsAllowed
);
132 tests
.setDetails(verbose
);
134 result
= tests
.launch();
136 IOUtils
.deltree(tmpConfig
);
137 prepareCache(localCache
, forceRefresh
);
142 // This is usually done in Fanfix.Main:
143 Instance
.getTempFiles().close();
150 * Prepare the cache (or clean it up).
152 * The cache directory will always exist if this method succeed
155 * the cache directory
156 * @param forceRefresh
157 * TRUE to force acache refresh (delete all files)
159 * @throw IOException if the cache cannot be created
161 private static void prepareCache(File localCache
, boolean forceRefresh
)
166 if (!localCache
.isDirectory()) {
167 throw new IOException("Cannot get a cache");
170 // delete local cached files (_*) or all files if forceRefresh
171 for (File f
: localCache
.listFiles()) {
172 if (forceRefresh
|| f
.getName().startsWith("_")) {