View Javadoc

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 static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.Arrays;
26  import java.util.Iterator;
27  
28  import javax.xml.transform.Transformer;
29  import javax.xml.transform.TransformerFactory;
30  import javax.xml.transform.dom.DOMResult;
31  import javax.xml.transform.sax.SAXSource;
32  
33  import org.apache.commons.jxpath.JXPathContext;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Node;
38  import org.xml.sax.InputSource;
39  import org.xml.sax.SAXException;
40  import org.xml.sax.helpers.DefaultHandler;
41  
42  /**
43   * Test class for BaseConfigurationXMLReader.
44   *
45   * @version $Id: TestBaseConfigurationXMLReader.java 1222835 2011-12-23 20:37:22Z oheger $
46   */
47  public class TestBaseConfigurationXMLReader
48  {
49      private static final String[] CONTINENTS =
50      {
51          "Africa", "America", "Asia", "Australia", "Europe"
52      };
53  
54      private BaseConfiguration config;
55      private BaseConfigurationXMLReader configReader;
56  
57      @Before
58      public void setUp() throws Exception
59      {
60          config = new BaseConfiguration();
61          config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS));
62          config.addProperty("world.greeting", "Hello");
63          config.addProperty("world.greeting", "Salute");
64          config.addProperty("world.wish", "Peace");
65          config.addProperty("application.mail.smtp", "smtp.mymail.org");
66          config.addProperty("application.mail.pop", "pop3.mymail.org");
67          config.addProperty("application.mail.account.type", "pop3");
68          config.addProperty("application.mail.account.user", "postmaster");
69          config.addProperty("application.mail.account.pwd", "?.-gulp*#");
70          config.addProperty("application.mail.timeout", new Integer(42));
71          config.addProperty("test", Boolean.TRUE);
72  
73          configReader = new BaseConfigurationXMLReader(config);
74      }
75  
76      @Test
77      public void testParse() throws Exception
78      {
79          checkDocument(configReader, "config");
80      }
81  
82      @Test(expected = SAXException.class)
83      public void testParseSAXException() throws IOException, SAXException
84      {
85          configReader.setContentHandler(new TestContentHandler());
86          configReader.parse("systemID");
87      }
88  
89      @Test(expected = IOException.class)
90      public void testParseIOException() throws SAXException, IOException
91      {
92          BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
93          reader.parse("document");
94      }
95  
96      @Test
97      public void testSetRootName() throws Exception
98      {
99          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 }