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