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.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   * Test class for HierarchicalConfigurationXMLReader.
36   *
37   * @version $Id: TestHierarchicalConfigurationXMLReader.java 1224644 2011-12-25 20:34:22Z oheger $
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  }