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.hamcrest.MatcherAssert.assertThat;
21  import static org.hamcrest.Matchers.containsInAnyOrder;
22  import static org.hamcrest.Matchers.containsString;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertFalse;
25  import static org.junit.jupiter.api.Assertions.assertNotNull;
26  import static org.junit.jupiter.api.Assertions.assertNull;
27  import static org.junit.jupiter.api.Assertions.assertSame;
28  import static org.junit.jupiter.api.Assertions.assertThrows;
29  import static org.junit.jupiter.api.Assertions.assertTrue;
30  
31  import java.math.BigInteger;
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  import org.apache.commons.configuration2.ex.ConversionException;
38  import org.apache.commons.configuration2.io.ConfigurationLogger;
39  import org.junit.jupiter.api.Test;
40  
41  /**
42   * Abstract TestCase for implementations of {@link AbstractConfiguration}.
43   */
44  public abstract class TestAbstractConfiguration {
45      /**
46       * Gets an abstract configuration with the following data:<br>
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 testAddPropertyDirect() {
64          final AbstractConfiguration config = getConfiguration();
65          config.addPropertyDirect("key3", "value3");
66          assertEquals("value3", config.getProperty("key3"));
67  
68          config.addPropertyDirect("key3", "value4");
69          config.addPropertyDirect("key3", "value5");
70          final List<Object> list = config.getList("key3");
71          assertNotNull(list);
72  
73          final List<Object> expected = new ArrayList<>();
74          expected.add("value3");
75          expected.add("value4");
76          expected.add("value5");
77  
78          assertEquals(expected, list);
79      }
80  
81      @Test
82      public void testClearProperty() {
83          final Configuration config = getConfiguration();
84          config.clearProperty("key2");
85          assertFalse(config.containsKey("key2"));
86      }
87  
88      @Test
89      public void testContainsKey() {
90          final Configuration config = getConfiguration();
91          assertTrue(config.containsKey("key1"));
92          assertFalse(config.containsKey("key3"));
93      }
94  
95      /**
96       * Tests the exception message triggered by the conversion to BigInteger. This test is related to CONFIGURATION-357.
97       */
98      @Test
99      public void testGetBigIntegerConversion() {
100         final Configuration config = getConfiguration();
101         final ConversionException cex = assertThrows(ConversionException.class, () -> config.getBigInteger("key1"));
102         assertThat(cex.getMessage(), containsString("'key1'"));
103         assertThat(cex.getMessage(), containsString(BigInteger.class.getName()));
104         assertThat(cex.getMessage(), containsString(config.getString("key1")));
105     }
106 
107     @Test
108     public void testGetKeys() {
109         final Configuration config = getConfiguration();
110         final Iterator<String> keys = config.getKeys();
111 
112         final String[] expectedKeys = {"key1", "key2", "list", "listesc"};
113 
114         assertNotNull(keys);
115         assertTrue(keys.hasNext());
116 
117         final List<String> actualKeys = new ArrayList<>();
118         while (keys.hasNext()) {
119             actualKeys.add(keys.next());
120         }
121 
122         assertThat("keys", actualKeys, containsInAnyOrder(expectedKeys));
123     }
124 
125     @Test
126     public void testGetProperty() {
127         final Configuration config = getConfiguration();
128         assertEquals("value1", config.getProperty("key1"));
129         assertEquals("value2", config.getProperty("key2"));
130         assertNull(config.getProperty("key3"));
131     }
132 
133     @Test
134     public void testIsEmpty() {
135         final Configuration config = getConfiguration();
136         assertFalse(config.isEmpty());
137         assertTrue(getEmptyConfiguration().isEmpty());
138     }
139 
140     @Test
141     public void testList() {
142         final Configuration config = getConfiguration();
143 
144         final List<?> list = config.getList("list");
145         assertNotNull(config.getProperty("list"));
146         assertEquals(Arrays.asList("value1", "value2"), list);
147     }
148 
149     /**
150      * Tests whether the escape character for list delimiters is recocknized and removed.
151      */
152     @Test
153     public void testListEscaped() {
154         assertEquals("value1,value2", getConfiguration().getString("listesc"));
155     }
156 
157     /**
158      * Tests accessing the configuration's logger.
159      */
160     @Test
161     public void testSetLogger() {
162         final AbstractConfiguration config = getEmptyConfiguration();
163         assertNotNull(config.getLogger());
164         final ConfigurationLogger log = new ConfigurationLogger(config.getClass());
165         config.setLogger(log);
166         assertSame(log, config.getLogger());
167     }
168 
169     @Test
170     public void testSize() {
171         assertEquals(4, getConfiguration().size());
172     }
173 
174     @Test
175     public void testSizeEmpty() {
176         assertEquals(0, getEmptyConfiguration().size());
177     }
178 }