1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.builder;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertNotSame;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertSame;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.Mockito.mock;
28
29 import java.io.File;
30 import java.net.URL;
31 import java.nio.charset.StandardCharsets;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.apache.commons.configuration2.ConfigurationAssert;
36 import org.apache.commons.configuration2.beanutils.BeanHelper;
37 import org.apache.commons.configuration2.io.FileBased;
38 import org.apache.commons.configuration2.io.FileHandler;
39 import org.apache.commons.configuration2.io.FileLocationStrategy;
40 import org.apache.commons.configuration2.io.FileSystem;
41 import org.junit.jupiter.api.Test;
42
43
44
45
46 public class TestFileBasedBuilderParameters {
47
48
49
50 @Test
51 public void testBeanPropertiesAccess() throws Exception {
52 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
53 BeanHelper.setProperty(params, "throwExceptionOnMissing", Boolean.TRUE);
54 BeanHelper.setProperty(params, "fileName", "test.xml");
55 assertEquals("test.xml", params.getFileHandler().getFileName());
56 final Map<String, Object> map = params.getParameters();
57 assertEquals(Boolean.TRUE, map.get("throwExceptionOnMissing"));
58 }
59
60
61
62
63 @Test
64 public void testClone() {
65 final FileBased content = mock(FileBased.class);
66 final FileHandler fh = new FileHandler(content);
67 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl(fh);
68 params.setThrowExceptionOnMissing(true);
69 params.setFileName("test.xml");
70 final FileBasedBuilderParametersImpl clone = params.clone();
71 assertEquals(Boolean.TRUE, clone.getParameters().get("throwExceptionOnMissing"));
72 assertEquals("test.xml", clone.getFileHandler().getFileName());
73 assertSame(content, clone.getFileHandler().getContent());
74 assertNotSame(params.getFileHandler(), clone.getFileHandler());
75 }
76
77
78
79
80 @Test
81 public void testFromMap() {
82 final ReloadingDetectorFactory factory = mock(ReloadingDetectorFactory.class);
83 final Map<String, Object> map = new HashMap<>();
84 final String fileName = "someFileName";
85 final String basePath = "someBasePath";
86 final Long refreshDelay = 20140628222302L;
87 map.put("basePath", basePath);
88 map.put("fileName", fileName);
89 map.put("reloadingDetectorFactory", factory);
90 map.put("reloadingRefreshDelay", refreshDelay);
91
92 final FileBasedBuilderParametersImpl params = FileBasedBuilderParametersImpl.fromMap(map);
93 assertEquals(basePath, params.getFileHandler().getBasePath());
94 assertEquals(fileName, params.getFileHandler().getFileName());
95 assertEquals(factory, params.getReloadingDetectorFactory());
96 assertEquals(refreshDelay, params.getReloadingRefreshDelay());
97 }
98
99
100
101
102 @Test
103 public void testFromMapNull() {
104 final FileBasedBuilderParametersImpl params = FileBasedBuilderParametersImpl.fromMap(null);
105 assertNull(params.getReloadingRefreshDelay());
106 assertNull(params.getFileHandler().getFileName());
107 }
108
109
110
111
112 @Test
113 public void testFromParametersDefaultInstance() {
114 final FileBasedBuilderParametersImpl params = FileBasedBuilderParametersImpl.fromParameters(new HashMap<>(), true);
115 assertFalse(params.getFileHandler().isLocationDefined());
116 }
117
118
119
120
121 @Test
122 public void testFromParametersExtract() {
123 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
124 final Map<String, Object> map = params.getParameters();
125 assertSame(params, FileBasedBuilderParametersImpl.fromParameters(map));
126 }
127
128
129
130
131 @Test
132 public void testFromParametersNotFound() {
133 assertNull(FileBasedBuilderParametersImpl.fromParameters(new HashMap<>()));
134 }
135
136
137
138
139 @Test
140 public void testFromParametersNull() {
141 assertThrows(IllegalArgumentException.class, () -> FileBasedBuilderParametersImpl.fromParameters(null));
142 }
143
144
145
146
147 @Test
148 public void testGetParameters() {
149 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl().setReloadingRefreshDelay(1000L);
150 params.setThrowExceptionOnMissing(true);
151 final Map<String, Object> map = params.getParameters();
152 assertTrue(map.containsValue(params));
153 assertEquals(Boolean.TRUE, params.getParameters().get("throwExceptionOnMissing"));
154 }
155
156
157
158
159 @Test
160 public void testInheritFrom() {
161 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
162 params.setEncoding("ISO-8856-1");
163 params.setPath("A path");
164 params.setReloadingDetectorFactory(mock(ReloadingDetectorFactory.class));
165 params.setFileSystem(mock(FileSystem.class));
166 params.setLocationStrategy(mock(FileLocationStrategy.class));
167 params.setReloadingRefreshDelay(20160213171737L);
168 params.setThrowExceptionOnMissing(true);
169 final FileBasedBuilderParametersImpl params2 = new FileBasedBuilderParametersImpl();
170
171 params2.inheritFrom(params.getParameters());
172 assertEquals(params.getFileHandler().getEncoding(), params2.getFileHandler().getEncoding());
173 assertEquals(params.getFileHandler().getFileSystem(), params2.getFileHandler().getFileSystem());
174 assertEquals(params.getFileHandler().getLocationStrategy(), params2.getFileHandler().getLocationStrategy());
175 assertEquals(params.getReloadingDetectorFactory(), params2.getReloadingDetectorFactory());
176 assertEquals(params.getReloadingRefreshDelay(), params2.getReloadingRefreshDelay());
177 assertNull(params2.getFileHandler().getPath());
178 assertEquals(Boolean.TRUE, params2.getParameters().get("throwExceptionOnMissing"));
179 }
180
181
182
183
184 @Test
185 public void testInheritFromNoParametersObject() {
186 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl().setReloadingRefreshDelay(20160213211429L);
187
188 params.inheritFrom(new HashMap<>());
189 assertNotNull(params.getReloadingRefreshDelay());
190 }
191
192
193
194
195 @Test
196 public void testInheritFromSkipMissingProperties() {
197 final String encoding = StandardCharsets.UTF_16.name();
198 final ReloadingDetectorFactory factory = mock(ReloadingDetectorFactory.class);
199 final Long refreshDelay = 20160213172611L;
200 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl().setEncoding(encoding).setReloadingDetectorFactory(factory)
201 .setReloadingRefreshDelay(refreshDelay);
202
203 params.inheritFrom(new FileBasedBuilderParametersImpl().getParameters());
204 assertEquals(encoding, params.getFileHandler().getEncoding());
205 assertEquals(factory, params.getReloadingDetectorFactory());
206 assertEquals(refreshDelay, params.getReloadingRefreshDelay());
207 }
208
209
210
211
212 @Test
213 public void testInitDefaults() {
214 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
215 assertFalse(params.getFileHandler().isLocationDefined());
216 assertNull(params.getReloadingRefreshDelay());
217 }
218
219
220
221
222 @Test
223 public void testInitFileHandler() {
224 final FileHandler handler = new FileHandler();
225 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl(handler);
226 assertSame(handler, params.getFileHandler());
227 }
228
229
230
231
232 @Test
233 public void testSetBasePath() {
234 final String path = ConfigurationAssert.getTestFile("test.properties").getParentFile().getAbsolutePath();
235 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
236 assertSame(params, params.setBasePath(path));
237 assertEquals(path, params.getFileHandler().getBasePath());
238 }
239
240
241
242
243 @Test
244 public void testSetEncoding() {
245 final String enc = StandardCharsets.ISO_8859_1.name();
246 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
247 assertSame(params, params.setEncoding(enc));
248 assertSame(enc, params.getFileHandler().getEncoding());
249 }
250
251
252
253
254 @Test
255 public void testSetFile() {
256 final File file = ConfigurationAssert.getTestFile("test.properties").getAbsoluteFile();
257 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
258 assertSame(params, params.setFile(file));
259 assertEquals(file, params.getFileHandler().getFile());
260 }
261
262
263
264
265 @Test
266 public void testSetFileName() {
267 final String name = "testConfig.xml";
268 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
269 assertSame(params, params.setFileName(name));
270 assertEquals(name, params.getFileHandler().getFileName());
271 }
272
273
274
275
276 @Test
277 public void testSetFileSystem() {
278 final FileSystem fs = mock(FileSystem.class);
279 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
280 assertSame(params, params.setFileSystem(fs));
281 assertSame(fs, params.getFileHandler().getFileSystem());
282 }
283
284
285
286
287 @Test
288 public void testSetLocationStrategy() {
289 final FileLocationStrategy strat = mock(FileLocationStrategy.class);
290 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
291 assertSame(params, params.setLocationStrategy(strat));
292 assertSame(strat, params.getFileHandler().getLocationStrategy());
293 }
294
295
296
297
298 @Test
299 public void testSetPath() {
300 final String path = ConfigurationAssert.getTestFile("test.properties").getAbsolutePath();
301 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
302 assertSame(params, params.setPath(path));
303 assertEquals(path, params.getFileHandler().getPath());
304 }
305
306
307
308
309 @Test
310 public void testSetReloadingDetectorFactory() {
311 final ReloadingDetectorFactory factory = mock(ReloadingDetectorFactory.class);
312 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
313 assertNull(params.getReloadingDetectorFactory());
314 assertSame(params, params.setReloadingDetectorFactory(factory));
315 assertSame(factory, params.getReloadingDetectorFactory());
316 }
317
318
319
320
321 @Test
322 public void testSetReloadingRefreshDelay() {
323 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
324 final Long delay = 10000L;
325 assertSame(params, params.setReloadingRefreshDelay(delay));
326 assertEquals(delay, params.getReloadingRefreshDelay());
327 }
328
329
330
331
332 @Test
333 public void testSetURL() {
334 final URL url = ConfigurationAssert.getTestURL("test.properties");
335 final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
336 assertSame(params, params.setURL(url));
337 assertEquals(url.toExternalForm(), params.getFileHandler().getURL().toExternalForm());
338 }
339 }