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.assertNotNull; 022 023 import java.util.List; 024 import java.util.Map; 025 import java.util.Properties; 026 027 import org.apache.commons.collections.ExtendedProperties; 028 import org.junit.Test; 029 030 /** 031 * Tests the ConfigurationConverter class. 032 * 033 * @author Martin Poeschl 034 * @author Emmanuel Bourg 035 * @version $Id: TestConfigurationConverter.java 1223010 2011-12-24 20:21:36Z oheger $ 036 */ 037 public class TestConfigurationConverter 038 { 039 @Test 040 public void testExtendedPropertiesToConfiguration() 041 { 042 ExtendedProperties eprops = new ExtendedProperties(); 043 eprops.setProperty("string", "teststring"); 044 eprops.setProperty("int", "123"); 045 eprops.addProperty("list", "item 1"); 046 eprops.addProperty("list", "item 2"); 047 048 Configuration config = ConfigurationConverter.getConfiguration(eprops); 049 050 assertEquals("This returns 'teststring'", "teststring", config.getString("string")); 051 List<Object> item1 = config.getList("list"); 052 assertEquals("This returns 'item 1'", "item 1", item1.get(0)); 053 054 assertEquals("This returns 123", 123, config.getInt("int")); 055 } 056 057 @Test 058 public void testPropertiesToConfiguration() 059 { 060 Properties props = new Properties(); 061 props.setProperty("string", "teststring"); 062 props.setProperty("int", "123"); 063 props.setProperty("list", "item 1, item 2"); 064 065 Configuration config = ConfigurationConverter.getConfiguration(props); 066 067 assertEquals("This returns 'teststring'", "teststring", config.getString("string")); 068 List<Object> item1 = config.getList("list"); 069 assertEquals("This returns 'item 1'", "item 1", item1.get(0)); 070 071 assertEquals("This returns 123", 123, config.getInt("int")); 072 } 073 074 @Test 075 public void testConfigurationToExtendedProperties() 076 { 077 Configuration config = new BaseConfiguration(); 078 config.setProperty("string", "teststring"); 079 config.setProperty("int", "123"); 080 config.addProperty("list", "item 1"); 081 config.addProperty("list", "item 2"); 082 083 ExtendedProperties eprops = ConfigurationConverter.getExtendedProperties(config); 084 085 assertEquals("This returns 'teststring'", "teststring", eprops.getString("string")); 086 List<?> list = eprops.getVector("list"); 087 assertEquals("This returns 'item 1'", "item 1", list.get(0)); 088 assertEquals("This returns 123", 123, eprops.getInt("int")); 089 } 090 091 @Test 092 public void testConfigurationToProperties() 093 { 094 BaseConfiguration config = new BaseConfiguration(); 095 config.addProperty("string", "teststring"); 096 config.addProperty("array", "item 1"); 097 config.addProperty("array", "item 2"); 098 config.addProperty("interpolated", "${string}"); 099 config.addProperty("interpolated-array", "${interpolated}"); 100 config.addProperty("interpolated-array", "${interpolated}"); 101 102 Properties props = ConfigurationConverter.getProperties(config); 103 104 assertNotNull("null properties", props); 105 assertEquals("'string' property", "teststring", props.getProperty("string")); 106 assertEquals("'interpolated' property", "teststring", props.getProperty("interpolated")); 107 assertEquals("'array' property", "item 1,item 2", props.getProperty("array")); 108 assertEquals("'interpolated-array' property", "teststring,teststring", props.getProperty("interpolated-array")); 109 110 // change the list delimiter 111 config.setListDelimiter(';'); 112 props = ConfigurationConverter.getProperties(config); 113 assertEquals("'array' property", "item 1;item 2", props.getProperty("array")); 114 } 115 116 /** 117 * Tests the conversion of a configuration object to properties if scalar 118 * values are involved. This test is related to CONFIGURATION-432. 119 */ 120 @Test 121 public void testConfigurationToPropertiesScalarValue() 122 { 123 BaseConfiguration config = new BaseConfiguration(); 124 config.addProperty("scalar", new Integer(42)); 125 Properties props = ConfigurationConverter.getProperties(config); 126 assertEquals("Wrong value", "42", props.getProperty("scalar")); 127 } 128 129 @Test 130 public void testConfigurationToMap() 131 { 132 Configuration config = new BaseConfiguration(); 133 config.addProperty("string", "teststring"); 134 135 Map<Object, Object> map = ConfigurationConverter.getMap(config); 136 137 assertNotNull("null map", map); 138 assertEquals("'string' property", "teststring", map.get("string")); 139 } 140 141 }