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    package org.apache.commons.configuration.tree.xpath;
018    
019    import static org.junit.Assert.assertEquals;
020    
021    import org.apache.commons.configuration.XMLConfiguration;
022    import org.junit.Before;
023    import org.junit.Test;
024    
025    /**
026     * A test class for XPathExpressionEngine that tests the engine integrated into
027     * a hierarchical configuration.
028     *
029     * @author <a
030     *         href="http://commons.apache.org/configuration/team-list.html">Commons
031     *         Configuration team</a>
032     * @version $Id: TestXPathExpressionEngineInConfig.java 1226113 2011-12-31 15:46:53Z oheger $
033     */
034    public class TestXPathExpressionEngineInConfig
035    {
036        /** Constant for a test key. */
037        private static final String KEY = "test/expression/xpath";
038    
039        /** Constant for a value for test properties. */
040        private static final String VALUE = "success";
041    
042        /** The test configuration. */
043        private XMLConfiguration config;
044    
045        @Before
046        public void setUp() throws Exception
047        {
048            config = new XMLConfiguration();
049            config.setExpressionEngine(new XPathExpressionEngine());
050        }
051    
052        /**
053         * Tests whether an already existing property can be changed using
054         * setProperty().
055         */
056        @Test
057        public void testSetPropertyExisting()
058        {
059            config.addProperty(" " + KEY, "failure");
060            config.setProperty(KEY, VALUE);
061            assertEquals("Value not changed", VALUE, config.getString(KEY));
062        }
063    
064        /**
065         * Tests setProperty() if the specified path partly exists.
066         */
067        @Test
068        public void testSetPropertyPartlyExisting()
069        {
070            final String testKey = KEY + "/sub";
071            config.addProperty(" " + KEY, "test");
072            config.setProperty(testKey, VALUE);
073            assertEquals("Value not set", VALUE, config.getString(testKey));
074        }
075    
076        /**
077         * Tests whether setProperty() can be used to add a new attribute.
078         */
079        @Test
080        public void testSetPropertyNewAttribute()
081        {
082            final String keyAttr = KEY + "/@attr";
083            config.addProperty(" " + KEY, "test");
084            config.setProperty(keyAttr, VALUE);
085            assertEquals("Value not set", VALUE, config.getString(keyAttr));
086        }
087    
088        /**
089         * Tests whether setProperty() can be used to create a completely new key.
090         */
091        @Test
092        public void testSetPropertyNewKey()
093        {
094            config.setProperty(KEY, VALUE);
095            assertEquals("Value not set", VALUE, config.getString(KEY));
096        }
097    
098        /**
099         * Tests whether addProperty() can be used to create more complex
100         * hierarchical structures.
101         */
102        @Test
103        public void testAddPropertyComplexStructures()
104        {
105            config.addProperty("tables/table/name", "tasks");
106            config.addProperty("tables/table[last()]/@type", "system");
107            config.addProperty("tables/table[last()]/fields/field/name", "taskid");
108            config.addProperty("tables/table[last()]/fields/field[last()]/@type",
109                    "int");
110            config.addProperty("tables table/name", "documents");
111            assertEquals("Wrong table 1", "tasks",
112                    config.getString("tables/table[1]/name"));
113            assertEquals("Wrong table 2", "documents",
114                    config.getString("tables/table[2]/name"));
115            assertEquals("Wrong field type", "int",
116                    config.getString("tables/table[1]/fields/field[1]/@type"));
117        }
118    }