    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.util.*;
    22
    23  import junit.framework.TestCase;
    24  import junitx.framework.ObjectAssert;
    25  import junitx.framework.ArrayAssert;
    26  import junitx.framework.ListAssert;
    27  import org.apache.commons.configuration.FileConfiguration;
    28  import org.apache.commons.configuration.Configuration;
    29  import org.apache.commons.configuration.HierarchicalConfiguration;
    30  import org.apache.commons.configuration.StrictConfigurationComparator;
    31  import org.apache.commons.configuration.ConfigurationComparator;
    32
    33  /***
    34   * @author Emmanuel Bourg
    35   * @version $Revision: 530587 $, $Date: 2007-04-20 01:08:36 +0200 (Fr, 20 Apr 2007) $
    36   */
    37  public class TestXMLPropertyListConfiguration extends TestCase
    38  {
    39      private FileConfiguration config;
    40
    41      protected void setUp() throws Exception
    42      {
    43          config = new XMLPropertyListConfiguration();
    44          config.setFileName("conf/test.plist.xml");
    45          config.load();
    46      }
    47
    48      public void testString() throws Exception
    49      {
    50          assertEquals("'string' property", "value1", config.getString("string"));
    51      }
    52
    53      public void testInteger() throws Exception
    54      {
    55          assertEquals("'integer' property", 12345, config.getInt("integer"));
    56      }
    57
    58      public void testReal() throws Exception
    59      {
    60          assertEquals("'real' property", -12.345, config.getDouble("real"), 0);
    61      }
    62
    63      public void testBoolean() throws Exception
    64      {
    65          assertEquals("'boolean1' property", true, config.getBoolean("boolean1"));
    66          assertEquals("'boolean2' property", false, config.getBoolean("boolean2"));
    67      }
    68
    69      public void testDictionary()
    70      {
    71          assertEquals("1st element", "value1", config.getProperty("dictionary.key1"));
    72          assertEquals("2nd element", "value2", config.getProperty("dictionary.key2"));
    73          assertEquals("3rd element", "value3", config.getProperty("dictionary.key3"));
    74      }
    75
    76      public void testSubset()
    77      {
    78          Configuration subset = config.subset("dictionary");
    79          Iterator keys = subset.getKeys();
    80
    81          String key = (String) keys.next();
    82          assertEquals("1st key", "key1", key);
    83          assertEquals("1st value", "value1", subset.getString(key));
    84
    85          key = (String) keys.next();
    86          assertEquals("2nd key", "key2", key);
    87          assertEquals("2nd value", "value2", subset.getString(key));
    88
    89          key = (String) keys.next();
    90          assertEquals("3rd key", "key3", key);
    91          assertEquals("3rd value", "value3", subset.getString(key));
    92
    93          assertFalse("more than 3 properties founds", keys.hasNext());
    94      }
    95
    96      public void testArray()
    97      {
    98          Object array = config.getProperty("array");
    99
    100         assertNotNull("array not found", array);
    101         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
    102         List list = config.getList("array");
    103
    104         assertFalse("empty array", list.isEmpty());
    105         assertEquals("size", 3, list.size());
    106         assertEquals("1st element", "value1", list.get(0));
    107         assertEquals("2nd element", "value2", list.get(1));
    108         assertEquals("3rd element", "value3", list.get(2));
    109     }
    110
    111     public void testNestedArray()
    112     {
    113         String key = "nested-array";
    114
    115         Object array = config.getProperty(key);
    116
    117         // root array
    118         assertNotNull("array not found", array);
    119         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
    120         List list = config.getList(key);
    121
    122         assertFalse("empty array", list.isEmpty());
    123         assertEquals("size", 2, list.size());
    124
    125         // 1st array
    126         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0));
    127         List list1 = (List) list.get(0);
    128         assertFalse("nested array 1 is empty", list1.isEmpty());
    129         assertEquals("size", 2, list1.size());
    130         assertEquals("1st element", "a", list1.get(0));
    131         assertEquals("2nd element", "b", list1.get(1));
    132
    133         // 2nd array
    134         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1));
    135         List list2 = (List) list.get(1);
    136         assertFalse("nested array 2 is empty", list2.isEmpty());
    137         assertEquals("size", 2, list2.size());
    138         assertEquals("1st element", "c", list2.get(0));
    139         assertEquals("2nd element", "d", list2.get(1));
    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 testNested()
    170     {
    171         assertEquals("nested property", "value", config.getString("nested.node1.node2.node3"));
    172     }
    173
    174     public void testSave() throws Exception
    175     {
    176         File savedFile = new File("target/testsave.plist.xml");
    177
    178         // remove the file previously saved if necessary
    179         if (savedFile.exists())
    180         {
    181             assertTrue(savedFile.delete());
    182         }
    183
    184         // add an array of strings to the configuration
    185         /*
    186         config.addProperty("string", "value1");
    187         List list = new ArrayList();
    188         for (int i = 1; i < 5; i++)
    189         {
    190             list.add("value" + i);
    191         }
    192         config.addProperty("newarray", list);*/
    193         // todo : investigate why the array structure of 'newarray' is lost in the saved file
    194
    195         // add a map of strings
    196         /*
    197         Map map = new HashMap();
    198         map.put("foo", "bar");
    199         map.put("int", new Integer(123));
    200         config.addProperty("newmap", map);
    201         */
    202         // todo : a Map added to a HierarchicalConfiguration should be decomposed as list of nodes
    203
    204         // save the configuration
    205         String filename = savedFile.getAbsolutePath();
    206         config.save(filename);
    207
    208         assertTrue("The saved file doesn't exist", savedFile.exists());
    209
    210         // read the configuration and compare the properties
    211         Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename));
    212
    213         Iterator it = config.getKeys();
    214         while (it.hasNext())
    215         {
    216             String key = (String) it.next();
    217             assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
    218
    219             Object value = checkConfig.getProperty(key);
    220             if (value instanceof byte[])
    221             {
    222                 byte[] array = (byte[]) value;
    223                 ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
    224             }
    225             else if (value instanceof List)
    226             {
    227                 List list1 = (List) config.getProperty(key);
    228                 List list2 = (List) value;
    229
    230                 assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
    231
    232                 for (int i = 0; i < list2.size(); i++)
    233                 {
    234                     Object value1 = list1.get(i);
    235                     Object value2 = list2.get(i);
    236
    237                     if (value1 instanceof Configuration)
    238                     {
    239                         ConfigurationComparator comparator = new StrictConfigurationComparator();
    240                         assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2));
    241                     }
    242                     else
    243                     {
    244                         assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
    245                     }
    246                 }
    247
    248                 ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1);
    249             }
    250             else
    251             {
    252                 assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
    253             }
    254
    255         }
    256     }
    257
    258     /***
    259      * Ensure that setProperty doesn't alter an array of byte
    260      * since it's a first class type in plist file
    261      */
    262     public void testSetDataProperty() throws Exception
    263     {
    264         byte[] expected = new byte[]{1, 2, 3, 4};
    265         XMLPropertyListConfiguration config = new XMLPropertyListConfiguration();
    266         config.setProperty("foo", expected);
    267         config.save("target/testdata.plist.xml");
    268
    269         XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml");
    270         Object array = config2.getProperty("foo");
    271
    272         assertNotNull("data not found", array);
    273         assertEquals("property type", byte[].class, array.getClass());
    274         ArrayAssert.assertEquals(expected, (byte[]) array);
    275     }
    276
    277     /***
    278      * Ensure that addProperty doesn't alter an array of byte
    279      */
    280     public void testAddDataProperty() throws Exception
    281     {
    282         byte[] expected = new byte[]{1, 2, 3, 4};
    283         XMLPropertyListConfiguration config = new XMLPropertyListConfiguration();
    284         config.addProperty("foo", expected);
    285         config.save("target/testdata.plist.xml");
    286
    287         XMLPropertyListConfiguration config2 = new XMLPropertyListConfiguration("target/testdata.plist.xml");
    288         Object array = config2.getProperty("foo");
    289
    290         assertNotNull("data not found", array);
    291         assertEquals("property type", byte[].class, array.getClass());
    292         ArrayAssert.assertEquals(expected, (byte[]) array);
    293     }
    294
    295     public void testInitCopy()
    296 	{
    297 		XMLPropertyListConfiguration copy = new XMLPropertyListConfiguration((HierarchicalConfiguration) config);
    298 		StrictConfigurationComparator comp = new StrictConfigurationComparator();
    299 		assertTrue("Configurations are not equal", comp.compare(config, copy));
    300 	}
    301 }