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.configuration2;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.util.Arrays;
27  import java.util.Iterator;
28  
29  import javax.xml.transform.Transformer;
30  import javax.xml.transform.TransformerFactory;
31  import javax.xml.transform.dom.DOMResult;
32  import javax.xml.transform.sax.SAXSource;
33  
34  import org.apache.commons.jxpath.JXPathContext;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Node;
39  import org.xml.sax.InputSource;
40  import org.xml.sax.SAXException;
41  import org.xml.sax.helpers.DefaultHandler;
42  
43  /**
44   * Test class for BaseConfigurationXMLReader.
45   */
46  public class TestBaseConfigurationXMLReader {
47      // A ContentHandler that raises an exception
48      private static final class TestContentHandler extends DefaultHandler {
49          @Override
50          public void characters(final char[] ch, final int start, final int length) throws SAXException {
51              throw new SAXException("Test exception during parsing");
52          }
53      }
54  
55      private static final String[] CONTINENTS = {"Africa", "America", "Asia", "Australia", "Europe"};
56      private BaseConfiguration config;
57  
58      private BaseConfigurationXMLReader configReader;
59  
60      private void check(final JXPathContext ctx, final String path, final String value) {
61          check(ctx, path, new String[] {value});
62      }
63  
64      /**
65       * Helper method for checking values in the created document.
66       *
67       * @param ctx the JXPath context
68       * @param path the path to be checked
69       * @param values the expected element values
70       */
71      private void check(final JXPathContext ctx, final String path, final String[] values) {
72          final Iterator<?> it = ctx.iterate(path);
73          for (final String value : values) {
74              assertTrue(it.hasNext());
75              assertEquals(value, it.next());
76          }
77          assertFalse(it.hasNext());
78      }
79  
80      private void checkDocument(final BaseConfigurationXMLReader creader, final String rootName) throws Exception {
81          final SAXSource source = new SAXSource(creader, new InputSource());
82          final DOMResult result = new DOMResult();
83          final Transformer trans = TransformerFactory.newInstance().newTransformer();
84          trans.transform(source, result);
85          final Node root = ((Document) result.getNode()).getDocumentElement();
86          final JXPathContext ctx = JXPathContext.newContext(root);
87  
88          assertEquals(rootName, root.getNodeName());
89          assertEquals(3, ctx.selectNodes("/*").size());
90  
91          check(ctx, "world/continents/continent", CONTINENTS);
92          check(ctx, "world/greeting", new String[] {"Hello", "Salute"});
93          check(ctx, "world/wish", "Peace");
94          check(ctx, "application/mail/smtp", "smtp.mymail.org");
95          check(ctx, "application/mail/timeout", "42");
96          check(ctx, "application/mail/account/type", "pop3");
97          check(ctx, "application/mail/account/user", "postmaster");
98          check(ctx, "test", "true");
99      }
100 
101     @BeforeEach
102     public void setUp() throws Exception {
103         config = new BaseConfiguration();
104         config.addProperty("world.continents.continent", Arrays.asList(CONTINENTS));
105         config.addProperty("world.greeting", "Hello");
106         config.addProperty("world.greeting", "Salute");
107         config.addProperty("world.wish", "Peace");
108         config.addProperty("application.mail.smtp", "smtp.mymail.org");
109         config.addProperty("application.mail.pop", "pop3.mymail.org");
110         config.addProperty("application.mail.account.type", "pop3");
111         config.addProperty("application.mail.account.user", "postmaster");
112         config.addProperty("application.mail.account.pwd", "?.-gulp*#");
113         config.addProperty("application.mail.timeout", Integer.valueOf(42));
114         config.addProperty("test", Boolean.TRUE);
115 
116         configReader = new BaseConfigurationXMLReader(config);
117     }
118 
119     @Test
120     public void testParse() throws Exception {
121         checkDocument(configReader, "config");
122     }
123 
124     @Test
125     public void testParseIOException() {
126         final BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader();
127         assertThrows(IOException.class, () -> reader.parse("document"));
128     }
129 
130     @Test
131     public void testParseSAXException() {
132         configReader.setContentHandler(new TestContentHandler());
133         assertThrows(SAXException.class, () -> configReader.parse("systemID"));
134     }
135 
136     @Test
137     public void testSetRootName() throws Exception {
138         final BaseConfigurationXMLReader reader = new BaseConfigurationXMLReader(config);
139         reader.setRootName("apache");
140         checkDocument(reader, "apache");
141     }
142 }