    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;
    20  import java.util.Properties;
    21  import java.util.Vector;
    22
    23  import junit.framework.TestCase;
    24  import org.apache.commons.collections.ExtendedProperties;
    25
    26  /***
    27   * Tests the ConfigurationConverter class.
    28   *
    29   * @author Martin Poeschl
    30   * @author Emmanuel Bourg
    31   * @version $Revision: 1.6 $, $Date: 2004/08/16 22:16:31 $
    32   */
    33  public class TestConfigurationConverter extends TestCase
    34  {
    35      public void testExtendedPropertiesToConfiguration()
    36      {
    37          ExtendedProperties eprops = new ExtendedProperties();
    38          eprops.setProperty("string", "teststring");
    39          eprops.setProperty("int", "123");
    40          eprops.addProperty("list", "item 1");
    41          eprops.addProperty("list", "item 2");
    42
    43          Configuration config = ConfigurationConverter.getConfiguration(eprops);
    44
    45          assertEquals("This returns 'teststring'", config.getString("string"), "teststring");
    46          List item1 = config.getList("list");
    47          assertEquals("This returns 'item 1'", (String) item1.get(0), "item 1");
    48
    49          Vector item2 = config.getVector("list");
    50          assertEquals("This returns 'item 1'", (String) item2.get(0), "item 1");
    51
    52          assertEquals("This returns 123", config.getInt("int"), 123);
    53      }
    54
    55      public void testPropertiesToConfiguration()
    56      {
    57          Properties props = new Properties();
    58          props.setProperty("string", "teststring");
    59          props.setProperty("int", "123");
    60          props.setProperty("list", "item 1, item 2");
    61
    62          Configuration config = ConfigurationConverter.getConfiguration(props);
    63
    64          assertEquals("This returns 'teststring'", config.getString("string"), "teststring");
    65          List item1 = config.getList("list");
    66          assertEquals("This returns 'item 1'", (String) item1.get(0), "item 1");
    67
    68          Vector item2 = config.getVector("list");
    69          assertEquals("This returns 'item 1'", (String) item2.get(0), "item 1");
    70
    71          assertEquals("This returns 123", config.getInt("int"), 123);
    72      }
    73
    74      public void testConfigurationToExtendedProperties()
    75      {
    76          Configuration config = new BaseConfiguration();
    77          config.setProperty("string", "teststring");
    78          config.setProperty("int", "123");
    79          config.addProperty("list", "item 1");
    80          config.addProperty("list", "item 2");
    81
    82          ExtendedProperties eprops = ConfigurationConverter.getExtendedProperties(config);
    83
    84          assertEquals("This returns 'teststring'", eprops.getString("string"), "teststring");
    85          List list = eprops.getVector("list");
    86          assertEquals("This returns 'item 1'", (String) list.get(0), "item 1");
    87          assertEquals("This returns 123", eprops.getInt("int"), 123);
    88      }
    89
    90      public void testConfigurationToProperties()
    91      {
    92          Configuration config = new BaseConfiguration();
    93          config.addProperty("string", "teststring");
    94          config.addProperty("array", "item 1");
    95          config.addProperty("array", "item 2");
    96
    97          Properties props = ConfigurationConverter.getProperties(config);
    98
    99          assertNotNull("null properties", props);
    100         assertEquals("'string' property", "teststring", props.getProperty("string"));
    101         assertEquals("'array' property", "item 1, item 2", props.getProperty("array"));
    102     }
    103
    104 }