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