harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r905238 - in /harmony/enhanced/classlib/branches/java6/modules/luni/src: main/java/java/util/Properties.java test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
Date Mon, 01 Feb 2010 10:26:03 GMT
Author: zhoukevin
Date: Mon Feb  1 10:26:02 2010
New Revision: 905238

URL: http://svn.apache.org/viewvc?rev=905238&view=rev
Log:
Fix a bug of stringPropertyNames and add 2 test cases

Modified:
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java
    harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java?rev=905238&r1=905237&r2=905238&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java
Mon Feb  1 10:26:02 2010
@@ -493,10 +493,10 @@
      */
     public Enumeration<?> propertyNames() {
         Hashtable<Object, Object> selected = new Hashtable<Object, Object>();
-        selectProperties(selected);
+        selectProperties(selected, false);
         return selected.keys();
     }
-    
+
     /**
      * Answers a set of keys in this property list whose key and value are
      * strings.
@@ -504,30 +504,39 @@
      * @return a set of keys in the property list
      * 
      * @since 1.6
-     */    
-    public Set<String> stringPropertyNames(){
-        Hashtable<Object, Object> selected = new Hashtable<Object, Object>();
-        selectProperties(selected);
-        Enumeration<?> keys = selected.keys();
-        HashSet<String> set = new HashSet<String>();
+     */
+    public Set<String> stringPropertyNames() {
+        Hashtable<String, String> stringProperties = new Hashtable<String, String>();
+        selectProperties(stringProperties, true);
+        return Collections.unmodifiableSet(stringProperties.keySet());
+    }
+
+    /*
+     * Select properties including defaults according to requirements
+     */
+    private void selectProperties(Hashtable selectProperties,
+            final boolean isStringOnly) {
+        if (defaults != null) {
+            defaults.selectProperties(selectProperties, isStringOnly);
+        }
+
+        Enumeration<?> keys = keys();
         Object key, value;
         while (keys.hasMoreElements()) {
             key = keys.nextElement();
-            if (key instanceof String) {
-                value = selected.get(key);
-                if (value instanceof String){
-                    set.add((String)key);    
+            if (isStringOnly) {
+                // Only select property with string key and value
+                if (key instanceof String) {
+                    value = get(key);
+                    if (value instanceof String) {
+                        selectProperties.put(key, value);
+                    }
                 }
-            }           
-        }
-        return Collections.unmodifiableSet(set);
-    }
-
-    private void selectProperties(Hashtable<Object, Object> selected) {
-        if(defaults != null) {
-            defaults.selectProperties(selected);
+            } else {
+                value = get(key);
+                selectProperties.put(key, value);
+            }
         }
-        selected.putAll(this);
     }
 
     /**

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java?rev=905238&r1=905237&r2=905238&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
Mon Feb  1 10:26:02 2010
@@ -956,6 +956,76 @@
         assertEquals(1, props.size());
     }
 
+    public void test_SequentialpropertyNames() {
+        Properties parent = new Properties();
+        parent.setProperty("parent.a.key", "parent.a.value");
+        parent.setProperty("parent.b.key", "parent.b.value");
+
+        Enumeration<?> names = parent.propertyNames();
+        assertEquals("parent.a.key", names.nextElement());
+        assertEquals("parent.b.key", names.nextElement());
+        assertFalse(names.hasMoreElements());
+
+        Properties current = new Properties(parent);
+        current.setProperty("current.a.key", "current.a.value");
+        current.setProperty("current.b.key", "current.b.value");
+
+        names = current.propertyNames();
+        assertEquals("parent.a.key", names.nextElement());
+        assertEquals("current.b.key", names.nextElement());
+        assertEquals("parent.b.key", names.nextElement());
+        assertEquals("current.a.key", names.nextElement());
+        assertFalse(names.hasMoreElements());
+
+        Properties child = new Properties(current);
+        child.setProperty("child.a.key", "child.a.value");
+        child.setProperty("child.b.key", "child.b.value");
+
+        names = child.propertyNames();
+        assertEquals("parent.a.key", names.nextElement());
+        assertEquals("child.b.key", names.nextElement());
+        assertEquals("current.b.key", names.nextElement());
+        assertEquals("parent.b.key", names.nextElement());
+        assertEquals("child.a.key", names.nextElement());
+        assertEquals("current.a.key", names.nextElement());
+        assertFalse(names.hasMoreElements());
+    }
+
+    public void test_SequentialstringPropertyNames() {
+        Properties parent = new Properties();
+        parent.setProperty("parent.a.key", "parent.a.value");
+        parent.setProperty("parent.b.key", "parent.b.value");
+
+        Iterator<String> nameIterator = parent.stringPropertyNames().iterator();
+        assertEquals("parent.a.key", nameIterator.next());
+        assertEquals("parent.b.key", nameIterator.next());
+        assertFalse(nameIterator.hasNext());
+
+        Properties current = new Properties(parent);
+        current.setProperty("current.a.key", "current.a.value");
+        current.setProperty("current.b.key", "current.b.value");
+
+        nameIterator = current.stringPropertyNames().iterator();
+        assertEquals("parent.a.key", nameIterator.next());
+        assertEquals("current.b.key", nameIterator.next());
+        assertEquals("parent.b.key", nameIterator.next());
+        assertEquals("current.a.key", nameIterator.next());
+        assertFalse(nameIterator.hasNext());
+
+        Properties child = new Properties(current);
+        child.setProperty("child.a.key", "child.a.value");
+        child.setProperty("child.b.key", "child.b.value");
+
+        nameIterator = child.stringPropertyNames().iterator();
+        assertEquals("parent.a.key", nameIterator.next());
+        assertEquals("child.b.key", nameIterator.next());
+        assertEquals("current.b.key", nameIterator.next());
+        assertEquals("parent.b.key", nameIterator.next());
+        assertEquals("child.a.key", nameIterator.next());
+        assertEquals("current.a.key", nameIterator.next());
+        assertFalse(nameIterator.hasNext());
+    }
+
     /**
      * Sets up the fixture, for example, open a network connection. This method
      * is called before a test is executed.



Mime
View raw message