You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ep...@apache.org on 2004/08/12 18:06:01 UTC

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

epugh       2004/08/12 09:06:01

  Modified:    configuration/src/java/org/apache/commons/configuration
                        ConfigurationConverter.java
               configuration/xdocs changes.xml
  Added:       configuration/src/java/org/apache/commons/configuration
                        ConfigurationMap.java ConfigurationSet.java
               configuration/src/test/org/apache/commons/configuration
                        TestConfigurationMap.java
  Log:
  New ConfigurationSet and ConfigurationMap helper classes
  
  Revision  Changes    Path
  1.7       +13 -1     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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ConfigurationConverter.java	24 Jun 2004 12:35:15 -0000	1.6
  +++ ConfigurationConverter.java	12 Aug 2004 16:06:01 -0000	1.7
  @@ -19,6 +19,7 @@
   import java.util.Enumeration;
   import java.util.Iterator;
   import java.util.List;
  +import java.util.Map;
   import java.util.Properties;
   import java.util.Vector;
   
  @@ -144,4 +145,15 @@
   
           return props;
       }
  +
  +    /**
  +     * Convert a Configuration class into a Map class.
  +     *
  +     * @param config Configuration object to convert
  +     * @return Map created from the Configuration
  +     */
  +    public static Map getMap(Configuration config) {
  +        return new ConfigurationMap(config);
  +    }
  +
   }
  
  
  
  1.1                  jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationMap.java
  
  Index: ConfigurationMap.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.configuration;
  
  import java.util.AbstractMap;
  import java.util.Set;
  
  import org.apache.commons.configuration.Configuration;
  
  /**
   * <p>The <code>ConfigurationMap</code> wraps a
   * configuration-collection
   * {@link org.apache.commons.configuration.Configuration}
   * instance to provide a <code>Map</code> interface.</p>
   * 
   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
   */
  public class ConfigurationMap extends AbstractMap {
  
      /**
       * The <code>Configuration</code> wrapped by this class.
       */
      Configuration configuration;
  
      /**
       * Creates a new instance of a <code>ConfigurationMap</code>
       * that wraps the specified <code>Configuration</code>
       * instance.
       * @param configuration <code>Configuration</code>
       * instance.
       */
      public ConfigurationMap(Configuration configuration) {
          this.configuration = configuration;
      }
  
  	/**
  	 * @see java.util.Map#entrySet()
  	 */
  	public Set entrySet() {
  		return new ConfigurationSet(configuration);
  	}
  
      /**
       * @see java.util.Map#put(java.lang.Object, java.lang.Object)
       */
      public Object put(Object key, Object value) {
          Object old = configuration.getProperty((String) key);
          configuration.setProperty((String) key,value);
          return old;
      }
  
  }
  
  
  
  1.1                  jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationSet.java
  
  Index: ConfigurationSet.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.configuration;
  
  import java.util.AbstractSet;
  import java.util.Iterator;
  import java.util.Map;
  
  import org.apache.commons.configuration.Configuration;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
   */
  class ConfigurationSet extends AbstractSet {
  
      private final static Log log = LogFactory.getLog(ConfigurationSet.class);
  
      class Entry implements Map.Entry {
  
          Object key;
          Object value;
          
          public Entry(Object key) {
              this.key = key;
          }
  
          public Object getKey() {
              return key;
          }
  
          public Object getValue() {
              return configuration.getProperty((String) key);
          }
    
          public Object setValue(Object value) {
              Object old = getValue();
              configuration.setProperty((String) key, value);
              return old;
          }
          
      }
  
      class ConfigurationEntrySetIterator implements Iterator {
  
          Iterator keys;
  
          public ConfigurationEntrySetIterator() {
          	keys = configuration.getKeys();
          }
  
          public boolean hasNext() {
              return keys.hasNext();
           }
  
          public Object next() {
              return new Entry(keys.next());
           }
  
           public void remove() {
           	keys.remove();
           }
  
      }
  
      Configuration configuration;
  
      public ConfigurationSet(Configuration configuration) {
          if(log.isTraceEnabled()) log.trace("ConfigurationEntrySet("+configuration+")");
          this.configuration = configuration;
      }
  
      /**
  	 * @see java.util.Collection#size()
  	 */
  	public int size() {
          if(log.isTraceEnabled()) log.trace("size()");
          int count = 0;
  		Iterator iterator = configuration.getKeys();
          while(iterator.hasNext()) count++;
          return count;
  	}
  
  	/**
  	 * @see java.util.Collection#iterator()
  	 */
  	public Iterator iterator() {
          if(log.isTraceEnabled()) log.trace("iterator()");
  		return new ConfigurationEntrySetIterator();
  	}
  
  }
  
  
  
  1.29      +1 -0      jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- changes.xml	12 Aug 2004 15:45:21 -0000	1.28
  +++ changes.xml	12 Aug 2004 16:06:01 -0000	1.29
  @@ -7,6 +7,7 @@
   
     <body>
       <release version="1.0rc1" date="2004-06-??">
  +      <action dev="epugh" type="add" due-to="Ricardo Gladwell" issue="29611">new ConfigurationMap and ConfigurationSet</action>        
         <action dev="epugh" type="fix" due-to="Ricardo Gladwell" issue="30598">Problem adding property XMLConfiguration</action>        
         <action dev="epugh" type="remove">ConfigurationXMLDocument removed until post 1.0.</action>
   	  <action dev="epugh" type="fix" issue="29734">DatabaseConfiguration doesn't support List properties.</action>
  
  
  
  1.1                  jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationMap.java
  
  Index: TestConfigurationMap.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License")
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.commons.configuration;
  
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
   */
  public class TestConfigurationMap extends TestCase
  {
  
      ConfigurationMap map;
  
      String[] properties = {
              "booleanProperty",
              "booleanSecond",
              "doubleProperty",
              "floatProperty",
              "intProperty",
              "longProperty",
              "mappedProperty.key1",
              "mappedProperty.key2",
              "mappedProperty.key3",
              "mappedIntProperty.key1",
              "shortProperty",
              "stringProperty"
      };
      
      Object[] values = {
              Boolean.TRUE,
              Boolean.TRUE,
              new Double(Double.MAX_VALUE),
              new Float(Float.MAX_VALUE),
              new Integer(Integer.MAX_VALUE),
              new Long(Long.MAX_VALUE),
              "First Value",
              "Second Value",
              "Third Value",
              new Integer(Integer.MAX_VALUE),
              new Short(Short.MAX_VALUE),
              "This is a string"
      };
  
      /**
       * Construct a new instance of this test case.
       * @param name Name of the test case
       */
      public TestConfigurationMap(String name)
      {
          super(name);
      }
  
      /**
       * Set up instance variables required by this test case.
       */
      public void setUp() throws Exception
      {
          BaseConfiguration configuration = new BaseConfiguration();
          for(int i = 0; i < properties.length ; i++)
              configuration.setProperty(properties[i], values[i]);
          map = new ConfigurationMap(configuration);
      }
  
      /**
       * Return the tests included in this test suite.
       */
      public static Test suite()
      {
          return (new TestSuite(TestConfigurationMap.class));
      }
  
      /**
       * Tear down instance variables required by this test case.
       */
      public void tearDown()
      {
          map = null;
      }
  
      /**
       * Class under test for Set entrySet()
       */
      public void testEntrySet()
      {
          Set entrySet = map.entrySet();
          Iterator iterator = entrySet.iterator();
          while(iterator.hasNext()) {
              Object object = iterator.next();
              assertTrue("Entry set iterator did not return EntrySet object, returned "
                      + object.getClass().getName(), object instanceof Map.Entry);
              Map.Entry entry = (Map.Entry) object;
              boolean found = false;
              for(int i = 0; i < properties.length; i++) {
                  if(entry.getKey().equals(properties[i])) {
                      assertEquals("Incorrect value for property " +
                              properties[i],values[i],entry.getValue());
                      found = true;
                      break;
                  }
              }
              assertTrue("Could not find property " + entry.getKey(),found);
          }
      }
  
      /**
       * Class under test for Object put(Object, Object)
       */
      public void testPut()
      {
          for(int i = 0; i < properties.length; i++) {
              Object object = map.put(properties[i], values[i]);
              assertNotNull("Returned null from put.",object);
              assertEquals("Returned wrong result.",values[i],object);
              object = map.get(properties[i]);
              assertNotNull("Returned null from get.",object);
              assertEquals("Returned wrong result.",values[i],object);
          }
      }
  
  }
  
  
  

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