Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / utils / test_code / BundleTest.java
CommitLineData
fbcc2a2a 1package be.nikiroo.utils.test_code;
80383c14
NR
2
3import java.io.File;
e9ca6bb8
NR
4import java.util.ArrayList;
5import java.util.List;
80383c14
NR
6
7import be.nikiroo.utils.IOUtils;
8import be.nikiroo.utils.resources.Bundle;
9import be.nikiroo.utils.resources.Bundles;
10import be.nikiroo.utils.resources.Meta;
fbcc2a2a
NR
11import be.nikiroo.utils.test.TestCase;
12import be.nikiroo.utils.test.TestLauncher;
80383c14 13
32ae2079 14class BundleTest extends TestLauncher {
80383c14
NR
15 private File tmp;
16 private B b = new B();
17
80383c14
NR
18 public BundleTest(String[] args) {
19 this("Bundle test", args);
20 }
21
22 protected BundleTest(String name, String[] args) {
23 super(name, args);
24
e9ca6bb8
NR
25 for (TestCase test : getSimpleTests()) {
26 addTest(test);
27 }
80383c14 28
e9ca6bb8
NR
29 addSeries(new TestLauncher("After saving/reloading the resources", args) {
30 {
31 for (TestCase test : getSimpleTests()) {
32 addTest(test);
80383c14 33 }
e9ca6bb8 34 }
80383c14 35
e9ca6bb8
NR
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 {
e9ca6bb8 57 String def = b.getString(E.ONE);
487926f7
NR
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
e9ca6bb8
NR
67 b.reload(false);
68
e9ca6bb8 69 assertEquals("We should have reloaded the same files",
487926f7 70 val, b.getString(E.ONE));
e9ca6bb8
NR
71
72 // reset values for next tests
487926f7
NR
73 b.reload(true);
74 b.updateFile();
e9ca6bb8
NR
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
487926f7 88 b.restoreSnapshot(null);
e9ca6bb8
NR
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);
487926f7 99 Object snap = b.takeSnapshot();
e9ca6bb8 100
487926f7 101 b.restoreSnapshot(null);
e9ca6bb8
NR
102 assertEquals(
103 "restoreChanges(null) should clear the changes",
104 def, b.getString(E.ONE));
487926f7 105 b.restoreSnapshot(snap);
e9ca6bb8
NR
106 assertEquals(
107 "restoreChanges(snapshot) should restore the changes",
108 val, b.getString(E.ONE));
109
110 // reset values for next tests
487926f7 111 b.restoreSnapshot(null);
e9ca6bb8
NR
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 });
80383c14
NR
150 }
151
e9ca6bb8 152 private List<TestCase> getSimpleTests() {
80383c14
NR
153 String pre = "";
154
e9ca6bb8
NR
155 List<TestCase> list = new ArrayList<TestCase>();
156
157 list.add(new TestCase(pre + "getString simple") {
80383c14
NR
158 @Override
159 public void test() throws Exception {
160 assertEquals("un", b.getString(E.ONE));
161 }
162 });
163
e9ca6bb8 164 list.add(new TestCase(pre + "getStringX with null suffix") {
80383c14
NR
165 @Override
166 public void test() throws Exception {
167 assertEquals("un", b.getStringX(E.ONE, null));
168 }
169 });
170
e9ca6bb8 171 list.add(new TestCase(pre + "getStringX with empty suffix") {
80383c14
NR
172 @Override
173 public void test() throws Exception {
174 assertEquals(null, b.getStringX(E.ONE, ""));
175 }
176 });
177
e9ca6bb8 178 list.add(new TestCase(pre + "getStringX with existing suffix") {
80383c14
NR
179 @Override
180 public void test() throws Exception {
181 assertEquals("un + suffix", b.getStringX(E.ONE, "suffix"));
182 }
183 });
184
e9ca6bb8 185 list.add(new TestCase(pre + "getStringX with not existing suffix") {
80383c14
NR
186 @Override
187 public void test() throws Exception {
188 assertEquals(null, b.getStringX(E.ONE, "fake"));
189 }
190 });
191
e9ca6bb8 192 list.add(new TestCase(pre + "getString with UTF-8 content") {
80383c14
NR
193 @Override
194 public void test() throws Exception {
195 assertEquals("日本語 Nihongo", b.getString(E.JAPANESE));
196 }
197 });
e9ca6bb8
NR
198
199 return list;
80383c14
NR
200 }
201
202 /**
203 * {@link Bundle}.
204 *
205 * @author niki
206 */
207 private class B extends Bundle<E> {
208 protected B() {
db31c358 209 super(E.class, N.bundle_test, null);
80383c14
NR
210 }
211
e9ca6bb8
NR
212 @Override
213 // ...and make it public
487926f7
NR
214 public Object takeSnapshot() {
215 return super.takeSnapshot();
e9ca6bb8
NR
216 }
217
218 @Override
219 // ...and make it public
487926f7
NR
220 public void restoreSnapshot(Object snap) {
221 super.restoreSnapshot(snap);
e9ca6bb8 222 }
80383c14
NR
223 }
224
225 /**
226 * Key enum for the {@link Bundle}.
227 *
228 * @author niki
229 */
230 private enum E {
db31c358 231 @Meta
80383c14 232 ONE, //
db31c358 233 @Meta
80383c14 234 ONE_SUFFIX, //
db31c358 235 @Meta
80383c14 236 TWO, //
db31c358 237 @Meta
80383c14
NR
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}