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;
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
import static org.junit.Assert.fail;
27
import java.io.StringReader;
28
import java.util.Calendar;
29
import java.util.Date;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.TimeZone;
34
import junitx.framework.ArrayAssert;
35
import junitx.framework.ListAssert;
36
import junitx.framework.ObjectAssert;
38
import org.apache.commons.configuration.Configuration;
39
import org.apache.commons.configuration.ConfigurationAssert;
40
import org.apache.commons.configuration.ConfigurationComparator;
41
import org.apache.commons.configuration.ConfigurationException;
42
import org.apache.commons.configuration.StrictConfigurationComparator;
43
import org.junit.Before;
44
import org.junit.Test;
47
* @author Emmanuel Bourg
48
* @version $Id: TestPropertyListConfiguration.java 1225902 2011-12-30 19:46:24Z oheger $
50
public class TestPropertyListConfiguration
52
private PropertyListConfiguration config;
54
private String testProperties = ConfigurationAssert.getTestFile("test.plist").getAbsolutePath();
57
public void setUp() throws Exception
59
config = new PropertyListConfiguration();
60
config.setFileName(testProperties);
65
public void testLoad()
67
assertFalse("the configuration is empty", config.isEmpty());
71
public void testLoadWithError()
73
config = new PropertyListConfiguration();
75
config.load(new StringReader(""));
76
fail("No exception thrown on loading an empty file");
77
} catch (ConfigurationException e) {
79
assertNotNull(e.getMessage());
84
public void testString()
86
assertEquals("simple-string", "string1", config.getProperty("simple-string"));
90
public void testQuotedString()
92
assertEquals("quoted-string", "string2", config.getProperty("quoted-string"));
93
assertEquals("quoted-string2", "this is a string", config.getProperty("quoted-string2"));
94
assertEquals("complex-string", "this is a \"complex\" string {(=,;)}", config.getProperty("complex-string"));
98
public void testEmptyArray()
100
String key = "empty-array";
101
assertNotNull("array null", config.getProperty(key));
103
List<?> list = (List<?>) config.getProperty(key);
104
assertTrue("array is not empty", list.isEmpty());
108
public void testArray()
110
String key = "array";
111
assertNotNull("array null", config.getProperty(key));
113
List<?> list = (List<?>) config.getProperty(key);
114
assertFalse("array is empty", list.isEmpty());
116
assertEquals("1st value", "value1", list.get(0));
117
assertEquals("2nd value", "value2", list.get(1));
118
assertEquals("3rd value", "value3", list.get(2));
122
public void testNestedArrays()
124
String key = "nested-arrays";
126
Object array = config.getProperty(key);
129
assertNotNull("array not found", array);
130
ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
131
List<?> list = config.getList(key);
133
assertFalse("empty array", list.isEmpty());
134
assertEquals("size", 2, list.size());
137
ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0));
138
List<?> list1 = (List<?>) list.get(0);
139
assertFalse("nested array 1 is empty", list1.isEmpty());
140
assertEquals("size", 2, list1.size());
141
assertEquals("1st element", "a", list1.get(0));
142
assertEquals("2nd element", "b", list1.get(1));
145
ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1));
146
List<?> list2 = (List<?>) list.get(1);
147
assertFalse("nested array 2 is empty", list2.isEmpty());
148
assertEquals("size", 2, list2.size());
149
assertEquals("1st element", "c", list2.get(0));
150
assertEquals("2nd element", "d", list2.get(1));
154
public void testDictionary()
156
assertEquals("1st element in dictionary", "bar1", config.getProperty("dictionary.foo1"));
157
assertEquals("2nd element in dictionary", "bar2", config.getProperty("dictionary.foo2"));
161
public void testDictionaryArray()
163
String key = "dictionary-array";
165
Object array = config.getProperty(key);
168
assertNotNull("array not found", array);
169
ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
170
List<?> list = config.getList(key);
172
assertFalse("empty array", list.isEmpty());
173
assertEquals("size", 2, list.size());
176
ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0));
177
Configuration conf1 = (Configuration) list.get(0);
178
assertFalse("configuration 1 is empty", conf1.isEmpty());
179
assertEquals("configuration element", "bar", conf1.getProperty("foo"));
182
ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1));
183
Configuration conf2 = (Configuration) list.get(1);
184
assertFalse("configuration 2 is empty", conf2.isEmpty());
185
assertEquals("configuration element", "value", conf2.getProperty("key"));
189
public void testNestedDictionaries()
191
assertEquals("nested property", "value", config.getString("nested-dictionaries.foo.bar.key"));
195
public void testData()
197
ObjectAssert.assertInstanceOf("data", (new byte[0]).getClass(), config.getProperty("data"));
198
ArrayAssert.assertEquals("data", "foo bar".getBytes(), (byte[]) config.getProperty("data"));
202
public void testDate() throws Exception
204
Calendar cal = Calendar.getInstance();
206
cal.set(2002, 2, 22, 11, 30, 0);
207
cal.setTimeZone(TimeZone.getTimeZone("GMT+0100"));
208
Date date = cal.getTime();
210
assertEquals("date", date, config.getProperty("date"));
214
public void testSave() throws Exception
216
File savedFile = new File("target/testsave.plist");
218
// remove the file previously saved if necessary
219
if (savedFile.exists())
221
assertTrue(savedFile.delete());
224
// save the configuration
225
String filename = savedFile.getAbsolutePath();
226
config.save(filename);
228
assertTrue("The saved file doesn't exist", savedFile.exists());
230
// read the configuration and compare the properties
231
Configuration checkConfig = new PropertyListConfiguration(new File(filename));
233
Iterator<String> it = config.getKeys();
236
String key = it.next();
237
assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
239
Object value = checkConfig.getProperty(key);
240
if (value instanceof byte[])
242
byte[] array = (byte[]) value;
243
ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
245
else if (value instanceof List)
247
List<?> list1 = (List<?>) config.getProperty(key);
248
List<?> list2 = (List<?>) value;
250
assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
252
for (int i = 0; i < list2.size(); i++)
254
Object value1 = list1.get(i);
255
Object value2 = list2.get(i);
257
if (value1 instanceof Configuration)
259
ConfigurationComparator comparator = new StrictConfigurationComparator();
260
assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2));
264
assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
268
ListAssert.assertEquals("Value of the '" + key + "' property", (List<?>) config.getProperty(key), list1);
272
assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
279
public void testSaveEmptyDictionary() throws Exception
281
File savedFile = new File("target/testsave.plist");
283
// remove the file previously saved if necessary
284
if (savedFile.exists())
286
assertTrue(savedFile.delete());
289
// save the configuration
290
String filename = savedFile.getAbsolutePath();
291
config.save(filename);
293
assertTrue("The saved file doesn't exist", savedFile.exists());
295
// read the configuration and compare the properties
296
PropertyListConfiguration checkConfig = new PropertyListConfiguration(new File(filename));
298
assertFalse(config.getRootNode().getChildren("empty-dictionary").isEmpty());
299
assertFalse(checkConfig.getRootNode().getChildren("empty-dictionary").isEmpty());
303
public void testQuoteString()
305
assertEquals("null string", null, config.quoteString(null));
306
assertEquals("simple string", "abcd", config.quoteString("abcd"));
307
assertEquals("string with a space", "\"ab cd\"", config.quoteString("ab cd"));
308
assertEquals("string with a quote", "\"foo\\\"bar\"", config.quoteString("foo\"bar"));
309
assertEquals("string with a special char", "\"foo;bar\"", config.quoteString("foo;bar"));
313
* Ensure that setProperty doesn't alter an array of byte
314
* since it's a first class type in plist file
317
public void testSetDataProperty() throws Exception
319
byte[] expected = new byte[]{1, 2, 3, 4};
320
PropertyListConfiguration config = new PropertyListConfiguration();
321
config.setProperty("foo", expected);
322
config.save("target/testdata.plist");
324
PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist");
325
Object array = config2.getProperty("foo");
327
assertNotNull("data not found", array);
328
assertEquals("property type", byte[].class, array.getClass());
329
ArrayAssert.assertEquals(expected, (byte[]) array);
333
* Ensure that addProperty doesn't alter an array of byte
336
public void testAddDataProperty() throws Exception
338
byte[] expected = new byte[]{1, 2, 3, 4};
339
PropertyListConfiguration config = new PropertyListConfiguration();
340
config.addProperty("foo", expected);
341
config.save("target/testdata.plist");
343
PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist");
344
Object array = config2.getProperty("foo");
346
assertNotNull("data not found", array);
347
assertEquals("property type", byte[].class, array.getClass());
348
ArrayAssert.assertEquals(expected, (byte[]) array);
352
public void testInitCopy()
354
PropertyListConfiguration copy = new PropertyListConfiguration(config);
355
assertFalse("Nothing was copied", copy.isEmpty());
359
* Tests parsing a date with an invalid numeric value.
361
@Test(expected = ParseException.class)
362
public void testParseDateNoNumber() throws ParseException
364
PropertyListConfiguration
365
.parseDate("<*D2002-03-22 1c:30:00 +0100>");
369
* Tests parsing a date that is not long enough.
371
@Test(expected = ParseException.class)
372
public void testParseDateTooShort() throws ParseException
374
PropertyListConfiguration.parseDate("<*D2002-03-22 11:3>");
378
* Tests parsing a date that contains an invalid separator character.
380
@Test(expected = ParseException.class)
381
public void testParseDateInvalidChar() throws ParseException
383
PropertyListConfiguration
384
.parseDate("<*D2002+03-22 11:30:00 +0100>");
388
* Tries parsing a null date. This should cause an exception.n
390
@Test(expected = ParseException.class)
391
public void testParseDateNull() throws ParseException
393
PropertyListConfiguration.parseDate(null);
397
* Tests formatting a date.
400
public void testFormatDate()
402
Calendar cal = Calendar.getInstance();
404
cal.set(2007, 9, 29, 23, 4, 30);
405
cal.setTimeZone(TimeZone.getTimeZone("GMT-0230"));
406
assertEquals("Wrong date literal (1)", "<*D2007-10-29 23:04:30 -0230>",
407
PropertyListConfiguration.formatDate(cal));
409
cal.set(2007, 9, 30, 22, 2, 15);
410
cal.setTimeZone(TimeZone.getTimeZone("GMT+1111"));
411
assertEquals("Wrong date literal (2)", "<*D2007-10-30 22:02:15 +1111>",
412
PropertyListConfiguration.formatDate(cal));