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 java.io.IOException;
21  import java.util.Arrays;
22  import java.util.Iterator;
23  
24  import javax.xml.transform.Transformer;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.dom.DOMResult;
27  import javax.xml.transform.sax.SAXSource;
28  
29  import org.apache.commons.jxpath.JXPathContext;
30  import org.w3c.dom.Document;
31  import org.w3c.dom.Node;
32  import org.xml.sax.InputSource;
33  import org.xml.sax.SAXException;
34  import org.xml.sax.helpers.DefaultHandler;
35  
36  import junit.framework.TestCase;
37  
38  /***
39   * Test class for BaseConfigurationXMLReader.
40   *
41   * @version $Id: TestBaseConfigurationXMLReader.java 439648 2006-09-02 20:42:10Z oheger $
42   */
43  public class TestBaseConfigurationXMLReader extends TestCase
44  {
45      private static final String[] CONTINENTS =
46      {
47          "Africa", "America", "Asia", "Australia", "Europe"
48      };
49      
50      private BaseConfiguration config;
51      private BaseConfigurationXMLReader configReader;
52      
53      protected void setUp() throws Exception
54      {
55          config = new BaseConfiguration();
56          config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS));
57          config.addProperty("world.greeting", "Hello");
58          config.addProperty("world.greeting", "Salute");
59          config.addProperty("world.wish", "Peace");
60          config.addProperty("application.mail.smtp", "smtp.mymail.org");
61          config.addProperty("application.mail.pop", "pop3.mymail.org");
62          config.addProperty("application.mail.account.type", "pop3");
63          config.addProperty("application.mail.account.user", "postmaster");
64          config.addProperty("application.mail.account.pwd", "?.-gulp*#");
65          config.addProperty("application.mail.timeout", new Integer(42));
66          config.addProperty("test", Boolean.TRUE);
67          
68          configReader = new BaseConfigurationXMLReader(config);
69      }
70  
71      public void testParse() throws Exception
72      {
73          checkDocument(configReader, "config");
74      }
75      
76      public void testParseSAXException() throws IOException
77      {
78          configReader.setContentHandler(new TestContentHandler());
79          try
80          {
81              configReader.parse("systemID");
82              fail("Expected exception was not thrown!");
83          }
84          catch(SAXException ex)
85          {
86          }
87      }
88      
89      public void testParseIOException() throws SAXException
90      {
91          BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
92          try
93          {
94              reader.parse("document");
95              fail("Expected exception was not thrown!");
96          }
97          catch(IOException ex)
98          {
99          }
100     }
101     
102     public void testSetRootName() throws Exception
103     {
104         BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(config);
105         reader.setRootName("apache");
106         checkDocument(reader, "apache");
107     }
108     
109     private void checkDocument(BaseConfigurationXMLReader creader,
110     String rootName) throws Exception
111     {
112         SAXSource source = new SAXSource(creader, new InputSource());
113         DOMResult result = new DOMResult();
114         Transformer trans = TransformerFactory.newInstance().newTransformer();
115         try
116         {
117             //When executed on a JDK 1.3 this line throws a NoSuchMethodError
118             //somewhere deep in Xalan. We simply ignore this.
119             trans.transform(source, result);
120         }
121         catch(NoSuchMethodError ex)
122         {
123             return;
124         }
125         Node root = ((Document) result.getNode()).getDocumentElement();
126         JXPathContext ctx = JXPathContext.newContext(root);
127         
128         assertEquals("Wrong root name", rootName, root.getNodeName());
129         assertEquals("Wrong number of children", 3, ctx.selectNodes("/*").size());
130 
131         check(ctx, "world/continents/continent", CONTINENTS);
132         check(ctx, "world/greeting", new String[] { "Hello", "Salute" });
133         check(ctx, "world/wish", "Peace");
134         check(ctx, "application/mail/smtp", "smtp.mymail.org");
135         check(ctx, "application/mail/timeout", "42");
136         check(ctx, "application/mail/account/type", "pop3");
137         check(ctx, "application/mail/account/user", "postmaster");
138         check(ctx, "test", "true");        
139     }
140     
141     /***
142      * Helper method for checking values in the created document.
143      *
144      * @param ctx the JXPath context
145      * @param path the path to be checked
146      * @param values the expected element values
147      */
148     private void check(JXPathContext ctx, String path, String[] values)
149     {
150         Iterator it = ctx.iterate(path);
151         for (int i = 0; i < values.length; i++)
152         {
153             assertTrue("Too few values", it.hasNext());
154             assertEquals("Wrong property value", values[i], it.next());
155         } /* for */
156         assertFalse("Too many values", it.hasNext());
157     }
158 
159     private void check(JXPathContext ctx, String path, String value)
160     {
161         check(ctx, path, new String[]
162         { value });
163     }
164     
165     // A ContentHandler that raises an exception
166     private static class TestContentHandler extends DefaultHandler
167      {
168         public void characters(char[] ch, int start, int length)
169             throws SAXException
170         {
171             throw new SAXException("Test exception during parsing");
172         }
173     }
174 }