View Javadoc
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.configuration2;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import javax.xml.transform.Transformer;
23  import javax.xml.transform.TransformerFactory;
24  import javax.xml.transform.dom.DOMResult;
25  import javax.xml.transform.sax.SAXSource;
26  
27  import org.apache.commons.configuration2.io.FileHandler;
28  import org.apache.commons.configuration2.tree.ImmutableNode;
29  import org.apache.commons.jxpath.JXPathContext;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  import org.w3c.dom.Document;
33  import org.w3c.dom.Node;
34  import org.xml.sax.InputSource;
35  
36  /**
37   * Test class for HierarchicalConfigurationXMLReader.
38   */
39  public class TestHierarchicalConfigurationXMLReader {
40      private static final String TEST_FILE = ConfigurationAssert.getTestFile("testHierarchicalXMLConfiguration.xml").getAbsolutePath();
41  
42      private HierarchicalConfigurationXMLReader<ImmutableNode> parser;
43  
44      @BeforeEach
45      public void setUp() throws Exception {
46          final XMLConfiguration config = new XMLConfiguration();
47          final FileHandler handler = new FileHandler(config);
48          handler.load(TEST_FILE);
49          parser = new HierarchicalConfigurationXMLReader<>(config);
50      }
51  
52      @Test
53      public void testParse() throws Exception {
54          final SAXSource source = new SAXSource(parser, new InputSource());
55          final DOMResult result = new DOMResult();
56          final Transformer trans = TransformerFactory.newInstance().newTransformer();
57          trans.transform(source, result);
58          final Node root = ((Document) result.getNode()).getDocumentElement();
59          final JXPathContext ctx = JXPathContext.newContext(root);
60  
61          assertEquals("database", root.getNodeName());
62          assertEquals(1, ctx.selectNodes("/*").size());
63          assertEquals(2, ctx.selectNodes("/tables/table").size());
64          assertEquals("users", ctx.getValue("/tables/table[1]/name"));
65          assertEquals(5, ctx.selectNodes("/tables/table[1]/fields/field").size());
66          assertEquals("system", ctx.getValue("/tables/table[1]/@tableType"));
67      }
68  }