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 static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertFalse;
022    import static org.junit.Assert.assertTrue;
023    
024    import java.io.IOException;
025    import java.util.Arrays;
026    import java.util.Iterator;
027    
028    import javax.xml.transform.Transformer;
029    import javax.xml.transform.TransformerFactory;
030    import javax.xml.transform.dom.DOMResult;
031    import javax.xml.transform.sax.SAXSource;
032    
033    import org.apache.commons.jxpath.JXPathContext;
034    import org.junit.Before;
035    import org.junit.Test;
036    import org.w3c.dom.Document;
037    import org.w3c.dom.Node;
038    import org.xml.sax.InputSource;
039    import org.xml.sax.SAXException;
040    import org.xml.sax.helpers.DefaultHandler;
041    
042    /**
043     * Test class for BaseConfigurationXMLReader.
044     *
045     * @version $Id: TestBaseConfigurationXMLReader.java 1222835 2011-12-23 20:37:22Z oheger $
046     */
047    public class TestBaseConfigurationXMLReader
048    {
049        private static final String[] CONTINENTS =
050        {
051            "Africa", "America", "Asia", "Australia", "Europe"
052        };
053    
054        private BaseConfiguration config;
055        private BaseConfigurationXMLReader configReader;
056    
057        @Before
058        public void setUp() throws Exception
059        {
060            config = new BaseConfiguration();
061            config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS));
062            config.addProperty("world.greeting", "Hello");
063            config.addProperty("world.greeting", "Salute");
064            config.addProperty("world.wish", "Peace");
065            config.addProperty("application.mail.smtp", "smtp.mymail.org");
066            config.addProperty("application.mail.pop", "pop3.mymail.org");
067            config.addProperty("application.mail.account.type", "pop3");
068            config.addProperty("application.mail.account.user", "postmaster");
069            config.addProperty("application.mail.account.pwd", "?.-gulp*#");
070            config.addProperty("application.mail.timeout", new Integer(42));
071            config.addProperty("test", Boolean.TRUE);
072    
073            configReader = new BaseConfigurationXMLReader(config);
074        }
075    
076        @Test
077        public void testParse() throws Exception
078        {
079            checkDocument(configReader, "config");
080        }
081    
082        @Test(expected = SAXException.class)
083        public void testParseSAXException() throws IOException, SAXException
084        {
085            configReader.setContentHandler(new TestContentHandler());
086            configReader.parse("systemID");
087        }
088    
089        @Test(expected = IOException.class)
090        public void testParseIOException() throws SAXException, IOException
091        {
092            BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
093            reader.parse("document");
094        }
095    
096        @Test
097        public void testSetRootName() throws Exception
098        {
099            BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(config);
100            reader.setRootName("apache");
101            checkDocument(reader, "apache");
102        }
103    
104        private void checkDocument(BaseConfigurationXMLReader creader,
105        String rootName) throws Exception
106        {
107            SAXSource source = new SAXSource(creader, new InputSource());
108            DOMResult result = new DOMResult();
109            Transformer trans = TransformerFactory.newInstance().newTransformer();
110            trans.transform(source, result);
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("/*").size());
116    
117            check(ctx, "world/continents/continent", CONTINENTS);
118            check(ctx, "world/greeting", new String[] { "Hello", "Salute" });
119            check(ctx, "world/wish", "Peace");
120            check(ctx, "application/mail/smtp", "smtp.mymail.org");
121            check(ctx, "application/mail/timeout", "42");
122            check(ctx, "application/mail/account/type", "pop3");
123            check(ctx, "application/mail/account/user", "postmaster");
124            check(ctx, "test", "true");
125        }
126    
127        /**
128         * Helper method for checking values in the created document.
129         *
130         * @param ctx the JXPath context
131         * @param path the path to be checked
132         * @param values the expected element values
133         */
134        private void check(JXPathContext ctx, String path, String[] values)
135        {
136            Iterator<?> it = ctx.iterate(path);
137            for (int i = 0; i < values.length; i++)
138            {
139                assertTrue("Too few values", it.hasNext());
140                assertEquals("Wrong property value", values[i], it.next());
141            }
142            assertFalse("Too many values", it.hasNext());
143        }
144    
145        private void check(JXPathContext ctx, String path, String value)
146        {
147            check(ctx, path, new String[]
148            { value });
149        }
150    
151        // A ContentHandler that raises an exception
152        private static class TestContentHandler extends DefaultHandler
153         {
154            @Override
155            public void characters(char[] ch, int start, int length)
156                throws SAXException
157            {
158                throw new SAXException("Test exception during parsing");
159            }
160        }
161    }