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.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
31  import org.apache.commons.configuration2.convert.DisabledListDelimiterHandler;
32  import org.apache.commons.configuration2.event.ConfigurationEvent;
33  import org.apache.commons.configuration2.event.EventListenerTestImpl;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests for MapConfiguration.
38   */
39  public class TestMapConfiguration extends TestAbstractConfiguration {
40  
41      /** Constant for a test key. */
42      private static final String KEY = "key1";
43  
44      /** Constant for a test property value with whitespace. */
45      private static final String SPACE_VALUE = "   Value with whitespace  ";
46  
47      /** The trimmed test value. */
48      private static final String TRIM_VALUE = SPACE_VALUE.trim();
49  
50      @Override
51      protected AbstractConfiguration getConfiguration() {
52          final Map<String, Object> map = new HashMap<>();
53          map.put(KEY, "value1");
54          map.put("key2", "value2");
55          map.put("list", "value1, value2");
56          map.put("listesc", "value1\\,value2");
57  
58          final MapConfiguration config = new MapConfiguration(map);
59          config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
60          return config;
61      }
62  
63      @Override
64      protected AbstractConfiguration getEmptyConfiguration() {
65          return new MapConfiguration(new HashMap<>());
66      }
67  
68      /**
69       * Tests adding another value to an existing property.
70       */
71      @Test
72      void testAddProperty() {
73          final MapConfiguration config = (MapConfiguration) getConfiguration();
74          config.addProperty(KEY, TRIM_VALUE);
75          config.addProperty(KEY, "anotherValue");
76          final List<Object> values = config.getList(KEY);
77          assertEquals(3, values.size());
78      }
79  
80      @Test
81      void testClone() {
82          final MapConfiguration config = (MapConfiguration) getConfiguration();
83          final MapConfiguration copy = (MapConfiguration) config.clone();
84          final StrictConfigurationComparator comp = new StrictConfigurationComparator();
85          assertTrue(comp.compare(config, copy));
86      }
87  
88      /**
89       * Tests whether interpolation works as expected after cloning.
90       */
91      @Test
92      void testCloneInterpolation() {
93          final String keyAnswer = "answer";
94          final String keyValue = "value";
95          final MapConfiguration config = (MapConfiguration) getConfiguration();
96          config.addProperty(keyAnswer, "The answer is ${" + keyValue + "}.");
97          config.addProperty(keyValue, 42);
98          final MapConfiguration clone = (MapConfiguration) config.clone();
99          clone.setProperty(keyValue, 43);
100         assertEquals("The answer is 42.", config.getString(keyAnswer));
101         assertEquals("The answer is 43.", clone.getString(keyAnswer));
102     }
103 
104     /**
105      * Tests if the cloned configuration is decoupled from the original.
106      */
107     @Test
108     void testCloneModify() {
109         final MapConfiguration config = (MapConfiguration) getConfiguration();
110         config.addEventListener(ConfigurationEvent.ANY, new EventListenerTestImpl(config));
111         final MapConfiguration copy = (MapConfiguration) config.clone();
112         assertTrue(copy.getEventListeners(ConfigurationEvent.ANY).isEmpty());
113 
114         config.addProperty("cloneTest", Boolean.TRUE);
115         assertFalse(copy.containsKey("cloneTest"));
116         copy.clearProperty("key1");
117         assertEquals("value1", config.getString("key1"));
118     }
119 
120     @Override
121     @Test
122     public void testContainsValue() {
123         assertFalse(getConfiguration().containsValue(null));
124         assertFalse(getConfiguration().containsValue(""));
125     }
126 
127     @Test
128     void testGetMap() {
129         final Map<String, Object> map = new HashMap<>();
130 
131         final MapConfiguration conf = new MapConfiguration(map);
132         assertEquals(map, conf.getMap());
133     }
134 
135     /**
136      * Tests querying a property when trimming is active.
137      */
138     @Test
139     void testGetPropertyTrim() {
140         final MapConfiguration config = (MapConfiguration) getConfiguration();
141         config.getMap().put(KEY, SPACE_VALUE);
142         assertEquals(TRIM_VALUE, config.getProperty(KEY));
143     }
144 
145     /**
146      * Tests querying a property when trimming is disabled.
147      */
148     @Test
149     void testGetPropertyTrimDisabled() {
150         final MapConfiguration config = (MapConfiguration) getConfiguration();
151         config.getMap().put(KEY, SPACE_VALUE);
152         config.setTrimmingDisabled(true);
153         assertEquals(SPACE_VALUE, config.getProperty(KEY));
154     }
155 
156     /**
157      * Tests querying a property if trimming is enabled, but list splitting is disabled. In this case no trimming is
158      * performed (trimming only works if list splitting is enabled).
159      */
160     @Test
161     void testGetPropertyTrimNoSplit() {
162         final MapConfiguration config = (MapConfiguration) getConfiguration();
163         config.getMap().put(KEY, SPACE_VALUE);
164         config.setListDelimiterHandler(new DisabledListDelimiterHandler());
165         assertEquals(SPACE_VALUE, config.getProperty(KEY));
166     }
167 
168     @Test
169     void testNullMap() {
170         assertThrows(NullPointerException.class, () -> new MapConfiguration((Map) null));
171         assertThrows(NullPointerException.class, () -> new MapConfiguration((Properties) null));
172     }
173 }