1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration.plist;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.util.Calendar;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.TimeZone;
30
31 import junitx.framework.ArrayAssert;
32 import junitx.framework.ListAssert;
33 import junitx.framework.ObjectAssert;
34
35 import org.apache.commons.configuration.Configuration;
36 import org.apache.commons.configuration.ConfigurationAssert;
37 import org.apache.commons.configuration.ConfigurationComparator;
38 import org.apache.commons.configuration.ConfigurationException;
39 import org.apache.commons.configuration.FileConfiguration;
40 import org.apache.commons.configuration.HierarchicalConfiguration;
41 import org.apache.commons.configuration.StrictConfigurationComparator;
42 import org.junit.Before;
43 import org.junit.Test;
44
45
46
47
48
49 public class TestXMLPropertyListConfiguration
50 {
51 private FileConfiguration config;
52
53 @Before
54 public void setUp() throws Exception
55 {
56 config = new XMLPropertyListConfiguration();
57 config.setFile(ConfigurationAssert.getTestFile("test.plist.xml"));
58 config.load();
59 }
60
61 @Test
62 public void testString() throws Exception
63 {
64 assertEquals("'string' property", "value1", config.getString("string"));
65 }
66
67 @Test
68 public void testInteger() throws Exception
69 {
70 assertEquals("'integer' property", 12345678900L, config.getLong("integer"));
71 }
72
73 @Test
74 public void testReal() throws Exception
75 {
76 assertEquals("'real' property", -12.345, config.getDouble("real"), 0);
77 }
78
79 @Test
80 public void testBoolean() throws Exception
81 {
82 assertEquals("'boolean1' property", true, config.getBoolean("boolean1"));
83 assertEquals("'boolean2' property", false, config.getBoolean("boolean2"));
84 }
85
86 @Test
87 public void testDictionary()
88 {
89 assertEquals("1st element", "value1", config.getProperty("dictionary.key1"));
90 assertEquals("2nd element", "value2", config.getProperty("dictionary.key2"));
91 assertEquals("3rd element", "value3", config.getProperty("dictionary.key3"));
92 }
93
94 @Test
95 public void testDate() throws Exception
96 {
97 Calendar calendar = Calendar.getInstance();
98 calendar.clear();
99 calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
100 calendar.set(2005, Calendar.JANUARY, 1, 12, 0, 0);
101
102 assertEquals("'date' property", calendar.getTime(), config.getProperty("date"));
103
104 calendar.setTimeZone(TimeZone.getTimeZone("CET"));
105 calendar.set(2002, Calendar.MARCH, 22, 11, 30, 0);
106
107 assertEquals("'date-gnustep' property", calendar.getTime(), config.getProperty("date-gnustep"));
108 }
109
110 @Test
111 public void testSubset()
112 {
113 Configuration subset = config.subset("dictionary");
114 Iterator<String> keys = subset.getKeys();
115
116 String key = keys.next();
117 assertEquals("1st key", "key1", key);
118 assertEquals("1st value", "value1", subset.getString(key));
119
120 key = keys.next();
121 assertEquals("2nd key", "key2", key);
122 assertEquals("2nd value", "value2", subset.getString(key));
123
124 key = keys.next();
125 assertEquals("3rd key", "key3", key);
126 assertEquals("3rd value", "value3", subset.getString(key));
127
128 assertFalse("more than 3 properties founds", keys.hasNext());
129 }
130
131 @Test
132 public void testArray()
133 {
134 Object array = config.getProperty("array");
135
136 assertNotNull("array not found", array);
137 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
138 List<?> list = config.getList("array");
139
140 assertFalse("empty array", list.isEmpty());
141 assertEquals("size", 3, list.size());
142 assertEquals("1st element", "value1", list.get(0));
143 assertEquals("2nd element", "value2", list.get(1));
144 assertEquals("3rd element", "value3", list.get(2));
145 }
146
147 @Test
148 public void testNestedArray()
149 {
150 String key = "nested-array";
151
152 Object array = config.getProperty(key);
153
154
155 assertNotNull("array not found", array);
156 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
157 List<?> list = config.getList(key);
158
159 assertFalse("empty array", list.isEmpty());
160 assertEquals("size", 2, list.size());
161
162
163 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0));
164 List<?> list1 = (List<?>) list.get(0);
165 assertFalse("nested array 1 is empty", list1.isEmpty());
166 assertEquals("size", 2, list1.size());
167 assertEquals("1st element", "a", list1.get(0));
168 assertEquals("2nd element", "b", list1.get(1));
169
170
171 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1));
172 List<?> list2 = (List<?>) list.get(1);
173 assertFalse("nested array 2 is empty", list2.isEmpty());
174 assertEquals("size", 2, list2.size());
175 assertEquals("1st element", "c", list2.get(0));
176 assertEquals("2nd element", "d", list2.get(1));
177 }
178
179 @Test
180 public void testDictionaryArray()
181 {
182 String key = "dictionary-array";
183
184 Object array = config.getProperty(key);
185
186
187 assertNotNull("array not found", array);
188 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
189 List<?> list = config.getList(key);
190
191 assertFalse("empty array", list.isEmpty());
192 assertEquals("size", 2, list.size());
193
194
195 ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0));
196 Configuration conf1 = (Configuration) list.get(0);
197 assertFalse("configuration 1 is empty", conf1.isEmpty());
198 assertEquals("configuration element", "bar", conf1.getProperty("foo"));
199
200
201 ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1));
202 Configuration conf2 = (Configuration) list.get(1);
203 assertFalse("configuration 2 is empty", conf2.isEmpty());
204 assertEquals("configuration element", "value", conf2.getProperty("key"));
205 }
206
207 @Test
208 public void testNested()
209 {
210 assertEquals("nested property", "value", config.getString("nested.node1.node2.node3"));
211 }
212
213 @Test
214 public void testSave() throws Exception
215 {
216 File savedFile = new File("target/testsave.plist.xml");
217
218
219 if (savedFile.exists())
220 {
221 assertTrue(savedFile.delete());
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 String filename = savedFile.getAbsolutePath();
246 config.save(filename);
247
248 assertTrue("The saved file doesn't exist", savedFile.exists());
249
250
251 Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename));
252
253 Iterator<String> it = config.getKeys();
254 while (it.hasNext())
255 {
256 String key = it.next();
257 assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
258
259 Object value = checkConfig.getProperty(key);
260 if (value instanceof byte[])
261 {
262 byte[] array = (byte[]) value;
263 ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
264 }
265 else if (value instanceof List)
266 {
267 List<?> list1 = (List<?>) config.getProperty(key);
268 List<?> list2 = (List<?>) value;
269
270 assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
271
272 for (int i = 0; i < list2.size(); i++)
273 {
274 Object value1 = list1.get(i);
275 Object value2 = list2.get(i);
276
277 if (value1 instanceof Configuration)
278 {
279 ConfigurationComparator comparator = new StrictConfigurationComparator();
280 assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2));
281 }
282 else
283 {
284 assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
285 }
286 }
287
288 ListAssert.assertEquals("Value of the '" + key + "' property", (List<?>) config.getProperty(key), list1);
289 }
290 else
291 {
292 assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
293 }
294
295 }
296 }
297
298 @Test
299 public void testSaveEmptyDictionary() throws Exception
300 {
301 File savedFile = new File("target/testsave.plist.xml");
302
303
304 if (savedFile.exists())
305 {
306 assertTrue(savedFile.delete());
307 }
308
309
310 String filename = savedFile.getAbsolutePath();
311 config.save(filename);
312
313 assertTrue("The saved file doesn't exist", savedFile.exists());
314
315
316 Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename));
317
318 assertEquals(null, config.getProperty("empty-dictionary"));
319 assertEquals(null, checkConfig.getProperty("empty-dictionary"));
320 }
321
322
323
324
325
326 @Test
327 public void testSetDataProperty() throws Exception
328 {
329 byte[] expected = new byte[]{1, 2, 3, 4};
330 XMLPropertyListConfiguration config = new XMLPropertyListConfiguration();
331 config.setProperty("foo", expected);
332 config.save("target/testdata.plist.xml");
333
334 XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml");
335 Object array = config2.getProperty("foo");
336
337 assertNotNull("data not found", array);
338 assertEquals("property type", byte[].class, array.getClass());
339 ArrayAssert.assertEquals(expected, (byte[]) array);
340 }
341
342
343
344
345 @Test
346 public void testAddDataProperty() throws Exception
347 {
348 byte[] expected = new byte[]{1, 2, 3, 4};
349 XMLPropertyListConfiguration config = new XMLPropertyListConfiguration();
350 config.addProperty("foo", expected);
351 config.save("target/testdata.plist.xml");
352
353 XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml");
354 Object array = config2.getProperty("foo");
355
356 assertNotNull("data not found", array);
357 assertEquals("property type", byte[].class, array.getClass());
358 ArrayAssert.assertEquals(expected, (byte[]) array);
359 }
360
361 @Test
362 public void testInitCopy()
363 {
364 XMLPropertyListConfiguration copy = new XMLPropertyListConfiguration((HierarchicalConfiguration) config);
365 StrictConfigurationComparator comp = new StrictConfigurationComparator();
366 assertTrue("Configurations are not equal", comp.compare(config, copy));
367 }
368
369
370
371
372
373
374 @Test
375 public void testLoadNoDict() throws ConfigurationException
376 {
377 XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration();
378 plist.setFile(ConfigurationAssert.getTestFile("test2.plist.xml"));
379 plist.load();
380 assertFalse("Configuration is empty", plist.isEmpty());
381 }
382
383
384
385
386
387
388 @Test
389 public void testLoadNoDictConstr() throws ConfigurationException
390 {
391 XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration(
392 ConfigurationAssert.getTestFile("test2.plist.xml"));
393 assertFalse("Configuration is empty", plist.isEmpty());
394 }
395
396
397
398
399
400 @Test
401 public void testSetDatePropertyInvalid() throws ConfigurationException
402 {
403 config.clear();
404 config.setFile(ConfigurationAssert.getTestFile("test_invalid_date.plist.xml"));
405 config.load();
406 assertEquals("'string' property", "value1", config.getString("string"));
407 assertFalse("Date property was loaded", config.containsKey("date"));
408 }
409 }