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