View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  
27  import org.apache.commons.collections.ExtendedProperties;
28  import org.junit.Test;
29  
30  /**
31   * Tests the ConfigurationConverter class.
32   *
33   * @author Martin Poeschl
34   * @author Emmanuel Bourg
35   * @version $Id: TestConfigurationConverter.java 1223010 2011-12-24 20:21:36Z oheger $
36   */
37  public class TestConfigurationConverter
38  {
39      @Test
40      public void testExtendedPropertiesToConfiguration()
41      {
42          ExtendedProperties eprops = new ExtendedProperties();
43          eprops.setProperty("string", "teststring");
44          eprops.setProperty("int", "123");
45          eprops.addProperty("list", "item 1");
46          eprops.addProperty("list", "item 2");
47  
48          Configuration config = ConfigurationConverter.getConfiguration(eprops);
49  
50          assertEquals("This returns 'teststring'", "teststring", config.getString("string"));
51          List<Object> item1 = config.getList("list");
52          assertEquals("This returns 'item 1'", "item 1", item1.get(0));
53  
54          assertEquals("This returns 123", 123, config.getInt("int"));
55      }
56  
57      @Test
58      public void testPropertiesToConfiguration()
59      {
60          Properties props = new Properties();
61          props.setProperty("string", "teststring");
62          props.setProperty("int", "123");
63          props.setProperty("list", "item 1, item 2");
64  
65          Configuration config = ConfigurationConverter.getConfiguration(props);
66  
67          assertEquals("This returns 'teststring'", "teststring", config.getString("string"));
68          List<Object> item1 = config.getList("list");
69          assertEquals("This returns 'item 1'", "item 1", item1.get(0));
70  
71          assertEquals("This returns 123", 123, config.getInt("int"));
72      }
73  
74      @Test
75      public void testConfigurationToExtendedProperties()
76      {
77          Configuration config = new BaseConfiguration();
78          config.setProperty("string", "teststring");
79          config.setProperty("int", "123");
80          config.addProperty("list", "item 1");
81          config.addProperty("list", "item 2");
82  
83          ExtendedProperties eprops = ConfigurationConverter.getExtendedProperties(config);
84  
85          assertEquals("This returns 'teststring'", "teststring", eprops.getString("string"));
86          List<?> list = eprops.getVector("list");
87          assertEquals("This returns 'item 1'", "item 1", list.get(0));
88          assertEquals("This returns 123", 123, eprops.getInt("int"));
89      }
90  
91      @Test
92      public void testConfigurationToProperties()
93      {
94          BaseConfiguration config = new BaseConfiguration();
95          config.addProperty("string", "teststring");
96          config.addProperty("array", "item 1");
97          config.addProperty("array", "item 2");
98          config.addProperty("interpolated", "${string}");
99          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 }