~ubuntu-branches/ubuntu/saucy/commons-configuration/saucy

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-07-01 16:29:44 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130701162944-98waq5gogha5gpn1
Tags: 1.9-1
* New upstream release
* debian/control:
  - Updated Standards-Version to 3.9.4 (no changes)
  - Use canonical URLs for the Vcs-* fields
  - Added new build dependencies (libjavacc-maven-plugin-java, junit4)
  - Upgraded the dependency on the Servlet API (2.5 -> 3.0)
  - Removed the dependency on the Activation Framework (glassfish-activation)
  - Replaced the dependency on glassfish-mail with libgnumail-java
  - Removed the unused dependencies:
    liblog4j1.2-java-doc, libmaven-assembly-plugin-java
  - Replaced the dependency on libcommons-jexl-java by libcommons-jexl2-java
* debian/watch: Changed to point the official Apache distribution server
* Removed the obsolete file debian/ant.properties
* Installed the upstream changelog in the binary packages
* Added the report plugins to maven.ignoreRules
* Added the classpath attribute to the jar manifest

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.commons.configuration.plist;
19
 
 
20
 
import java.io.File;
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;
27
 
 
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;
36
 
 
37
 
/**
38
 
 * @author Emmanuel Bourg
39
 
 * @version $Revision: 952624 $, $Date: 2010-06-08 14:00:58 +0200 (Di, 08. Jun 2010) $
40
 
 */
41
 
public class TestPropertyListConfiguration extends TestCase
42
 
{
43
 
    private PropertyListConfiguration config;
44
 
 
45
 
    private String testProperties = new File("conf/test.plist").getAbsolutePath();
46
 
 
47
 
    protected void setUp() throws Exception
48
 
    {
49
 
        config = new PropertyListConfiguration();
50
 
        config.setFileName(testProperties);
51
 
        config.load();
52
 
    }
53
 
 
54
 
    public void testLoad()
55
 
    {
56
 
        assertFalse("the configuration is empty", config.isEmpty());
57
 
    }
58
 
 
59
 
    public void testLoadWithError()
60
 
    {
61
 
        config = new PropertyListConfiguration();
62
 
        try {
63
 
            config.load(new StringReader(""));
64
 
            fail("No exception thrown on loading an empty file");
65
 
        } catch (ConfigurationException e) {
66
 
            // expected
67
 
            assertNotNull(e.getMessage());
68
 
        }
69
 
    }
70
 
 
71
 
    public void testString()
72
 
    {
73
 
        assertEquals("simple-string", "string1", config.getProperty("simple-string"));
74
 
    }
75
 
 
76
 
    public void testQuotedString()
77
 
    {
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"));
81
 
    }
82
 
 
83
 
    public void testEmptyArray()
84
 
    {
85
 
        String key = "empty-array";
86
 
        assertNotNull("array null", config.getProperty(key));
87
 
 
88
 
        List list = (List) config.getProperty(key);
89
 
        assertTrue("array is not empty", list.isEmpty());
90
 
    }
91
 
 
92
 
    public void testArray()
93
 
    {
94
 
        String key = "array";
95
 
        assertNotNull("array null", config.getProperty(key));
96
 
 
97
 
        List list = (List) config.getProperty(key);
98
 
        assertFalse("array is empty", list.isEmpty());
99
 
 
100
 
        assertEquals("1st value", "value1", list.get(0));
101
 
        assertEquals("2nd value", "value2", list.get(1));
102
 
        assertEquals("3rd value", "value3", list.get(2));
103
 
    }
104
 
 
105
 
    public void testNestedArrays()
106
 
    {
107
 
        String key = "nested-arrays";
108
 
 
109
 
        Object array = config.getProperty(key);
110
 
 
111
 
        // root array
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);
115
 
 
116
 
        assertFalse("empty array", list.isEmpty());
117
 
        assertEquals("size", 2, list.size());
118
 
 
119
 
        // 1st array
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));
126
 
 
127
 
        // 2nd array
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));
134
 
    }
135
 
 
136
 
    public void testDictionary()
