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