1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }