Fix a bug of sequence of return from Properties.propertyNames() method

git-svn-id: https://svn.apache.org/repos/asf/harmony/enhanced/classlib/trunk@899035 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/modules/luni/src/main/java/java/util/Properties.java b/modules/luni/src/main/java/java/util/Properties.java
index 14691c9..8d32977 100644
--- a/modules/luni/src/main/java/java/util/Properties.java
+++ b/modules/luni/src/main/java/java/util/Properties.java
@@ -17,9 +17,9 @@
 
 package java.util;
 
+import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.BufferedInputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintStream;
@@ -34,19 +34,17 @@
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
+import org.apache.harmony.luni.internal.nls.Messages;
+import org.apache.harmony.luni.util.PriviAction;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 import org.xml.sax.EntityResolver;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-
-import org.apache.harmony.luni.internal.nls.Messages;
-import org.apache.harmony.luni.util.PriviAction;
-
 /**
  * A {@code Properties} object is a {@code Hashtable} where the keys and values
  * must be {@code String}s. Each property can have a default
@@ -455,22 +453,16 @@
      *         that this {@code Properties} object contains.
      */
     public Enumeration<?> propertyNames() {
-        if (defaults == null) {
-            return keys();
-        }
+        Hashtable<Object, Object> selected = new Hashtable<Object, Object>();
+        selectProperties(selected);
+        return selected.keys();
+    }
 
-        Hashtable<Object, Object> set = new Hashtable<Object, Object>(defaults
-                .size()
-                + size());
-        Enumeration<?> keys = defaults.propertyNames();
-        while (keys.hasMoreElements()) {
-            set.put(keys.nextElement(), set);
+    private void selectProperties(Hashtable<Object, Object> selected) {
+        if(defaults != null) {
+            defaults.selectProperties(selected);
         }
-        keys = keys();
-        while (keys.hasMoreElements()) {
-            set.put(keys.nextElement(), set);
-        }
-        return set.keys();
+        selected.putAll(this);
     }
 
     /**
diff --git a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
index 062e0f3..a5c4d8b 100644
--- a/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
+++ b/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/PropertiesTest.java
@@ -304,6 +304,41 @@
         }
     }
 
+    public void test_propertyNames_sequence() {
+        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());
+    }
+
     /**
 
      * @tests java.util.Properties#save(java.io.OutputStream, java.lang.String)