001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.configuration; 019 020 import static org.junit.Assert.assertEquals; 021 import static org.junit.Assert.assertFalse; 022 import static org.junit.Assert.assertTrue; 023 024 import java.util.HashMap; 025 import java.util.List; 026 import java.util.Map; 027 028 import org.apache.commons.configuration.event.ConfigurationEvent; 029 import org.apache.commons.configuration.event.ConfigurationListener; 030 import org.junit.Test; 031 032 /** 033 * Tests for MapConfiguration. 034 * 035 * @author Emmanuel Bourg 036 * @version $Id: TestMapConfiguration.java 1222465 2011-12-22 21:32:56Z oheger $ 037 */ 038 public class TestMapConfiguration extends TestAbstractConfiguration 039 { 040 /** Constant for a test key.*/ 041 private static final String KEY = "key1"; 042 043 /** Constant for a test property value with whitespace.*/ 044 private static final String SPACE_VALUE = " Value with whitespace "; 045 046 /** The trimmed test value.*/ 047 private static final String TRIM_VALUE = SPACE_VALUE.trim(); 048 049 @Override 050 protected AbstractConfiguration getConfiguration() 051 { 052 Map<String, Object> map = new HashMap<String, Object>(); 053 map.put(KEY, "value1"); 054 map.put("key2", "value2"); 055 map.put("list", "value1, value2"); 056 map.put("listesc", "value1\\,value2"); 057 058 return new MapConfiguration(map); 059 } 060 061 @Override 062 protected AbstractConfiguration getEmptyConfiguration() 063 { 064 return new MapConfiguration(new HashMap<String, Object>()); 065 } 066 067 @Test 068 public void testGetMap() 069 { 070 Map<String, Object> map = new HashMap<String, Object>(); 071 072 MapConfiguration conf = new MapConfiguration(map); 073 assertEquals(map, conf.getMap()); 074 } 075 076 @Test 077 public void testClone() 078 { 079 MapConfiguration config = (MapConfiguration) getConfiguration(); 080 MapConfiguration copy = (MapConfiguration) config.clone(); 081 StrictConfigurationComparator comp = new StrictConfigurationComparator(); 082 assertTrue("Configurations are not equal", comp.compare(config, copy)); 083 } 084 085 /** 086 * Tests if the cloned configuration is decoupled from the original. 087 */ 088 @Test 089 public void testCloneModify() 090 { 091 MapConfiguration config = (MapConfiguration) getConfiguration(); 092 config.addConfigurationListener(new ConfigurationListener() 093 { 094 public void configurationChanged(ConfigurationEvent event) 095 { 096 // Just a dummy 097 } 098 }); 099 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 }