Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 85219 invoked from network); 18 Feb 2008 08:34:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 18 Feb 2008 08:34:31 -0000 Received: (qmail 52785 invoked by uid 500); 18 Feb 2008 08:34:23 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 52728 invoked by uid 500); 18 Feb 2008 08:34:23 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 52719 invoked by uid 99); 18 Feb 2008 08:34:23 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 18 Feb 2008 00:34:23 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 18 Feb 2008 08:33:42 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id B24CE1A9832; Mon, 18 Feb 2008 00:33:59 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r628642 - in /commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist: TestPropertyListConfiguration.java TestPropertyListParser.java TestXMLPropertyListConfiguration.java Date: Mon, 18 Feb 2008 08:33:58 -0000 To: commits@commons.apache.org From: ebourg@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080218083359.B24CE1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: ebourg Date: Mon Feb 18 00:33:54 2008 New Revision: 628642 URL: http://svn.apache.org/viewvc?rev=628642&view=rev Log: Fixed some eol-style properties (thanks to sebb for spotting these) Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListConfiguration.java (contents, props changed) commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListParser.java (contents, props changed) commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java (contents, props changed) Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListConfiguration.java?rev=628642&r1=628641&r2=628642&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListConfiguration.java Mon Feb 18 00:33:54 2008 @@ -1,423 +1,423 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.configuration2.plist; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.StringReader; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.Iterator; -import java.util.List; -import java.util.TimeZone; - -import org.apache.commons.configuration2.Configuration; -import org.apache.commons.configuration2.ConfigurationAssert; -import org.apache.commons.configuration2.ConfigurationComparator; -import org.apache.commons.configuration2.ConfigurationException; -import org.apache.commons.configuration2.StrictConfigurationComparator; - -import junit.framework.TestCase; -import junitx.framework.ArrayAssert; -import junitx.framework.ListAssert; -import junitx.framework.ObjectAssert; - -/** - * @author Emmanuel Bourg - * @version $Revision$, $Date$ - */ -public class TestPropertyListConfiguration extends TestCase -{ - private PropertyListConfiguration config; - - private String testProperties = ConfigurationAssert.getTestFile("test.plist").getAbsolutePath(); - - protected void setUp() throws Exception - { - config = new PropertyListConfiguration(); - config.setFileName(testProperties); - config.load(); - } - - public void testLoad() - { - assertFalse("the configuration is empty", config.isEmpty()); - } - - public void testLoadWithError() - { - config = new PropertyListConfiguration(); - try { - config.load(new StringReader("")); - fail("No exception thrown on loading an empty file"); - } catch (ConfigurationException e) { - // expected - assertNotNull(e.getMessage()); - } - } - - public void testString() - { - assertEquals("simple-string", "string1", config.getProperty("simple-string")); - } - - public void testQuotedString() - { - assertEquals("quoted-string", "string2", config.getProperty("quoted-string")); - assertEquals("quoted-string2", "this is a string", config.getProperty("quoted-string2")); - assertEquals("complex-string", "this is a \"complex\" string {(=,;)}", config.getProperty("complex-string")); - } - - public void testEmptyArray() - { - String key = "empty-array"; - assertNotNull("array null", config.getProperty(key)); - - List list = (List) config.getProperty(key); - assertTrue("array is not empty", list.isEmpty()); - } - - public void testArray() - { - String key = "array"; - assertNotNull("array null", config.getProperty(key)); - - List list = (List) config.getProperty(key); - assertFalse("array is empty", list.isEmpty()); - - assertEquals("1st value", "value1", list.get(0)); - assertEquals("2nd value", "value2", list.get(1)); - assertEquals("3rd value", "value3", list.get(2)); - } - - public void testNestedArrays() - { - String key = "nested-arrays"; - - Object array = config.getProperty(key); - - // root array - assertNotNull("array not found", array); - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); - List list = config.getList(key); - - assertFalse("empty array", list.isEmpty()); - assertEquals("size", 2, list.size()); - - // 1st array - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0)); - List list1 = (List) list.get(0); - assertFalse("nested array 1 is empty", list1.isEmpty()); - assertEquals("size", 2, list1.size()); - assertEquals("1st element", "a", list1.get(0)); - assertEquals("2nd element", "b", list1.get(1)); - - // 2nd array - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1)); - List list2 = (List) list.get(1); - assertFalse("nested array 2 is empty", list2.isEmpty()); - assertEquals("size", 2, list2.size()); - assertEquals("1st element", "c", list2.get(0)); - assertEquals("2nd element", "d", list2.get(1)); - } - - public void testDictionary() - { - assertEquals("1st element in dictionary", "bar1", config.getProperty("dictionary.foo1")); - assertEquals("2nd element in dictionary", "bar2", config.getProperty("dictionary.foo2")); - } - - public void testDictionaryArray() - { - String key = "dictionary-array"; - - Object array = config.getProperty(key); - - // root array - assertNotNull("array not found", array); - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); - List list = config.getList(key); - - assertFalse("empty array", list.isEmpty()); - assertEquals("size", 2, list.size()); - - // 1st dictionary - ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0)); - Configuration conf1 = (Configuration) list.get(0); - assertFalse("configuration 1 is empty", conf1.isEmpty()); - assertEquals("configuration element", "bar", conf1.getProperty("foo")); - - // 2nd dictionary - ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1)); - Configuration conf2 = (Configuration) list.get(1); - assertFalse("configuration 2 is empty", conf2.isEmpty()); - assertEquals("configuration element", "value", conf2.getProperty("key")); - } - - public void testNestedDictionaries() - { - assertEquals("nested property", "value", config.getString("nested-dictionaries.foo.bar.key")); - } - - public void testData() - { - ObjectAssert.assertInstanceOf("data", (new byte[0]).getClass(), config.getProperty("data")); - ArrayAssert.assertEquals("data", "foo bar".getBytes(), (byte[]) config.getProperty("data")); - } - - public void testDate() throws Exception - { - Calendar cal = Calendar.getInstance(); - cal.clear(); - cal.set(2002, 2, 22, 11, 30, 0); - cal.setTimeZone(TimeZone.getTimeZone("GMT+0100")); - Date date = cal.getTime(); - - assertEquals("date", date, config.getProperty("date")); - } - - /** - * Test if the calendar objets are saved with their time zone - */ - public void testCalendar() throws Exception - { - File savedFile = new File("target/testcalendar.plist"); - - // remove the file previously saved if necessary - if (savedFile.exists()) - { - assertTrue(savedFile.delete()); - } - - Calendar calendar = new GregorianCalendar(2008, Calendar.JANUARY, 1); - calendar.setTimeZone(TimeZone.getTimeZone("GMT-0200")); - - PropertyListConfiguration config = new PropertyListConfiguration(); - config.setProperty("calendar", calendar); - config.save(savedFile); - - BufferedReader in = new BufferedReader(new FileReader(savedFile)); - in.readLine(); - assertEquals("calendar output", "calendar = <*D2008-01-01 00:00:00 -0200>;", in.readLine().trim()); - - in.close(); - } - - public void testSave() throws Exception - { - File savedFile = new File("target/testsave.plist"); - - // remove the file previously saved if necessary - if (savedFile.exists()) - { - assertTrue(savedFile.delete()); - } - - // save the configuration - String filename = savedFile.getAbsolutePath(); - config.save(filename); - - assertTrue("The saved file doesn't exist", savedFile.exists()); - - // read the configuration and compare the properties - Configuration checkConfig = new PropertyListConfiguration(new File(filename)); - - Iterator it = config.getKeys(); - while (it.hasNext()) - { - String key = (String) it.next(); - assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key)); - - Object value = checkConfig.getProperty(key); - if (value instanceof byte[]) - { - byte[] array = (byte[]) value; - ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array); - } - else if (value instanceof List) - { - List list1 = (List) config.getProperty(key); - List list2 = (List) value; - - assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size()); - - for (int i = 0; i < list2.size(); i++) - { - Object value1 = list1.get(i); - Object value2 = list2.get(i); - - if (value1 instanceof Configuration) - { - ConfigurationComparator comparator = new StrictConfigurationComparator(); - assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2)); - } - else - { - assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2); - } - } - - ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1); - } - else - { - assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key)); - } - - } - } - - public void testQuoteString() - { - assertEquals("null string", null, config.quoteString(null)); - assertEquals("simple string", "abcd", config.quoteString("abcd")); - assertEquals("string with a space", "\"ab cd\"", config.quoteString("ab cd")); - assertEquals("string with a quote", "\"foo\\\"bar\"", config.quoteString("foo\"bar")); - assertEquals("string with a special char", "\"foo;bar\"", config.quoteString("foo;bar")); - } - - /** - * Ensure that setProperty doesn't alter an array of byte - * since it's a first class type in plist file - */ - public void testSetDataProperty() throws Exception - { - byte[] expected = new byte[]{1, 2, 3, 4}; - PropertyListConfiguration config = new PropertyListConfiguration(); - config.setProperty("foo", expected); - config.save("target/testdata.plist"); - - PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist"); - Object array = config2.getProperty("foo"); - - assertNotNull("data not found", array); - assertEquals("property type", byte[].class, array.getClass()); - ArrayAssert.assertEquals(expected, (byte[]) array); - } - - /** - * Ensure that addProperty doesn't alter an array of byte - */ - public void testAddDataProperty() throws Exception - { - byte[] expected = new byte[]{1, 2, 3, 4}; - PropertyListConfiguration config = new PropertyListConfiguration(); - config.addProperty("foo", expected); - config.save("target/testdata.plist"); - - PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist"); - Object array = config2.getProperty("foo"); - - assertNotNull("data not found", array); - assertEquals("property type", byte[].class, array.getClass()); - ArrayAssert.assertEquals(expected, (byte[]) array); - } - - public void testInitCopy() - { - PropertyListConfiguration copy = new PropertyListConfiguration(config); - assertFalse("Nothing was copied", copy.isEmpty()); - } - - /** - * Tests parsing a date with an invalid numeric value. - */ - public void testParseDateNoNumber() - { - try - { - PropertyListConfiguration.parseDate("<*D2002-03-22 1c:30:00 +0100>"); - fail("Could parse date with an invalid number!"); - } - catch (ParseException pex) - { - // ok - } - } - - /** - * Tests parsing a date that is not long enough. - */ - public void testParseDateTooShort() - { - try - { - PropertyListConfiguration.parseDate("<*D2002-03-22 11:3>"); - fail("Could parse too short date!"); - } - catch (ParseException pex) - { - // ok - } - } - - /** - * Tests parsing a date that contains an invalid separator character. - */ - public void testParseDateInvalidChar() - { - try - { - PropertyListConfiguration.parseDate("<*D2002+03-22 11:30:00 +0100>"); - fail("Could parse date with an invalid separator!"); - } - catch (ParseException pex) - { - // ok - } - } - - /** - * Tries parsing a null date. This should cause an exception.n - */ - public void testParseDateNull() - { - try - { - PropertyListConfiguration.parseDate(null); - fail("Could parse null date!"); - } - catch (ParseException pex) - { - // ok - } - } - - /** - * Tests formatting a date. - */ - public void testFormatDate() - { - SimpleDateFormat format = config.DATE_FORMAT; - - Calendar cal = new GregorianCalendar(2007, Calendar.OCTOBER, 29, 23, 4, 30); - cal.setTimeZone(TimeZone.getTimeZone("GMT-0230")); - format.setTimeZone(cal.getTimeZone()); - - assertEquals("Wrong date literal (1)", "<*D2007-10-29 23:04:30 -0230>", format.format(cal.getTime())); - - cal = new GregorianCalendar(2007, Calendar.OCTOBER, 30, 22, 2, 15); - cal.setTimeZone(TimeZone.getTimeZone("GMT+1111")); - format.setTimeZone(cal.getTimeZone()); - - assertEquals("Wrong date literal (2)", "<*D2007-10-30 22:02:15 +1111>", format.format(cal.getTime())); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.configuration2.plist; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.StringReader; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Iterator; +import java.util.List; +import java.util.TimeZone; + +import org.apache.commons.configuration2.Configuration; +import org.apache.commons.configuration2.ConfigurationAssert; +import org.apache.commons.configuration2.ConfigurationComparator; +import org.apache.commons.configuration2.ConfigurationException; +import org.apache.commons.configuration2.StrictConfigurationComparator; + +import junit.framework.TestCase; +import junitx.framework.ArrayAssert; +import junitx.framework.ListAssert; +import junitx.framework.ObjectAssert; + +/** + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + */ +public class TestPropertyListConfiguration extends TestCase +{ + private PropertyListConfiguration config; + + private String testProperties = ConfigurationAssert.getTestFile("test.plist").getAbsolutePath(); + + protected void setUp() throws Exception + { + config = new PropertyListConfiguration(); + config.setFileName(testProperties); + config.load(); + } + + public void testLoad() + { + assertFalse("the configuration is empty", config.isEmpty()); + } + + public void testLoadWithError() + { + config = new PropertyListConfiguration(); + try { + config.load(new StringReader("")); + fail("No exception thrown on loading an empty file"); + } catch (ConfigurationException e) { + // expected + assertNotNull(e.getMessage()); + } + } + + public void testString() + { + assertEquals("simple-string", "string1", config.getProperty("simple-string")); + } + + public void testQuotedString() + { + assertEquals("quoted-string", "string2", config.getProperty("quoted-string")); + assertEquals("quoted-string2", "this is a string", config.getProperty("quoted-string2")); + assertEquals("complex-string", "this is a \"complex\" string {(=,;)}", config.getProperty("complex-string")); + } + + public void testEmptyArray() + { + String key = "empty-array"; + assertNotNull("array null", config.getProperty(key)); + + List list = (List) config.getProperty(key); + assertTrue("array is not empty", list.isEmpty()); + } + + public void testArray() + { + String key = "array"; + assertNotNull("array null", config.getProperty(key)); + + List list = (List) config.getProperty(key); + assertFalse("array is empty", list.isEmpty()); + + assertEquals("1st value", "value1", list.get(0)); + assertEquals("2nd value", "value2", list.get(1)); + assertEquals("3rd value", "value3", list.get(2)); + } + + public void testNestedArrays() + { + String key = "nested-arrays"; + + Object array = config.getProperty(key); + + // root array + assertNotNull("array not found", array); + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); + List list = config.getList(key); + + assertFalse("empty array", list.isEmpty()); + assertEquals("size", 2, list.size()); + + // 1st array + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0)); + List list1 = (List) list.get(0); + assertFalse("nested array 1 is empty", list1.isEmpty()); + assertEquals("size", 2, list1.size()); + assertEquals("1st element", "a", list1.get(0)); + assertEquals("2nd element", "b", list1.get(1)); + + // 2nd array + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1)); + List list2 = (List) list.get(1); + assertFalse("nested array 2 is empty", list2.isEmpty()); + assertEquals("size", 2, list2.size()); + assertEquals("1st element", "c", list2.get(0)); + assertEquals("2nd element", "d", list2.get(1)); + } + + public void testDictionary() + { + assertEquals("1st element in dictionary", "bar1", config.getProperty("dictionary.foo1")); + assertEquals("2nd element in dictionary", "bar2", config.getProperty("dictionary.foo2")); + } + + public void testDictionaryArray() + { + String key = "dictionary-array"; + + Object array = config.getProperty(key); + + // root array + assertNotNull("array not found", array); + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); + List list = config.getList(key); + + assertFalse("empty array", list.isEmpty()); + assertEquals("size", 2, list.size()); + + // 1st dictionary + ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0)); + Configuration conf1 = (Configuration) list.get(0); + assertFalse("configuration 1 is empty", conf1.isEmpty()); + assertEquals("configuration element", "bar", conf1.getProperty("foo")); + + // 2nd dictionary + ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1)); + Configuration conf2 = (Configuration) list.get(1); + assertFalse("configuration 2 is empty", conf2.isEmpty()); + assertEquals("configuration element", "value", conf2.getProperty("key")); + } + + public void testNestedDictionaries() + { + assertEquals("nested property", "value", config.getString("nested-dictionaries.foo.bar.key")); + } + + public void testData() + { + ObjectAssert.assertInstanceOf("data", (new byte[0]).getClass(), config.getProperty("data")); + ArrayAssert.assertEquals("data", "foo bar".getBytes(), (byte[]) config.getProperty("data")); + } + + public void testDate() throws Exception + { + Calendar cal = Calendar.getInstance(); + cal.clear(); + cal.set(2002, 2, 22, 11, 30, 0); + cal.setTimeZone(TimeZone.getTimeZone("GMT+0100")); + Date date = cal.getTime(); + + assertEquals("date", date, config.getProperty("date")); + } + + /** + * Test if the calendar objets are saved with their time zone + */ + public void testCalendar() throws Exception + { + File savedFile = new File("target/testcalendar.plist"); + + // remove the file previously saved if necessary + if (savedFile.exists()) + { + assertTrue(savedFile.delete()); + } + + Calendar calendar = new GregorianCalendar(2008, Calendar.JANUARY, 1); + calendar.setTimeZone(TimeZone.getTimeZone("GMT-0200")); + + PropertyListConfiguration config = new PropertyListConfiguration(); + config.setProperty("calendar", calendar); + config.save(savedFile); + + BufferedReader in = new BufferedReader(new FileReader(savedFile)); + in.readLine(); + assertEquals("calendar output", "calendar = <*D2008-01-01 00:00:00 -0200>;", in.readLine().trim()); + + in.close(); + } + + public void testSave() throws Exception + { + File savedFile = new File("target/testsave.plist"); + + // remove the file previously saved if necessary + if (savedFile.exists()) + { + assertTrue(savedFile.delete()); + } + + // save the configuration + String filename = savedFile.getAbsolutePath(); + config.save(filename); + + assertTrue("The saved file doesn't exist", savedFile.exists()); + + // read the configuration and compare the properties + Configuration checkConfig = new PropertyListConfiguration(new File(filename)); + + Iterator it = config.getKeys(); + while (it.hasNext()) + { + String key = (String) it.next(); + assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key)); + + Object value = checkConfig.getProperty(key); + if (value instanceof byte[]) + { + byte[] array = (byte[]) value; + ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array); + } + else if (value instanceof List) + { + List list1 = (List) config.getProperty(key); + List list2 = (List) value; + + assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size()); + + for (int i = 0; i < list2.size(); i++) + { + Object value1 = list1.get(i); + Object value2 = list2.get(i); + + if (value1 instanceof Configuration) + { + ConfigurationComparator comparator = new StrictConfigurationComparator(); + assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2)); + } + else + { + assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2); + } + } + + ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1); + } + else + { + assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key)); + } + + } + } + + public void testQuoteString() + { + assertEquals("null string", null, config.quoteString(null)); + assertEquals("simple string", "abcd", config.quoteString("abcd")); + assertEquals("string with a space", "\"ab cd\"", config.quoteString("ab cd")); + assertEquals("string with a quote", "\"foo\\\"bar\"", config.quoteString("foo\"bar")); + assertEquals("string with a special char", "\"foo;bar\"", config.quoteString("foo;bar")); + } + + /** + * Ensure that setProperty doesn't alter an array of byte + * since it's a first class type in plist file + */ + public void testSetDataProperty() throws Exception + { + byte[] expected = new byte[]{1, 2, 3, 4}; + PropertyListConfiguration config = new PropertyListConfiguration(); + config.setProperty("foo", expected); + config.save("target/testdata.plist"); + + PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist"); + Object array = config2.getProperty("foo"); + + assertNotNull("data not found", array); + assertEquals("property type", byte[].class, array.getClass()); + ArrayAssert.assertEquals(expected, (byte[]) array); + } + + /** + * Ensure that addProperty doesn't alter an array of byte + */ + public void testAddDataProperty() throws Exception + { + byte[] expected = new byte[]{1, 2, 3, 4}; + PropertyListConfiguration config = new PropertyListConfiguration(); + config.addProperty("foo", expected); + config.save("target/testdata.plist"); + + PropertyListConfiguration config2 = new PropertyListConfiguration("target/testdata.plist"); + Object array = config2.getProperty("foo"); + + assertNotNull("data not found", array); + assertEquals("property type", byte[].class, array.getClass()); + ArrayAssert.assertEquals(expected, (byte[]) array); + } + + public void testInitCopy() + { + PropertyListConfiguration copy = new PropertyListConfiguration(config); + assertFalse("Nothing was copied", copy.isEmpty()); + } + + /** + * Tests parsing a date with an invalid numeric value. + */ + public void testParseDateNoNumber() + { + try + { + PropertyListConfiguration.parseDate("<*D2002-03-22 1c:30:00 +0100>"); + fail("Could parse date with an invalid number!"); + } + catch (ParseException pex) + { + // ok + } + } + + /** + * Tests parsing a date that is not long enough. + */ + public void testParseDateTooShort() + { + try + { + PropertyListConfiguration.parseDate("<*D2002-03-22 11:3>"); + fail("Could parse too short date!"); + } + catch (ParseException pex) + { + // ok + } + } + + /** + * Tests parsing a date that contains an invalid separator character. + */ + public void testParseDateInvalidChar() + { + try + { + PropertyListConfiguration.parseDate("<*D2002+03-22 11:30:00 +0100>"); + fail("Could parse date with an invalid separator!"); + } + catch (ParseException pex) + { + // ok + } + } + + /** + * Tries parsing a null date. This should cause an exception.n + */ + public void testParseDateNull() + { + try + { + PropertyListConfiguration.parseDate(null); + fail("Could parse null date!"); + } + catch (ParseException pex) + { + // ok + } + } + + /** + * Tests formatting a date. + */ + public void testFormatDate() + { + SimpleDateFormat format = config.DATE_FORMAT; + + Calendar cal = new GregorianCalendar(2007, Calendar.OCTOBER, 29, 23, 4, 30); + cal.setTimeZone(TimeZone.getTimeZone("GMT-0230")); + format.setTimeZone(cal.getTimeZone()); + + assertEquals("Wrong date literal (1)", "<*D2007-10-29 23:04:30 -0230>", format.format(cal.getTime())); + + cal = new GregorianCalendar(2007, Calendar.OCTOBER, 30, 22, 2, 15); + cal.setTimeZone(TimeZone.getTimeZone("GMT+1111")); + format.setTimeZone(cal.getTimeZone()); + + assertEquals("Wrong date literal (2)", "<*D2007-10-30 22:02:15 +1111>", format.format(cal.getTime())); + } +} Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListConfiguration.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListParser.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListParser.java?rev=628642&r1=628641&r2=628642&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListParser.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListParser.java Mon Feb 18 00:33:54 2008 @@ -1,76 +1,76 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.configuration2.plist; - -import java.io.Reader; -import java.util.Calendar; -import java.util.SimpleTimeZone; - -import org.apache.commons.configuration2.plist.PropertyListParser; - -import junit.framework.TestCase; -import junitx.framework.ArrayAssert; - -/** - * @author Emmanuel Bourg - * @version $Revision$, $Date$ - */ -public class TestPropertyListParser extends TestCase -{ - private PropertyListParser parser = new PropertyListParser((Reader) null); - - public void testRemoveQuotes() - { - assertEquals("unquoted string", "abc", parser.removeQuotes("abc")); - assertEquals("quoted string", "abc", parser.removeQuotes("\"abc\"")); - assertEquals("empty quotes", "", parser.removeQuotes("\"\"")); - assertEquals("empty string", "", parser.removeQuotes("")); - assertEquals("null string", null, parser.removeQuotes(null)); - } - - public void testUnescapeQuotes() - { - assertEquals("non escaped quotes", "aaa\"bbb\"ccc", parser.unescapeQuotes("aaa\"bbb\"ccc")); - assertEquals("escaped quotes", "aaa\"bbb\"ccc", parser.unescapeQuotes("aaa\\\"bbb\\\"ccc")); - } - - public void testParseDate() throws Exception - { - Calendar calendar = Calendar.getInstance(); - calendar.set(Calendar.YEAR, 2002); - calendar.set(Calendar.MONTH, Calendar.MARCH); - calendar.set(Calendar.DAY_OF_MONTH, 22); - calendar.set(Calendar.HOUR_OF_DAY, 11); - calendar.set(Calendar.MINUTE, 30); - calendar.set(Calendar.SECOND, 0); - calendar.set(Calendar.MILLISECOND, 0); - calendar.setTimeZone(new SimpleTimeZone(60 * 60 * 1000, "Apache/Jakarta")); - - assertEquals("parsed date", calendar.getTime(), parser.parseDate("<*D2002-03-22 11:30:00 +0100>")); - } - - public void testFilterData() throws Exception - { - byte[] expected = new byte[] {0x20, 0x20}; - ArrayAssert.assertEquals("null string", null, parser.filterData(null)); - ArrayAssert.assertEquals("data with < >", expected, parser.filterData("<2020>")); - ArrayAssert.assertEquals("data without < >", expected, parser.filterData("2020")); - ArrayAssert.assertEquals("data with space", expected, parser.filterData("20 20")); - ArrayAssert.assertEquals("odd length", new byte[]{9, 0x20}, parser.filterData("920")); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.configuration2.plist; + +import java.io.Reader; +import java.util.Calendar; +import java.util.SimpleTimeZone; + +import org.apache.commons.configuration2.plist.PropertyListParser; + +import junit.framework.TestCase; +import junitx.framework.ArrayAssert; + +/** + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + */ +public class TestPropertyListParser extends TestCase +{ + private PropertyListParser parser = new PropertyListParser((Reader) null); + + public void testRemoveQuotes() + { + assertEquals("unquoted string", "abc", parser.removeQuotes("abc")); + assertEquals("quoted string", "abc", parser.removeQuotes("\"abc\"")); + assertEquals("empty quotes", "", parser.removeQuotes("\"\"")); + assertEquals("empty string", "", parser.removeQuotes("")); + assertEquals("null string", null, parser.removeQuotes(null)); + } + + public void testUnescapeQuotes() + { + assertEquals("non escaped quotes", "aaa\"bbb\"ccc", parser.unescapeQuotes("aaa\"bbb\"ccc")); + assertEquals("escaped quotes", "aaa\"bbb\"ccc", parser.unescapeQuotes("aaa\\\"bbb\\\"ccc")); + } + + public void testParseDate() throws Exception + { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.YEAR, 2002); + calendar.set(Calendar.MONTH, Calendar.MARCH); + calendar.set(Calendar.DAY_OF_MONTH, 22); + calendar.set(Calendar.HOUR_OF_DAY, 11); + calendar.set(Calendar.MINUTE, 30); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + calendar.setTimeZone(new SimpleTimeZone(60 * 60 * 1000, "Apache/Jakarta")); + + assertEquals("parsed date", calendar.getTime(), parser.parseDate("<*D2002-03-22 11:30:00 +0100>")); + } + + public void testFilterData() throws Exception + { + byte[] expected = new byte[] {0x20, 0x20}; + ArrayAssert.assertEquals("null string", null, parser.filterData(null)); + ArrayAssert.assertEquals("data with < >", expected, parser.filterData("<2020>")); + ArrayAssert.assertEquals("data without < >", expected, parser.filterData("2020")); + ArrayAssert.assertEquals("data with space", expected, parser.filterData("20 20")); + ArrayAssert.assertEquals("odd length", new byte[]{9, 0x20}, parser.filterData("920")); + } +} Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestPropertyListParser.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java?rev=628642&r1=628641&r2=628642&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java Mon Feb 18 00:33:54 2008 @@ -1,304 +1,304 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.configuration2.plist; - -import java.io.File; -import java.util.*; - -import junit.framework.TestCase; -import junitx.framework.ObjectAssert; -import junitx.framework.ArrayAssert; -import junitx.framework.ListAssert; - -import org.apache.commons.configuration2.Configuration; -import org.apache.commons.configuration2.ConfigurationAssert; -import org.apache.commons.configuration2.ConfigurationComparator; -import org.apache.commons.configuration2.FileConfiguration; -import org.apache.commons.configuration2.HierarchicalConfiguration; -import org.apache.commons.configuration2.StrictConfigurationComparator; -import org.apache.commons.configuration2.plist.XMLPropertyListConfiguration; - -/** - * @author Emmanuel Bourg - * @version $Revision$, $Date$ - */ -public class TestXMLPropertyListConfiguration extends TestCase -{ - private FileConfiguration config; - - protected void setUp() throws Exception - { - config = new XMLPropertyListConfiguration(); - config.setFile(ConfigurationAssert.getTestFile("test.plist.xml")); - config.load(); - } - - public void testString() throws Exception - { - assertEquals("'string' property", "value1", config.getString("string")); - } - - public void testInteger() throws Exception - { - assertEquals("'integer' property", 12345, config.getInt("integer")); - } - - public void testReal() throws Exception - { - assertEquals("'real' property", -12.345, config.getDouble("real"), 0); - } - - public void testBoolean() throws Exception - { - assertEquals("'boolean1' property", true, config.getBoolean("boolean1")); - assertEquals("'boolean2' property", false, config.getBoolean("boolean2")); - } - - public void testDictionary() - { - assertEquals("1st element", "value1", config.getProperty("dictionary.key1")); - assertEquals("2nd element", "value2", config.getProperty("dictionary.key2")); - assertEquals("3rd element", "value3", config.getProperty("dictionary.key3")); - } - - public void testSubset() - { - Configuration subset = config.subset("dictionary"); - Iterator keys = subset.getKeys(); - - String key = (String) keys.next(); - assertEquals("1st key", "key1", key); - assertEquals("1st value", "value1", subset.getString(key)); - - key = (String) keys.next(); - assertEquals("2nd key", "key2", key); - assertEquals("2nd value", "value2", subset.getString(key)); - - key = (String) keys.next(); - assertEquals("3rd key", "key3", key); - assertEquals("3rd value", "value3", subset.getString(key)); - - assertFalse("more than 3 properties founds", keys.hasNext()); - } - - public void testArray() - { - Object array = config.getProperty("array"); - - assertNotNull("array not found", array); - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); - List list = config.getList("array"); - - assertFalse("empty array", list.isEmpty()); - assertEquals("size", 3, list.size()); - assertEquals("1st element", "value1", list.get(0)); - assertEquals("2nd element", "value2", list.get(1)); - assertEquals("3rd element", "value3", list.get(2)); - } - - public void testNestedArray() - { - String key = "nested-array"; - - Object array = config.getProperty(key); - - // root array - assertNotNull("array not found", array); - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); - List list = config.getList(key); - - assertFalse("empty array", list.isEmpty()); - assertEquals("size", 2, list.size()); - - // 1st array - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0)); - List list1 = (List) list.get(0); - assertFalse("nested array 1 is empty", list1.isEmpty()); - assertEquals("size", 2, list1.size()); - assertEquals("1st element", "a", list1.get(0)); - assertEquals("2nd element", "b", list1.get(1)); - - // 2nd array - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1)); - List list2 = (List) list.get(1); - assertFalse("nested array 2 is empty", list2.isEmpty()); - assertEquals("size", 2, list2.size()); - assertEquals("1st element", "c", list2.get(0)); - assertEquals("2nd element", "d", list2.get(1)); - } - - public void testDictionaryArray() - { - String key = "dictionary-array"; - - Object array = config.getProperty(key); - - // root array - assertNotNull("array not found", array); - ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); - List list = config.getList(key); - - assertFalse("empty array", list.isEmpty()); - assertEquals("size", 2, list.size()); - - // 1st dictionary - ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0)); - Configuration conf1 = (Configuration) list.get(0); - assertFalse("configuration 1 is empty", conf1.isEmpty()); - assertEquals("configuration element", "bar", conf1.getProperty("foo")); - - // 2nd dictionary - ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1)); - Configuration conf2 = (Configuration) list.get(1); - assertFalse("configuration 2 is empty", conf2.isEmpty()); - assertEquals("configuration element", "value", conf2.getProperty("key")); - } - - public void testNested() - { - assertEquals("nested property", "value", config.getString("nested.node1.node2.node3")); - } - - public void testSave() throws Exception - { - File savedFile = new File("target/testsave.plist.xml"); - - // remove the file previously saved if necessary - if (savedFile.exists()) - { - assertTrue(savedFile.delete()); - } - - // add an array of strings to the configuration - /* - config.addProperty("string", "value1"); - List list = new ArrayList(); - for (int i = 1; i < 5; i++) - { - list.add("value" + i); - } - config.addProperty("newarray", list);*/ - // todo : investigate why the array structure of 'newarray' is lost in the saved file - - // add a map of strings - /* - Map map = new HashMap(); - map.put("foo", "bar"); - map.put("int", new Integer(123)); - config.addProperty("newmap", map); - */ - // todo : a Map added to a HierarchicalConfiguration should be decomposed as list of nodes - - // save the configuration - String filename = savedFile.getAbsolutePath(); - config.save(filename); - - assertTrue("The saved file doesn't exist", savedFile.exists()); - - // read the configuration and compare the properties - Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename)); - - Iterator it = config.getKeys(); - while (it.hasNext()) - { - String key = (String) it.next(); - assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key)); - - Object value = checkConfig.getProperty(key); - if (value instanceof byte[]) - { - byte[] array = (byte[]) value; - ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array); - } - else if (value instanceof List) - { - List list1 = (List) config.getProperty(key); - List list2 = (List) value; - - assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size()); - - for (int i = 0; i < list2.size(); i++) - { - Object value1 = list1.get(i); - Object value2 = list2.get(i); - - if (value1 instanceof Configuration) - { - ConfigurationComparator comparator = new StrictConfigurationComparator(); - assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2)); - } - else - { - assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2); - } - } - - ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1); - } - else - { - assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key)); - } - - } - } - - /** - * Ensure that setProperty doesn't alter an array of byte - * since it's a first class type in plist file - */ - public void testSetDataProperty() throws Exception - { - byte[] expected = new byte[]{1, 2, 3, 4}; - XMLPropertyListConfiguration config = new XMLPropertyListConfiguration(); - config.setProperty("foo", expected); - config.save("target/testdata.plist.xml"); - - XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml"); - Object array = config2.getProperty("foo"); - - assertNotNull("data not found", array); - assertEquals("property type", byte[].class, array.getClass()); - ArrayAssert.assertEquals(expected, (byte[]) array); - } - - /** - * Ensure that addProperty doesn't alter an array of byte - */ - public void testAddDataProperty() throws Exception - { - byte[] expected = new byte[]{1, 2, 3, 4}; - XMLPropertyListConfiguration config = new XMLPropertyListConfiguration(); - config.addProperty("foo", expected); - config.save("target/testdata.plist.xml"); - - XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml"); - Object array = config2.getProperty("foo"); - - assertNotNull("data not found", array); - assertEquals("property type", byte[].class, array.getClass()); - ArrayAssert.assertEquals(expected, (byte[]) array); - } - - public void testInitCopy() - { - XMLPropertyListConfiguration copy = new XMLPropertyListConfiguration((HierarchicalConfiguration) config); - StrictConfigurationComparator comp = new StrictConfigurationComparator(); - assertTrue("Configurations are not equal", comp.compare(config, copy)); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.configuration2.plist; + +import java.io.File; +import java.util.*; + +import junit.framework.TestCase; +import junitx.framework.ObjectAssert; +import junitx.framework.ArrayAssert; +import junitx.framework.ListAssert; + +import org.apache.commons.configuration2.Configuration; +import org.apache.commons.configuration2.ConfigurationAssert; +import org.apache.commons.configuration2.ConfigurationComparator; +import org.apache.commons.configuration2.FileConfiguration; +import org.apache.commons.configuration2.HierarchicalConfiguration; +import org.apache.commons.configuration2.StrictConfigurationComparator; +import org.apache.commons.configuration2.plist.XMLPropertyListConfiguration; + +/** + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + */ +public class TestXMLPropertyListConfiguration extends TestCase +{ + private FileConfiguration config; + + protected void setUp() throws Exception + { + config = new XMLPropertyListConfiguration(); + config.setFile(ConfigurationAssert.getTestFile("test.plist.xml")); + config.load(); + } + + public void testString() throws Exception + { + assertEquals("'string' property", "value1", config.getString("string")); + } + + public void testInteger() throws Exception + { + assertEquals("'integer' property", 12345, config.getInt("integer")); + } + + public void testReal() throws Exception + { + assertEquals("'real' property", -12.345, config.getDouble("real"), 0); + } + + public void testBoolean() throws Exception + { + assertEquals("'boolean1' property", true, config.getBoolean("boolean1")); + assertEquals("'boolean2' property", false, config.getBoolean("boolean2")); + } + + public void testDictionary() + { + assertEquals("1st element", "value1", config.getProperty("dictionary.key1")); + assertEquals("2nd element", "value2", config.getProperty("dictionary.key2")); + assertEquals("3rd element", "value3", config.getProperty("dictionary.key3")); + } + + public void testSubset() + { + Configuration subset = config.subset("dictionary"); + Iterator keys = subset.getKeys(); + + String key = (String) keys.next(); + assertEquals("1st key", "key1", key); + assertEquals("1st value", "value1", subset.getString(key)); + + key = (String) keys.next(); + assertEquals("2nd key", "key2", key); + assertEquals("2nd value", "value2", subset.getString(key)); + + key = (String) keys.next(); + assertEquals("3rd key", "key3", key); + assertEquals("3rd value", "value3", subset.getString(key)); + + assertFalse("more than 3 properties founds", keys.hasNext()); + } + + public void testArray() + { + Object array = config.getProperty("array"); + + assertNotNull("array not found", array); + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); + List list = config.getList("array"); + + assertFalse("empty array", list.isEmpty()); + assertEquals("size", 3, list.size()); + assertEquals("1st element", "value1", list.get(0)); + assertEquals("2nd element", "value2", list.get(1)); + assertEquals("3rd element", "value3", list.get(2)); + } + + public void testNestedArray() + { + String key = "nested-array"; + + Object array = config.getProperty(key); + + // root array + assertNotNull("array not found", array); + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); + List list = config.getList(key); + + assertFalse("empty array", list.isEmpty()); + assertEquals("size", 2, list.size()); + + // 1st array + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0)); + List list1 = (List) list.get(0); + assertFalse("nested array 1 is empty", list1.isEmpty()); + assertEquals("size", 2, list1.size()); + assertEquals("1st element", "a", list1.get(0)); + assertEquals("2nd element", "b", list1.get(1)); + + // 2nd array + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1)); + List list2 = (List) list.get(1); + assertFalse("nested array 2 is empty", list2.isEmpty()); + assertEquals("size", 2, list2.size()); + assertEquals("1st element", "c", list2.get(0)); + assertEquals("2nd element", "d", list2.get(1)); + } + + public void testDictionaryArray() + { + String key = "dictionary-array"; + + Object array = config.getProperty(key); + + // root array + assertNotNull("array not found", array); + ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array); + List list = config.getList(key); + + assertFalse("empty array", list.isEmpty()); + assertEquals("size", 2, list.size()); + + // 1st dictionary + ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0)); + Configuration conf1 = (Configuration) list.get(0); + assertFalse("configuration 1 is empty", conf1.isEmpty()); + assertEquals("configuration element", "bar", conf1.getProperty("foo")); + + // 2nd dictionary + ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1)); + Configuration conf2 = (Configuration) list.get(1); + assertFalse("configuration 2 is empty", conf2.isEmpty()); + assertEquals("configuration element", "value", conf2.getProperty("key")); + } + + public void testNested() + { + assertEquals("nested property", "value", config.getString("nested.node1.node2.node3")); + } + + public void testSave() throws Exception + { + File savedFile = new File("target/testsave.plist.xml"); + + // remove the file previously saved if necessary + if (savedFile.exists()) + { + assertTrue(savedFile.delete()); + } + + // add an array of strings to the configuration + /* + config.addProperty("string", "value1"); + List list = new ArrayList(); + for (int i = 1; i < 5; i++) + { + list.add("value" + i); + } + config.addProperty("newarray", list);*/ + // todo : investigate why the array structure of 'newarray' is lost in the saved file + + // add a map of strings + /* + Map map = new HashMap(); + map.put("foo", "bar"); + map.put("int", new Integer(123)); + config.addProperty("newmap", map); + */ + // todo : a Map added to a HierarchicalConfiguration should be decomposed as list of nodes + + // save the configuration + String filename = savedFile.getAbsolutePath(); + config.save(filename); + + assertTrue("The saved file doesn't exist", savedFile.exists()); + + // read the configuration and compare the properties + Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename)); + + Iterator it = config.getKeys(); + while (it.hasNext()) + { + String key = (String) it.next(); + assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key)); + + Object value = checkConfig.getProperty(key); + if (value instanceof byte[]) + { + byte[] array = (byte[]) value; + ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array); + } + else if (value instanceof List) + { + List list1 = (List) config.getProperty(key); + List list2 = (List) value; + + assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size()); + + for (int i = 0; i < list2.size(); i++) + { + Object value1 = list1.get(i); + Object value2 = list2.get(i); + + if (value1 instanceof Configuration) + { + ConfigurationComparator comparator = new StrictConfigurationComparator(); + assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2)); + } + else + { + assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2); + } + } + + ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1); + } + else + { + assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key)); + } + + } + } + + /** + * Ensure that setProperty doesn't alter an array of byte + * since it's a first class type in plist file + */ + public void testSetDataProperty() throws Exception + { + byte[] expected = new byte[]{1, 2, 3, 4}; + XMLPropertyListConfiguration config = new XMLPropertyListConfiguration(); + config.setProperty("foo", expected); + config.save("target/testdata.plist.xml"); + + XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml"); + Object array = config2.getProperty("foo"); + + assertNotNull("data not found", array); + assertEquals("property type", byte[].class, array.getClass()); + ArrayAssert.assertEquals(expected, (byte[]) array); + } + + /** + * Ensure that addProperty doesn't alter an array of byte + */ + public void testAddDataProperty() throws Exception + { + byte[] expected = new byte[]{1, 2, 3, 4}; + XMLPropertyListConfiguration config = new XMLPropertyListConfiguration(); + config.addProperty("foo", expected); + config.save("target/testdata.plist.xml"); + + XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml"); + Object array = config2.getProperty("foo"); + + assertNotNull("data not found", array); + assertEquals("property type", byte[].class, array.getClass()); + ArrayAssert.assertEquals(expected, (byte[]) array); + } + + public void testInitCopy() + { + XMLPropertyListConfiguration copy = new XMLPropertyListConfiguration((HierarchicalConfiguration) config); + StrictConfigurationComparator comp = new StrictConfigurationComparator(); + assertTrue("Configurations are not equal", comp.compare(config, copy)); + } +} Propchange: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java ------------------------------------------------------------------------------ svn:eol-style = native