1 16 17 package org.apache.commons.configuration; 18 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import junit.framework.TestCase; 24 25 import org.apache.commons.collections.ExtendedProperties; 26 27 34 public class TestConfigurationConverter extends TestCase 35 { 36 public void testExtendedPropertiesToConfiguration() 37 { 38 ExtendedProperties eprops = new ExtendedProperties(); 39 eprops.setProperty("string", "teststring"); 40 eprops.setProperty("int", "123"); 41 eprops.addProperty("list", "item 1"); 42 eprops.addProperty("list", "item 2"); 43 44 Configuration config = ConfigurationConverter.getConfiguration(eprops); 45 46 assertEquals("This returns 'teststring'", "teststring", config.getString("string")); 47 List item1 = config.getList("list"); 48 assertEquals("This returns 'item 1'", "item 1", (String ) item1.get(0)); 49 50 assertEquals("This returns 123", 123, config.getInt("int")); 51 } 52 53 public void testPropertiesToConfiguration() 54 { 55 Properties props = new Properties (); 56 props.setProperty("string", "teststring"); 57 props.setProperty("int", "123"); 58 props.setProperty("list", "item 1, item 2"); 59 60 Configuration config = ConfigurationConverter.getConfiguration(props); 61 62 assertEquals("This returns 'teststring'", "teststring", config.getString("string")); 63 List item1 = config.getList("list"); 64 assertEquals("This returns 'item 1'", "item 1", (String ) item1.get(0)); 65 66 assertEquals("This returns 123", 123, config.getInt("int")); 67 } 68 69 public void testConfigurationToExtendedProperties() 70 { 71 Configuration config = new BaseConfiguration(); 72 config.setProperty("string", "teststring"); 73 config.setProperty("int", "123"); 74 config.addProperty("list", "item 1"); 75 config.addProperty("list", "item 2"); 76 77 ExtendedProperties eprops = ConfigurationConverter.getExtendedProperties(config); 78 79 assertEquals("This returns 'teststring'", "teststring", eprops.getString("string")); 80 List list = eprops.getVector("list"); 81 assertEquals("This returns 'item 1'", "item 1", (String ) list.get(0)); 82 assertEquals("This returns 123", 123, eprops.getInt("int")); 83 } 84 85 public void testConfigurationToProperties() 86 { 87 Configuration config = new BaseConfiguration(); 88 config.addProperty("string", "teststring"); 89 config.addProperty("array", "item 1"); 90 config.addProperty("array", "item 2"); 91 92 Properties props = ConfigurationConverter.getProperties(config); 93 94 assertNotNull("null properties", props); 95 assertEquals("'string' property", "teststring", props.getProperty("string")); 96 assertEquals("'array' property", "item 1, item 2", props.getProperty("array")); 97 } 98 99 public void testConfigurationToMap() 100 { 101 Configuration config = new BaseConfiguration(); 102 config.addProperty("string", "teststring"); 103 104 Map map = ConfigurationConverter.getMap(config); 105 106 assertNotNull("null map", map); 107 assertEquals("'string' property", "teststring", map.get("string")); 108 } 109 110 } 111 | Popular Tags |