1   /*
2    * Copyright 2004 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  
21  import junit.framework.TestCase;
22  
23  /***
24   * @author Emmanuel Bourg
25   * @version $Revision$, $Date: 2005-07-20 20:33:14 +0200 (Wed, 20 Jul 2005) $
26   */
27  public class TestXMLPropertiesConfiguration extends TestCase
28  {
29      public void testLoad() throws Exception
30      {
31          XMLPropertiesConfiguration conf = new XMLPropertiesConfiguration("test.properties.xml");
32  
33          assertEquals("header", "Description of the property list", conf.getHeader());
34  
35          assertFalse("The configuration is empty", conf.isEmpty());
36          assertEquals("'key1' property", "value1", conf.getProperty("key1"));
37          assertEquals("'key2' property", "value2", conf.getProperty("key2"));
38          assertEquals("'key3' property", "value3", conf.getProperty("key3"));
39      }
40  
41      public void testSave() throws Exception
42      {
43          // load the configuration
44          XMLPropertiesConfiguration conf = new XMLPropertiesConfiguration("test.properties.xml");
45  
46          // update the configuration
47          conf.addProperty("key4", "value4");
48          conf.clearProperty("key2");
49          conf.setHeader("Description of the new property list");
50  
51          // save the configuration
52          File saveFile = new File("target/test2.properties.xml");
53          if (saveFile.exists())
54          {
55              assertTrue(saveFile.delete());
56          }
57          conf.save(saveFile);
58  
59          // reload the configuration
60          XMLPropertiesConfiguration conf2 = new XMLPropertiesConfiguration(saveFile);
61  
62          // test the configuration
63          assertEquals("header", "Description of the new property list", conf2.getHeader());
64  
65          assertFalse("The configuration is empty", conf2.isEmpty());
66          assertEquals("'key1' property", "value1", conf2.getProperty("key1"));
67          assertEquals("'key3' property", "value3", conf2.getProperty("key3"));
68          assertEquals("'key4' property", "value4", conf2.getProperty("key4"));
69      }
70  }