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.configuration;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.configuration.event.ConfigurationEvent;
29  import org.apache.commons.configuration.event.ConfigurationListener;
30  import org.junit.Test;
31  
32  /**
33   * Tests for MapConfiguration.
34   *
35   * @author Emmanuel Bourg
36   * @version $Id: TestMapConfiguration.java 1222465 2011-12-22 21:32:56Z oheger $
37   */
38  public class TestMapConfiguration extends TestAbstractConfiguration
39  {
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      {
52          Map<String, Object> map = new HashMap<String, Object>();
53          map.put(KEY, "value1");
54          map.put("key2", "value2");
55          map.put("list", "value1, value2");
56          map.put("listesc", "value1\\,value2");
57  
58          return new MapConfiguration(map);
59      }
60  
61      @Override
62      protected AbstractConfiguration getEmptyConfiguration()
63      {
64          return new MapConfiguration(new HashMap<String, Object>());
65      }
66  
67      @Test
68      public void testGetMap()
69      {
70          Map<String, Object> map = new HashMap<String, Object>();
71  
72          MapConfiguration conf = new MapConfiguration(map);
73          assertEquals(map, conf.getMap());
74      }
75  
76      @Test
77      public void testClone()
78      {
79          MapConfiguration config = (MapConfiguration) getConfiguration();
80          MapConfiguration copy = (MapConfiguration) config.clone();
81          StrictConfigurationComparator comp = new StrictConfigurationComparator();
82          assertTrue("Configurations are not equal", comp.compare(config, copy));
83      }
84  
85      /**
86       * Tests if the cloned configuration is decoupled from the original.
87       */
88      @Test
89      public void testCloneModify()
90      {
91          MapConfiguration config = (MapConfiguration) getConfiguration();
92          config.addConfigurationListener(new ConfigurationListener()
93          {
94              public void configurationChanged(ConfigurationEvent event)
95              {
96                  // Just a dummy
97              }
98          });
99          MapConfiguration copy = (MapConfiguration) config.clone();
100         assertTrue("Event listeners were copied", copy
101                 .getConfigurationListeners().isEmpty());
102 
103         config.addProperty("cloneTest", Boolean.TRUE);
104         assertFalse("Map not decoupled", copy.containsKey("cloneTest"));
105         copy.clearProperty("key1");
106         assertEquals("Map not decoupled (2)", "value1", config
107                 .getString("key1"));
108     }
109 
110     /**
111      * Tests adding another value to an existing property.
112      */
113     @Test
114     public void testAddProperty()
115     {
116         MapConfiguration config = (MapConfiguration) getConfiguration();
117         config.addProperty(KEY, TRIM_VALUE);
118         config.addProperty(KEY, "anotherValue");
119         List<Object> values = config.getList(KEY);
120         assertEquals("Wrong number of values", 3, values.size());
121     }
122 
123     /**
124      * Tests querying a property when trimming is active.
125      */
126     @Test
127     public void testGetPropertyTrim()
128     {
129         MapConfiguration config = (MapConfiguration) getConfiguration();
130         config.getMap().put(KEY, SPACE_VALUE);
131         assertEquals("Wrong trimmed value", TRIM_VALUE, config.getProperty(KEY));
132     }
133 
134     /**
135      * Tests querying a property when trimming is disabled.
136      */
137     @Test
138     public void testGetPropertyTrimDisabled()
139     {
140         MapConfiguration config = (MapConfiguration) getConfiguration();
141         config.getMap().put(KEY, SPACE_VALUE);
142         config.setTrimmingDisabled(true);
143         assertEquals("Wrong trimmed value", SPACE_VALUE, config.getProperty(KEY));
144     }
145 
146     /**
147      * Tests querying a property when trimming is enabled, but list splitting is
148      * disabled. In this case no trimming is performed (trimming only works if
149      * list splitting is enabled).
150      */
151     @Test
152     public void testGetPropertyTrimNoSplit()
153     {
154         MapConfiguration config = (MapConfiguration) getConfiguration();
155         config.getMap().put(KEY, SPACE_VALUE);
156         config.setDelimiterParsingDisabled(true);
157         assertEquals("Wrong trimmed value", SPACE_VALUE, config.getProperty(KEY));
158     }
159 }