commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henn...@apache.org
Subject cvs commit: jakarta-commons/configuration/xdocs changes.xml
Date Sun, 19 Sep 2004 22:01:50 GMT
henning     2004/09/19 15:01:50

  Modified:    configuration/src/java/org/apache/commons/configuration
                        AbstractConfiguration.java
                        CompositeConfiguration.java
               configuration/src/test/org/apache/commons/configuration
                        TestBaseConfiguration.java
                        TestCompositeConfiguration.java
                        TestJNDIEnvironmentValues.java
               configuration/xdocs changes.xml
  Added:       configuration/src/test/org/apache/commons/configuration
                        TestNullCompositeConfiguration.java
                        TestNullJNDIEnvironmentValues.java
  Log:
  - Went ahead and added the proposted "throwExceptionOnMissing"
    property to AbstractConfiguration.
  
  - Changed the default behaviour back (!) to return null values unless
    this property has been set to true. This actually hits only three
    methods: getString() (which is the bad one), getBigInteger() and
    getBigDecimal(). The latter two could even be exempted from this
    property, leaving only getString(). But e.g. Turbine relies on
    this returning null for missing values.
  
    The internal implementation is
  
    foo = getString(key, null);
    if (foo == null) throw NoSuchElementException(...
  
    so that using a decorator for this would really be possible because
    you can't configure null as a valid return value. Might need more
    discussion.
  
  - Reworked all the Unit tests to explicitly set the throwExceptionOnMissing
    property. Where tests behave differently, added additional test classes.
  
  TODO: like commons-logging, commons-configuration could read a properties
  file from the classpath (commons-configuration.properties) with the default
  settings of the throwExceptionOnMissing flag.
  
  Revision  Changes    Path
  1.22      +45 -5     jakarta-commons/configuration/src/java/org/apache/commons/configuration/AbstractConfiguration.java
  
  Index: AbstractConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/AbstractConfiguration.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- AbstractConfiguration.java	16 Sep 2004 22:35:35 -0000	1.21
  +++ AbstractConfiguration.java	19 Sep 2004 22:01:49 -0000	1.22
  @@ -55,6 +55,12 @@
       /** how big the initial arraylist for splitting up name value pairs */
       private static final int INITIAL_LIST_SIZE = 2;
   
  +    /** 
  +     * Whether the configuration should throw NoSuchElementExceptions or simply return
null
  +     * when a property does not exist. Defaults to return null.
  +     */
  +    private boolean throwExceptionOnMissing = false;
  +
       /**
        * For configurations extending AbstractConfiguration, allow them to
        * change the delimiter from the default comma (",").
  @@ -71,6 +77,28 @@
       public static char getDelimiter(){
           return AbstractConfiguration.DELIMITER;
       }
  +
  +    /**
  +     * If set to false, missing elements return null if possible (for objects).
  +     * 
  +     * @param throwExceptionOnMissing The new value for the property
  +     */
  +    public void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
  +    {
  +        this.throwExceptionOnMissing = throwExceptionOnMissing;
  +    }
  +
  +    /**
  +     * Returns true if missing values throw Exceptions.
  +     *
  +     * @return true if missing values throw Exceptions
  +     */
  +    public boolean isThrowExceptionOnMissing()
  +    {
  +        return throwExceptionOnMissing;
  +    }
  +
  +
       /**
        * {@inheritDoc}
        */
  @@ -802,11 +830,15 @@
           {
               return number;
           }
  -        else
  +        else if(throwExceptionOnMissing)
           {
               throw new NoSuchElementException(
                   '\'' + key + "' doesn't map to an existing object");
           }
  +        else
  +        {
  +            return null;
  +        }
       }
   
       /**
  @@ -852,10 +884,14 @@
           {
               return number;
           }
  -        else
  +        else if(throwExceptionOnMissing)
           {
               throw new NoSuchElementException(
  -                '\'' + key + "' doesn't map to an existing object");
  +                    '\'' + key + "' doesn't map to an existing object");
  +        }
  +        else
  +        {
  +            return null;
           }
       }
   
  @@ -903,10 +939,14 @@
           {
               return s;
           }
  -        else
  +        else if(throwExceptionOnMissing)
           {
               throw new NoSuchElementException(
                   '\'' + key + "' doesn't map to an existing object");
  +        }
  +        else
  +        {
  +            return null;
           }
       }
   
  
  
  
  1.18      +7 -1      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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- CompositeConfiguration.java	16 Aug 2004 22:16:31 -0000	1.17
  +++ CompositeConfiguration.java	19 Sep 2004 22:01:50 -0000	1.18
  @@ -81,6 +81,11 @@
               // configuration add their keys at the end of the configuration and
               // we want to mimic this behaviour.
               configList.add(configList.indexOf(inMemoryConfiguration), config);
  +
  +            if (config instanceof AbstractConfiguration)
  +            {
  +                ((AbstractConfiguration) config).setThrowExceptionOnMissing(isThrowExceptionOnMissing());
  +            }
           }
       }
   
  @@ -117,6 +122,7 @@
           configList.clear();
           // recreate the in memory configuration
           inMemoryConfiguration = new BaseConfiguration();
  +        ((BaseConfiguration) inMemoryConfiguration).setThrowExceptionOnMissing(isThrowExceptionOnMissing());
           configList.add(inMemoryConfiguration);
       }
   
  
  
  
  1.15      +37 -3     jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestBaseConfiguration.java
  
  Index: TestBaseConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestBaseConfiguration.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- TestBaseConfiguration.java	16 Aug 2004 22:16:31 -0000	1.14
  +++ TestBaseConfiguration.java	19 Sep 2004 22:01:50 -0000	1.15
  @@ -28,17 +28,30 @@
   import junitx.framework.ObjectAssert;
   
   /**
  - * Tests some basic functions of the BaseConfiguration class
  + * Tests some basic functions of the BaseConfiguration class. Missing keys will
  + * throw Exceptions
    *
    * @version $Id$
    */
   public class TestBaseConfiguration extends TestCase
   {
  -	protected BaseConfiguration config = new BaseConfiguration();
  +        protected BaseConfiguration config = null;
   
   	protected static Class missingElementException = NoSuchElementException.class;
   	protected static Class incompatibleElementException = ConversionException.class;
   
  +        protected void setUp()
  +            throws Exception
  +        {
  +            config = new BaseConfiguration();
  +            config.setThrowExceptionOnMissing(true);
  +        }
  +
  +        public void testThrowExceptionOnMissing()
  +        {
  +            assertTrue("Throw Exception Property is not set!", config.isThrowExceptionOnMissing());
  +        }
  +
   	public void testGetProperty()
   	{
   		/* should be empty and return null */
  @@ -272,6 +285,27 @@
   		}
   		assertNotNull("No exception thrown for incompatible values", t);
   		ObjectAssert.assertInstanceOf("Exception thrown for incompatible values", incompatibleElementException,
t);
  +	}
  +
  +	public void testGetString()
  +	{
  +		config.setProperty("testString", "The quick brown fox");
  +		String string = new String("The quick brown fox");
  +		String defaultValue = new String("jumps over the lazy dog");
  +
  +		assertEquals("Existing key", string, config.getString("testString"));
  +		assertEquals("Existing key with default value", string, config.getString("testString",
defaultValue));
  +		assertEquals("Missing key with default value", defaultValue, config.getString("stringNotInConfig",
defaultValue));
  +
  +		// missing key without default value
  +		Throwable t = null;
  +		try {
  +			config.getString("stringNotInConfig");
  +		} catch (Throwable T) {
  +			t = T;
  +		}
  +		assertNotNull("No exception thrown for missing keys", t);
  +		ObjectAssert.assertInstanceOf("Exception thrown for missing keys", missingElementException,
t);
   	}
   
   	public void testGetBoolean()
  
  
  
  1.11      +14 -4     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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- TestCompositeConfiguration.java	16 Aug 2004 22:16:31 -0000	1.10
  +++ TestCompositeConfiguration.java	19 Sep 2004 22:01:50 -0000	1.11
  @@ -48,6 +48,13 @@
           conf1 = new PropertiesConfiguration(testProperties);
           conf2 = new PropertiesConfiguration(testProperties2);
           xmlConf = new XMLConfiguration(new File(testPropertiesXML));
  +
  +        cc.setThrowExceptionOnMissing(true);
  +    }
  +
  +    public void testThrowExceptionOnMissing()
  +    {
  +        assertTrue("Throw Exception Property is not set!", cc.isThrowExceptionOnMissing());
       }
   
       public void testAddRemoveConfigurations() throws Exception
  @@ -418,14 +425,17 @@
           BaseConfiguration defaults = new BaseConfiguration();
           defaults.addProperty("default", "default string");
   
  -        Configuration c = new CompositeConfiguration(defaults);
  -        
  +        CompositeConfiguration c = new CompositeConfiguration(defaults);
  +        c.setThrowExceptionOnMissing(cc.isThrowExceptionOnMissing());
           c.addProperty("string", "test string");
   
           assertEquals("test string", c.getString("string"));
           try
           {
  -            c.getString("XXX");
  +            System.out.println(cc.getClass().getName());
  +            assertTrue(cc.isThrowExceptionOnMissing());
  +            String foo = c.getString("XXX");
  +            assertNull(foo);
               fail("Should throw NoSuchElementException exception");
           }
           catch (NoSuchElementException e)
  
  
  
  1.8       +6 -0      jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestJNDIEnvironmentValues.java
  
  Index: TestJNDIEnvironmentValues.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestJNDIEnvironmentValues.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestJNDIEnvironmentValues.java	8 Jul 2004 15:30:24 -0000	1.7
  +++ TestJNDIEnvironmentValues.java	19 Sep 2004 22:01:50 -0000	1.8
  @@ -30,6 +30,12 @@
           System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
           
           conf = new JNDIConfiguration();
  +        conf.setThrowExceptionOnMissing(true);
  +    }
  +
  +    public void testThrowExceptionOnMissing()
  +    {
  +        assertTrue("Throw Exception Property is not set!", conf.isThrowExceptionOnMissing());
       }
   
       public void testSimpleGet() throws Exception
  
  
  
  1.1                  jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestNullCompositeConfiguration.java
  
  Index: TestNullCompositeConfiguration.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.io.File;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Vector;
  
  import junit.framework.TestCase;
  
  /**
   * Test loading multiple configurations.
   *
   * @version $Id: TestNullCompositeConfiguration.java,v 1.1 2004/09/19 22:01:50 henning Exp
$
   */
  public class TestNullCompositeConfiguration extends TestCase
  {
      protected BasePropertiesConfiguration conf1;
      protected BasePropertiesConfiguration conf2;
      protected XMLConfiguration xmlConf;
      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();
      private String testPropertiesXML = new File("conf/test.xml").getAbsolutePath();
  
      protected void setUp() throws Exception
      {
          cc = new CompositeConfiguration();
          conf1 = new PropertiesConfiguration(testProperties);
          conf2 = new PropertiesConfiguration(testProperties2);
          xmlConf = new XMLConfiguration(new File(testPropertiesXML));
  
          cc.setThrowExceptionOnMissing(false);
      }
  
      public void testThrowExceptionOnMissing()
      {
          assertFalse("Throw Exception Property is set!", cc.isThrowExceptionOnMissing());
      }
  
      public void testAddRemoveConfigurations() throws Exception
      {
          cc.addConfiguration(conf1);
          assertEquals(2, cc.getNumberOfConfigurations());
          cc.addConfiguration(conf1);
          assertEquals(2, cc.getNumberOfConfigurations());
          cc.addConfiguration(conf2);
          assertEquals(3, cc.getNumberOfConfigurations());
          cc.removeConfiguration(conf1);
          assertEquals(2, cc.getNumberOfConfigurations());
          cc.clear();
          assertEquals(1, cc.getNumberOfConfigurations());
      }
  
      public void testGetPropertyWIncludes() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(conf2);
          List l = cc.getList("packages");
          assertTrue(l.contains("packagea"));
  
          Vector v = cc.getVector("packages");
          assertTrue(v.contains("packagea"));
      }
      
      public void testGetProperty() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(conf2);
          assertEquals("Make sure we get the property from conf1 first", "test.properties",
cc.getString("propertyInOrder"));
          cc.clear();
  
          cc.addConfiguration(conf2);
          cc.addConfiguration(conf1);
          assertEquals("Make sure we get the property from conf2 first", "test2.properties",
cc.getString("propertyInOrder"));
      }
  
      public void testCantRemoveMemoryConfig() throws Exception
      {
          cc.clear();
          assertEquals(1, cc.getNumberOfConfigurations());
  
          Configuration internal = cc.getConfiguration(0);
          cc.removeConfiguration(internal);
  
          assertEquals(1, cc.getNumberOfConfigurations());
  
      }
  
      public void testGetPropertyMissing() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(conf2);
  
          assertNull("Bogus property is not null!", cc.getString("bogus.property"));
  
          assertTrue("Should be false", !cc.getBoolean("test.missing.boolean", false));
          assertTrue("Should be true", cc.getBoolean("test.missing.boolean.true", true));
  
      }
  
      /**
       * Tests <code>List</code> parsing.
       */
      public void testMultipleTypesOfConfigs() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
          assertEquals("Make sure we get the property from conf1 first", 1, cc.getInt("test.short"));
          cc.clear();
  
          cc.addConfiguration(xmlConf);
          cc.addConfiguration(conf1);
          assertEquals("Make sure we get the property from xml", 8, cc.getInt("test.short"));
      }
  
      /**
       * Tests <code>List</code> parsing.
       */
      public void testPropertyExistsInOnlyOneConfig() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
          assertEquals("value", cc.getString("element"));
      }
  
      /**
       * Tests getting a default when the key doesn't exist
       */
      public void testDefaultValueWhenKeyMissing() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
          assertEquals("default", cc.getString("bogus", "default"));
          assertTrue(1.4 == cc.getDouble("bogus", 1.4));
          assertTrue(1.4 == cc.getDouble("bogus", 1.4));
      }
  
      /**
       * Tests <code>List</code> parsing.
       */
      public void testGettingConfiguration() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
          assertEquals(PropertiesConfiguration.class, cc.getConfiguration(0).getClass());
          assertEquals(XMLConfiguration.class, cc.getConfiguration(1).getClass());
      }
  
      /**
       * Tests setting values.  These are set in memory mode only!
       */
      public void testClearingProperty() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
          cc.clearProperty("test.short");
          assertTrue("Make sure test.short is gone!", !cc.containsKey("test.short"));
      }
  
      /**
       * Tests adding values.  Make sure they _DON'T_ override any other properties but add
to the
       * existing properties  and keep sequence
       */
      public void testAddingProperty() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
  
          String[] values = cc.getStringArray("test.short");
  
          assertEquals("Number of values before add is wrong!", 1, values.length);
          assertEquals("First Value before add is wrong", "1", values[0]);
  
          cc.addProperty("test.short", "88");
  
          values = cc.getStringArray("test.short");
  
          assertEquals("Number of values is wrong!", 2, values.length);
          assertEquals("First Value is wrong", "1", values[0]);
          assertEquals("Third Value is wrong", "88", values[1]);
      }
  
      /**
       * Tests setting values.  These are set in memory mode only!
       */
      public void testSettingMissingProperty() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
          cc.setProperty("my.new.property", "supernew");
          assertEquals("supernew", cc.getString("my.new.property"));
      }
  
      /**
       * Tests retrieving subsets of configurations
       */
      public void testGettingSubset() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
  
          Configuration subset = null;
          subset = cc.subset("test");
          assertNotNull(subset);
          assertFalse("Shouldn't be empty", subset.isEmpty());
          assertEquals("Make sure the initial loaded configs subset overrides any later add
configs subset", "1", subset.getString("short"));
  
          cc.setProperty("test.short", "43");
          subset = cc.subset("test");
          assertEquals("Make sure the initial loaded configs subset overrides any later add
configs subset", "43", subset.getString("short"));
      }
  
      /**
       * Tests subsets and still can resolve elements
       */
      public void testSubsetCanResolve() throws Exception
      {
          cc = new CompositeConfiguration();
          final BaseConfiguration config = new BaseConfiguration();
          config.addProperty("subset.tempfile", "${java.io.tmpdir}/file.tmp");
          cc.addConfiguration(config);
          cc.addConfiguration(ConfigurationConverter.getConfiguration(System.getProperties()));
  
          Configuration subset = cc.subset("subset");
          assertEquals(System.getProperty("java.io.tmpdir") + "/file.tmp", subset.getString("tempfile"));
      }
  
      /**
        * Tests <code>List</code> parsing.
        */
      public void testList() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
  
          List packages = cc.getList("packages");
          // we should get 3 packages here
          assertEquals(3, packages.size());
  
          Vector vpackages = cc.getVector("packages");
          // we should get 3 packages here
          assertEquals(3, vpackages.size());
  
          List defaultList = new ArrayList();
          defaultList.add("1");
          defaultList.add("2");
  
          packages = cc.getList("packages.which.dont.exist", defaultList);
          // we should get 2 packages here
          assertEquals(2, packages.size());
  
          Vector defaultVector = new Vector();
          defaultVector.add("1");
          defaultVector.add("2");
  
          vpackages = cc.getVector("packages.which.dont.exist", defaultVector);
          // we should get 2 packages here
          assertEquals(2, vpackages.size());
      }
  
      /**
        * Tests <code>String</code> array parsing.
        */
      public void testStringArray() throws Exception
      {
          cc.addConfiguration(conf1);
          cc.addConfiguration(xmlConf);
  
          String[] packages = cc.getStringArray("packages");
          // we should get 3 packages here
          assertEquals(3, packages.length);
  
          packages = cc.getStringArray("packages.which.dont.exist");
          // 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"));
      }
  
      public void testGetVector()
      {
          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
          Vector vector = cc.getVector("array");
          assertNotNull("null vector", vector);
          assertEquals("vector size", 2, vector.size());
          assertTrue("'value1' not found in the vector", vector.contains("value1"));
          assertTrue("'value2' not found in the vector", vector.contains("value2"));
  
          // add an element to the vector in the composite configuration
          cc.addProperty("array", "value5");
  
          List list = cc.getList("array");
          
          for (Iterator it = list.iterator(); it.hasNext(); )
          {
              Object value = it.next();
              System.out.println(value.getClass().getName() + " -> " + value);
          }
  
          Vector lVector = cc.getVector("array");
          
          for (Iterator it = lVector.iterator(); it.hasNext(); )
          {
              Object value = it.next();
              System.out.println(value.getClass().getName() + " -> " + value);
          }
  
          // test the new vector
          vector = cc.getVector("array");
          assertNotNull("null vector", vector);
          assertEquals("vector size", 3, vector.size());
          assertTrue("'value1' not found in the vector", vector.contains("value1"));
          assertTrue("'value2' not found in the vector", vector.contains("value2"));
          assertTrue("'value5' not found in the vector", vector.contains("value5"));
      }
  
      /**
        * Tests <code>getKeys</code> preserves the order
        */
      public void testGetKeysPreservesOrder() throws Exception
      {
          cc.addConfiguration(conf1);
          List orderedList = new ArrayList();
          for (Iterator keys = conf1.getKeys();keys.hasNext();){
              orderedList.add(keys.next());
          }
          List iteratedList = new ArrayList();
          for (Iterator keys = cc.getKeys();keys.hasNext();){
              iteratedList.add(keys.next());
          }
          assertEquals(orderedList.size(),iteratedList.size());
          for (int i =0;i<orderedList.size();i++){
              assertEquals(orderedList.get(i),iteratedList.get(i));
          }        
      }    
  
      /**
        * Tests <code>getKeys(String key)</code> preserves the order
        */
      public void testGetKeys2PreservesOrder() throws Exception
      {
          cc.addConfiguration(conf1);
          List orderedList = new ArrayList();
          for (Iterator keys = conf1.getKeys("test");keys.hasNext();){
              orderedList.add(keys.next());
          }
          List iteratedList = new ArrayList();
          for (Iterator keys = cc.getKeys("test");keys.hasNext();){
              iteratedList.add(keys.next());
          }
          assertEquals(orderedList.size(),iteratedList.size());
          for (int i =0;i<orderedList.size();i++){
              assertEquals(orderedList.get(i),iteratedList.get(i));
          }        
      }        
      
      public void testGetStringWithDefaults()
      {
          BaseConfiguration defaults = new BaseConfiguration();
          defaults.addProperty("default", "default string");
  
          Configuration c = new CompositeConfiguration(defaults);
          
          c.addProperty("string", "test string");
  
          assertEquals("test string", c.getString("string"));
  
          assertNull("XXX should have been null!", c.getString("XXX"));
  
          //test defaults
          assertEquals(
              "test string",
              c.getString("string", "some default value"));
          assertEquals("default string", c.getString("default"));
          assertEquals(
              "default string",
              c.getString("default", "some default value"));
          assertEquals(
              "some default value",
              c.getString("XXX", "some default value"));
      }
      
      public void testCheckingInMemoryConfiguration() throws Exception
      {
          String TEST_KEY = "testKey";
          Configuration defaults = new PropertiesConfiguration();
          defaults.setProperty(TEST_KEY,"testValue");
          Configuration testConfiguration = new CompositeConfiguration(defaults);
          assertTrue(testConfiguration.containsKey(TEST_KEY));
          assertFalse(testConfiguration.isEmpty());
          boolean foundTestKey = false;
          Iterator i = testConfiguration.getKeys();
          //assertTrue(i instanceof IteratorChain);
          //IteratorChain ic = (IteratorChain)i;
          //assertEquals(2,i.size());
          for (;i.hasNext();){
              String key = (String)i.next();
              if(key.equals(TEST_KEY)){
                  foundTestKey = true;
              }
          }
          assertTrue(foundTestKey);
          testConfiguration.clearProperty(TEST_KEY);
          assertFalse(testConfiguration.containsKey(TEST_KEY));
      }    
  }
  
  
  
  1.1                  jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestNullJNDIEnvironmentValues.java
  
  Index: TestNullJNDIEnvironmentValues.java
  ===================================================================
  /*
   * Copyright 2002-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.NoSuchElementException;
  
  import junit.framework.TestCase;
  
  public class TestNullJNDIEnvironmentValues extends TestCase
  {
      private JNDIConfiguration conf = null;
  
      public void setUp() throws Exception
      {
          System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
          
          conf = new JNDIConfiguration();
          conf.setThrowExceptionOnMissing(false);
      }
  
      public void testThrowExceptionOnMissing()
      {
          assertFalse("Throw Exception Property is set!", conf.isThrowExceptionOnMissing());
      }
  
      public void testSimpleGet() throws Exception
      {
          String s = conf.getString("test.key");
          assertEquals("jndivalue", s);
      }
  
      public void testMoreGets() throws Exception
      {
          String s = conf.getString("test.key");
          assertEquals("jndivalue", s);
          assertEquals("jndivalue2", conf.getString("test.key2"));
          assertEquals(1, conf.getShort("test.short"));
      }
  
      public void testGetMissingKey() throws Exception
      {
          assertNull("Missing Key is not null!", conf.getString("test.imaginarykey"));
      }
  
      public void testGetMissingKeyWithDefault() throws Exception
      {
          String result = conf.getString("test.imaginarykey", "bob");
          assertEquals("bob", result);
      }
  
      public void testContainsKey() throws Exception
      {
          assertTrue(conf.containsKey("test.key"));
          assertTrue(!conf.containsKey("test.imaginerykey"));
      }
      
      public void testClearProperty()
      {
          assertNotNull("null short for the 'test.short' key", conf.getShort("test.short",
null));
          conf.clearProperty("test.short");
          assertNull("'test.short' property not cleared", conf.getShort("test.short", null));
      }
      
      public void testIsEmpty()
      {
          assertFalse("the configuration shouldn't be empty", conf.isEmpty());
      }
      
      public void testGetKeys() throws Exception
      {
          boolean found = false;
          Iterator it = conf.getKeys();
  
          assertTrue("no key found", it.hasNext());
  
          while (it.hasNext() && !found)
          {
              found = "test.boolean".equals(it.next());
          }
  
          assertTrue("'test.boolean' key not found", found);
      }
  
      public void testGetKeysWithUnknownPrefix()
      {
          // test for a unknown prefix
          Iterator it = conf.getKeys("foo.bar");
          assertFalse("no key should be found", it.hasNext());
      }
  
      public void testGetKeysWithExistingPrefix()
      {
          // test for an existing prefix
          Iterator it = conf.getKeys("test");
          boolean found = false;
          while (it.hasNext() && !found)
          {
              found = "test.boolean".equals(it.next());
          }
  
          assertTrue("'test.boolean' key not found", found);
      }
  
      public void testGetKeysWithKeyAsPrefix()
      {
          // test for a prefix matching exactly the key of a property
          Iterator it = conf.getKeys("test.boolean");
          boolean found = false;
          while (it.hasNext() && !found)
          {
              found = "test.boolean".equals(it.next());
          }
  
          assertTrue("'test.boolean' key not found", found);
      }
  
  }
  
  
  
  1.43      +6 -0      jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- changes.xml	16 Sep 2004 22:35:34 -0000	1.42
  +++ changes.xml	19 Sep 2004 22:01:50 -0000	1.43
  @@ -7,6 +7,12 @@
   
     <body>
       <release version="1.0-rc2" date="in CVS">
  +      <action dev="henning" type="add">
  +        Make the behaviour on missing properties for the get methods that return objects
  +        configurable. A property <code>throwExceptionOnMissing</code> can be
set and then
  +        the getters throw an <code>NoSuchElementException</code>. The old default
behaviour
  +        of returning a <code>null</code> value has been restored.
  +      </action>         
         <action dev="epugh" type="add" issue="29714">
           Allow configurations extending AbstractConfiguration to change the delimiter
           used from "," to something else.
  
  
  

---------------------------------------------------------------------
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