allow getMetas without synchronized
[fanfix.git] / src / be / nikiroo / utils / test_code / BundleTest.java
1 package be.nikiroo.utils.test_code;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import be.nikiroo.utils.IOUtils;
8 import be.nikiroo.utils.resources.Bundle;
9 import be.nikiroo.utils.resources.Bundles;
10 import be.nikiroo.utils.resources.Meta;
11 import be.nikiroo.utils.test.TestCase;
12 import be.nikiroo.utils.test.TestLauncher;
13
14 class BundleTest extends TestLauncher {
15 private File tmp;
16 private B b = new B();
17
18 public BundleTest(String[] args) {
19 this("Bundle test", args);
20 }
21
22 protected BundleTest(String name, String[] args) {
23 super(name, args);
24
25 for (TestCase test : getSimpleTests()) {
26 addTest(test);
27 }
28
29 addSeries(new TestLauncher("After saving/reloading the resources", args) {
30 {
31 for (TestCase test : getSimpleTests()) {
32 addTest(test);
33 }
34 }
35
36 @Override
37 protected void start() throws Exception {
38 tmp = File.createTempFile("nikiroo-utils", ".test");
39 tmp.delete();
40 tmp.mkdir();
41 b.updateFile(tmp.getAbsolutePath());
42 Bundles.setDirectory(tmp.getAbsolutePath());
43 b.reload(false);
44 }
45
46 @Override
47 protected void stop() {
48 IOUtils.deltree(tmp);
49 }
50 });
51
52 addSeries(new TestLauncher("Read/Write support", args) {
53 {
54 addTest(new TestCase("Reload") {
55 @Override
56 public void test() throws Exception {
57 String def = b.getString(E.ONE);
58 String val = "Something";
59
60 b.setString(E.ONE, val);
61 b.updateFile();
62 b.reload(true);
63
64 assertEquals("We should have reset the bundle", def,
65 b.getString(E.ONE));
66
67 b.reload(false);
68
69 assertEquals("We should have reloaded the same files",
70 val, b.getString(E.ONE));
71
72 // reset values for next tests
73 b.reload(true);
74 b.updateFile();
75 }
76 });
77
78 addTest(new TestCase("Set/Get") {
79 @Override
80 public void test() throws Exception {
81 String val = "Newp";
82 b.setString(E.ONE, val);
83 String setGet = b.getString(E.ONE);
84
85 assertEquals(val, setGet);
86
87 // reset values for next tests
88 b.restoreSnapshot(null);
89 }
90 });
91
92 addTest(new TestCase("Snapshots") {
93 @Override
94 public void test() throws Exception {
95 String val = "Newp";
96 String def = b.getString(E.ONE);
97
98 b.setString(E.ONE, val);
99 Object snap = b.takeSnapshot();
100
101 b.restoreSnapshot(null);
102 assertEquals(
103 "restoreChanges(null) should clear the changes",
104 def, b.getString(E.ONE));
105 b.restoreSnapshot(snap);
106 assertEquals(
107 "restoreChanges(snapshot) should restore the changes",
108 val, b.getString(E.ONE));
109
110 // reset values for next tests
111 b.restoreSnapshot(null);
112 }
113 });
114
115 addTest(new TestCase("updateFile with changes") {
116 @Override
117 public void test() throws Exception {
118 String val = "Go to disk! (UTF-8 test: 日本語)";
119
120 String def = b.getString(E.ONE);
121 b.setString(E.ONE, val);
122 b.updateFile(tmp.getAbsolutePath());
123 b.reload(false);
124
125 assertEquals(val, b.getString(E.ONE));
126
127 // reset values for next tests
128 b.setString(E.ONE, def);
129 b.updateFile(tmp.getAbsolutePath());
130 b.reload(false);
131 }
132 });
133 }
134
135 @Override
136 protected void start() throws Exception {
137 tmp = File.createTempFile("nikiroo-utils", ".test");
138 tmp.delete();
139 tmp.mkdir();
140 b.updateFile(tmp.getAbsolutePath());
141 Bundles.setDirectory(tmp.getAbsolutePath());
142 b.reload(false);
143 }
144
145 @Override
146 protected void stop() {
147 IOUtils.deltree(tmp);
148 }
149 });
150 }
151
152 private List<TestCase> getSimpleTests() {
153 String pre = "";
154
155 List<TestCase> list = new ArrayList<TestCase>();
156
157 list.add(new TestCase(pre + "getString simple") {
158 @Override
159 public void test() throws Exception {
160 assertEquals("un", b.getString(E.ONE));
161 }
162 });
163
164 list.add(new TestCase(pre + "getStringX with null suffix") {
165 @Override
166 public void test() throws Exception {
167 assertEquals("un", b.getStringX(E.ONE, null));
168 }
169 });
170
171 list.add(new TestCase(pre + "getStringX with empty suffix") {
172 @Override
173 public void test() throws Exception {
174 assertEquals(null, b.getStringX(E.ONE, ""));
175 }
176 });
177
178 list.add(new TestCase(pre + "getStringX with existing suffix") {
179 @Override
180 public void test() throws Exception {
181 assertEquals("un + suffix", b.getStringX(E.ONE, "suffix"));
182 }
183 });
184
185 list.add(new TestCase(pre + "getStringX with not existing suffix") {
186 @Override
187 public void test() throws Exception {
188 assertEquals(null, b.getStringX(E.ONE, "fake"));
189 }
190 });
191
192 list.add(new TestCase(pre + "getString with UTF-8 content") {
193 @Override
194 public void test() throws Exception {
195 assertEquals("日本語 Nihongo", b.getString(E.JAPANESE));
196 }
197 });
198
199 return list;
200 }
201
202 /**
203 * {@link Bundle}.
204 *
205 * @author niki
206 */
207 private class B extends Bundle<E> {
208 protected B() {
209 super(E.class, N.bundle_test, null);
210 }
211
212 @Override
213 // ...and make it public
214 public Object takeSnapshot() {
215 return super.takeSnapshot();
216 }
217
218 @Override
219 // ...and make it public
220 public void restoreSnapshot(Object snap) {
221 super.restoreSnapshot(snap);
222 }
223 }
224
225 /**
226 * Key enum for the {@link Bundle}.
227 *
228 * @author niki
229 */
230 private enum E {
231 @Meta
232 ONE, //
233 @Meta
234 ONE_SUFFIX, //
235 @Meta
236 TWO, //
237 @Meta
238 JAPANESE
239 }
240
241 /**
242 * Name enum for the {@link Bundle}.
243 *
244 * @author niki
245 */
246 private enum N {
247 bundle_test
248 }
249 }