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