    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 java.util.List;
    21  import java.util.Map;
    22  import java.util.Properties;
    23
    24  import junit.framework.TestCase;
    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: 439648 $, $Date: 2006-09-02 22:42:10 +0200 (Sa, 02 Sep 2006) $
    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 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          BaseConfiguration config = new BaseConfiguration();
    88          config.addProperty("string", "teststring");
    89          config.addProperty("array", "item 1");
    90          config.addProperty("array", "item 2");
    91          config.addProperty("interpolated", "${string}");
    92          config.addProperty("interpolated-array", "${interpolated}");
    93          config.addProperty("interpolated-array", "${interpolated}");
    94
    95          Properties props = ConfigurationConverter.getProperties(config);
    96
    97          assertNotNull("null properties", props);
    98          assertEquals("'string' property", "teststring", props.getProperty("string"));
    99          assertEquals("'interpolated' property", "teststring", props.getProperty("interpolated"));
    100         assertEquals("'array' property", "item 1,item 2", props.getProperty("array"));
    101         assertEquals("'interpolated-array' property", "teststring,teststring", props.getProperty("interpolated-array"));
    102
    103         // change the list delimiter
    104         config.setListDelimiter(';');
    105         props = ConfigurationConverter.getProperties(config);
    106         assertEquals("'array' property", "item 1;item 2", props.getProperty("array"));
    107     }
    108
    109     public void testConfigurationToMap()
    110     {
    111         Configuration config = new BaseConfiguration();
    112         config.addProperty("string", "teststring");
    113
    114         Map map = ConfigurationConverter.getMap(config);
    115
    116         assertNotNull("null map", map);
    117         assertEquals("'string' property", "teststring", map.get("string"));
    118     }
    119
    120 }