commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ep...@apache.org
Subject cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration MockConfiguration.java TestBaseConfiguration.java
Date Sat, 14 Feb 2004 18:54:22 GMT
epugh       2004/02/14 10:54:21

  Modified:    configuration/xdocs changes.xml
               configuration/src/java/org/apache/commons/configuration
                        BaseConfiguration.java
               configuration/src/test/org/apache/commons/configuration
                        TestBaseConfiguration.java
  Added:       configuration/src/test/org/apache/commons/configuration
                        MockConfiguration.java
  Log:
  Bug 26694  
     BaseConfiguration: containsKey ignores default properties
  
  Revision  Changes    Path
  1.9       +4 -0      jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- changes.xml	12 Feb 2004 12:59:19 -0000	1.8
  +++ changes.xml	14 Feb 2004 18:54:21 -0000	1.9
  @@ -7,6 +7,10 @@
   
     <body>
       <release version="1.0-dev-4" date="">
  +	 <action dev="epugh" type="add">
  +      	Bugzilla 26694 (BaseConfiguration: containsKey ignores default properties).  I have
changed it so that
  +      	now the defaults are paid attention to.   
  +     </action>        
         <action dev="ebourg" type="add">
           The Configuration interface now supports BigDecimal and BigInteger numbers.
         </action>
  
  
  
  1.2       +15 -4     jakarta-commons/configuration/src/java/org/apache/commons/configuration/BaseConfiguration.java
  
  Index: BaseConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/BaseConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BaseConfiguration.java	23 Dec 2003 15:09:05 -0000	1.1
  +++ BaseConfiguration.java	14 Feb 2004 18:54:21 -0000	1.2
  @@ -57,6 +57,7 @@
   import java.util.Iterator;
   
   import org.apache.commons.collections.SequencedHashMap;
  +import org.apache.commons.collections.iterators.IteratorChain;
   
   /**
    * Basic configuration classe. Stores the configuration data but does not
  @@ -171,7 +172,7 @@
        */
       public boolean isEmpty()
       {
  -        return store.isEmpty();
  +        return store.isEmpty() &((defaults != null) && defaults.isEmpty());
       }
   
       /**
  @@ -184,7 +185,9 @@
        */
       public boolean containsKey(String key)
       {
  -        return store.containsKey(key);
  +        return store.containsKey(key)
  +        || ((defaults != null) && defaults.containsKey(key));
  +        
       }
   
       /**
  @@ -197,6 +200,9 @@
           if (containsKey(key))
           {
               store.remove(key);
  +            if(defaults != null){
  +                defaults.clearProperty(key);
  +            }
           }
       }
   
  @@ -208,6 +214,11 @@
        */
       public Iterator getKeys()
       {
  -        return store.iterator();
  +        if(defaults != null){
  +            return new IteratorChain(defaults.getKeys(),store.iterator());
  +        }
  +        else {
  +            return store.iterator();
  +        }
       }
   }
  
  
  
  1.5       +259 -125  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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestBaseConfiguration.java	12 Feb 2004 12:59:19 -0000	1.4
  +++ TestBaseConfiguration.java	14 Feb 2004 18:54:21 -0000	1.5
  @@ -56,10 +56,13 @@
   
   import java.math.BigDecimal;
   import java.math.BigInteger;
  +import java.util.Iterator;
   import java.util.List;
   import java.util.NoSuchElementException;
   import java.util.Properties;
   
  +import org.apache.commons.collections.iterators.IteratorChain;
  +
   import junit.framework.TestCase;
   
   /**
  @@ -74,7 +77,8 @@
   {
       protected BaseConfiguration eprop = new BaseConfiguration();
   
  -    public void testGetProperty() {
  +    public void testGetProperty()
  +    {
           /* should be empty and return null */
           assertEquals("This returns null", eprop.getProperty("foo"), null);
   
  @@ -84,122 +88,168 @@
           assertEquals("This returns '1'", eprop.getString("number"), "1");
       }
   
  -    public void testGetByte() {
  +    public void testGetByte()
  +    {
           eprop.setProperty("number", "1");
           byte oneB = 1, twoB = 2;
  -        assertEquals("This returns 1(byte)",eprop.getByte("number"),oneB);
  -        assertEquals("This returns 1(byte)",eprop.getByte("number",twoB),oneB);
  -        assertEquals("This returns 2(default byte)",
  -                     eprop.getByte("numberNotInConfig",twoB),
  -                     twoB);
  -        assertEquals("This returns 1(Byte)",
  -                     eprop.getByte("number",new Byte("2")),
  -                     new Byte(oneB));
  +        assertEquals("This returns 1(byte)", eprop.getByte("number"), oneB);
  +        assertEquals(
  +            "This returns 1(byte)",
  +            eprop.getByte("number", twoB),
  +            oneB);
  +        assertEquals(
  +            "This returns 2(default byte)",
  +            eprop.getByte("numberNotInConfig", twoB),
  +            twoB);
  +        assertEquals(
  +            "This returns 1(Byte)",
  +            eprop.getByte("number", new Byte("2")),
  +            new Byte(oneB));
       }
   
  -    public void testGetShort() {
  +    public void testGetShort()
  +    {
           eprop.setProperty("numberS", "1");
           short oneS = 1, twoS = 2;
  -        assertEquals("This returns 1(short)",eprop.getShort("numberS"),oneS);
  -        assertEquals("This returns 1(short)",
  -                     eprop.getShort("numberS",twoS),
  -                     oneS);
  -        assertEquals("This returns 2(default short)",
  -                     eprop.getShort("numberNotInConfig",twoS),
  -                     twoS);
  -        assertEquals("This returns 1(Short)",
  -                     eprop.getShort("numberS",new Short("2")),
  -                     new Short(oneS));
  +        assertEquals("This returns 1(short)", eprop.getShort("numberS"), oneS);
  +        assertEquals(
  +            "This returns 1(short)",
  +            eprop.getShort("numberS", twoS),
  +            oneS);
  +        assertEquals(
  +            "This returns 2(default short)",
  +            eprop.getShort("numberNotInConfig", twoS),
  +            twoS);
  +        assertEquals(
  +            "This returns 1(Short)",
  +            eprop.getShort("numberS", new Short("2")),
  +            new Short(oneS));
       }
   
  -    public void testGetLong() {
  +    public void testGetLong()
  +    {
           eprop.setProperty("numberL", "1");
           long oneL = 1, twoL = 2;
  -        assertEquals("This returns 1(long)",eprop.getLong("numberL"),oneL);
  -        assertEquals("This returns 1(long)",
  -                     eprop.getLong("numberL",twoL),
  -                     oneL);
  -        assertEquals("This returns 2(default long)",
  -                     eprop.getLong("numberNotInConfig",twoL),
  -                     twoL);
  -        assertEquals("This returns 1(Long)",
  -                     eprop.getLong("numberL",new Long("2")),
  -                     new Long(oneL));
  +        assertEquals("This returns 1(long)", eprop.getLong("numberL"), oneL);
  +        assertEquals(
  +            "This returns 1(long)",
  +            eprop.getLong("numberL", twoL),
  +            oneL);
  +        assertEquals(
  +            "This returns 2(default long)",
  +            eprop.getLong("numberNotInConfig", twoL),
  +            twoL);
  +        assertEquals(
  +            "This returns 1(Long)",
  +            eprop.getLong("numberL", new Long("2")),
  +            new Long(oneL));
       }
   
  -    public void testGetFloat() {
  +    public void testGetFloat()
  +    {
           eprop.setProperty("numberF", "1.0");
           float oneF = 1, twoF = 2;
  -        assertEquals("This returns 1(float)",eprop.getFloat("numberF"),oneF,0);
  -        assertEquals("This returns 1(float)",
  -                     eprop.getFloat("numberF",twoF),
  -                     oneF,
  -                     0);
  -        assertEquals("This returns 2(default float)",
  -                     eprop.getFloat("numberNotInConfig",twoF),
  -                     twoF,
  -                     0);
  -        assertEquals("This returns 1(Float)",
  -                     eprop.getFloat("numberF",new Float("2")),
  -                     new Float(oneF));
  +        assertEquals(
  +            "This returns 1(float)",
  +            eprop.getFloat("numberF"),
  +            oneF,
  +            0);
  +        assertEquals(
  +            "This returns 1(float)",
  +            eprop.getFloat("numberF", twoF),
  +            oneF,
  +            0);
  +        assertEquals(
  +            "This returns 2(default float)",
  +            eprop.getFloat("numberNotInConfig", twoF),
  +            twoF,
  +            0);
  +        assertEquals(
  +            "This returns 1(Float)",
  +            eprop.getFloat("numberF", new Float("2")),
  +            new Float(oneF));
       }
   
  -    public void testGetDouble() {
  +    public void testGetDouble()
  +    {
           eprop.setProperty("numberD", "1.0");
           double oneD = 1, twoD = 2;
  -        assertEquals("This returns 1(double)",
  -                     eprop.getDouble("numberD"),
  -                     oneD,
  -                     0);
  -        assertEquals("This returns 1(double)",
  -                     eprop.getDouble("numberD",twoD),
  -                     oneD,
  -                     0);
  -        assertEquals("This returns 2(default double)",
  -                     eprop.getDouble("numberNotInConfig",twoD),
  -                     twoD,
  -                     0);
  -        assertEquals("This returns 1(Double)",
  -                     eprop.getDouble("numberD",new Double("2")),
  -                     new Double(oneD));
  +        assertEquals(
  +            "This returns 1(double)",
  +            eprop.getDouble("numberD"),
  +            oneD,
  +            0);
  +        assertEquals(
  +            "This returns 1(double)",
  +            eprop.getDouble("numberD", twoD),
  +            oneD,
  +            0);
  +        assertEquals(
  +            "This returns 2(default double)",
  +            eprop.getDouble("numberNotInConfig", twoD),
  +            twoD,
  +            0);
  +        assertEquals(
  +            "This returns 1(Double)",
  +            eprop.getDouble("numberD", new Double("2")),
  +            new Double(oneD));
       }
   
  -    public void testGetBigDecimal() {
  +    public void testGetBigDecimal()
  +    {
           eprop.setProperty("numberBigD", "123.456");
           BigDecimal number = new BigDecimal("123.456");
           BigDecimal defaultValue = new BigDecimal("654.321");
   
           assertEquals("Existing key", number, eprop.getBigDecimal("numberBigD"));
  -        assertEquals("Existing key with default value", number, eprop.getBigDecimal("numberBigD",
defaultValue));
  -        assertEquals("Missing key with default value", defaultValue, eprop.getBigDecimal("numberNotInConfig",
defaultValue));
  +        assertEquals(
  +            "Existing key with default value",
  +            number,
  +            eprop.getBigDecimal("numberBigD", defaultValue));
  +        assertEquals(
  +            "Missing key with default value",
  +            defaultValue,
  +            eprop.getBigDecimal("numberNotInConfig", defaultValue));
       }
   
  -    public void testGetBigInteger() {
  +    public void testGetBigInteger()
  +    {
           eprop.setProperty("numberBigI", "1234567890");
           BigInteger number = new BigInteger("1234567890");
           BigInteger defaultValue = new BigInteger("654321");
   
           assertEquals("Existing key", number, eprop.getBigInteger("numberBigI"));
  -        assertEquals("Existing key with default value", number, eprop.getBigInteger("numberBigI",
defaultValue));
  -        assertEquals("Missing key with default value", defaultValue, eprop.getBigInteger("numberNotInConfig",
defaultValue));
  +        assertEquals(
  +            "Existing key with default value",
  +            number,
  +            eprop.getBigInteger("numberBigI", defaultValue));
  +        assertEquals(
  +            "Missing key with default value",
  +            defaultValue,
  +            eprop.getBigInteger("numberNotInConfig", defaultValue));
       }
   
  -    public void testGetBoolean() {
  +    public void testGetBoolean()
  +    {
           eprop.setProperty("boolA", Boolean.TRUE);
           boolean boolT = true, boolF = false;
  -        assertEquals("This returns true",eprop.getBoolean("boolA"),boolT);
  -        assertEquals("This returns true, not the default",
  -                     eprop.getBoolean("boolA",boolF),
  -                     boolT);
  -        assertEquals("This returns false(default)",
  -                     eprop.getBoolean("boolNotInConfig",boolF),
  -                     boolF);
  -        assertEquals("This returns true(Boolean)",
  -                     eprop.getBoolean("boolA",new Boolean(boolF)),
  -                     new Boolean(boolT));
  +        assertEquals("This returns true", eprop.getBoolean("boolA"), boolT);
  +        assertEquals(
  +            "This returns true, not the default",
  +            eprop.getBoolean("boolA", boolF),
  +            boolT);
  +        assertEquals(
  +            "This returns false(default)",
  +            eprop.getBoolean("boolNotInConfig", boolF),
  +            boolF);
  +        assertEquals(
  +            "This returns true(Boolean)",
  +            eprop.getBoolean("boolA", new Boolean(boolF)),
  +            new Boolean(boolT));
       }
   
  -    public void testGetList() {
  +    public void testGetList()
  +    {
           eprop.addProperty("number", "1");
           eprop.addProperty("number", "2");
           List list = eprop.getList("number");
  @@ -212,29 +262,51 @@
            *  now test dan's new fix where we get the first scalar
            *  when we access a list valued property
            */
  -        assertTrue("This returns scalar", ( eprop.getString("number")
  -                instanceof String ) );
  +        try
  +        {
  +            eprop.getString("number");
  +        }
  +        catch (NoSuchElementException nsse)
  +        {
  +            fail("Should return a string");
  +        }
       }
   
  -    public void testCommaSeparatedString() {
  +    public void testCommaSeparatedString()
  +    {
           String prop = "hey, that's a test";
           eprop.setProperty("prop.string", prop);
  -        assertTrue("This returns list", ( eprop.getList("prop.string")
  -                instanceof java.util.List ) );
  +        try
  +        {
  +            eprop.getList("prop.string");
  +        }
  +        catch (NoSuchElementException nsse)
  +        {
  +            fail("Should return a list");
  +        }
   
           String prop2 = "hey\\, that's a test";
           eprop.clearProperty("prop.string");
           eprop.setProperty("prop.string", prop2);
  -        assertTrue("This returns string", ( eprop.getString("prop.string")
  -                instanceof java.lang.String) );
  +        try
  +        {
  +            eprop.getString("prop.string");
  +        }
  +        catch (NoSuchElementException nsse)
  +        {
  +            fail("Should return a list");
  +        }
  +
       }
   
  -    public void testPropertyAccess() {
  +    public void testPropertyAccess()
  +    {
           eprop.clearProperty("prop.properties");
           eprop.setProperty("prop.properties", "");
  -        assertEquals("This returns an empty Properties object",
  -                     eprop.getProperties("prop.properties"),
  -                     new Properties());
  +        assertEquals(
  +            "This returns an empty Properties object",
  +            eprop.getProperties("prop.properties"),
  +            new Properties());
           eprop.clearProperty("prop.properties");
           eprop.setProperty("prop.properties", "foo=bar, baz=moo, seal=clubber");
   
  @@ -242,9 +314,10 @@
           p.setProperty("foo", "bar");
           p.setProperty("baz", "moo");
           p.setProperty("seal", "clubber");
  -        assertEquals("This returns a filled in Properties object",
  -                     eprop.getProperties("prop.properties"),
  -                     p);
  +        assertEquals(
  +            "This returns a filled in Properties object",
  +            eprop.getProperties("prop.properties"),
  +            p);
       }
   
       public void testSubset()
  @@ -260,9 +333,26 @@
   
           Configuration subEprop = eprop.subset("prop");
   
  -        assertEquals("Returns the full string", prop, subEprop.getString("string"));
  -        assertTrue("This returns string for subset", ( subEprop.getString("string") instanceof
java.lang.String) );
  -        assertTrue("This returns array for subset", ( subEprop.getList("string") instanceof
java.util.List) );
  +        assertEquals(
  +            "Returns the full string",
  +            prop,
  +            subEprop.getString("string"));
  +        try
  +        {
  +            subEprop.getString("string");
  +        }
  +        catch (NoSuchElementException nsse)
  +        {
  +            fail("Should return a string");
  +        }
  +        try
  +        {
  +            subEprop.getList("string");
  +        }
  +        catch (NoSuchElementException nsse)
  +        {
  +            fail("Should return a list");
  +        }
       }
   
       public void testInterpolation() throws Exception
  @@ -276,47 +366,57 @@
           //construct a new config, using eprop as the defaults config for it.
           BaseConfiguration superProp = new PropertiesConfiguration(eprop);
   
  -        assertTrue("Checking interpolated variable", superProp.getString("db")
  -                .equals(dbProp));
  -        assertEquals("lookup fails, leave variable as is",
  -                     superProp.getString("dbFailedInterpolate"),
  -                     unInterpolatedValue);
  +        assertTrue(
  +            "Checking interpolated variable",
  +            superProp.getString("db").equals(dbProp));
  +        assertEquals(
  +            "lookup fails, leave variable as is",
  +            superProp.getString("dbFailedInterpolate"),
  +            unInterpolatedValue);
   
  -        superProp.setProperty("arrayInt","${applicationRoot}/1");
  +        superProp.setProperty("arrayInt", "${applicationRoot}/1");
           String[] arrayInt = superProp.getStringArray("arrayInt");
  -        assertEquals("check first entry was interpolated",
  -                     "/home/applicationRoot/1",
  -                     arrayInt[0]);
  +        assertEquals(
  +            "check first entry was interpolated",
  +            "/home/applicationRoot/1",
  +            arrayInt[0]);
       }
   
       public void testMultipleInterpolation() throws Exception
       {
  -        eprop.setProperty( "test.base-level", "/base-level" );
  -        eprop.setProperty( "test.first-level", "${test.base-level}/first-level" );
  -        eprop.setProperty( "test.second-level", "${test.first-level}/second-level" );
  -        eprop.setProperty( "test.third-level", "${test.second-level}/third-level" );
  +        eprop.setProperty("test.base-level", "/base-level");
  +        eprop.setProperty("test.first-level", "${test.base-level}/first-level");
  +        eprop.setProperty(
  +            "test.second-level",
  +            "${test.first-level}/second-level");
  +        eprop.setProperty(
  +            "test.third-level",
  +            "${test.second-level}/third-level");
   
  -        String expectedValue = "/base-level/first-level/second-level/third-level";
  +        String expectedValue =
  +            "/base-level/first-level/second-level/third-level";
   
  -        assertEquals( eprop.getString( "test.third-level" ),  expectedValue );
  +        assertEquals(eprop.getString("test.third-level"), expectedValue);
       }
   
       public void testInterpolationLoop() throws Exception
       {
  -        eprop.setProperty( "test.a", "${test.b}" );
  -        eprop.setProperty( "test.b", "${test.a}" );
  +        eprop.setProperty("test.a", "${test.b}");
  +        eprop.setProperty("test.b", "${test.a}");
   
  -        try {
  -            eprop.getString( "test.a" );
  +        try
  +        {
  +            eprop.getString("test.a");
           }
  -        catch( IllegalStateException e ) {
  +        catch (IllegalStateException e)
  +        {
               return;
           }
   
  -        fail( "IllegalStateException should have been thrown for looped property references"
);
  +        fail("IllegalStateException should have been thrown for looped property references");
       }
   
  -    public void testGetString() 
  +    public void testGetString()
       {
           BaseConfiguration defaults = new BaseConfiguration();
           defaults.addProperty("default", "default string");
  @@ -326,20 +426,54 @@
           eprop.addProperty("string", "test string");
   
           assertEquals("test string", eprop.getString("string"));
  -        try 
  +        try
           {
               eprop.getString("XXX");
               fail("Should throw NoSuchElementException exception");
  -        } catch (NoSuchElementException e) {
  +        }
  +        catch (NoSuchElementException e)
  +        {
               //ok
  -        } catch (Exception e) {
  +        }
  +        catch (Exception e)
  +        {
               fail("Should throw NoSuchElementException exception, not " + e);
           }
   
           //test defaults
  -        assertEquals("test string", eprop.getString("string", "some default value"));	

  +        assertEquals(
  +            "test string",
  +            eprop.getString("string", "some default value"));
           assertEquals("default string", eprop.getString("default"));
  -        assertEquals("default string", eprop.getString("default", "some default value"));
  -        assertEquals("some default value", eprop.getString("XXX", "some default value"));
	
  +        assertEquals(
  +            "default string",
  +            eprop.getString("default", "some default value"));
  +        assertEquals(
  +            "some default value",
  +            eprop.getString("XXX", "some default value"));
  +    }
  +    
  +    public void testCheckingDefaultConfiguration() throws Exception{
  +        String TEST_KEY = "testKey";
  +        Configuration defaults = new PropertiesConfiguration();
  +        defaults.setProperty(TEST_KEY,"testValue");
  +        Configuration testConfiguration = new MockConfiguration(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,ic.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/MockConfiguration.java
  
  Index: MockConfiguration.java
  ===================================================================
  /*
   * Created on Feb 14, 2004
   *
   * To change the template for this generated file go to
   * Window - Preferences - Java - Code Generation - Code and Comments
   */
  package org.apache.commons.configuration;
  
  
  /**
   * @author Eric Pugh
   *
   * To change the template for this generated type comment go to
   * Window - Preferences - Java - Code Generation - Code and Comments
   */
  public class MockConfiguration extends BaseConfiguration
  {
      public MockConfiguration(){
          super();
      }
      
      public MockConfiguration(Configuration defaults){
          super(defaults);
      }
  
  }
  
  
  

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