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 private static final String TEST_PROPERTIES_FILE = "test.properties.xml";
49
50
51
52
53
54
55
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
65 @TempDir
66 public File tempFolder;
67
68 @Test
69 public void testDOMLoad() throws Exception {
70
71 assertThrows(NullPointerException.class, () -> new XMLPropertiesConfiguration(null));
72
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
92 final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);
93
94
95 conf.addProperty("key4", "value4");
96 conf.clearProperty("key2");
97 conf.setHeader("Description of the new property list");
98
99
100 final File saveFile = newFile("test2.properties.xml", tempFolder);
101
102
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
114 final XMLPropertiesConfiguration conf2 = load(saveFile.getAbsolutePath());
115
116
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
139 final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);
140
141
142 conf.addProperty("key4", "value4");
143 conf.clearProperty("key2");
144 conf.setHeader("Description of the new property list");
145
146
147 final File saveFile = newFile("test2.properties.xml", tempFolder);
148 final FileHandler saveHandler = new FileHandler(conf);
149 saveHandler.save(saveFile);
150
151
152 final XMLPropertiesConfiguration conf2 = load(saveFile.getAbsolutePath());
153
154
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 }