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    *     https://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  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   * Test class for {@code FileBasedBuilderParametersImpl}.
45   */
46  public class TestFileBasedBuilderParameters {
47  
48      /**
49       * Tests whether reflection-based property access through BeanUtils is possible.
50       */
51      @Test
52      void testBeanPropertiesAccess() throws Exception {
53          final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
54          BeanHelper.setProperty(params, "throwExceptionOnMissing", Boolean.TRUE);
55          BeanHelper.setProperty(params, "fileName", "test.xml");
56          assertEquals("test.xml", params.getFileHandler().getFileName());
57          final Map<String, Object> map = params.getParameters();
58          assertEquals(Boolean.TRUE, map.get("throwExceptionOnMissing"));
59      }
60  
61      /**
62       * Tests a clone operation.
63       */
64      @Test
65      void testClone() {
66          final FileBased content = mock(FileBased.class);
67          final FileHandler fh = new FileHandler(content);
68          final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl(fh);
69          params.setThrowExceptionOnMissing(true);
70          params.setFileName("test.xml");
71          final FileBasedBuilderParametersImpl clone = params.clone();
72          assertEquals(Boolean.TRUE, clone.getParameters().get("throwExceptionOnMissing"));
73          assertEquals("test.xml", clone.getFileHandler().getFileName());
74          assertSame(content, clone.getFileHandler().getContent());
75          assertNotSame(params.getFileHandler(), clone.getFileHandler());
76      }
77  
78      /**
79       * Tests whether an instance can be created from a map.
80       */
81      @Test
82      void testFromMap() {
83          final ReloadingDetectorFactory factory = mock(ReloadingDetectorFactory.class);
84          final Map<String, Object> map = new HashMap<>();
85          final String fileName = "someFileName";
86          final String basePath = "someBasePath";
87          final Long refreshDelay = 20140628222302L;
88          map.put("basePath", basePath);
89          map.put("fileName", fileName);
90          map.put("reloadingDetectorFactory", factory);
91          map.put("reloadingRefreshDelay", refreshDelay);
92  
93          final FileBasedBuilderParametersImpl params = FileBasedBuilderParametersImpl.fromMap(map);
94          assertEquals(basePath, params.getFileHandler().getBasePath());
95          assertEquals(fileName, params.getFileHandler().getFileName());
96          assertEquals(factory, params.getReloadingDetectorFactory());
97          assertEquals(refreshDelay, params.getReloadingRefreshDelay());
98      }
99  
100     /**
101      * Tests fromMap() for null input.
102      */
103     @Test
104     void testFromMapNull() {
105         final FileBasedBuilderParametersImpl params = FileBasedBuilderParametersImpl.fromMap(null);
106         assertNull(params.getReloadingRefreshDelay());
107         assertNull(params.getFileHandler().getFileName());
108     }
109 
110     /**
111      * Tests whether fromParameters() can return a default instance if the map does not contain an instance.
112      */
113     @Test
114     void testFromParametersDefaultInstance() {
115         final FileBasedBuilderParametersImpl params = FileBasedBuilderParametersImpl.fromParameters(new HashMap<>(), true);
116         assertFalse(params.getFileHandler().isLocationDefined());
117     }
118 
119     /**
120      * Tests whether an instance can be extracted from a parameters map.
121      */
122     @Test
123     void testFromParametersExtract() {
124         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
125         final Map<String, Object> map = params.getParameters();
126         assertSame(params, FileBasedBuilderParametersImpl.fromParameters(map));
127     }
128 
129     /**
130      * Tests fromParameters() if the map does not contain an instance.
131      */
132     @Test
133     void testFromParametersNotFound() {
134         assertNull(FileBasedBuilderParametersImpl.fromParameters(new HashMap<>()));
135     }
136 
137     /**
138      * Tries to obtain an instance from a null parameters map.
139      */
140     @Test
141     void testFromParametersNull() {
142         assertThrows(IllegalArgumentException.class, () -> FileBasedBuilderParametersImpl.fromParameters(null));
143     }
144 
145     /**
146      * Tests whether a map with parameters can be queried.
147      */
148     @Test
149     void testGetParameters() {
150         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl().setReloadingRefreshDelay(1000L);
151         params.setThrowExceptionOnMissing(true);
152         final Map<String, Object> map = params.getParameters();
153         assertTrue(map.containsValue(params));
154         assertEquals(Boolean.TRUE, params.getParameters().get("throwExceptionOnMissing"));
155     }
156 
157     /**
158      * Tests whether properties can be inherited from another object.
159      */
160     @Test
161     void testInheritFrom() {
162         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
163         params.setEncoding("ISO-8856-1");
164         params.setPath("A path");
165         params.setReloadingDetectorFactory(mock(ReloadingDetectorFactory.class));
166         params.setFileSystem(mock(FileSystem.class));
167         params.setLocationStrategy(mock(FileLocationStrategy.class));
168         params.setReloadingRefreshDelay(20160213171737L);
169         params.setThrowExceptionOnMissing(true);
170         final FileBasedBuilderParametersImpl params2 = new FileBasedBuilderParametersImpl();
171 
172         params2.inheritFrom(params.getParameters());
173         assertEquals(params.getFileHandler().getEncoding(), params2.getFileHandler().getEncoding());
174         assertEquals(params.getFileHandler().getFileSystem(), params2.getFileHandler().getFileSystem());
175         assertEquals(params.getFileHandler().getLocationStrategy(), params2.getFileHandler().getLocationStrategy());
176         assertEquals(params.getReloadingDetectorFactory(), params2.getReloadingDetectorFactory());
177         assertEquals(params.getReloadingRefreshDelay(), params2.getReloadingRefreshDelay());
178         assertNull(params2.getFileHandler().getPath());
179         assertEquals(Boolean.TRUE, params2.getParameters().get("throwExceptionOnMissing"));
180     }
181 
182     /**
183      * Tests inheritFrom() if no parameters object can be found in the map.
184      */
185     @Test
186     void testInheritFromNoParametersObject() {
187         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl().setReloadingRefreshDelay(20160213211429L);
188 
189         params.inheritFrom(new HashMap<>());
190         assertNotNull(params.getReloadingRefreshDelay());
191     }
192 
193     /**
194      * Tests that missing properties in the passed in map are skipped by inheritFrom().
195      */
196     @Test
197     void testInheritFromSkipMissingProperties() {
198         final String encoding = StandardCharsets.UTF_16.name();
199         final ReloadingDetectorFactory factory = mock(ReloadingDetectorFactory.class);
200         final Long refreshDelay = 20160213172611L;
201         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl().setEncoding(encoding).setReloadingDetectorFactory(factory)
202             .setReloadingRefreshDelay(refreshDelay);
203 
204         params.inheritFrom(new FileBasedBuilderParametersImpl().getParameters());
205         assertEquals(encoding, params.getFileHandler().getEncoding());
206         assertEquals(factory, params.getReloadingDetectorFactory());
207         assertEquals(refreshDelay, params.getReloadingRefreshDelay());
208     }
209 
210     /**
211      * Tests the standard constructor.
212      */
213     @Test
214     void testInitDefaults() {
215         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
216         assertFalse(params.getFileHandler().isLocationDefined());
217         assertNull(params.getReloadingRefreshDelay());
218     }
219 
220     /**
221      * Tests whether a file handler is accepted by the constructor.
222      */
223     @Test
224     void testInitFileHandler() {
225         final FileHandler handler = new FileHandler();
226         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl(handler);
227         assertSame(handler, params.getFileHandler());
228     }
229 
230     /**
231      * Tests whether a base path can be set.
232      */
233     @Test
234     void testSetBasePath() {
235         final String path = ConfigurationAssert.getTestFile("test.properties").getParentFile().getAbsolutePath();
236         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
237         assertSame(params, params.setBasePath(path));
238         assertEquals(path, params.getFileHandler().getBasePath());
239     }
240 
241     /**
242      * Tests whether an encoding can be set.
243      */
244     @Test
245     void testSetEncoding() {
246         final String enc = StandardCharsets.ISO_8859_1.name();
247         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
248         assertSame(params, params.setEncoding(enc));
249         assertSame(enc, params.getFileHandler().getEncoding());
250     }
251 
252     /**
253      * Tests whether a file can be set.
254      */
255     @Test
256     void testSetFile() {
257         final File file = ConfigurationAssert.getTestFile("test.properties").getAbsoluteFile();
258         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
259         assertSame(params, params.setFile(file));
260         assertEquals(file, params.getFileHandler().getFile());
261     }
262 
263     /**
264      * Tests whether a file name can be set.
265      */
266     @Test
267     void testSetFileName() {
268         final String name = "testConfig.xml";
269         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
270         assertSame(params, params.setFileName(name));
271         assertEquals(name, params.getFileHandler().getFileName());
272     }
273 
274     /**
275      * Tests whether a file system can be set.
276      */
277     @Test
278     void testSetFileSystem() {
279         final FileSystem fs = mock(FileSystem.class);
280         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
281         assertSame(params, params.setFileSystem(fs));
282         assertSame(fs, params.getFileHandler().getFileSystem());
283     }
284 
285     /**
286      * Tests whether a location strategy can be set.
287      */
288     @Test
289     void testSetLocationStrategy() {
290         final FileLocationStrategy strat = mock(FileLocationStrategy.class);
291         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
292         assertSame(params, params.setLocationStrategy(strat));
293         assertSame(strat, params.getFileHandler().getLocationStrategy());
294     }
295 
296     /**
297      * Tests whether a path can be set.
298      */
299     @Test
300     void testSetPath() {
301         final String path = ConfigurationAssert.getTestFile("test.properties").getAbsolutePath();
302         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
303         assertSame(params, params.setPath(path));
304         assertEquals(path, params.getFileHandler().getPath());
305     }
306 
307     /**
308      * Tests whether a factory for reloading detectors can be set.
309      */
310     @Test
311     void testSetReloadingDetectorFactory() {
312         final ReloadingDetectorFactory factory = mock(ReloadingDetectorFactory.class);
313         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
314         assertNull(params.getReloadingDetectorFactory());
315         assertSame(params, params.setReloadingDetectorFactory(factory));
316         assertSame(factory, params.getReloadingDetectorFactory());
317     }
318 
319     /**
320      * Tests whether the refresh delay can be set.
321      */
322     @Test
323     void testSetReloadingRefreshDelay() {
324         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
325         final Long delay = 10000L;
326         assertSame(params, params.setReloadingRefreshDelay(delay));
327         assertEquals(delay, params.getReloadingRefreshDelay());
328     }
329 
330     /**
331      * Tests whether a URL can be set.
332      */
333     @Test
334     void testSetURL() {
335         final URL url = ConfigurationAssert.getTestURL("test.properties");
336         final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
337         assertSame(params, params.setURL(url));
338         assertEquals(url.toExternalForm(), params.getFileHandler().getURL().toExternalForm());
339     }
340 }