1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.plist;
18
19 import java.io.File;
20 import java.util.*;
21
22 import junit.framework.TestCase;
23 import junitx.framework.ObjectAssert;
24 import junitx.framework.ArrayAssert;
25 import junitx.framework.ListAssert;
26 import org.apache.commons.configuration.FileConfiguration;
27 import org.apache.commons.configuration.Configuration;
28 import org.apache.commons.configuration.StrictConfigurationComparator;
29 import org.apache.commons.configuration.ConfigurationComparator;
30
31 /***
32 * @author Emmanuel Bourg
33 * @version $Revision$, $Date$
34 */
35 public class TestXMLPropertyListConfiguration extends TestCase
36 {
37 private FileConfiguration config;
38
39 protected void setUp() throws Exception
40 {
41 config = new XMLPropertyListConfiguration();
42 config.setFileName("conf/test.plist.xml");
43 config.load();
44 }
45
46 public void testString() throws Exception
47 {
48 assertEquals("'string' property", "value1", config.getString("string"));
49 }
50
51 public void testInteger() throws Exception
52 {
53 assertEquals("'integer' property", 12345, config.getInt("integer"));
54 }
55
56 public void testReal() throws Exception
57 {
58 assertEquals("'real' property", -12.345, config.getDouble("real"), 0);
59 }
60
61 public void testBoolean() throws Exception
62 {
63 assertEquals("'boolean1' property", true, config.getBoolean("boolean1"));
64 assertEquals("'boolean2' property", false, config.getBoolean("boolean2"));
65 }
66
67 public void testDictionary()
68 {
69 assertEquals("1st element", "value1", config.getProperty("dictionary.key1"));
70 assertEquals("2nd element", "value2", config.getProperty("dictionary.key2"));
71 assertEquals("3rd element", "value3", config.getProperty("dictionary.key3"));
72 }
73
74 public void testSubset()
75 {
76 Configuration subset = config.subset("dictionary");
77 Iterator keys = subset.getKeys();
78
79 String key = (String) keys.next();
80 assertEquals("1st key", "key1", key);
81 assertEquals("1st value", "value1", subset.getString(key));
82
83 key = (String) keys.next();
84 assertEquals("2nd key", "key2", key);
85 assertEquals("2nd value", "value2", subset.getString(key));
86
87 key = (String) keys.next();
88 assertEquals("3rd key", "key3", key);
89 assertEquals("3rd value", "value3", subset.getString(key));
90
91 assertFalse("more than 3 properties founds", keys.hasNext());
92 }
93
94 public void testArray()
95 {
96 Object array = config.getProperty("array");
97
98 assertNotNull("array not found", array);
99 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
100 List list = config.getList("array");
101
102 assertFalse("empty array", list.isEmpty());
103 assertEquals("size", 3, list.size());
104 assertEquals("1st element", "value1", list.get(0));
105 assertEquals("2nd element", "value2", list.get(1));
106 assertEquals("3rd element", "value3", list.get(2));
107 }
108
109 public void testNestedArray()
110 {
111 String key = "nested-array";
112
113 Object array = config.getProperty(key);
114
115
116 assertNotNull("array not found", array);
117 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
118 List list = config.getList(key);
119
120 assertFalse("empty array", list.isEmpty());
121 assertEquals("size", 2, list.size());
122
123
124 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0));
125 List list1 = (List) list.get(0);
126 assertFalse("nested array 1 is empty", list1.isEmpty());
127 assertEquals("size", 2, list1.size());
128 assertEquals("1st element", "a", list1.get(0));
129 assertEquals("2nd element", "b", list1.get(1));
130
131
132 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1));
133 List list2 = (List) list.get(1);
134 assertFalse("nested array 2 is empty", list2.isEmpty());
135 assertEquals("size", 2, list2.size());
136 assertEquals("1st element", "c", list2.get(0));
137 assertEquals("2nd element", "d", list2.get(1));
138 }
139
140 public void testDictionaryArray()
141 {
142 String key = "dictionary-array";
143
144 Object array = config.getProperty(key);
145
146
147 assertNotNull("array not found", array);
148 ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
149 List list = config.getList(key);
150
151 assertFalse("empty array", list.isEmpty());
152 assertEquals("size", 2, list.size());
153
154
155 ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0));
156 Configuration conf1 = (Configuration) list.get(0);
157 assertFalse("configuration 1 is empty", conf1.isEmpty());
158 assertEquals("configuration element", "bar", conf1.getProperty("foo"));
159
160
161 ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1));
162 Configuration conf2 = (Configuration) list.get(1);
163 assertFalse("configuration 2 is empty", conf2.isEmpty());
164 assertEquals("configuration element", "value", conf2.getProperty("key"));
165 }
166
167 public void testNested()
168 {
169 assertEquals("nested property", "value", config.getString("nested.node1.node2.node3"));
170 }
171
172 public void testSave() throws Exception
173 {
174 File savedFile = new File("target/testsave.plist.xml");
175
176
177 if (savedFile.exists())
178 {
179 assertTrue(savedFile.delete());
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 String filename = savedFile.getAbsolutePath();
204 config.save(filename);
205
206 assertTrue("The saved file doesn't exist", savedFile.exists());
207
208
209 Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename));
210
211 Iterator it = config.getKeys();
212 while (it.hasNext())
213 {
214 String key = (String) it.next();
215 assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
216
217 Object value = checkConfig.getProperty(key);
218 if (value instanceof byte[])
219 {
220 byte[] array = (byte[]) value;
221 ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
222 }
223 else if (value instanceof List)
224 {
225 List list1 = (List) config.getProperty(key);
226 List list2 = (List) value;
227
228 assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
229
230 for (int i = 0; i < list2.size(); i++)
231 {
232 Object value1 = list1.get(i);
233 Object value2 = list2.get(i);
234
235 if (value1 instanceof Configuration)
236 {
237 ConfigurationComparator comparator = new StrictConfigurationComparator();
238 assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2));
239 }
240 else
241 {
242 assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
243 }
244 }
245
246 ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1);
247 }
248 else
249 {
250 assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
251 }
252
253 }
254 }
255 }