1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 import static org.junit.Assert.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.jxpath.JXPathContext;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Node;
32 import org.xml.sax.InputSource;
33
34
35
36
37
38
39 public class TestHierarchicalConfigurationXMLReader
40 {
41 private static final String TEST_FILE = ConfigurationAssert.getTestFile(
42 "testHierarchicalXMLConfiguration.xml").getAbsolutePath();
43
44 private HierarchicalConfigurationXMLReader parser;
45
46 @Before
47 public void setUp() throws Exception
48 {
49 XMLConfiguration config = new XMLConfiguration();
50 config.setFileName(TEST_FILE);
51 config.load();
52 parser = new HierarchicalConfigurationXMLReader(config);
53 }
54
55 @Test
56 public void testParse() throws Exception
57 {
58 SAXSource source = new SAXSource(parser, new InputSource());
59 DOMResult result = new DOMResult();
60 Transformer trans = TransformerFactory.newInstance().newTransformer();
61 trans.transform(source, result);
62 Node root = ((Document) result.getNode()).getDocumentElement();
63 JXPathContext ctx = JXPathContext.newContext(root);
64
65 assertEquals("Wrong name of root element", "database", root.getNodeName());
66 assertEquals("Wrong number of children of root", 1, ctx.selectNodes(
67 "/*").size());
68 assertEquals("Wrong number of tables", 2, ctx.selectNodes(
69 "/tables/table").size());
70 assertEquals("Wrong name of first table", "users", ctx
71 .getValue("/tables/table[1]/name"));
72 assertEquals("Wrong number of fields in first table", 5, ctx
73 .selectNodes("/tables/table[1]/fields/field").size());
74 assertEquals("Wrong attribute value", "system", ctx
75 .getValue("/tables/table[1]/@tableType"));
76 }
77 }