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 java.io.File;
21 import java.util.*;
22
23 import junit.framework.TestCase;
24 import junitx.framework.ObjectAssert;
25 import junitx.framework.ArrayAssert;
26 import junitx.framework.ListAssert;
27 import org.apache.commons.configuration.FileConfiguration;
28 import org.apache.commons.configuration.Configuration;
29 import org.apache.commons.configuration.HierarchicalConfiguration;
30 import org.apache.commons.configuration.StrictConfigurationComparator;
31 import org.apache.commons.configuration.ConfigurationComparator;
32
33 /***
34 * @author Emmanuel Bourg
35 * @version $Revision: 645568 $, $Date: 2008-04-07 17:37:45 +0200 (Mo, 07 Apr 2008) $
36 */
37 public class TestXMLPropertyListConfiguration extends TestCase
38 {
39 private FileConfiguration config;
40
41 protected void setUp() throws Exception
42 {
43 config = new XMLPropertyListConfiguration();
44 config.setFileName("conf/test.plist.xml");
45 config.load();
46 }
47
48 public void testString() throws Exception
49 {
50 assertEquals("'string' property", "value1", config.getString("string"));
51 }
52
53 public void testInteger() throws Exception
54 {
55 assertEquals("'integer' property", 12345678900L, config.getLong("integer"));
56 }
57
58 public void testReal() throws Exception
59 {
60 assertEquals("'real' property", -12.345, config.getDouble("real"), 0);
61 }
62
63 public void testBoolean() throws Exception
64 {
65 assertEquals("'boolean1' property", true, config.getBoolean("boolean1"));
66 assertEquals("'boolean2' property", false, config.getBoolean("boolean2"));
67 }
68
69 public void testDictionary()
70 {
71 assertEquals("1st element", "value1", config.getProperty("dictionary.key1"));
72 assertEquals("2nd element", "value2", config.getProperty("dictionary.key2"));
73 assertEquals("3rd element", "value3", config.getProperty("dictionary.key3"));
74 }
75
76 public void testDate() throws Exception
77 {
78 Calendar calendar = Calendar.getInstance();
79 calendar.clear();
80 calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
81 calendar.set(2005, Calendar.JANUARY, 1, 12, 0, 0);
82
83 assertEquals("'date' property", calendar.getTime(), config.getProperty("date"));
84
85 calendar.setTimeZone(TimeZone.getTimeZone("CET"));
86 calendar.set(2002, Calendar.MARCH, 22, 11, 30, 0);
87
88 assertEquals("'date-gnustep' property", calendar.getTime(), config.getProperty("date-gnustep"));
89 }
90
91 public void testSubset()
92 {
93 Configuration subset = config.subset("dictionary");
94 Iterator keys = subset.getKeys();
95
96 String key = (String) keys.next();
97 assertEquals("1st key", "key1", key);
98 assertEquals("1st value", "value1", subset.getString(key));
99
100 key = (String) keys.next();
101 assertEquals("2nd key", "key2", key);
102 assertEquals("2nd value", "value2", subset.getString(key));
103
104 key = (String) keys.next();
105 assertEquals("3rd key", "key3", key);
106 assertEquals("3rd value", "value3", subset.getString(key));
107
108 assertFalse("more than 3 properties founds", keys.hasNext());
109 }
110
111 public void testArray()
112 {
113 Object array = config.getProperty("array");
114
115 assertNotNull("array not found", array);
116 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
117 List list = config.getList("array");
118
119 assertFalse("empty array", list.isEmpty());
120 assertEquals("size", 3, list.size());
121 assertEquals("1st element", "value1", list.get(0));
122 assertEquals("2nd element", "value2", list.get(1));
123 assertEquals("3rd element", "value3", list.get(2));
124 }
125
126 public void testNestedArray()
127 {
128 String key = "nested-array";
129
130 Object array = config.getProperty(key);
131
132
133 assertNotNull("array not found", array);
134 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
135 List list = config.getList(key);
136
137 assertFalse("empty array", list.isEmpty());
138 assertEquals("size", 2, list.size());
139
140
141 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0));
142 List list1 = (List) list.get(0);
143 assertFalse("nested array 1 is empty", list1.isEmpty());
144 assertEquals("size", 2, list1.size());
145 assertEquals("1st element", "a", list1.get(0));
146 assertEquals("2nd element", "b", list1.get(1));
147
148
149 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1));
150 List list2 = (List) list.get(1);
151 assertFalse("nested array 2 is empty", list2.isEmpty());
152 assertEquals("size", 2, list2.size());
153 assertEquals("1st element", "c", list2.get(0));
154 assertEquals("2nd element", "d", list2.get(1));
155 }
156
157 public void testDictionaryArray()
158 {
159 String key = "dictionary-array";
160
161 Object array = config.getProperty(key);
162
163
164 assertNotNull("array not found", array);
165 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
166 List list = config.getList(key);
167
168 assertFalse("empty array", list.isEmpty());
169 assertEquals("size", 2, list.size());
170
171
172 ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0));
173 Configuration conf1 = (Configuration) list.get(0);
174 assertFalse("configuration 1 is empty", conf1.isEmpty());
175 assertEquals("configuration element", "bar", conf1.getProperty("foo"));
176
177
178 ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1));
179 Configuration conf2 = (Configuration) list.get(1);
180 assertFalse("configuration 2 is empty", conf2.isEmpty());
181 assertEquals("configuration element", "value", conf2.getProperty("key"));
182 }
183
184 public void testNested()
185 {
186 assertEquals("nested property", "value", config.getString("nested.node1.node2.node3"));
187 }
188
189 public void testSave() throws Exception
190 {
191 File savedFile = new File("target/testsave.plist.xml");
192
193
194 if (savedFile.exists())
195 {
196 assertTrue(savedFile.delete());
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 String filename = savedFile.getAbsolutePath();
221 config.save(filename);
222
223 assertTrue("The saved file doesn't exist", savedFile.exists());
224
225
226 Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename));
227
228 Iterator it = config.getKeys();
229 while (it.hasNext())
230 {
231 String key = (String) it.next();
232 assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
233
234 Object value = checkConfig.getProperty(key);
235 if (value instanceof byte[])
236 {
237 byte[] array = (byte[]) value;
238 ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
239 }
240 else if (value instanceof List)
241 {
242 List list1 = (List) config.getProperty(key);
243 List list2 = (List) value;
244
245 assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
246
247 for (int i = 0; i < list2.size(); i++)
248 {
249 Object value1 = list1.get(i);
250 Object value2 = list2.get(i);
251
252 if (value1 instanceof Configuration)
253 {
254 ConfigurationComparator comparator = new StrictConfigurationComparator();
255 assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2));
256 }
257 else
258 {
259 assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
260 }
261 }
262
263 ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1);
264 }
265 else
266 {
267 assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
268 }
269
270 }
271 }
272
273 /***
274 * Ensure that setProperty doesn't alter an array of byte
275 * since it's a first class type in plist file
276 */
277 public void testSetDataProperty() throws Exception
278 {
279 byte[] expected = new byte[]{1, 2, 3, 4};
280 XMLPropertyListConfiguration config = new XMLPropertyListConfiguration();
281 config.setProperty("foo", expected);
282 config.save("target/testdata.plist.xml");
283
284 XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml");
285 Object array = config2.getProperty("foo");
286
287 assertNotNull("data not found", array);
288 assertEquals("property type", byte[].class, array.getClass());
289 ArrayAssert.assertEquals(expected, (byte[]) array);
290 }
291
292 /***
293 * Ensure that addProperty doesn't alter an array of byte
294 */
295 public void testAddDataProperty() throws Exception
296 {
297 byte[] expected = new byte[]{1, 2, 3, 4};
298 XMLPropertyListConfiguration config = new XMLPropertyListConfiguration();
299 config.addProperty("foo", expected);
300 config.save("target/testdata.plist.xml");
301
302 XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml");
303 Object array = config2.getProperty("foo");
304
305 assertNotNull("data not found", array);
306 assertEquals("property type", byte[].class, array.getClass());
307 ArrayAssert.assertEquals(expected, (byte[]) array);
308 }
309
310 public void testInitCopy()
311 {
312 XMLPropertyListConfiguration copy = new XMLPropertyListConfiguration((HierarchicalConfiguration) config);
313 StrictConfigurationComparator comp = new StrictConfigurationComparator();
314 assertTrue("Configurations are not equal", comp.compare(config, copy));
315 }
316 }