001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.configuration;
019:
020: import java.io.IOException;
021: import java.util.Arrays;
022: import java.util.Iterator;
023:
024: import javax.xml.transform.Transformer;
025: import javax.xml.transform.TransformerFactory;
026: import javax.xml.transform.dom.DOMResult;
027: import javax.xml.transform.sax.SAXSource;
028:
029: import org.apache.commons.jxpath.JXPathContext;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Node;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.SAXException;
034: import org.xml.sax.helpers.DefaultHandler;
035:
036: import junit.framework.TestCase;
037:
038: /**
039: * Test class for BaseConfigurationXMLReader.
040: *
041: * @version $Id: TestBaseConfigurationXMLReader.java 439648 2006-09-02 20:42:10Z oheger $
042: */
043: public class TestBaseConfigurationXMLReader extends TestCase {
044: private static final String[] CONTINENTS = { "Africa", "America",
045: "Asia", "Australia", "Europe" };
046:
047: private BaseConfiguration config;
048: private BaseConfigurationXMLReader configReader;
049:
050: protected void setUp() throws Exception {
051: config = new BaseConfiguration();
052: config.addProperty("world.continents.continent", Arrays
053: .asList(CONTINENTS));
054: config.addProperty("world.greeting", "Hello");
055: config.addProperty("world.greeting", "Salute");
056: config.addProperty("world.wish", "Peace");
057: config.addProperty("application.mail.smtp", "smtp.mymail.org");
058: config.addProperty("application.mail.pop", "pop3.mymail.org");
059: config.addProperty("application.mail.account.type", "pop3");
060: config.addProperty("application.mail.account.user",
061: "postmaster");
062: config.addProperty("application.mail.account.pwd", "?.-gulp*#");
063: config.addProperty("application.mail.timeout", new Integer(42));
064: config.addProperty("test", Boolean.TRUE);
065:
066: configReader = new BaseConfigurationXMLReader(config);
067: }
068:
069: public void testParse() throws Exception {
070: checkDocument(configReader, "config");
071: }
072:
073: public void testParseSAXException() throws IOException {
074: configReader.setContentHandler(new TestContentHandler());
075: try {
076: configReader.parse("systemID");
077: fail("Expected exception was not thrown!");
078: } catch (SAXException ex) {
079: }
080: }
081:
082: public void testParseIOException() throws SAXException {
083: BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
084: try {
085: reader.parse("document");
086: fail("Expected exception was not thrown!");
087: } catch (IOException ex) {
088: }
089: }
090:
091: public void testSetRootName() throws Exception {
092: BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(
093: config);
094: reader.setRootName("apache");
095: checkDocument(reader, "apache");
096: }
097:
098: private void checkDocument(BaseConfigurationXMLReader creader,
099: String rootName) throws Exception {
100: SAXSource source = new SAXSource(creader, new InputSource());
101: DOMResult result = new DOMResult();
102: Transformer trans = TransformerFactory.newInstance()
103: .newTransformer();
104: try {
105: //When executed on a JDK 1.3 this line throws a NoSuchMethodError
106: //somewhere deep in Xalan. We simply ignore this.
107: trans.transform(source, result);
108: } catch (NoSuchMethodError ex) {
109: return;
110: }
111: Node root = ((Document) result.getNode()).getDocumentElement();
112: JXPathContext ctx = JXPathContext.newContext(root);
113:
114: assertEquals("Wrong root name", rootName, root.getNodeName());
115: assertEquals("Wrong number of children", 3, ctx.selectNodes(
116: "/*").size());
117:
118: check(ctx, "world/continents/continent", CONTINENTS);
119: check(ctx, "world/greeting", new String[] { "Hello", "Salute" });
120: check(ctx, "world/wish", "Peace");
121: check(ctx, "application/mail/smtp", "smtp.mymail.org");
122: check(ctx, "application/mail/timeout", "42");
123: check(ctx, "application/mail/account/type", "pop3");
124: check(ctx, "application/mail/account/user", "postmaster");
125: check(ctx, "test", "true");
126: }
127:
128: /**
129: * Helper method for checking values in the created document.
130: *
131: * @param ctx the JXPath context
132: * @param path the path to be checked
133: * @param values the expected element values
134: */
135: private void check(JXPathContext ctx, String path, String[] values) {
136: Iterator it = ctx.iterate(path);
137: for (int i = 0; i < values.length; i++) {
138: assertTrue("Too few values", it.hasNext());
139: assertEquals("Wrong property value", values[i], it.next());
140: } /* for */
141: assertFalse("Too many values", it.hasNext());
142: }
143:
144: private void check(JXPathContext ctx, String path, String value) {
145: check(ctx, path, new String[] { value });
146: }
147:
148: // A ContentHandler that raises an exception
149: private static class TestContentHandler extends DefaultHandler {
150: public void characters(char[] ch, int start, int length)
151: throws SAXException {
152: throw new SAXException("Test exception during parsing");
153: }
154: }
155: }
|