commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject cvs commit: jakarta-commons/configuration/xdocs changes.xml
Date Wed, 16 Jun 2004 15:17:09 GMT
ebourg      2004/06/16 08:17:09

  Modified:    configuration/src/java/org/apache/commons/configuration
                        ConfigurationConverter.java
               configuration/src/test/org/apache/commons/configuration
                        TestConfigurationConverter.java
               configuration/xdocs changes.xml
  Log:
  List values are now properly stored as comma separated values in the Properties object returned
by ConfigurationConverter.getProperties() (Bug 29607)
  
  Revision  Changes    Path
  1.5       +57 -32    jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationConverter.java
  
  Index: ConfigurationConverter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationConverter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ConfigurationConverter.java	2 Jun 2004 16:42:24 -0000	1.4
  +++ ConfigurationConverter.java	16 Jun 2004 15:17:09 -0000	1.5
  @@ -1,5 +1,3 @@
  -package org.apache.commons.configuration;
  -
   /*
    * Copyright 2001-2004 The Apache Software Foundation.
    *
  @@ -16,71 +14,83 @@
    * limitations under the License.
    */
   
  +package org.apache.commons.configuration;
  +
   import java.util.Enumeration;
   import java.util.Iterator;
  -import java.util.Properties;
   import java.util.List;
  +import java.util.Properties;
   import java.util.Vector;
   
   import org.apache.commons.collections.ExtendedProperties;
   
  -
   /**
  - * Configuration converter. <br>
  - * Helper class to convert between Configuration, ExtendedProperties and
  - * standard Properties.
  + * Configuration converter. Helper class to convert between Configuration,
  + * ExtendedProperties and standard Properties.
    *
  - * @version $Id$
  + * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
  + * @version $Revision$, $Date$
    */
   public class ConfigurationConverter
   {
       /**
        * Convert a ExtendedProperties class into a Configuration class.
        *
  -     * @param ep ExtendedProperties object to convert
  +     * @param eprops ExtendedProperties object to convert
        * @return Configuration created from the ExtendedProperties
        */
  -    public static Configuration getConfiguration(ExtendedProperties ep)
  +    public static Configuration getConfiguration(ExtendedProperties eprops)
       {
           Configuration config = new BaseConfiguration();
  -        for (Iterator i = ep.getKeys(); i.hasNext();)
  +
  +        Iterator keys = eprops.getKeys();
  +
  +        while (keys.hasNext())
           {
  -            String key = (String) i.next();
  -            config.setProperty(key, ep.getProperty(key));
  +            String key = (String) keys.next();
  +            config.setProperty(key, eprops.getProperty(key));
           }
  +
           return config;
       }
   
       /**
  -     * Convert a standard properties class into a configuration class.
  +     * Convert a standard Properties class into a configuration class.
        *
  -     * @param p properties object to convert
  +     * @param props properties object to convert
        * @return Configuration configuration created from the Properties
        */
  -    public static Configuration getConfiguration(Properties p)
  +    public static Configuration getConfiguration(Properties props)
       {
           Configuration config = new BaseConfiguration();
  -        for (Enumeration e = p.keys(); e.hasMoreElements();)
  +
  +        Enumeration keys = props.keys();
  +
  +        while (keys.hasMoreElements())
           {
  -            String key = (String) e.nextElement();
  -            config.setProperty(key, p.getProperty(key));
  +            String key = (String) keys.nextElement();
  +            config.setProperty(key, props.getProperty(key));
           }
  +
           return config;
       }
   
       /**
        * Convert a Configuration class into a ExtendedProperties class.
        *
  -     * @param c Configuration object to convert
  +     * @param config Configuration object to convert
        * @return ExtendedProperties created from the Configuration
        */
  -    public static ExtendedProperties getExtendedProperties(Configuration c)
  +    public static ExtendedProperties getExtendedProperties(Configuration config)
       {
           ExtendedProperties props = new ExtendedProperties();
  -        for (Iterator i = c.getKeys(); i.hasNext();)
  +
  +        Iterator keys = config.getKeys();
  +
  +        while (keys.hasNext())
           {
  -            String key = (String) i.next();
  -            Object property = c.getProperty(key);
  +            String key = (String) keys.next();
  +            Object property = config.getProperty(key);
   
               // turn lists into vectors
               if (property instanceof List)
  @@ -90,26 +100,41 @@
   
               props.setProperty(key, property);
           }
  +
           return props;
       }
   
       /**
  -     * Convert a Configuration class into a Properties class. Multvalue keys
  -     * will be collapsed by {@link Configuration#getString}.
  +     * Convert a Configuration class into a Properties class. Multivalue keys
  +     * will be collapsed into comma separated values.
        *
  -     * @param c Configuration object to convert
  +     * @param config Configuration object to convert
        * @return Properties created from the Configuration
        */
  -    public static Properties getProperties(Configuration c)
  +    public static Properties getProperties(Configuration config)
       {
           Properties props = new Properties();
   
  -        Iterator iter = c.getKeys();
  +        Iterator keys = config.getKeys();
   
  -        while (iter.hasNext())
  +        while (keys.hasNext())
           {
  -            String key = (String) iter.next();
  -            props.setProperty(key, c.getString(key));
  +            String key = (String) keys.next();
  +            List list = config.getList(key);
  +
  +            // turn lists into a string
  +            StringBuffer property = new StringBuffer();
  +            Iterator it = list.iterator();
  +            while (it.hasNext())
  +            {
  +                property.append(String.valueOf(it.next()));
  +                if (it.hasNext())
  +                {
  +                    property.append(", ");
  +                }
  +            }
  +
  +            props.setProperty(key, property.toString());
           }
   
           return props;
  
  
  
  1.5       +60 -27    jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationConverter.java
  
  Index: TestConfigurationConverter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationConverter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestConfigurationConverter.java	27 Feb 2004 17:41:34 -0000	1.4
  +++ TestConfigurationConverter.java	16 Jun 2004 15:17:09 -0000	1.5
  @@ -1,5 +1,3 @@
  -package org.apache.commons.configuration;
  -
   /*
    * Copyright 2001-2004 The Apache Software Foundation.
    *
  @@ -16,47 +14,82 @@
    * limitations under the License.
    */
   
  -import java.util.ArrayList;
  +package org.apache.commons.configuration;
  +
   import java.util.List;
  +import java.util.Properties;
   
   import junit.framework.TestCase;
   import org.apache.commons.collections.ExtendedProperties;
   
  -
   /**
  - * Tests the ConfigurationConverter class
  + * Tests the ConfigurationConverter class.
    *
  - * @version $Id$
  + * @author Martin Poeschl
  + * @author Emmanuel Bourg
  + * @version $Revision$, $Date$
    */
   public class TestConfigurationConverter extends TestCase
   {
  -    protected Configuration config = new BaseConfiguration();
  +    public void testExtendedPropertiesToConfiguration()
  +    {
  +        ExtendedProperties eprops = new ExtendedProperties();
  +        eprops.setProperty("string", "teststring");
  +        eprops.setProperty("int", "123");
  +        eprops.addProperty("list", "item 1");
  +        eprops.addProperty("list", "item 2");
  +
  +        Configuration config = ConfigurationConverter.getConfiguration(eprops);
  +
  +        assertEquals("This returns 'teststring'", config.getString("string"), "teststring");
  +        List item1 = config.getList("list");
  +        assertEquals("This returns 'item 1'", (String) item1.get(0), "item 1");
  +        assertEquals("This returns 123", config.getInt("int"), 123);
  +    }
  +
  +    public void testPropertiesToConfiguration()
  +    {
  +        Properties props = new Properties();
  +        props.setProperty("string", "teststring");
  +        props.setProperty("int", "123");
  +        props.setProperty("list", "item 1, item 2");
  +
  +        Configuration config = ConfigurationConverter.getConfiguration(props);
  +
  +        assertEquals("This returns 'teststring'", config.getString("string"), "teststring");
  +        List item1 = config.getList("list");
  +        assertEquals("This returns 'item 1'", (String) item1.get(0), "item 1");
  +        assertEquals("This returns 123", config.getInt("int"), 123);
  +    }
   
  -    public void testConverter()
  +    public void testConfigurationToExtendedProperties()
       {
  +        Configuration config = new BaseConfiguration();
           config.setProperty("string", "teststring");
           config.setProperty("int", "123");
  -        List list = new ArrayList();
  -        list.add("item 1");
  -        list.add("item 2");
  -        config.setProperty("list", list);
  -
  -        ExtendedProperties ep = ConfigurationConverter.getExtendedProperties(config);
  -
  +        config.addProperty("list", "item 1");
  +        config.addProperty("list", "item 2");
   
  -        assertEquals("This returns 'teststring'", ep.getString("string"),
  -                "teststring");
  -        List v = ep.getVector("list");
  -        assertEquals("This returns 'item 1'", (String) v.get(0), "item 1");
  -        assertEquals("This returns 123", ep.getInt("int"), 123);
  -
  -        Configuration c = ConfigurationConverter.getConfiguration(ep);
  +        ExtendedProperties eprops = ConfigurationConverter.getExtendedProperties(config);
   
  +        assertEquals("This returns 'teststring'", eprops.getString("string"), "teststring");
  +        List list = eprops.getVector("list");
  +        assertEquals("This returns 'item 1'", (String) list.get(0), "item 1");
  +        assertEquals("This returns 123", eprops.getInt("int"), 123);
  +    }
   
  -        assertEquals("This returns 'teststring'", c.getString("string"),
  -                "teststring");
  -        List v1 = c.getList("list");
  -        assertEquals("This returns 'item 1'", (String) v1.get(0), "item 1");
  -        assertEquals("This returns 123", c.getInt("int"), 123);
  +    public void testConfigurationToProperties()
  +    {
  +        Configuration config = new BaseConfiguration();
  +        config.addProperty("string", "teststring");
  +        config.addProperty("array", "item 1");
  +        config.addProperty("array", "item 2");
  +
  +        Properties props = ConfigurationConverter.getProperties(config);
  +
  +        assertNotNull("null properties", props);
  +        assertEquals("'string' property", "teststring", props.getProperty("string"));
  +        assertEquals("'array' property", "item 1, item 2", props.getProperty("array"));
       }
  +
   }
  
  
  
  1.22      +5 -0      jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- changes.xml	15 Jun 2004 15:53:58 -0000	1.21
  +++ changes.xml	16 Jun 2004 15:17:09 -0000	1.22
  @@ -7,6 +7,11 @@
   
     <body>
       <release version="1.0rc1" date="2004-06-??">
  +      <action dev="ebourg" type="fix">
  +        List values are now properly stored as comma separated values in the
  +        Properties object returned by ConfigurationConverter.getProperties()
  +        (Bug 29607)
  +      </action>
         <action dev="ebourg" type="update">
           Introduced a ConversionException thrown when the value of a property is
           not compatible the type requested. It replaces the ClassCastException
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message