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 java.util.List;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import junit.framework.TestCase;
025: import org.apache.commons.collections.ExtendedProperties;
026:
027: /**
028: * Tests the ConfigurationConverter class.
029: *
030: * @author Martin Poeschl
031: * @author Emmanuel Bourg
032: * @version $Revision: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
033: */
034: public class TestConfigurationConverter extends TestCase {
035: public void testExtendedPropertiesToConfiguration() {
036: ExtendedProperties eprops = new ExtendedProperties();
037: eprops.setProperty("string", "teststring");
038: eprops.setProperty("int", "123");
039: eprops.addProperty("list", "item 1");
040: eprops.addProperty("list", "item 2");
041:
042: Configuration config = ConfigurationConverter
043: .getConfiguration(eprops);
044:
045: assertEquals("This returns 'teststring'", "teststring", config
046: .getString("string"));
047: List item1 = config.getList("list");
048: assertEquals("This returns 'item 1'", "item 1", (String) item1
049: .get(0));
050:
051: assertEquals("This returns 123", 123, config.getInt("int"));
052: }
053:
054: public void testPropertiesToConfiguration() {
055: Properties props = new Properties();
056: props.setProperty("string", "teststring");
057: props.setProperty("int", "123");
058: props.setProperty("list", "item 1, item 2");
059:
060: Configuration config = ConfigurationConverter
061: .getConfiguration(props);
062:
063: assertEquals("This returns 'teststring'", "teststring", config
064: .getString("string"));
065: List item1 = config.getList("list");
066: assertEquals("This returns 'item 1'", "item 1", (String) item1
067: .get(0));
068:
069: assertEquals("This returns 123", 123, config.getInt("int"));
070: }
071:
072: public void testConfigurationToExtendedProperties() {
073: Configuration config = new BaseConfiguration();
074: config.setProperty("string", "teststring");
075: config.setProperty("int", "123");
076: config.addProperty("list", "item 1");
077: config.addProperty("list", "item 2");
078:
079: ExtendedProperties eprops = ConfigurationConverter
080: .getExtendedProperties(config);
081:
082: assertEquals("This returns 'teststring'", "teststring", eprops
083: .getString("string"));
084: List list = eprops.getVector("list");
085: assertEquals("This returns 'item 1'", "item 1", (String) list
086: .get(0));
087: assertEquals("This returns 123", 123, eprops.getInt("int"));
088: }
089:
090: public void testConfigurationToProperties() {
091: BaseConfiguration config = new BaseConfiguration();
092: config.addProperty("string", "teststring");
093: config.addProperty("array", "item 1");
094: config.addProperty("array", "item 2");
095: config.addProperty("interpolated", "${string}");
096: config.addProperty("interpolated-array", "${interpolated}");
097: config.addProperty("interpolated-array", "${interpolated}");
098:
099: Properties props = ConfigurationConverter.getProperties(config);
100:
101: assertNotNull("null properties", props);
102: assertEquals("'string' property", "teststring", props
103: .getProperty("string"));
104: assertEquals("'interpolated' property", "teststring", props
105: .getProperty("interpolated"));
106: assertEquals("'array' property", "item 1,item 2", props
107: .getProperty("array"));
108: assertEquals("'interpolated-array' property",
109: "teststring,teststring", props
110: .getProperty("interpolated-array"));
111:
112: // change the list delimiter
113: config.setListDelimiter(';');
114: props = ConfigurationConverter.getProperties(config);
115: assertEquals("'array' property", "item 1;item 2", props
116: .getProperty("array"));
117: }
118:
119: public void testConfigurationToMap() {
120: Configuration config = new BaseConfiguration();
121: config.addProperty("string", "teststring");
122:
123: Map map = ConfigurationConverter.getMap(config);
124:
125: assertNotNull("null map", map);
126: assertEquals("'string' property", "teststring", map
127: .get("string"));
128: }
129:
130: }
|