1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
45
46 public class TestXMLPropertiesConfiguration {
47
48
49 private static final String TEST_PROPERTIES_FILE = "test.properties.xml";
50
51
52
53
54
55
56
57
58 private static XMLPropertiesConfiguration load(final String fileName) throws ConfigurationException {
59 final XMLPropertiesConfiguration conf = new XMLPropertiesConfiguration();
60 final FileHandler handler = new FileHandler(conf);
61 handler.load(fileName);
62 return conf;
63 }
64
65
66 @TempDir
67 public File tempFolder;
68
69 @Test
70 void testDOMLoad() throws Exception {
71
72 assertThrows(NullPointerException.class, () -> new XMLPropertiesConfiguration(null));
73
74 final URL location = ConfigurationAssert.getTestURL(TEST_PROPERTIES_FILE);
75 final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
76 final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
77 dBuilder.setEntityResolver((publicId, systemId) -> new InputSource(getClass().getClassLoader().getResourceAsStream("properties.dtd")));
78 final File file = new File(location.toURI());
79 final Document doc = dBuilder.parse(file);
80 final XMLPropertiesConfiguration conf = new XMLPropertiesConfiguration(doc.getDocumentElement());
81
82 assertEquals("Description of the property list", conf.getHeader());
83
84 assertFalse(conf.isEmpty());
85 assertEquals("value1", conf.getProperty("key1"));
86 assertEquals("value2", conf.getProperty("key2"));
87 assertEquals("value3", conf.getProperty("key3"));
88 }
89
90 @Test
91 void testDOMSave() throws Exception {
92
93 final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);
94
95
96 conf.addProperty("key4", "value4");
97 conf.clearProperty("key2");
98 conf.setHeader("Description of the new property list");
99
100
101 final File saveFile = newFile("test2.properties.xml", tempFolder);
102
103
104 final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
105 final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
106 final Document document = dBuilder.newDocument();
107 conf.save(document, document);
108 final TransformerFactory tFactory = TransformerFactory.newInstance();
109 final Transformer transformer = tFactory.newTransformer();
110 final DOMSource source = new DOMSource(document);
111 final Result result = new StreamResult(saveFile);
112 transformer.transform(source, result);
113
114
115 final XMLPropertiesConfiguration conf2 = load(saveFile.getAbsolutePath());
116
117
118 assertEquals("Description of the new property list", conf2.getHeader());
119
120 assertFalse(conf2.isEmpty());
121 assertEquals("value1", conf2.getProperty("key1"));
122 assertEquals("value3", conf2.getProperty("key3"));
123 assertEquals("value4", conf2.getProperty("key4"));
124 }
125
126 @Test
127 void testLoad() throws Exception {
128 final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);
129 assertEquals("Description of the property list", conf.getHeader());
130
131 assertFalse(conf.isEmpty());
132 assertEquals("value1", conf.getProperty("key1"));
133 assertEquals("value2", conf.getProperty("key2"));
134 assertEquals("value3", conf.getProperty("key3"));
135 }
136
137 @Test
138 void testSave() throws Exception {
139
140 final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);
141
142
143 conf.addProperty("key4", "value4");
144 conf.clearProperty("key2");
145 conf.setHeader("Description of the new property list");
146
147
148 final File saveFile = newFile("test2.properties.xml", tempFolder);
149 final FileHandler saveHandler = new FileHandler(conf);
150 saveHandler.save(saveFile);
151
152
153 final XMLPropertiesConfiguration conf2 = load(saveFile.getAbsolutePath());
154
155
156 assertEquals("Description of the new property list", conf2.getHeader());
157
158 assertFalse(conf2.isEmpty());
159 assertEquals("value1", conf2.getProperty("key1"));
160 assertEquals("value3", conf2.getProperty("key3"));
161 assertEquals("value4", conf2.getProperty("key4"));
162 }
163 }