KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.dom4j.Document;
25 import org.dom4j.Element;
26 import org.dom4j.io.SAXReader;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.helpers.DefaultHandler JavaDoc;
29
30 import junit.framework.TestCase;
31
32 /**
33  * Test class for BaseConfigurationXMLReader.
34  *
35  * @version $Id: TestBaseConfigurationXMLReader.java 155408 2005-02-26 12:56:39Z dirkv $
36  */

37 public class TestBaseConfigurationXMLReader extends TestCase
38 {
39     private static final String JavaDoc[] CONTINENTS =
40     {
41         "Africa", "America", "Asia", "Australia", "Europe"
42     };
43     
44     private BaseConfiguration config;
45     private BaseConfigurationXMLReader configReader;
46     
47     protected void setUp() throws Exception JavaDoc
48     {
49         config = new BaseConfiguration();
50         config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS));
51         config.addProperty("world.greeting", "Hello");
52         config.addProperty("world.greeting", "Salute");
53         config.addProperty("world.wish", "Peace");
54         config.addProperty("application.mail.smtp", "smtp.mymail.org");
55         config.addProperty("application.mail.pop", "pop3.mymail.org");
56         config.addProperty("application.mail.account.type", "pop3");
57         config.addProperty("application.mail.account.user", "postmaster");
58         config.addProperty("application.mail.account.pwd", "?.-gulp*#");
59         config.addProperty("application.mail.timeout", new Integer JavaDoc(42));
60         config.addProperty("test", Boolean.TRUE);
61         
62         configReader = new BaseConfigurationXMLReader(config);
63     }
64
65     public void testParse() throws Exception JavaDoc
66     {
67         checkDocument(configReader, "config");
68     }
69     
70     public void testParseSAXException() throws IOException JavaDoc
71     {
72         configReader.setContentHandler(new TestContentHandler());
73         try
74         {
75             configReader.parse("systemID");
76             fail("Expected exception was not thrown!");
77         }
78         catch(SAXException JavaDoc ex)
79         {
80         }
81     }
82     
83     public void testParseIOException() throws SAXException JavaDoc
84     {
85         BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
86         try
87         {
88             reader.parse("document");
89             fail("Expected exception was not thrown!");
90         }
91         catch(IOException JavaDoc ex)
92         {
93         }
94     }
95     
96     public void testSetRootName() throws Exception JavaDoc
97     {
98         BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(config);
99         reader.setRootName("apache");
100         checkDocument(reader, "apache");
101     }
102     
103     private void checkDocument(BaseConfigurationXMLReader creader,
104     String JavaDoc rootName) throws Exception JavaDoc
105     {
106         SAXReader reader = new SAXReader(creader);
107         Document document = reader.read("config");
108         
109         Element root = document.getRootElement();
110         assertEquals(rootName, root.getName());
111         assertEquals(3, root.elements().size());
112
113         check(root, "world.continents.continent", CONTINENTS);
114         check(root, "world.greeting", new String JavaDoc[] { "Hello", "Salute" });
115         check(root, "world.wish", "Peace");
116         check(root, "application.mail.smtp", "smtp.mymail.org");
117         check(root, "application.mail.timeout", "42");
118         check(root, "application.mail.account.type", "pop3");
119         check(root, "application.mail.account.user", "postmaster");
120         check(root, "test", "true");
121     }
122     
123     /**
124      * Helper method for checking values in the DOM4J document.
125      * @param root the root element
126      * @param path the path to be checked
127      * @param values the expected element values
128      */

129     private void check(Element root, String JavaDoc path, String JavaDoc[] values)
130     {
131         ConfigurationKey.KeyIterator keyIt =
132         new ConfigurationKey(path).iterator();
133         Element e = root;
134         
135         for(keyIt.nextKey(); keyIt.hasNext(); keyIt.nextKey())
136         {
137             Element child = e.element(keyIt.currentKey());
138             assertNotNull(child);
139             e = child;
140         } /* for */
141         
142         List JavaDoc elems = e.elements(keyIt.currentKey());
143         assertEquals(values.length, elems.size());
144         Iterator JavaDoc it = elems.iterator();
145         for(int i = 0; i < values.length; i++)
146         {
147             Element child = (Element) it.next();
148             assertEquals(values[i], child.getTextTrim());
149         } /* for */
150     }
151     
152     private void check(Element root, String JavaDoc path, String JavaDoc value)
153     {
154         check(root, path, new String JavaDoc[] { value });
155     }
156     
157     // A ContentHandler that raises an exception
158
private static class TestContentHandler extends DefaultHandler JavaDoc
159      {
160         public void characters(char[] ch, int start, int length)
161             throws SAXException JavaDoc
162         {
163             throw new SAXException JavaDoc("Test exception during parsing");
164         }
165     }
166 }
167
Popular Tags