01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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 java.util.HashMap;
21: import java.util.Map;
22:
23: import org.apache.commons.configuration.event.ConfigurationEvent;
24: import org.apache.commons.configuration.event.ConfigurationListener;
25:
26: /**
27: * Tests for MapConfiguration.
28: *
29: * @author Emmanuel Bourg
30: * @version $Revision: 515306 $, $Date: 2007-03-06 22:15:00 +0100 (Di, 06 Mrz 2007) $
31: */
32: public class TestMapConfiguration extends TestAbstractConfiguration {
33: protected AbstractConfiguration getConfiguration() {
34: Map map = new HashMap();
35: map.put("key1", "value1");
36: map.put("key2", "value2");
37: map.put("list", "value1, value2");
38: map.put("listesc", "value1\\,value2");
39:
40: return new MapConfiguration(map);
41: }
42:
43: protected AbstractConfiguration getEmptyConfiguration() {
44: return new MapConfiguration(new HashMap());
45: }
46:
47: public void testGetMap() {
48: Map map = new HashMap();
49:
50: MapConfiguration conf = new MapConfiguration(map);
51: assertEquals(map, conf.getMap());
52: }
53:
54: public void testClone() {
55: MapConfiguration config = (MapConfiguration) getConfiguration();
56: MapConfiguration copy = (MapConfiguration) config.clone();
57: StrictConfigurationComparator comp = new StrictConfigurationComparator();
58: assertTrue("Configurations are not equal", comp.compare(config,
59: copy));
60: }
61:
62: /**
63: * Tests if the cloned configuration decoupled from the original.
64: */
65: public void testCloneModify() {
66: MapConfiguration config = (MapConfiguration) getConfiguration();
67: config.addConfigurationListener(new ConfigurationListener() {
68: public void configurationChanged(ConfigurationEvent event) {
69: // Just a dummy
70: }
71: });
72: MapConfiguration copy = (MapConfiguration) config.clone();
73: assertTrue("Event listeners were copied", copy
74: .getConfigurationListeners().isEmpty());
75:
76: config.addProperty("cloneTest", Boolean.TRUE);
77: assertFalse("Map not decoupled", copy.containsKey("cloneTest"));
78: copy.clearProperty("key1");
79: assertEquals("Map not decoupled (2)", "value1", config
80: .getString("key1"));
81: }
82: }
|