137
 
    {
138
 
        assertEquals("1st element in dictionary", "bar1", config.getProperty("dictionary.foo1"));
139
 
        assertEquals("2nd element in dictionary", "bar2", config.getProperty("dictionary.foo2"));
140
 
    }
141
 
 
142
 
    public void testDictionaryArray()
143
 
    {
144
 
        String key = "dictionary-array";
145
 
 
146
 
        Object array = config.getProperty(key);
147
 
 
148
 
        // root array
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);
152
 
 
153
 
        assertFalse("empty array", list.isEmpty());
154
 
        assertEquals("size", 2, list.size());
155
 
 
156
 
        // 1st dictionary
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"));
161
 
 
162
 
        // 2nd dictionary
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"));
167
 
    }
168
 
 
169
 
    public void testNestedDictionaries()
170
 
    {
171
 
        assertEquals("nested property", "value", config.getString("nested-dictionaries.foo.bar.key"));
172
 
    }
173
 
 
174
 
    public void testData()
175
 
    {
176
 
        ObjectAssert.assertInstanceOf("data", (new byte[0]).getClass(), config.getProperty("data"));
177
 
        ArrayAssert.assertEquals("data", "foo bar".getBytes(), (byte[]) config.getProperty("data"));
178
 
    }
179
 
 
180
 
    public void testDate() throws Exception
181
 
    {
182
 
        Calendar cal = Calendar.getInstance();
183
 
        cal.clear();
184
 
        cal.set(2002, 2, 22, 11, 30, 0);
185
 
        cal.setTimeZone(TimeZone.getTimeZone("GMT+0100"));
186
 
        Date date = cal.getTime();
187
 
 
188
 
        assertEquals("date", date, config.getProperty("date"));
189
 
    }
190
 
 
191
 
    public void testSave() throws Exception
192
 
    {
193
 
        File savedFile = new File("target/testsave.plist");
194
 
 
195
 
        // remove the file previously saved if necessary
196
 
        if (savedFile.exists())
197
 
        {
198
 
            assertTrue(savedFile.delete());
199
 
        }
200
 
 
201
 
        // save the configuration
202
 
        String filename = savedFile.getAbsolutePath();
203
 
        config.save(filename);
204
 
 
205
 
        assertTrue("The saved file doesn't exist", savedFile.exists());
206
 
 
207
 
        // read the configuration and compare the properties
208
 
        Configuration checkConfig = new PropertyListConfiguration(new File(filename));
209
 
 
210
 
        Iterator it = config.getKeys();
211
 
        while (it.hasNext())
212
 
        {
213
 
            String key = (String) it.next();
214
 
            assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
215
 
 
216
 
            Object value = checkConfig.getProperty(key);
217
 
            if (value instanceof byte[])
218
 
            {
219
 
                byte[] array = (byte[]) value;
220
 
                ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
221
 
            }
222
 
            else if (value instanceof List)
223
 
            {
224
 
                List list1 = (List) config.getProperty(key);
225
 
                List list2 = (List) value;
226
 
 
227
 
                assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
228
 
 
229
 
                for (int i = 0; i < list2.size(); i++)
230
 
                {
231
 
                    Object value1 = list1.get(i);
232
 
                    Object value2 = list2.get(i);
233
 
 
234
 
                    if (value1 instanceof Configuration)
235
 
                    {
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));
238
 
                    }
239
 
                    else
240
 
                    {
241
 
                        assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
242
 
                    }
243
 
                }
244
 
 
245
 
                ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1);
246
 
            }
247
 
            else
248
 
            {
249
 
                assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
250
 
            }
251
 
 
252
 
        }
253
 
    }
254
 
 
255
 
    public void testSaveEmptyDictionary() throws Exception
256
 
    {
257
 
        File savedFile = new File("target/testsave.plist");
258
 
 
259
 
        // remove the file previously saved if necessary
260
 
        if (savedFile.exists())
261
 
        {
262
 
            assertTrue(savedFile.delete());
263
 
        }
264
 
 
265
 
        // save the configuration
266
 
        String filename = savedFile.getAbsolutePath();
267
 
        config.save(filename);
268
 
 
269
 
        assertTrue("The saved file doesn't exist", savedFile.exists());
270
 
 
271
 
        // read the configuration and compare the properties
272
 
        PropertyListConfiguration checkConfig = new PropertyListConfiguration(new File(filename));
273
 
 
274
 
        assertFalse(config.getRootNode().getChildren("empty-dictionary").isEmpty());
275
 
        assertFalse(checkConfig.getRootNode().getChildren("empty-dictionary").isEmpty());
276
 
    }
