2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.configuration.plist;
21
import java.io.StringReader;
22
import java.util.Calendar;
23
import java.util.Iterator;
24
import java.util.List;
25
import java.util.Date;
26
import java.util.TimeZone;
28
import junit.framework.TestCase;
29
import junitx.framework.ArrayAssert;
30
import junitx.framework.ListAssert;
31
import junitx.framework.ObjectAssert;
32
import org.apache.commons.configuration.Configuration;
33
import org.apache.commons.configuration.ConfigurationComparator;
34
import org.apache.commons.configuration.ConfigurationException;
35
import org.apache.commons.configuration.StrictConfigurationComparator;
38
* @author Emmanuel Bourg
39
* @version $Revision: 952624 $, $Date: 2010-06-08 14:00:58 +0200 (Di, 08. Jun 2010) $
41
public class TestPropertyListConfiguration extends TestCase
43
private PropertyListConfiguration config;
45
private String testProperties = new File("conf/test.plist").getAbsolutePath();
47
protected void setUp() throws Exception
49
config = new PropertyListConfiguration();
50
config.setFileName(testProperties);
54
public void testLoad()
56
assertFalse("the configuration is empty", config.isEmpty());
59
public void testLoadWithError()
61
config = new PropertyListConfiguration();
63
config.load(new StringReader(""));
64
fail("No exception thrown on loading an empty file");
65
} catch (ConfigurationException e) {
67
assertNotNull(e.getMessage());
71
public void testString()
73
assertEquals("simple-string", "string1", config.getProperty("simple-string"));
76
public void testQuotedString()
78
assertEquals("quoted-string", "string2", config.getProperty("quoted-string"));
79
assertEquals("quoted-string2", "this is a string", config.getProperty("quoted-string2"));
80
assertEquals("complex-string", "this is a \"complex\" string {(=,;)}", config.getProperty("complex-string"));
83
public void testEmptyArray()
85
String key = "empty-array";
86
assertNotNull("array null", config.getProperty(key));
88
List list = (List) config.getProperty(key);
89
assertTrue("array is not empty", list.isEmpty());
92
public void testArray()
95
assertNotNull("array null", config.getProperty(key));
97
List list = (List) config.getProperty(key);
98
assertFalse("array is empty", list.isEmpty());
100
assertEquals("1st value", "value1", list.get(0));
101
assertEquals("2nd value", "value2", list.get(1));
102
assertEquals("3rd value", "value3", list.get(2));
105
public void testNestedArrays()
107
String key = "nested-arrays";
109
Object array = config.getProperty(key);
112
assertNotNull("array not found", array);
113
ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
114
List list = config.getList(key);
116
assertFalse("empty array", list.isEmpty());
117
assertEquals("size", 2, list.size());
120
ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0));
121
List list1 = (List) list.get(0);
122
assertFalse("nested array 1 is empty", list1.isEmpty());
123
assertEquals("size", 2, list1.size());
124
assertEquals("1st element", "a", list1.get(0));
125
assertEquals("2nd element", "b", list1.get(1));
128
ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1));
129
List list2 = (List) list.get(1);
130
assertFalse("nested array 2 is empty", list2.isEmpty());
131
assertEquals("size", 2, list2.size());
132
assertEquals("1st element", "c", list2.get(0));
133
assertEquals("2nd element", "d", list2.get(1));
136
public void testDictionary()
138
assertEquals("1st element in dictionary", "bar1", config.getProperty("dictionary.foo1"));
139
assertEquals("2nd element in dictionary", "bar2", config.getProperty("dictionary.foo2"));
142
public void testDictionaryArray()
144
String key = "dictionary-array";
146
Object array = config.getProperty(key);
149
assertNotNull("array not found", array);
150
ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
151
List list = config.getList(key);
153
assertFalse("empty array", list.isEmpty());
154
assertEquals("size", 2, list.size());
157
ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0));
158
Configuration conf1 = (Configuration) list.get(0);
159
assertFalse("configuration 1 is empty", conf1.isEmpty());
160
assertEquals("configuration element", "bar", conf1.getProperty("foo"));
163
ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1));
164
Configuration conf2 = (Configuration) list.get(1);
165
assertFalse("configuration 2 is empty", conf2.isEmpty());
166
assertEquals("configuration element", "value", conf2.getProperty("key"));
169
public void testNestedDictionaries()
171
assertEquals("nested property", "value", config.getString("nested-dictionaries.foo.bar.key"));
174
public void testData()
176
ObjectAssert.assertInstanceOf("data", (new byte[0]).getClass(), config.getProperty("data"));
177
ArrayAssert.assertEquals("data", "foo bar".getBytes(), (byte[]) config.getProperty("data"));
180
public void testDate() throws Exception
182
Calendar cal = Calendar.getInstance();
184
cal.set(2002, 2, 22, 11, 30, 0);
185
cal.setTimeZone(TimeZone.getTimeZone("GMT+0100"));
186
Date date = cal.getTime();
188
assertEquals("date", date, config.getProperty("date"));
191
public void testSave() throws Exception
193
File savedFile = new File("target/testsave.plist");
195
// remove the file previously saved if necessary
196
if (savedFile.exists())
198
assertTrue(savedFile.delete());
201
// save the configuration
202
String filename = savedFile.getAbsolutePath();
203
config.save(filename);
205
assertTrue("The saved file doesn't exist", savedFile.exists());
207
// read the configuration and compare the properties
208
Configuration checkConfig = new PropertyListConfiguration(new File(filename));
210
Iterator it = config.getKeys();
213
String key = (String) it.next();
214
assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
216
Object value = checkConfig.getProperty(key);
217
if (value instanceof byte[])
219
byte[] array = (byte[]) value;
220
ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
222
else if (value instanceof List)
224
List list1 = (List) config.getProperty(key);
225
List list2 = (List) value;
227
assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
229
for (int i = 0; i < list2.size(); i++)
231
Object value1 = list1.get(i);
232
Object value2 = list2.get(i);
234
if (value1 instanceof Configuration)
236
ConfigurationComparator comparator = new StrictConfigurationComparator();
237
assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2));
241
assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
245
ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1);
249
assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
255
public void testSaveEmptyDictionary() throws Exception
257
File savedFile = new File("target/testsave.plist");
259
// remove the file previously saved if necessary
260
if (savedFile.exists())
262
assertTrue(savedFile.delete());
265
// save the configuration
266
String filename = savedFile.getAbsolutePath();
267
config.save(filename);
269
assertTrue("The saved file doesn't exist", savedFile.exists());
271
// read the configuration and compare the properties
272
PropertyListConfiguration checkConfig = new PropertyListConfiguration(new File(filename));
274
assertFalse(config.getRootNode().getChildren("empty-dictionary").isEmpty());
275
assertFalse(checkConfig.getRootNode().getChildren("empty-dictionary").isEmpty());
278
public void testQuoteString()
280
assertEquals("null string", null, config.quoteString(null));
281
assertEquals("simple string", "abcd", config.quoteString("abcd"));
282
assertEquals("string with a space", "\"ab cd\"", config.quoteString("ab cd"));
283
assertEquals("string with a quote", "\"foo\\\"bar\"", config.quoteString("foo\"bar"));
284
assertEquals("string with a special char", "\"foo;bar\"", config.quoteString("foo;bar"));
288
* Ensure that setProperty doesn't alter an array of byte
289
* since it's a first class type in plist file
291
public void testSetDataProperty() throws Exception
293
byte[] expected = new byte[]{1, 2, 3, 4};
294
PropertyListConfiguration config = new PropertyListConfiguration();
295
config.setProperty("foo", expected);
296
config.save("target/testdata.plist");
298
PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist");
299
Object array = config2.getProperty("foo");
301
assertNotNull("data not found", array);
302
assertEquals("property type", byte[].class, array.getClass());
303
ArrayAssert.assertEquals(expected, (byte[]) array);
307
* Ensure that addProperty doesn't alter an array of byte
309
public void testAddDataProperty() throws Exception
311
byte[] expected = new byte[]{1, 2, 3, 4};
312
PropertyListConfiguration config = new PropertyListConfiguration();
313
config.addProperty("foo", expected);
314
config.save("target/testdata.plist");
316
PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist");
317
Object array = config2.getProperty("foo");
319
assertNotNull("data not found", array);
320
assertEquals("property type", byte[].class, array.getClass());
321
ArrayAssert.assertEquals(expected, (byte[]) array);
324
public void testInitCopy()
326
PropertyListConfiguration copy = new PropertyListConfiguration(config);
327
assertFalse("Nothing was copied", copy.isEmpty());
331
* Tests parsing a date with an invalid numeric value.
333
public void testParseDateNoNumber()
337
PropertyListConfiguration
338
.parseDate("<*D2002-03-22 1c:30:00 +0100>");
339
fail("Could parse date with an invalid number!");
341
catch (ParseException pex)
348
* Tests parsing a date that is not long enough.
350
public void testParseDateTooShort()
354
PropertyListConfiguration.parseDate("<*D2002-03-22 11:3>");
355
fail("Could parse too short date!");
357
catch (ParseException pex)
364
* Tests parsing a date that contains an invalid separator character.
366
public void testParseDateInvalidChar()
370
PropertyListConfiguration
371
.parseDate("<*D2002+03-22 11:30:00 +0100>");
372
fail("Could parse date with an invalid separator!");
374
catch (ParseException pex)
381
* Tries parsing a null date. This should cause an exception.n
383
public void testParseDateNull()
387
PropertyListConfiguration.parseDate(null);
388
fail("Could parse null date!");
390
catch (ParseException pex)
397
* Tests formatting a date.
399
public void testFormatDate()
401
Calendar cal = Calendar.getInstance();
403
cal.set(2007, 9, 29, 23, 4, 30);
404
cal.setTimeZone(TimeZone.getTimeZone("GMT-0230"));
405
assertEquals("Wrong date literal (1)", "<*D2007-10-29 23:04:30 -0230>",
406
PropertyListConfiguration.formatDate(cal));
408
cal.set(2007, 9, 30, 22, 2, 15);
409
cal.setTimeZone(TimeZone.getTimeZone("GMT+1111"));
410
assertEquals("Wrong date literal (2)", "<*D2007-10-30 22:02:15 +1111>",
411
PropertyListConfiguration.formatDate(cal));