KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > TestHierarchicalConfigurationXMLReader


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.configuration;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.dom4j.Attribute;
23 import org.dom4j.Document;
24 import org.dom4j.Element;
25 import org.dom4j.io.SAXReader;
26
27 import junit.framework.TestCase;
28
29 /**
30  * Test class for HierarchicalConfigurationXMLReader.
31  *
32  * @version $Id: TestHierarchicalConfigurationXMLReader.java 155408 2005-02-26 12:56:39Z dirkv $
33  */

34 public class TestHierarchicalConfigurationXMLReader extends TestCase
35 {
36     private static final String JavaDoc TEST_FILE = "conf/testHierarchicalXMLConfiguration.xml";
37     
38     private HierarchicalConfigurationXMLReader parser;
39     
40     protected void setUp() throws Exception JavaDoc
41     {
42         XMLConfiguration config =
43         new XMLConfiguration();
44         config.setFileName(TEST_FILE);
45         config.load();
46         parser = new HierarchicalConfigurationXMLReader(config);
47     }
48
49     public void testParse() throws Exception JavaDoc
50     {
51         SAXReader reader = new SAXReader(parser);
52         Document document = reader.read("someSysID");
53         
54         Element root = document.getRootElement();
55         assertEquals("config", root.getName());
56         assertEquals(1, root.elements().size());
57         Iterator JavaDoc itRoot = root.elementIterator();
58         Element elemTabs = (Element) itRoot.next();
59         
60         assertEquals(2, elemTabs.elements().size());
61         List JavaDoc tables = elemTabs.elements();
62         Element tabUsr = (Element) tables.get(0);
63         assertEquals("table", tabUsr.getName());
64         Element elemName = tabUsr.element("name");
65         assertNotNull(elemName);
66         assertEquals("users", elemName.getTextTrim());
67         Element elemFields = tabUsr.element("fields");
68         assertNotNull(elemFields);
69         assertEquals(5, elemFields.elements().size());
70         
71         List JavaDoc attribs = tabUsr.attributes();
72         assertEquals(1, attribs.size());
73         Attribute attr = (Attribute) attribs.get(0);
74         assertEquals("tableType", attr.getName());
75         assertEquals("system", attr.getValue());
76     }
77 }
78
Popular Tags