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.Iterator;
23  import java.util.List;
24  
25  import junit.framework.TestCase;
26  import junitx.framework.ArrayAssert;
27  import junitx.framework.ListAssert;
28  import junitx.framework.ObjectAssert;
29  import org.apache.commons.configuration.Configuration;
30  import org.apache.commons.configuration.ConfigurationComparator;
31  import org.apache.commons.configuration.ConfigurationException;
32  import org.apache.commons.configuration.StrictConfigurationComparator;
33  
34  /***
35   * @author Emmanuel Bourg
36   * @version $Revision$, $Date$
37   */
38  public class TestPropertyListConfiguration extends TestCase
39  {
40      private PropertyListConfiguration config;
41  
42      private String testProperties = new File("conf/test.plist").getAbsolutePath();
43  
44      protected void setUp() throws Exception
45      {
46          config = new PropertyListConfiguration();
47          config.setFileName(testProperties);
48          config.load();
49      }
50  
51      public void testLoad()
52      {
53          assertFalse("the configuration is empty", config.isEmpty());
54      }
55  
56      public void testLoadWithError()
57      {
58          config = new PropertyListConfiguration();
59          try {
60              config.load(new StringReader(""));
61              fail("No exception thrown on loading an empty file");
62          } catch (ConfigurationException e) {
63              // expected
64              assertNotNull(e.getMessage());
65          }
66      }
67  
68      public void testString()
69      {
70          assertEquals("simple-string", "string1", config.getProperty("simple-string"));
71      }
72  
73      public void testQuotedString()
74      {
75          assertEquals("quoted-string", "string2", config.getProperty("quoted-string"));
76          assertEquals("quoted-string2", "this is a string", config.getProperty("quoted-string2"));
77          assertEquals("complex-string", "this is a \"complex\" string {(=,;)}", config.getProperty("complex-string"));
78      }
79  
80      public void testEmptyArray()
81      {
82          String key = "empty-array";
83          assertNotNull("array null", config.getProperty(key));
84  
85          List list = (List) config.getProperty(key);
86          assertTrue("array is not empty", list.isEmpty());
87      }
88  
89      public void testArray()
90      {
91          String key = "array";
92          assertNotNull("array null", config.getProperty(key));
93  
94          List list = (List) config.getProperty(key);
95          assertFalse("array is empty", list.isEmpty());
96  
97          assertEquals("1st value", "value1", list.get(0));
98          assertEquals("2nd value", "value2", list.get(1));
99          assertEquals("3rd value", "value3", list.get(2));
100     }
101 
102     public void testNestedArrays()
103     {
104         String key = "nested-arrays";
105 
106         Object array = config.getProperty(key);
107 
108         // root array
109         assertNotNull("array not found", array);
110         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
111         List list = config.getList(key);
112 
113         assertFalse("empty array", list.isEmpty());
114         assertEquals("size", 2, list.size());
115 
116         // 1st array
117         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(0));
118         List list1 = (List) list.get(0);
119         assertFalse("nested array 1 is empty", list1.isEmpty());
120         assertEquals("size", 2, list1.size());
121         assertEquals("1st element", "a", list1.get(0));
122         assertEquals("2nd element", "b", list1.get(1));
123 
124         // 2nd array
125         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, list.get(1));
126         List list2 = (List) list.get(1);
127         assertFalse("nested array 2 is empty", list2.isEmpty());
128         assertEquals("size", 2, list2.size());
129         assertEquals("1st element", "c", list2.get(0));
130         assertEquals("2nd element", "d", list2.get(1));
131     }
132 
133     public void testDictionary()
134     {
135         assertEquals("1st element in dictionary", "bar1", config.getProperty("dictionary.foo1"));
136         assertEquals("2nd element in dictionary", "bar2", config.getProperty("dictionary.foo2"));
137     }
138 
139     public void testDictionaryArray()
140     {
141         String key = "dictionary-array";
142 
143         Object array = config.getProperty(key);
144 
145         // root array
146         assertNotNull("array not found", array);
147         ObjectAssert.assertInstanceOf("the array element is not parsed as a List", List.class, array);
148         List list = config.getList(key);
149 
150         assertFalse("empty array", list.isEmpty());
151         assertEquals("size", 2, list.size());
152 
153         // 1st dictionary
154         ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(0));
155         Configuration conf1 = (Configuration) list.get(0);
156         assertFalse("configuration 1 is empty", conf1.isEmpty());
157         assertEquals("configuration element", "bar", conf1.getProperty("foo"));
158 
159         // 2nd dictionary
160         ObjectAssert.assertInstanceOf("the dict element is not parsed as a Configuration", Configuration.class, list.get(1));
161         Configuration conf2 = (Configuration) list.get(1);
162         assertFalse("configuration 2 is empty", conf2.isEmpty());
163         assertEquals("configuration element", "value", conf2.getProperty("key"));
164     }
165 
166     public void testNestedDictionaries()
167     {
168         assertEquals("nested property", "value", config.getString("nested-dictionaries.foo.bar.key"));
169     }
170 
171     public void testData()
172     {
173         ObjectAssert.assertInstanceOf("data", (new byte[0]).getClass(), config.getProperty("data"));
174         ArrayAssert.assertEquals("data", "foo bar".getBytes(), (byte[]) config.getProperty("data"));
175     }
176 
177 
178     public void testSave() throws Exception
179     {
180         File savedFile = new File("target/testsave.plist");
181 
182         // remove the file previously saved if necessary
183         if (savedFile.exists())
184         {
185             assertTrue(savedFile.delete());
186         }
187 
188         // save the configuration
189         String filename = savedFile.getAbsolutePath();
190         config.save(filename);
191 
192         assertTrue("The saved file doesn't exist", savedFile.exists());
193 
194         // read the configuration and compare the properties
195         Configuration checkConfig = new PropertyListConfiguration(new File(filename));
196 
197         Iterator it = config.getKeys();
198         while (it.hasNext())
199         {
200             String key = (String) it.next();
201             assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
202 
203             Object value = checkConfig.getProperty(key);
204             if (value instanceof byte[])
205             {
206                 byte[] array = (byte[]) value;
207                 ArrayAssert.assertEquals("Value of the '" + key + "' property", (byte[]) config.getProperty(key), array);
208             }
209             else if (value instanceof List)
210             {
211                 List list1 = (List) config.getProperty(key);
212                 List list2 = (List) value;
213 
214                 assertEquals("The size of the list for the key '" + key + "' doesn't match", list1.size(), list2.size());
215 
216                 for (int i = 0; i < list2.size(); i++)
217                 {
218                     Object value1 = list1.get(i);
219                     Object value2 = list2.get(i);
220 
221                     if (value1 instanceof Configuration)
222                     {
223                         ConfigurationComparator comparator = new StrictConfigurationComparator();
224                         assertTrue("The dictionnary at index " + i + " for the key '" + key + "' doesn't match", comparator.compare((Configuration) value1, (Configuration) value2));
225                     }
226                     else
227                     {
228                         assertEquals("Element at index " + i + " for the key '" + key + "'", value1, value2);
229                     }
230                 }
231 
232                 ListAssert.assertEquals("Value of the '" + key + "' property", (List) config.getProperty(key), list1);
233             }
234             else
235             {
236                 assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key));
237             }
238 
239         }
240     }
241 
242     public void testQuoteString()
243     {
244         assertEquals("null string", null, config.quoteString(null));
245         assertEquals("simple string", "abcd", config.quoteString("abcd"));
246         assertEquals("string with a space", "\"ab cd\"", config.quoteString("ab cd"));
247         assertEquals("string with a quote", "\"foo//\"bar\"", config.quoteString("foo\"bar"));
248         assertEquals("string with a special char", "\"foo;bar\"", config.quoteString("foo;bar"));
249     }
250 }