1 package be
.nikiroo
.utils
.test_code
;
4 import java
.util
.ArrayList
;
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
;
14 class BundleTest
extends TestLauncher
{
16 private B b
= new B();
18 public BundleTest(String
[] args
) {
19 this("Bundle test", args
);
22 protected BundleTest(String name
, String
[] args
) {
25 for (TestCase test
: getSimpleTests()) {
29 addSeries(new TestLauncher("After saving/reloading the resources", args
) {
31 for (TestCase test
: getSimpleTests()) {
37 protected void start() throws Exception
{
38 tmp
= File
.createTempFile("nikiroo-utils", ".test");
41 b
.updateFile(tmp
.getAbsolutePath());
42 Bundles
.setDirectory(tmp
.getAbsolutePath());
47 protected void stop() {
52 addSeries(new TestLauncher("Read/Write support", args
) {
54 addTest(new TestCase("Reload") {
56 public void test() throws Exception
{
57 String def
= b
.getString(E
.ONE
);
58 String val
= "Something";
60 b
.setString(E
.ONE
, val
);
64 assertEquals("We should have reset the bundle", def
,
69 assertEquals("We should have reloaded the same files",
70 val
, b
.getString(E
.ONE
));
72 // reset values for next tests
78 addTest(new TestCase("Set/Get") {
80 public void test() throws Exception
{
82 b
.setString(E
.ONE
, val
);
83 String setGet
= b
.getString(E
.ONE
);
85 assertEquals(val
, setGet
);
87 // reset values for next tests
88 b
.restoreSnapshot(null);
92 addTest(new TestCase("Snapshots") {
94 public void test() throws Exception
{
96 String def
= b
.getString(E
.ONE
);
98 b
.setString(E
.ONE
, val
);
99 Object snap
= b
.takeSnapshot();
101 b
.restoreSnapshot(null);
103 "restoreChanges(null) should clear the changes",
104 def
, b
.getString(E
.ONE
));
105 b
.restoreSnapshot(snap
);
107 "restoreChanges(snapshot) should restore the changes",
108 val
, b
.getString(E
.ONE
));
110 // reset values for next tests
111 b
.restoreSnapshot(null);
115 addTest(new TestCase("updateFile with changes") {
117 public void test() throws Exception
{
118 String val
= "Go to disk! (UTF-8 test: 日本語)";
120 String def
= b
.getString(E
.ONE
);
121 b
.setString(E
.ONE
, val
);
122 b
.updateFile(tmp
.getAbsolutePath());
125 assertEquals(val
, b
.getString(E
.ONE
));
127 // reset values for next tests
128 b
.setString(E
.ONE
, def
);
129 b
.updateFile(tmp
.getAbsolutePath());
136 protected void start() throws Exception
{
137 tmp
= File
.createTempFile("nikiroo-utils", ".test");
140 b
.updateFile(tmp
.getAbsolutePath());
141 Bundles
.setDirectory(tmp
.getAbsolutePath());
146 protected void stop() {
147 IOUtils
.deltree(tmp
);
152 private List
<TestCase
> getSimpleTests() {
155 List
<TestCase
> list
= new ArrayList
<TestCase
>();
157 list
.add(new TestCase(pre
+ "getString simple") {
159 public void test() throws Exception
{
160 assertEquals("un", b
.getString(E
.ONE
));
164 list
.add(new TestCase(pre
+ "getStringX with null suffix") {
166 public void test() throws Exception
{
167 assertEquals("un", b
.getStringX(E
.ONE
, null));
171 list
.add(new TestCase(pre
+ "getStringX with empty suffix") {
173 public void test() throws Exception
{
174 assertEquals(null, b
.getStringX(E
.ONE
, ""));
178 list
.add(new TestCase(pre
+ "getStringX with existing suffix") {
180 public void test() throws Exception
{
181 assertEquals("un + suffix", b
.getStringX(E
.ONE
, "suffix"));
185 list
.add(new TestCase(pre
+ "getStringX with not existing suffix") {
187 public void test() throws Exception
{
188 assertEquals(null, b
.getStringX(E
.ONE
, "fake"));
192 list
.add(new TestCase(pre
+ "getString with UTF-8 content") {
194 public void test() throws Exception
{
195 assertEquals("日本語 Nihongo", b
.getString(E
.JAPANESE
));
207 private class B
extends Bundle
<E
> {
209 super(E
.class, N
.bundle_test
, null);
213 // ...and make it public
214 public Object
takeSnapshot() {
215 return super.takeSnapshot();
219 // ...and make it public
220 public void restoreSnapshot(Object snap
) {
221 super.restoreSnapshot(snap
);
226 * Key enum for the {@link Bundle}.
242 * Name enum for the {@link Bundle}.