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