1   /*
2    * Copyright 2001-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;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import junit.framework.TestCase;
25  
26  /***
27   * test for loading and saving xml properties files
28   *
29   * @version $Id: TestXMLConfiguration.java 156237 2005-03-05 10:26:22Z oheger $
30   */
31  public class TestXMLConfiguration extends TestCase
32  {
33      /*** The File that we test with */
34      private String testProperties = new File("conf/test.xml").getAbsolutePath();
35      private String testProperties2 = new File("conf/testDigesterConfigurationInclude1.xml").getAbsolutePath();
36      private String testBasePath = new File("conf").getAbsolutePath();
37      private File testSaveConf = new File("target/testsave.xml");
38  
39      private XMLConfiguration conf;
40  
41      protected void setUp() throws Exception
42      {
43          conf = new XMLConfiguration();
44          conf.setFile(new File(testProperties));
45          conf.load();
46      }
47  
48      public void testGetProperty()
49      {
50          assertEquals("value", conf.getProperty("element"));
51      }
52  
53      public void testGetCommentedProperty()
54      {
55          assertEquals(null, conf.getProperty("test.comment"));
56      }
57  
58      public void testGetPropertyWithXMLEntity()
59      {
60          assertEquals("1<2", conf.getProperty("test.entity"));
61      }
62  
63      public void testClearProperty() throws ConfigurationException, IOException
64      {
65          // test non-existent element
66          String key = "clearly";
67          conf.clearProperty(key);
68          assertNull(key, conf.getProperty(key));
69          assertNull(key, conf.getProperty(key));
70  
71          // test single element
72          conf.load();
73          key = "clear.element";
74          conf.clearProperty(key);
75          assertNull(key, conf.getProperty(key));
76          assertNull(key, conf.getProperty(key));
77  
78          // test single element with attribute
79          conf.load();
80          key = "clear.element2";
81          conf.clearProperty(key);
82          assertNull(key, conf.getProperty(key));
83          assertNull(key, conf.getProperty(key));
84          key = "clear.element2[@id]";
85          assertNotNull(key, conf.getProperty(key));
86          assertNotNull(key, conf.getProperty(key));
87  
88          // test non-text/cdata element
89          conf.load();
90          key = "clear.comment";
91          conf.clearProperty(key);
92          assertNull(key, conf.getProperty(key));
93          assertNull(key, conf.getProperty(key));
94  
95          // test cdata element
96          conf.load();
97          key = "clear.cdata";
98          conf.clearProperty(key);
99          assertNull(key, conf.getProperty(key));
100         assertNull(key, conf.getProperty(key));
101 
102         // test multiple sibling elements
103         conf.load();
104         key = "clear.list.item";
105         conf.clearProperty(key);
106         assertNull(key, conf.getProperty(key));
107         assertNull(key, conf.getProperty(key));
108         key = "clear.list.item[@id]";
109         assertNotNull(key, conf.getProperty(key));
110         assertNotNull(key, conf.getProperty(key));
111 
112         // test multiple, disjoined elements
113         conf.load();
114         key = "list.item";
115         conf.clearProperty(key);
116         assertNull(key, conf.getProperty(key));
117         assertNull(key, conf.getProperty(key));
118     }
119 
120     public void testgetProperty() {
121         // test non-leaf element
122         Object property = conf.getProperty("clear");
123         assertNull(property);
124 
125         // test non-existent element
126         property = conf.getProperty("e");
127         assertNull(property);
128 
129         // test non-existent element
130         property = conf.getProperty("element3[@n]");
131         assertNull(property);
132 
133         // test single element
134         property = conf.getProperty("element");
135         assertNotNull(property);
136         assertTrue(property instanceof String);
137         assertEquals("value", property);
138 
139         // test single attribute
140         property = conf.getProperty("element3[@name]");
141         assertNotNull(property);
142         assertTrue(property instanceof String);
143         assertEquals("foo", property);
144 
145         // test non-text/cdata element
146         property = conf.getProperty("test.comment");
147         assertNull(property);
148 
149         // test cdata element
150         property = conf.getProperty("test.cdata");
151         assertNotNull(property);
152         assertTrue(property instanceof String);
153         assertEquals("<cdata value>", property);
154 
155         // test multiple sibling elements
156         property = conf.getProperty("list.sublist.item");
157         assertNotNull(property);
158         assertTrue(property instanceof List);
159         List list = (List)property;
160         assertEquals(2, list.size());
161         assertEquals("five", list.get(0));
162         assertEquals("six", list.get(1));
163 
164         // test multiple, disjoined elements
165         property = conf.getProperty("list.item");
166         assertNotNull(property);
167         assertTrue(property instanceof List);
168         list = (List)property;
169         assertEquals(4, list.size());
170         assertEquals("one", list.get(0));
171         assertEquals("two", list.get(1));
172         assertEquals("three", list.get(2));
173         assertEquals("four", list.get(3));
174 
175         // test multiple, disjoined attributes
176         property = conf.getProperty("list.item[@name]");
177         assertNotNull(property);
178         assertTrue(property instanceof List);
179         list = (List)property;
180         assertEquals(2, list.size());
181         assertEquals("one", list.get(0));
182         assertEquals("three", list.get(1));
183     }
184 
185     public void testGetAttribute()
186     {
187         assertEquals("element3[@name]", "foo", conf.getProperty("element3[@name]"));
188     }
189 
190     public void testClearAttribute() throws Exception
191     {
192         // test non-existent attribute
193         String key = "clear[@id]";
194         conf.clearProperty(key);
195         assertNull(key, conf.getProperty(key));
196         assertNull(key, conf.getProperty(key));
197 
198         // test single attribute
199         conf.load();
200         key = "clear.element2[@id]";
201         Object p = conf.getProperty(key);
202         p = conf.getProperty("clear.element2");
203         conf.clearProperty(key);
204         assertNull(key, conf.getProperty(key));
205         assertNull(key, conf.getProperty(key));
206         key = "clear.element2";
207         assertNotNull(key, conf.getProperty(key));
208         assertNotNull(key, conf.getProperty(key));
209 
210         // test multiple, disjoined attributes
211         conf.load();
212         key = "clear.list.item[@id]";
213         conf.clearProperty(key);
214         assertNull(key, conf.getProperty(key));
215         assertNull(key, conf.getProperty(key));
216         key = "clear.list.item";
217         assertNotNull(key, conf.getProperty(key));
218         assertNotNull(key, conf.getProperty(key));
219     }
220 
221     public void testSetAttribute()
222     {
223         // replace an existing attribute
224         conf.setProperty("element3[@name]", "bar");
225         assertEquals("element3[@name]", "bar", conf.getProperty("element3[@name]"));
226 
227         // set a new attribute
228         conf.setProperty("foo[@bar]", "value");
229         assertEquals("foo[@bar]", "value", conf.getProperty("foo[@bar]"));
230         
231         conf.setProperty("name1","value1");
232         assertEquals("value1",conf.getProperty("name1"));
233     }
234 
235     public void testAddAttribute()
236     {
237         conf.addProperty("element3[@name]", "bar");
238 
239         List list = conf.getList("element3[@name]");
240         assertNotNull("null list", list);
241         assertTrue("'foo' element missing", list.contains("foo"));
242         assertTrue("'bar' element missing", list.contains("bar"));
243         assertEquals("list size", 2, list.size());
244     }
245 
246     public void testAddObjectAttribute()
247     {
248         conf.addProperty("test.boolean[@value]", Boolean.TRUE);
249         assertTrue("test.boolean[@value]", conf.getBoolean("test.boolean[@value]"));
250     }
251 
252     public void testAddList()
253     {
254         conf.addProperty("test.array", "value1");
255         conf.addProperty("test.array", "value2");
256 
257         List list = conf.getList("test.array");
258         assertNotNull("null list", list);
259         assertTrue("'value1' element missing", list.contains("value1"));
260         assertTrue("'value2' element missing", list.contains("value2"));
261         assertEquals("list size", 2, list.size());
262     }
263 
264     public void testGetComplexProperty()
265     {
266         assertEquals("I'm complex!", conf.getProperty("element2.subelement.subsubelement"));
267     }
268 
269     public void testSettingFileNames()
270     {
271         conf = new XMLConfiguration();
272         conf.setFileName(testProperties);
273         assertEquals(testProperties.toString(), conf.getFileName());
274 
275         conf.setBasePath(testBasePath);
276         conf.setFileName("hello.xml");
277         assertEquals("hello.xml", conf.getFileName());
278         assertEquals(testBasePath.toString(), conf.getBasePath());
279         assertEquals(new File(testBasePath, "hello.xml"), conf.getFile());
280 
281         conf.setBasePath(testBasePath);
282         conf.setFileName("subdir/hello.xml");
283         assertEquals("subdir/hello.xml", conf.getFileName());
284         assertEquals(testBasePath.toString(), conf.getBasePath());
285         assertEquals(new File(testBasePath, "subdir/hello.xml"), conf.getFile());
286     }
287 
288     public void testLoad() throws Exception
289     {
290         conf = new XMLConfiguration();
291         conf.setFileName(testProperties);
292         conf.load();
293 
294         assertEquals("I'm complex!", conf.getProperty("element2.subelement.subsubelement"));
295     }
296 
297     public void testLoadWithBasePath() throws Exception
298     {
299         conf = new XMLConfiguration();
300 
301         conf.setFileName("test.xml");
302         conf.setBasePath(testBasePath);
303         conf.load();
304 
305         assertEquals("I'm complex!", conf.getProperty("element2.subelement.subsubelement"));
306     }
307 
308     public void testLoadFromJAR() throws Exception
309     {
310         conf = new XMLConfiguration();
311         conf.setFileName("test-jar.xml");
312         conf.load();
313 
314         assertEquals("I'm complex!", conf.getProperty("element2.subelement.subsubelement"));
315     }
316 
317     public void testSetProperty() throws Exception
318     {
319         conf.setProperty("element.string", "hello");
320 
321         assertEquals("'element.string'", "hello", conf.getString("element.string"));
322         assertEquals("XML value of element.string", "hello", conf.getProperty("element.string"));
323     }
324 
325     public void testAddProperty()
326     {
327         // add a property to a non initialized xml configuration
328         XMLConfiguration config = new XMLConfiguration();
329         config.addProperty("test.string", "hello");
330 
331         assertEquals("'test.string'", "hello", config.getString("test.string"));
332     }
333 
334     public void testAddObjectProperty()
335     {
336         // add a non string property
337         conf.addProperty("test.boolean", Boolean.TRUE);
338         assertTrue("'test.boolean'", conf.getBoolean("test.boolean"));
339     }
340 
341     public void testSave() throws Exception
342     {
343         // remove the file previously saved if necessary
344         if(testSaveConf.exists()){
345             assertTrue(testSaveConf.delete());
346         }
347 
348         // add an array of strings to the configuration
349         conf.addProperty("string", "value1");
350         for (int i = 1; i < 5; i++)
351         {
352             conf.addProperty("test.array", "value" + i);
353         }
354 
355         // add an array of strings in an attribute
356         for (int i = 1; i < 5; i++)
357         {
358            conf.addProperty("test.attribute[@array]", "value" + i);
359         }
360 
361         // save the configuration
362         conf.save(testSaveConf.getAbsolutePath());
363 
364         // read the configuration and compare the properties
365         XMLConfiguration checkConfig = new XMLConfiguration();
366         checkConfig.setFileName(testSaveConf.getAbsolutePath());
367         checkConfig.load();
368 
369         for (Iterator i = conf.getKeys(); i.hasNext();)
370         {
371             String key = (String) i.next();
372             assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key));
373             assertEquals("Value of the '" + key + "' property", conf.getProperty(key), checkConfig.getProperty(key));
374         }
375     }
376 
377     public void testAutoSave() throws Exception
378     {
379         conf.setFile(new File("target/testsave.xml"));
380         conf.setAutoSave(true);
381         conf.setProperty("autosave", "ok");
382 
383         // reload the configuration
384         XMLConfiguration conf2 = new XMLConfiguration(conf.getFile());
385         assertEquals("'autosave' property", "ok", conf2.getString("autosave"));
386         
387         conf.clearTree("clear");
388         conf2 = new XMLConfiguration(conf.getFile());
389         Configuration sub = conf2.subset("clear");
390         assertTrue(sub.isEmpty());
391     }
392     
393     /***
394      * Tests if a second file can be appended to a first.
395      */
396     public void testAppend() throws Exception
397     {
398         conf = new XMLConfiguration();
399         conf.setFileName(testProperties);
400         conf.load();
401         conf.load(testProperties2);
402         assertEquals("value", conf.getString("element"));
403         assertEquals("tasks", conf.getString("table.name"));
404         
405         if (testSaveConf.exists())
406         {
407             assertTrue(testSaveConf.delete());
408         }
409         conf.save(testSaveConf);
410         
411         conf = new XMLConfiguration(testSaveConf);
412         assertEquals("value", conf.getString("element"));
413         assertEquals("tasks", conf.getString("table.name"));
414     }
415 }