001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.configuration;
019    
020    import static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertFalse;
022    import static org.junit.Assert.assertTrue;
023    
024    import java.io.File;
025    
026    import org.junit.Test;
027    
028    /**
029     * @author Emmanuel Bourg
030     * @version $Id: TestXMLPropertiesConfiguration.java 1225341 2011-12-28 21:18:28Z oheger $
031     */
032    public class TestXMLPropertiesConfiguration
033    {
034        @Test
035        public void testLoad() throws Exception
036        {
037            XMLPropertiesConfiguration conf = new XMLPropertiesConfiguration("test.properties.xml");
038    
039            assertEquals("header", "Description of the property list", conf.getHeader());
040    
041            assertFalse("The configuration is empty", conf.isEmpty());
042            assertEquals("'key1' property", "value1", conf.getProperty("key1"));
043            assertEquals("'key2' property", "value2", conf.getProperty("key2"));
044            assertEquals("'key3' property", "value3", conf.getProperty("key3"));
045        }
046    
047        @Test
048        public void testSave() throws Exception
049        {
050            // load the configuration
051            XMLPropertiesConfiguration conf = new XMLPropertiesConfiguration("test.properties.xml");
052    
053            // update the configuration
054            conf.addProperty("key4", "value4");
055            conf.clearProperty("key2");
056            conf.setHeader("Description of the new property list");
057    
058            // save the configuration
059            File saveFile = new File("target/test2.properties.xml");
060            if (saveFile.exists())
061            {
062                assertTrue(saveFile.delete());
063            }
064            conf.save(saveFile);
065    
066            // reload the configuration
067            XMLPropertiesConfiguration conf2 = new XMLPropertiesConfiguration(saveFile);
068    
069            // test the configuration
070            assertEquals("header", "Description of the new property list", conf2.getHeader());
071    
072            assertFalse("The configuration is empty", conf2.isEmpty());
073            assertEquals("'key1' property", "value1", conf2.getProperty("key1"));
074            assertEquals("'key3' property", "value3", conf2.getProperty("key3"));
075            assertEquals("'key4' property", "value4", conf2.getProperty("key4"));
076        }
077    }