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  
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.assertNotNull;
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  
28  import java.math.BigInteger;
29  import java.util.ArrayList;
30  import java.util.Arrays;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Set;
35  
36  import org.apache.commons.configuration2.ex.ConversionException;
37  import org.apache.commons.configuration2.io.ConfigurationLogger;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Abstract TestCase for implementations of {@link AbstractConfiguration}.
42   */
43  public abstract class TestAbstractConfiguration {
44      /**
45       * Gets an abstract configuration with the following data:<br>
46       *
47       * <pre>
48       * key1 = value1
49       * key2 = value2
50       * list = value1, value2
51       * listesc = value1\\,value2
52       * </pre>
53       */
54      protected abstract AbstractConfiguration getConfiguration();
55  
56      /**
57       * Gets an empty configuration.
58       */
59      protected abstract AbstractConfiguration getEmptyConfiguration();
60  
61      @Test
62      public void givenNullIteratorTestContains() {
63          final AbstractConfiguration config = getConfiguration();
64  
65          assertThrows(NullPointerException.class, () -> config.contains(null, "value1"));
66      }
67  
68      @Test
69      public void givenNullValueTestContains() {
70          final AbstractConfiguration config = getConfiguration();
71          final Iterator<String> keys = config.getKeys();
72          assertFalse(config.contains(keys, null));
73          assertFalse(config.contains(keys, ""));
74      }
75  
76      @Test
77      public void testAddPropertyDirect() {
78          final AbstractConfiguration config = getConfiguration();
79          config.addPropertyDirect("key3", "value3");
80          assertEquals("value3", config.getProperty("key3"));
81  
82          config.addPropertyDirect("key3", "value4");
83          config.addPropertyDirect("key3", "value5");
84          final List<Object> list = config.getList("key3");
85          assertNotNull(list);
86  
87          final List<Object> expected = new ArrayList<>();
88          expected.add("value3");
89          expected.add("value4");
90          expected.add("value5");
91  
92          assertEquals(expected, list);
93      }
94  
95      @Test
96      public void testClearProperty() {
97          final Configuration config = getConfiguration();
98          config.clearProperty("key2");
99          assertFalse(config.containsKey("key2"));
100     }
101 
102     @Test
103     void testContains() {
104         final AbstractConfiguration config = getConfiguration();
105         assertTrue(config.contains(config.getKeys(), "value1"));
106         assertFalse(config.contains(config.getKeys(), "value99999"));
107     }
108 
109     @Test
110     void testContainsKey() {
111         final Configuration config = getConfiguration();
112         assertTrue(config.containsKey("key1"));
113         assertFalse(config.containsKey("key3"));
114     }
115 
116     @Test
117     public void testContainsValue() {
118         final Configuration config = getConfiguration();
119         assertFalse(config.containsValue(null));
120         assertFalse(config.containsValue(""));
121         assertTrue(config.containsValue("value1"));
122         assertFalse(config.containsValue("value99999"));
123     }
124 
125     /**
126      * Tests the exception message triggered by the conversion to BigInteger. This test is related to CONFIGURATION-357.
127      */
128     @Test
129     void testGetBigIntegerConversion() {
130         final Configuration config = getConfiguration();
131         final ConversionException cex = assertThrows(ConversionException.class, () -> config.getBigInteger("key1"));
132         assertTrue(cex.getMessage().contains("'key1'"));
133         assertTrue(cex.getMessage().contains(BigInteger.class.getName()));
134         assertTrue(cex.getMessage().contains(config.getString("key1")));
135     }
136 
137     @Test
138     void testGetKeys() {
139         final Configuration config = getConfiguration();
140         final Iterator<String> keys = config.getKeys();
141         final Set<String> expectedKeys = new HashSet<>(Arrays.asList("key1", "key2", "list", "listesc"));
142         assertNotNull(keys);
143         assertTrue(keys.hasNext());
144         final Set<String> actualKeys = new HashSet<>();
145         while (keys.hasNext()) {
146             actualKeys.add(keys.next());
147         }
148         assertEquals(actualKeys, expectedKeys, "keys");
149     }
150 
151     @Test
152     void testGetProperty() {
153         final Configuration config = getConfiguration();
154         assertEquals("value1", config.getProperty("key1"));
155         assertEquals("value2", config.getProperty("key2"));
156         assertNull(config.getProperty("key3"));
157     }
158 
159     @Test
160     void testIsEmpty() {
161         final Configuration config = getConfiguration();
162         assertFalse(config.isEmpty());
163         assertTrue(getEmptyConfiguration().isEmpty());
164     }
165 
166     @Test
167     void testList() {
168         final Configuration config = getConfiguration();
169 
170         final List<?> list = config.getList("list");
171         assertNotNull(config.getProperty("list"));
172         assertEquals(Arrays.asList("value1", "value2"), list);
173     }
174 
175     /**
176      * Tests whether the escape character for list delimiters is recocknized and removed.
177      */
178     @Test
179     void testListEscaped() {
180         assertEquals("value1,value2", getConfiguration().getString("listesc"));
181     }
182 
183     /**
184      * Tests accessing the configuration's logger.
185      */
186     @Test
187     void testSetLogger() {
188         final AbstractConfiguration config = getEmptyConfiguration();
189         assertNotNull(config.getLogger());
190         final ConfigurationLogger log = new ConfigurationLogger(config.getClass());
191         config.setLogger(log);
192         assertSame(log, config.getLogger());
193     }
194 
195     @Test
196     void testSize() {
197         assertEquals(4, getConfiguration().size());
198     }
199 
200     @Test
201     void testSizeEmpty() {
202         assertEquals(0, getEmptyConfiguration().size());
203     }
204 }