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.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.math.BigDecimal;
26  import java.math.BigInteger;
27  import java.time.Duration;
28  import java.util.Collection;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.NoSuchElementException;
34  import java.util.Objects;
35  import java.util.Properties;
36  
37  import org.apache.commons.configuration2.ex.ConversionException;
38  import org.junit.jupiter.api.AfterEach;
39  import org.junit.jupiter.api.BeforeEach;
40  import org.junit.jupiter.api.Test;
41  
42  /**
43   * Tests {@link ImmutableConfiguration} default methods.
44   */
45  public class TestDefaultImmutableConfiguration {
46  
47      /** Tests default methods. This class MUST NOT override the default methods! */
48      private static final class MapImmutableConfiguration implements ImmutableConfiguration {
49  
50          private final Map<String, Object> map = new HashMap<>();
51  
52          @Override
53          public boolean containsKey(final String key) {
54              // Super is not a default method.
55              return false;
56          }
57  
58          @Override
59          public <T> T get(final Class<T> cls, final String key) {
60              // Super is not a default method.
61              return null;
62          }
63  
64          @Override
65          public <T> T get(final Class<T> cls, final String key, final T defaultValue) {
66              // Super is not a default method.
67              return null;
68          }
69  
70          @Override
71          public Object getArray(final Class<?> cls, final String key) {
72              // Super is not a default method.
73              return null;
74          }
75  
76          @Override
77          public Object getArray(final Class<?> cls, final String key, final Object defaultValue) {
78              // Super is not a default method.
79              return null;
80          }
81  
82          @Override
83          public BigDecimal getBigDecimal(final String key) {
84              // Super is not a default method.
85              return null;
86          }
87  
88          @Override
89          public BigDecimal getBigDecimal(final String key, final BigDecimal defaultValue) {
90              // Super is not a default method.
91              return null;
92          }
93  
94          @Override
95          public BigInteger getBigInteger(final String key) {
96              // Super is not a default method.
97              return null;
98          }
99  
100         @Override
101         public BigInteger getBigInteger(final String key, final BigInteger defaultValue) {
102             // Super is not a default method.
103             return null;
104         }
105 
106         @Override
107         public boolean getBoolean(final String key) {
108             // Super is not a default method.
109             return false;
110         }
111 
112         @Override
113         public boolean getBoolean(final String key, final boolean defaultValue) {
114             // Super is not a default method.
115             return false;
116         }
117 
118         @Override
119         public Boolean getBoolean(final String key, final Boolean defaultValue) {
120             // Super is not a default method.
121             return null;
122         }
123 
124         @Override
125         public byte getByte(final String key) {
126             // Super is not a default method.
127             return 0;
128         }
129 
130         @Override
131         public byte getByte(final String key, final byte defaultValue) {
132             // Super is not a default method.
133             return 0;
134         }
135 
136         @Override
137         public Byte getByte(final String key, final Byte defaultValue) {
138             // Super is not a default method.
139             return null;
140         }
141 
142         @Override
143         public <T> Collection<T> getCollection(final Class<T> cls, final String key, final Collection<T> target) {
144             // Super is not a default method.
145             return null;
146         }
147 
148         @Override
149         public <T> Collection<T> getCollection(final Class<T> cls, final String key, final Collection<T> target, final Collection<T> defaultValue) {
150             // Super is not a default method.
151             return null;
152         }
153 
154         @Override
155         public double getDouble(final String key) {
156             // Super is not a default method.
157             return 0;
158         }
159 
160         @Override
161         public double getDouble(final String key, final double defaultValue) {
162             // Super is not a default method.
163             return 0;
164         }
165 
166         @Override
167         public Double getDouble(final String key, final Double defaultValue) {
168             // Super is not a default method.
169             return null;
170         }
171 
172         @Override
173         public String getEncodedString(final String key) {
174             // Super is not a default method.
175             return null;
176         }
177 
178         @Override
179         public String getEncodedString(final String key, final ConfigurationDecoder decoder) {
180             // Super is not a default method.
181             return null;
182         }
183 
184         @Override
185         public float getFloat(final String key) {
186             // Super is not a default method.
187             return 0;
188         }
189 
190         @Override
191         public float getFloat(final String key, final float defaultValue) {
192             // Super is not a default method.
193             return 0;
194         }
195 
196         @Override
197         public Float getFloat(final String key, final Float defaultValue) {
198             // Super is not a default method.
199             return null;
200         }
201 
202         @Override
203         public int getInt(final String key) {
204             // Super is not a default method.
205             return 0;
206         }
207 
208         @Override
209         public int getInt(final String key, final int defaultValue) {
210             // Super is not a default method.
211             return 0;
212         }
213 
214         @Override
215         public Integer getInteger(final String key, final Integer defaultValue) {
216             // Super is not a default method.
217             return null;
218         }
219 
220         @Override
221         public Iterator<String> getKeys() {
222             return this.map.keySet().iterator();
223         }
224 
225         @Override
226         public Iterator<String> getKeys(final String prefix) {
227             // Super is not a default method.
228             return null;
229         }
230 
231         @Override
232         public <T> List<T> getList(final Class<T> cls, final String key) {
233             // Super is not a default method.
234             return null;
235         }
236 
237         @Override
238         public <T> List<T> getList(final Class<T> cls, final String key, final List<T> defaultValue) {
239             // Super is not a default method.
240             return null;
241         }
242 
243         @Override
244         public List<Object> getList(final String key) {
245             // Super is not a default method.
246             return null;
247         }
248 
249         @Override
250         public List<Object> getList(final String key, final List<?> defaultValue) {
251             // Super is not a default method.
252             return null;
253         }
254 
255         @Override
256         public long getLong(final String key) {
257             // Super is not a default method.
258             return 0;
259         }
260 
261         @Override
262         public long getLong(final String key, final long defaultValue) {
263             // Super is not a default method.
264             return 0;
265         }
266 
267         @Override
268         public Long getLong(final String key, final Long defaultValue) {
269             // Super is not a default method.
270             return null;
271         }
272 
273         @Override
274         public Properties getProperties(final String key) {
275             // Super is not a default method.
276             return null;
277         }
278 
279         @Override
280         public Object getProperty(final String key) {
281             // Super is not a default method.
282             return map.get(key);
283         }
284 
285         @Override
286         public short getShort(final String key) {
287             // Super is not a default method.
288             return 0;
289         }
290 
291         @Override
292         public short getShort(final String key, final short defaultValue) {
293             // Super is not a default method.
294             return 0;
295         }
296 
297         @Override
298         public Short getShort(final String key, final Short defaultValue) {
299             // Super is not a default method.
300             return null;
301         }
302 
303         @Override
304         public String getString(final String key) {
305             return Objects.toString(map.get(key), null);
306         }
307 
308         @Override
309         public String getString(final String key, final String defaultValue) {
310             // Super is not a default method.
311             return null;
312         }
313 
314         @Override
315         public String[] getStringArray(final String key) {
316             // Super is not a default method.
317             return null;
318         }
319 
320         @Override
321         public ImmutableConfiguration immutableSubset(final String prefix) {
322             // Super is not a default method.
323             return null;
324         }
325 
326         @Override
327         public boolean isEmpty() {
328             // Super is not a default method.
329             return false;
330         }
331 
332         @Override
333         public int size() {
334             // Super is not a default method.
335             return 0;
336         }
337 
338     }
339 
340     private final MapImmutableConfiguration config = new MapImmutableConfiguration();
341 
342     @BeforeEach
343     @AfterEach
344     public void clearMap() {
345         config.map.clear();
346     }
347 
348     @Test
349     public void testContainsValueDefaultImplementation() {
350         config.map.put("test", "213123");
351         assertFalse(config.containsValue(""));
352         assertFalse(config.containsValue(null));
353         assertTrue(config.containsValue("213123"));
354     }
355 
356     @Test
357     public void testGetDuration() {
358         final Duration d = Duration.ofSeconds(1);
359         config.map.put("durationD", d.toString());
360         final Duration oneD = Duration.ofSeconds(1);
361         final Duration twoD = Duration.ofSeconds(2);
362         assertEquals(oneD, config.getDuration("durationD"));
363         assertEquals(oneD, config.getDuration("durationD", twoD));
364         assertEquals(twoD, config.getDuration("numberNotInConfig", twoD));
365         assertEquals(oneD, config.getDuration("durationD", twoD));
366     }
367 
368     @Test
369     public void testGetDurationIncompatibleType() {
370         config.map.put("test.empty", "");
371         assertThrows(ConversionException.class, () -> config.getDuration("test.empty"));
372     }
373 
374     @Test
375     public void testGetDurationUnknown() {
376         assertThrows(NoSuchElementException.class, () -> config.getDuration("numberNotInConfig"));
377     }
378 }