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    
022    import javax.xml.transform.Transformer;
023    import javax.xml.transform.TransformerFactory;
024    import javax.xml.transform.dom.DOMResult;
025    import javax.xml.transform.sax.SAXSource;
026    
027    import org.apache.commons.jxpath.JXPathContext;
028    import org.junit.Before;
029    import org.junit.Test;
030    import org.w3c.dom.Document;
031    import org.w3c.dom.Node;
032    import org.xml.sax.InputSource;
033    
034    /**
035     * Test class for HierarchicalConfigurationXMLReader.
036     *
037     * @version $Id: TestHierarchicalConfigurationXMLReader.java 1224644 2011-12-25 20:34:22Z oheger $
038     */
039    public class TestHierarchicalConfigurationXMLReader
040    {
041        private static final String TEST_FILE = ConfigurationAssert.getTestFile(
042                "testHierarchicalXMLConfiguration.xml").getAbsolutePath();
043    
044        private HierarchicalConfigurationXMLReader parser;
045    
046        @Before
047        public void setUp() throws Exception
048        {
049            XMLConfiguration config = new XMLConfiguration();
050            config.setFileName(TEST_FILE);
051            config.load();
052            parser = new HierarchicalConfigurationXMLReader(config);
053        }
054    
055        @Test
056        public void testParse() throws Exception
057        {
058            SAXSource source = new SAXSource(parser, new InputSource());
059            DOMResult result = new DOMResult();
060            Transformer trans = TransformerFactory.newInstance().newTransformer();
061            trans.transform(source, result);
062            Node root = ((Document) result.getNode()).getDocumentElement();
063            JXPathContext ctx = JXPathContext.newContext(root);
064    
065            assertEquals("Wrong name of root element", "database", root.getNodeName());
066            assertEquals("Wrong number of children of root", 1, ctx.selectNodes(
067                    "/*").size());
068            assertEquals("Wrong number of tables", 2, ctx.selectNodes(
069                    "/tables/table").size());
070            assertEquals("Wrong name of first table", "users", ctx
071                    .getValue("/tables/table[1]/name"));
072            assertEquals("Wrong number of fields in first table", 5, ctx
073                    .selectNodes("/tables/table[1]/fields/field").size());
074            assertEquals("Wrong attribute value", "system", ctx
075                    .getValue("/tables/table[1]/@tableType"));
076        }
077    }