277
 
 
278
 
    public void testQuoteString()
279
 
    {
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"));
285
 
    }
286
 
 
287
 
    /**
288
 
     * Ensure that setProperty doesn't alter an array of byte
289
 
     * since it's a first class type in plist file
290
 
     */
291
 
    public void testSetDataProperty() throws Exception
292
 
    {
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");
297
 
 
298
 
        PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist");
299
 
        Object array = config2.getProperty("foo");
300
 
 
301
 
        assertNotNull("data not found", array);
302
 
        assertEquals("property type", byte[].class, array.getClass());
303
 
        ArrayAssert.assertEquals(expected, (byte[]) array);
304
 
    }
305
 
 
306
 
    /**
307
 
     * Ensure that addProperty doesn't alter an array of byte
308
 
     */
309
 
    public void testAddDataProperty() throws Exception
310
 
    {
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");
315
 
 
316
 
        PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist");
317
 
        Object array = config2.getProperty("foo");
318
 
 
319
 
        assertNotNull("data not found", array);
320
 
        assertEquals("property type", byte[].class, array.getClass());
321
 
        ArrayAssert.assertEquals(expected, (byte[]) array);
322
 
    }
323
 
 
324
 
    public void testInitCopy()
325
 
    {
326
 
        PropertyListConfiguration copy = new PropertyListConfiguration(config);
327
 
        assertFalse("Nothing was copied", copy.isEmpty());
328
 
    }
329
 
 
330
 
    /**
331
 
     * Tests parsing a date with an invalid numeric value.
332
 
     */
333
 
    public void testParseDateNoNumber()
334
 
    {
335
 
        try
336
 
        {
337
 
            PropertyListConfiguration
338
 
                    .parseDate("<*D2002-03-22 1c:30:00 +0100>");
339
 
            fail("Could parse date with an invalid number!");
340
 
        }
341
 
        catch (ParseException pex)
342
 
        {
343
 
            // ok
344
 
        }
345
 
    }
346
 
 
347
 
    /**
348
 
     * Tests parsing a date that is not long enough.
349
 
     */
350
 
    public void testParseDateTooShort()
351
 
    {
352
 
        try
353
 
        {
354
 
            PropertyListConfiguration.parseDate("<*D2002-03-22 11:3>");
355
 
            fail("Could parse too short date!");
356
 
        }
357
 
        catch (ParseException pex)
358
 
        {
359
 
            // ok
360
 
        }
361
 
    }
362
 
 
363
 
    /**
364
 
     * Tests parsing a date that contains an invalid separator character.
365
 
     */
366
 
    public void testParseDateInvalidChar()
367
 
    {
368
 
        try
369
 
        {
370
 
            PropertyListConfiguration
371
 
                    .parseDate("<*D2002+03-22 11:30:00 +0100>");
372
 
            fail("Could parse date with an invalid separator!");
373
 
        }
374
 
        catch (ParseException pex)
375
 
        {
376
 
            // ok
377
 
        }
378
 
    }
379
 
 
380
 
    /**
381
 
     * Tries parsing a null date. This should cause an exception.n
382
 
     */
383
 
    public void testParseDateNull()
384
 
    {
385
 
        try
386
 
        {
387
 
            PropertyListConfiguration.parseDate(null);
388
 
            fail("Could parse null date!");
389
 
        }
390
 
        catch (ParseException pex)
391
 
        {
392
 
            // ok
393
 
        }
394
 
    }
395
 
 
396
 
    /**
397
 
     * Tests formatting a date.
398
 
     */
399
 
    public void testFormatDate()
400
 
    {
401
 
        Calendar cal = Calendar.getInstance();
402
 
        cal.clear();
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));
407
 
        cal.clear();
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));
412
 
    }
413
 
}