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.StrictConfigurationComparator;
30  import org.apache.commons.configuration.ConfigurationComparator;
31  
32  /***
33   * @author Emmanuel Bourg
34   * @version $Revision$, $Date$
35   */
36  public class TestXMLPropertyListConfiguration extends TestCase
37  {
38      private FileConfiguration config;
39  
40      protected void setUp() throws Exception
41      {
42          config = new XMLPropertyListConfiguration();
43          config.setFileName("conf/test.plist.xml");
44          config.load();
45      }
46  
47      public void testString() throws Exception
48      {
49          assertEquals("'string' property", "value1", config.getString("string"));
50      }
51  
52      public void testInteger() throws Exception
53      {
54          assertEquals("'integer' property", 12345, config.getInt("integer"));
55      }
56  
57      public void testReal() throws Exception
58      {
59          assertEquals("'real' property", -12.345, config.getDouble("real"), 0);
60      }
61  
62      public void testBoolean() throws Exception
63      {
64          assertEquals("'boolean1' property", true, config.getBoolean("boolean1"));
65          assertEquals("'boolean2' property", false, config.getBoolean("boolean2"));
66      }
67  
68      public void testDictionary()
69      {
70          assertEquals("1st element", "value1", config.getProperty("dictionary.key1"));
71          assertEquals("2nd element", "value2", config.getProperty("dictionary.key2"));
72          assertEquals("3rd element", "value3", config.getProperty("dictionary.key3"));
73      }
74  
75      public void testSubset()
76      {
77          Configuration subset = config.subset("dictionary");
78          Iterator keys = subset.getKeys();
79  
80          String key = (String) keys.next();
81          assertEquals("1st key", "key1", key);
82          assertEquals("1st value", "value1", subset.getString(key));
83  
84          key = (String) keys.next();
85          assertEquals("2nd key", "key2", key);
86          assertEquals("2nd value", "value2", subset.getString(key));
87  
88          key = (String) keys.next();
89          assertEquals("3rd key", "key3", key);
90          assertEquals("3rd value", "value3", subset.getString(key));
91  
92          assertFalse("more than 3 properties founds", keys.hasNext());
93      }
94  
95      public void testArray()
96      {
97          Object array = config.getProperty("array");
98  
99          assertNotNull("array not found", array);
100         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
101         List list = config.getList("array");
102 
103         assertFalse("empty array", list.isEmpty());
104         assertEquals("size", 3, list.size());
105         assertEquals("1st element", "value1", list.get(0));
106         assertEquals("2nd element", "value2", list.get(1));
107         assertEquals("3rd element", "value3", list.get(2));
108     }
109 
110     public void testNestedArray()
111     {
112         String key = "nested-array";
113 
114         Object array = config.getProperty(key);
115 
116         // root array
117         assertNotNull("array not found", array);
118         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
119         List list = config.getList(key);
120 
121         assertFalse("empty array", list.isEmpty());
122         assertEquals("size", 2, list.size());
123 
124         // 1st array
125         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0));
126         List list1 = (List) list.get(0);
127         assertFalse("nested array 1 is empty", list1.isEmpty());
128         assertEquals("size", 2, list1.size());
129         assertEquals("1st element", "a", list1.get(0));
130         assertEquals("2nd element", "b", list1.get(1));
131 
132         // 2nd array
133         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1));
134         List list2 = (List) list.get(1);
135         assertFalse("nested array 2 is empty", list2.isEmpty());
136         assertEquals("size", 2, list2.size());
137         assertEquals("1st element", "c", list2.get(0));
138         assertEquals("2nd element", "d", list2.get(1));
139     }
140 
141     public void testDictionaryArray()
142     {
143         String key = "dictionary-array";
144 
145         Object array = config.getProperty(key);
146 
147         // root array
148         assertNotNull("array not found", array);
149         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
150         List list = config.getList(key);
151 
152         assertFalse("empty array", list.isEmpty());
153         assertEquals("size", 2, list.size());
154 
155         // 1st dictionary
156         ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0));
157         Configuration conf1 = (Configuration) list.get(0);
158         assertFalse("configuration 1 is empty", conf1.isEmpty());
159         assertEquals("configuration element", "bar", conf1.getProperty("foo"));
160 
161         // 2nd dictionary
162         ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1));
163         Configuration conf2 = (Configuration) list.get(1);
164         assertFalse("configuration 2 is empty", conf2.isEmpty());
165         assertEquals("configuration element", "value", conf2.getProperty("key"));
166     }
167 
168     public void testNested()
169     {
170         assertEquals("nested property", "value", config.getString("nested.node1.node2.node3"));
171     }
172 
173     public void testSave() throws Exception
174     {
175         File savedFile = new File("target/testsave.plist.xml");
176 
177         // remove the file previously saved if necessary
178         if (savedFile.exists())
179         {
180             assertTrue(savedFile.delete());
181         }
182 
183         // add an array of strings to the configuration
184         /*
185         config.addProperty("string", "value1");
186         List list = new ArrayList();
187         for (int i = 1; i < 5; i++)
188         {
189             list.add("value" + i);
190         }
191         config.addProperty("newarray", list);*/
192         // todo : investigate why the array structure of 'newarray' is lost in the saved file
193 
194         // add a map of strings
195         /*
196         Map map = new HashMap();
197         map.put("foo", "bar");
198         map.put("int", new Integer(123));
199         config.addProperty("newmap", map);
200         */
201         // todo : a Map added to a HierarchicalConfiguration should be decomposed as list of nodes
202 
203         // save the configuration
204         String filename = savedFile.getAbsolutePath();
205         config.save(filename);
206 
207         assertTrue("The saved file doesn't exist", savedFile.exists());
208 
209         // read the configuration and compare the properties
210         Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename));
211 
212         Iterator it = config.getKeys();
213         while (it.hasNext())
214         {
215             String key = (String) it.next();
216             assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
217 
218             Object value = checkConfig.getProperty(key);
219             if (value instanceof byte[])
220             {
221                 byte[] array = (byte[]) value;
222                 ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
223             }
224             else if (value instanceof List)
225             {
226                 List list1 = (List) config.getProperty(key);
227                 List list2 = (List) value;
228 
229                 assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
230 
231                 for (int i = 0; i < list2.size(); i++)
232                 {
233                     Object value1 = list1.get(i);
234                     Object value2 = list2.get(i);
235 
236                     if (value1 instanceof Configuration)
237                     {
238                         ConfigurationComparator comparator = new StrictConfigurationComparator();
239                         assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2));
240                     }
241                     else
242                     {
243                         assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
244                     }
245                 }
246 
247                 ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1);
248             }
249             else
250             {
251                 assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
252             }
253 
254         }
255     }
256 }