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.apache.commons.configuration2.TempDirUtils.newFile;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.io.File;
26  import java.net.URL;
27  
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import javax.xml.transform.Result;
31  import javax.xml.transform.Transformer;
32  import javax.xml.transform.TransformerFactory;
33  import javax.xml.transform.dom.DOMSource;
34  import javax.xml.transform.stream.StreamResult;
35  
36  import org.apache.commons.configuration2.ex.ConfigurationException;
37  import org.apache.commons.configuration2.io.FileHandler;
38  import org.junit.jupiter.api.Test;
39  import org.junit.jupiter.api.io.TempDir;
40  import org.w3c.dom.Document;
41  import org.xml.sax.InputSource;
42  
43  /**
44   * Test class for {@code XMLPropertiesConfiguration}.
45   */
46  public class TestXMLPropertiesConfiguration {
47      /** Constant for the name of the test file. */
48      private static final String TEST_PROPERTIES_FILE = "test.properties.xml";
49  
50      /**
51       * Helper method for loading a configuration file.
52       *
53       * @param fileName the name of the file to be loaded
54       * @return the configuration instance
55       * @throws ConfigurationException if an error occurs
56       */
57      private static XMLPropertiesConfiguration load(final String fileName) throws ConfigurationException {
58          final XMLPropertiesConfiguration conf = new XMLPropertiesConfiguration();
59          final FileHandler handler = new FileHandler(conf);
60          handler.load(fileName);
61          return conf;
62      }
63  
64      /** A folder for temporary files. */
65      @TempDir
66      public File tempFolder;
67  
68      @Test
69      public void testDOMLoad() throws Exception {
70          // Edge case
71          assertThrows(NullPointerException.class, () -> new XMLPropertiesConfiguration(null));
72          // Normal case
73          final URL location = ConfigurationAssert.getTestURL(TEST_PROPERTIES_FILE);
74          final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
75          final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
76          dBuilder.setEntityResolver((publicId, systemId) -> new InputSource(getClass().getClassLoader().getResourceAsStream("properties.dtd")));
77          final File file = new File(location.toURI());
78          final Document doc = dBuilder.parse(file);
79          final XMLPropertiesConfiguration conf = new XMLPropertiesConfiguration(doc.getDocumentElement());
80  
81          assertEquals("Description of the property list", conf.getHeader());
82  
83          assertFalse(conf.isEmpty());
84          assertEquals("value1", conf.getProperty("key1"));
85          assertEquals("value2", conf.getProperty("key2"));
86          assertEquals("value3", conf.getProperty("key3"));
87      }
88  
89      @Test
90      public void testDOMSave() throws Exception {
91          // load the configuration
92          final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);
93  
94          // update the configuration
95          conf.addProperty("key4", "value4");
96          conf.clearProperty("key2");
97          conf.setHeader("Description of the new property list");
98  
99          // save the configuration
100         final File saveFile = newFile("test2.properties.xml", tempFolder);
101 
102         // save as DOM into saveFile
103         final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
104         final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
105         final Document document = dBuilder.newDocument();
106         conf.save(document, document);
107         final TransformerFactory tFactory = TransformerFactory.newInstance();
108         final Transformer transformer = tFactory.newTransformer();
109         final DOMSource source = new DOMSource(document);
110         final Result result = new StreamResult(saveFile);
111         transformer.transform(source, result);
112 
113         // reload the configuration
114         final XMLPropertiesConfiguration conf2 = load(saveFile.getAbsolutePath());
115 
116         // test the configuration
117         assertEquals("Description of the new property list", conf2.getHeader());
118 
119         assertFalse(conf2.isEmpty());
120         assertEquals("value1", conf2.getProperty("key1"));
121         assertEquals("value3", conf2.getProperty("key3"));
122         assertEquals("value4", conf2.getProperty("key4"));
123     }
124 
125     @Test
126     public void testLoad() throws Exception {
127         final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);
128         assertEquals("Description of the property list", conf.getHeader());
129 
130         assertFalse(conf.isEmpty());
131         assertEquals("value1", conf.getProperty("key1"));
132         assertEquals("value2", conf.getProperty("key2"));
133         assertEquals("value3", conf.getProperty("key3"));
134     }
135 
136     @Test
137     public void testSave() throws Exception {
138         // load the configuration
139         final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);
140 
141         // update the configuration
142         conf.addProperty("key4", "value4");
143         conf.clearProperty("key2");
144         conf.setHeader("Description of the new property list");
145 
146         // save the configuration
147         final File saveFile = newFile("test2.properties.xml", tempFolder);
148         final FileHandler saveHandler = new FileHandler(conf);
149         saveHandler.save(saveFile);
150 
151         // reload the configuration
152         final XMLPropertiesConfiguration conf2 = load(saveFile.getAbsolutePath());
153 
154         // test the configuration
155         assertEquals("Description of the new property list", conf2.getHeader());
156 
157         assertFalse(conf2.isEmpty());
158         assertEquals("value1", conf2.getProperty("key1"));
159         assertEquals("value3", conf2.getProperty("key3"));
160         assertEquals("value4", conf2.getProperty("key4"));
161     }
162 }