tests: allow testing live URLs
[fanfix.git] / src / be / nikiroo / fanfix / 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
NR
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
2aac79c7
NR
31 /**
32 * The temporary files handler.
33 */
34 static TempFiles tempFiles;
35
2a25f781
NR
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
dabc4b95
NR
42 *
43 * @throws IOException
2a25f781 44 */
dabc4b95 45 public Test(String[] args) throws IOException {
68e370a4 46 super("Fanfix", args);
581d42c0 47 Instance.setTraceHandler(null);
8d59ce07
NR
48 addSeries(new BasicSupportUtilitiesTest(args));
49 addSeries(new BasicSupportDeprecatedTest(args));
6cac0e45 50 addSeries(new LibraryTest(args));
06befaee 51
dabc4b95
NR
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
06befaee
NR
73 addSeries(new ConversionTest(file.getName(), uri, expectedDir,
74 resultDir, args));
dabc4b95
NR
75 }
76 }
92fb0719
NR
77 }
78
79 /**
80 * Main entry point of the program.
81 *
82 * @param args
83 * the arguments passed to the {@link TestLauncher}s.
22848428 84 * @throws IOException
2a25f781 85 * in case of I/O error
92fb0719 86 */
22848428 87 static public void main(String[] args) throws IOException {
8d59ce07 88 Instance.init();
dabc4b95
NR
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
2aac79c7
NR
100 int result = 0;
101 tempFiles = new TempFiles("fanfix-test");
9fe3f177 102 try {
2aac79c7 103 File tmpConfig = tempFiles.createTempDir("fanfix-config");
dabc4b95
NR
104 File localCache = new File("test/CACHE");
105 if (forceRefresh) {
106 IOUtils.deltree(localCache);
107 }
108 localCache.mkdirs();
2aac79c7 109
2aac79c7
NR
110 ConfigBundle config = new ConfigBundle();
111 Bundles.setDirectory(tmpConfig.getAbsolutePath());
06befaee
NR
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);
2aac79c7 115 config.updateFile(tmpConfig.getPath());
2aac79c7 116 System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath());
22848428 117
06befaee
NR
118 Instance.init(true);
119 Instance.getCache().setOffline(offline);
120
dabc4b95
NR
121 TestLauncher tests = new Test(args);
122 tests.setDetails(verbose);
123
124 result = tests.launch();
22848428 125
2aac79c7 126 IOUtils.deltree(tmpConfig);
2aac79c7
NR
127 } finally {
128 // Test temp files
129 tempFiles.close();
130
131 // This is usually done in Fanfix.Main:
132 Instance.getTempFiles().close();
133 }
22848428
NR
134
135 System.exit(result);
92fb0719
NR
136 }
137}