KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > TestConfigurationConverter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.configuration;
18
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.apache.commons.collections.ExtendedProperties;
26
27 /**
28  * Tests the ConfigurationConverter class.
29  *
30  * @author Martin Poeschl
31  * @author Emmanuel Bourg
32  * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
33  */

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 JavaDoc item1 = config.getList("list");
48         assertEquals("This returns 'item 1'", "item 1", (String JavaDoc) item1.get(0));
49
50         assertEquals("This returns 123", 123, config.getInt("int"));
51     }
52
53     public void testPropertiesToConfiguration()
54     {
55         Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc item1 = config.getList("list");
64         assertEquals("This returns 'item 1'", "item 1", (String JavaDoc) 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 JavaDoc list = eprops.getVector("list");
81         assertEquals("This returns 'item 1'", "item 1", (String JavaDoc) 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 JavaDoc 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 JavaDoc map = ConfigurationConverter.getMap(config);
105
106         assertNotNull("null map", map);
107         assertEquals("'string' property", "teststring", map.get("string"));
108     }
109
110 }
111
Popular Tags