You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by eb...@apache.org on 2004/06/04 14:58:53 UTC

cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestCompositeConfiguration.java

ebourg      2004/06/04 05:58:53

  Modified:    configuration/src/java/org/apache/commons/configuration
                        CompositeConfiguration.java
               configuration/src/test/org/apache/commons/configuration
                        TestCompositeConfiguration.java
  Log:
  Fixed bug 28660, getList() now returns the list composed of the elements in the first matching configuration and the additional elements found in the in memory configuration
  
  Revision  Changes    Path
  1.12      +44 -29    jakarta-commons/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java
  
  Index: CompositeConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- CompositeConfiguration.java	1 Apr 2004 18:43:04 -0000	1.11
  +++ CompositeConfiguration.java	4 Jun 2004 12:58:53 -0000	1.12
  @@ -1,4 +1,3 @@
  -package org.apache.commons.configuration;
   /*
    * Copyright 2001-2004 The Apache Software Foundation.
    *
  @@ -15,11 +14,12 @@
    * limitations under the License.
    */
   
  +package org.apache.commons.configuration;
  +
   import java.util.ArrayList;
   import java.util.Iterator;
   import java.util.LinkedList;
   import java.util.List;
  -import java.util.ListIterator;
   
   /**
    * This Configuration class allows you to add multiple different types of Configuration
  @@ -32,8 +32,8 @@
    */
   public class CompositeConfiguration extends AbstractConfiguration
   {
  -    /** Array holding all the configuration */
  -    private LinkedList configList = new LinkedList();
  +    /** List holding all the configuration */
  +    private List configList = new LinkedList();
   
       /** 
        * Configuration that holds in memory stuff.  Inserted as first so any
  @@ -58,8 +58,8 @@
       public CompositeConfiguration(Configuration inMemoryConfiguration)
       {
           configList.clear();
  -        this.inMemoryConfiguration=inMemoryConfiguration;
  -        configList.addLast(inMemoryConfiguration);
  +        this.inMemoryConfiguration = inMemoryConfiguration;
  +        configList.add(inMemoryConfiguration);
       }    
   
       public void addConfiguration(Configuration config)
  @@ -73,6 +73,7 @@
               configList.add(configList.indexOf(inMemoryConfiguration), config);
           }
       }
  +
       public void removeConfiguration(Configuration config)
       {
           // Make sure that you can't remove the inMemoryConfiguration from
  @@ -82,18 +83,19 @@
               configList.remove(config);
           }
       }
  +
       public int getNumberOfConfigurations()
       {
           return configList.size();
       }
  +
       public void clear()
       {
           configList.clear();
           // recreate the in memory configuration
           inMemoryConfiguration = new BaseConfiguration();
  -        configList.addLast(inMemoryConfiguration);
  +        configList.add(inMemoryConfiguration);
       }
  -
       
       /**
        * Add this property to the inmemory Configuration.
  @@ -115,26 +117,29 @@
        */
       protected Object getPropertyDirect(String key) 
       {
  -        Configuration firstMatchingConfiguration=null;
  -        for (ListIterator i = configList.listIterator(); i.hasNext();)
  +        Configuration firstMatchingConfiguration = null;
  +        for (Iterator i = configList.iterator(); i.hasNext();)
           {
               Configuration config = (Configuration) i.next();
               if (config.containsKey(key))
               {
  -                firstMatchingConfiguration= config;
  +                firstMatchingConfiguration = config;
                   break;
               }
  +        }
   
  +        if (firstMatchingConfiguration != null)
  +        {
  +            return firstMatchingConfiguration.getProperty(key);
  +        }
  +        else
  +        {
  +            return null;
           }
  -       if(firstMatchingConfiguration!=null){
  -           return firstMatchingConfiguration.getProperty(key);
  -       }
  -       else {
  -           return null;
  -       }
           /*throw new NoSuchElementException(
               '\'' + key + "' doesn't map to an existing object");*/
       }
  +
       /**
        * Get the list of the keys contained in the configuration
        * repository.
  @@ -144,7 +149,7 @@
       public Iterator getKeys()
       {
           List keys = new ArrayList();
  -        for (ListIterator i = configList.listIterator(); i.hasNext();)
  +        for (Iterator i = configList.iterator(); i.hasNext();)
           {
               Configuration config = (Configuration) i.next();
               for (Iterator j = config.getKeys(); j.hasNext();)
  @@ -158,6 +163,7 @@
           }
           return keys.iterator();
       }
  +
       /**
        * Get the list of the keys contained in the configuration
        * repository.
  @@ -167,7 +173,7 @@
       public Iterator getKeys(String key)
       {
           List keys = new ArrayList();
  -        for (ListIterator i = configList.listIterator(); i.hasNext();)
  +        for (Iterator i = configList.iterator(); i.hasNext();)
           {
               Configuration config = (Configuration) i.next();
               for (Iterator j = config.getKeys(key); j.hasNext();)
  @@ -185,7 +191,7 @@
       public boolean isEmpty()
       {
           boolean isEmpty = true;
  -        for (ListIterator i = configList.listIterator(); i.hasNext();)
  +        for (Iterator i = configList.iterator(); i.hasNext();)
           {
               Configuration config = (Configuration) i.next();
               if (!config.isEmpty())
  @@ -195,6 +201,7 @@
           }
           return isEmpty;
       }
  +
       /**
        *  Gets a property from the configuration.
        *
  @@ -206,6 +213,7 @@
       {
           return getPropertyDirect(key);
       }
  +
       /**
        * Set a property, this will replace any previously
        * set values. Set values is implicitly a call
  @@ -219,6 +227,7 @@
           clearProperty(key);
           addProperty(key, value);
       }
  +
       /**
        * Clear a property in the configuration.
        *
  @@ -226,18 +235,19 @@
        */
       public void clearProperty(String key)
       {
  -        for (ListIterator i = configList.listIterator(); i.hasNext();)
  +        for (Iterator i = configList.iterator(); i.hasNext();)
           {
               Configuration config = (Configuration) i.next();
               config.clearProperty(key);
           }
       }
  +
       /**
        * check if the configuration contains the key
        */
       public boolean containsKey(String key)
       {
  -        for (ListIterator i = configList.listIterator(); i.hasNext();)
  +        for (Iterator i = configList.iterator(); i.hasNext();)
           {
               Configuration config = (Configuration) i.next();
               if (config.containsKey(key))
  @@ -260,15 +270,20 @@
       {
           List list = new ArrayList();
   
  -        for (ListIterator li = configList.listIterator(); li.hasNext();)
  +        // add all elements from the first configuration containing the requested key
  +        Iterator it = configList.iterator();
  +        while (it.hasNext() && list.isEmpty())
           {
  -            Configuration config = (Configuration) li.next();
  -            if (config.containsKey(key))
  +            Configuration config = (Configuration) it.next();
  +            if (config != inMemoryConfiguration && config.containsKey(key))
               {
                   list.addAll(config.getList(key));
               }
           }
   
  +        // add all elements from the in memory configuration
  +        list.addAll(inMemoryConfiguration.getList(key, null));
  +
           return list;
       }
   
  @@ -285,12 +300,11 @@
       {
           List list = getList(key);
   
  -        return (list.size() == 0) ? defaultValue : list;
  +        return list.isEmpty() ? defaultValue : list;
       }
       
       /**
  -     * Get an array of strings associated with the given configuration
  -     * key.
  +     * Get an array of strings associated with the given configuration key.
        *
        * @param key The configuration key.
        * @return The associated string array if key is found.
  @@ -308,6 +322,7 @@
       {
           return (Configuration) configList.get(index);
       }
  +
       /**
        * @return Returns the inMemoryConfiguration.
        */
  
  
  
  1.8       +43 -15    jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java
  
  Index: TestCompositeConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestCompositeConfiguration.java	1 Apr 2004 18:43:03 -0000	1.7
  +++ TestCompositeConfiguration.java	4 Jun 2004 12:58:53 -0000	1.8
  @@ -1,5 +1,3 @@
  -package org.apache.commons.configuration;
  -
   /*
    * Copyright 2001-2004 The Apache Software Foundation.
    *
  @@ -16,6 +14,8 @@
    * limitations under the License.
    */
   
  +package org.apache.commons.configuration;
  +
   import java.io.File;
   import java.util.ArrayList;
   import java.util.Iterator;
  @@ -35,6 +35,7 @@
       protected BasePropertiesConfiguration conf2;
       protected DOM4JConfiguration dom4jConf;
       protected CompositeConfiguration cc;
  +
       /** The File that we test with */
       private String testProperties = new File("conf/test.properties").getAbsolutePath();
       private String testProperties2 = new File("conf/test2.properties").getAbsolutePath();
  @@ -50,7 +51,6 @@
   
       public void testAddRemoveConfigurations() throws Exception
       {
  -
           cc.addConfiguration(conf1);
           assertEquals(2, cc.getNumberOfConfigurations());
           cc.addConfiguration(conf1);
  @@ -165,7 +165,6 @@
        */
       public void testClearingProperty() throws Exception
       {
  -
           cc.addConfiguration(conf1);
           cc.addConfiguration(dom4jConf);
           cc.clearProperty("test.short");
  @@ -178,24 +177,21 @@
        */
       public void testAddingProperty() throws Exception
       {
  -
           cc.addConfiguration(conf1);
           cc.addConfiguration(dom4jConf);
   
  -        String [] values = cc.getStringArray("test.short");
  +        String[] values = cc.getStringArray("test.short");
   
  -        assertEquals("Number of values before add is wrong!", 2, values.length);
  +        assertEquals("Number of values before add is wrong!", 1, values.length);
           assertEquals("First Value before add is wrong", "1", values[0]);
  -        assertEquals("Second Value is wrong", "8", values[1]);
   
           cc.addProperty("test.short", "88");
   
           values = cc.getStringArray("test.short");
   
  -        assertEquals("Number of values is wrong!", 3, values.length);
  +        assertEquals("Number of values is wrong!", 2, values.length);
           assertEquals("First Value is wrong", "1", values[0]);
  -        assertEquals("Second Value is wrong", "8", values[1]);
  -        assertEquals("Third Value is wrong", "88", values[2]);
  +        assertEquals("Third Value is wrong", "88", values[1]);
       }
   
       /**
  @@ -280,7 +276,39 @@
           // we should get 0 packages here
           assertEquals(0, packages.length);
       }
  -    
  +
  +    public void testGetList()
  +    {
  +        Configuration conf1 = new BaseConfiguration();
  +        conf1.addProperty("array", "value1");
  +        conf1.addProperty("array", "value2");
  +
  +        Configuration conf2 = new BaseConfiguration();
  +        conf2.addProperty("array", "value3");
  +        conf2.addProperty("array", "value4");
  +
  +        cc.addConfiguration(conf1);
  +        cc.addConfiguration(conf2);
  +
  +        // check the composite 'array' property
  +        List list = cc.getList("array");
  +        assertNotNull("null list", list);
  +        assertEquals("list size", 2, list.size());
  +        assertTrue("'value1' not found in the list", list.contains("value1"));
  +        assertTrue("'value2' not found in the list", list.contains("value2"));
  +
  +        // add an element to the list in the composite configuration
  +        cc.addProperty("array", "value5");
  +
  +        // test the new list
  +        list = cc.getList("array");
  +        assertNotNull("null list", list);
  +        assertEquals("list size", 3, list.size());
  +        assertTrue("'value1' not found in the list", list.contains("value1"));
  +        assertTrue("'value2' not found in the list", list.contains("value2"));
  +        assertTrue("'value5' not found in the list", list.contains("value5"));
  +    }
  +
       /**
         * Tests <code>getKeys</code> preserves the order
         */
  @@ -358,7 +386,8 @@
               c.getString("XXX", "some default value"));
       }
       
  -    public void testCheckingInMemoryConfiguration() throws Exception{
  +    public void testCheckingInMemoryConfiguration() throws Exception
  +    {
           String TEST_KEY = "testKey";
           Configuration defaults = new PropertiesConfiguration();
           defaults.setProperty(TEST_KEY,"testValue");
  @@ -379,6 +408,5 @@
           assertTrue(foundTestKey);
           testConfiguration.clearProperty(TEST_KEY);
           assertFalse(testConfiguration.containsKey(TEST_KEY));
  -        
       }    
   }
  
  
  

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