You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2009/08/10 14:55:32 UTC
svn commit: r802773 - 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
Author: tellison
Date: Mon Aug 10 12:55:32 2009
New Revision: 802773
URL: http://svn.apache.org/viewvc?rev=802773&view=rev
Log:
Apply patch for HARMONY-6146 ([classlib][luni][java6] java.util.Properties.Properties(defaults).stringPropertyNames() should return a set of keys including the keys in the default property list)
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=802773&r1=802772&r2=802773&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 Aug 10 12:55:32 2009
@@ -628,22 +628,8 @@
* that this {@code Properties} object contains.
*/
public Enumeration<?> propertyNames() {
- if (defaults == null) {
- return keys();
- }
-
- Hashtable<Object, Object> set = new Hashtable<Object, Object>(defaults
- .size()
- + size());
- Enumeration<?> keys = defaults.propertyNames();
- while (keys.hasMoreElements()) {
- set.put(keys.nextElement(), set);
- }
- keys = keys();
- while (keys.hasMoreElements()) {
- set.put(keys.nextElement(), set);
- }
- return set.keys();
+ Hashtable<Object, Object> allProperties = getAllProperties();
+ return allProperties.keys();
}
/**
@@ -655,15 +641,14 @@
* @since 1.6
*/
public Set<String> stringPropertyNames(){
- HashSet<String> set = new HashSet<String>();
- Enumeration<?> keys = propertyNames();
+ Hashtable<Object, Object> allProperties = getAllProperties();
+ Enumeration<?> keys = allProperties.keys();
+ HashSet<String> set = new HashSet<String>();
+ Object key, value;
while (keys.hasMoreElements()) {
- Object key = keys.nextElement();
+ key = keys.nextElement();
if (key instanceof String) {
- Object value = this.get(key);
- if (value == null){
- value = this.defaults.get(key);
- }
+ value = allProperties.get(key);
if (value instanceof String){
set.add((String)key);
}
@@ -672,6 +657,22 @@
return Collections.unmodifiableSet(set);
}
+ /*
+ * Add all of properties into the given allProperties hashtable including
+ * default properties
+ */
+ private Hashtable<Object, Object> getAllProperties() {
+ Hashtable<Object, Object> allProperties = new Hashtable<Object, Object>();
+ Properties properties = this;
+ Properties defaultProperties = null;
+ while (properties != null) {
+ defaultProperties = properties.defaults;
+ allProperties.putAll(properties);
+ properties = defaultProperties;
+ }
+ return allProperties;
+ }
+
/**
* Saves the mappings in this {@code Properties} to the specified {@code
* OutputStream}, putting the specified comment at the beginning. The output
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=802773&r1=802772&r2=802773&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 Aug 10 12:55:32 2009
@@ -27,9 +27,12 @@
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Enumeration;
import java.util.InvalidPropertiesFormatException;
import java.util.Iterator;
+import java.util.List;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Scanner;
@@ -501,6 +504,114 @@
}
/**
+ * @tests {@link java.util.Properties#stringPropertyNames()}
+ * @since 1.6
+ */
+ public void test_stringPropertyNames_scenario1() {
+ String[] keys = new String[] { "key1", "key2", "key3" };
+ String[] values = new String[] { "value1", "value2", "value3" };
+ List<String> keyList = Arrays.asList(keys);
+
+ Properties properties = new Properties();
+ for (int index = 0; index < keys.length; index++) {
+ properties.setProperty(keys[index], values[index]);
+ }
+
+ properties = new Properties(properties);
+ Set<String> nameSet = properties.stringPropertyNames();
+ assertEquals(keys.length, nameSet.size());
+ Iterator<String> iterator = nameSet.iterator();
+ while (iterator.hasNext()) {
+ assertTrue(keyList.contains(iterator.next()));
+ }
+
+ Enumeration<?> nameEnum = properties.propertyNames();
+ int count = 0;
+ while (nameEnum.hasMoreElements()) {
+ count++;
+ assertTrue(keyList.contains(nameEnum.nextElement()));
+ }
+ assertEquals(keys.length, count);
+
+ properties = new Properties(properties);
+ nameSet = properties.stringPropertyNames();
+ assertEquals(keys.length, nameSet.size());
+ iterator = nameSet.iterator();
+ while (iterator.hasNext()) {
+ assertTrue(keyList.contains(iterator.next()));
+ }
+
+ nameEnum = properties.propertyNames();
+ count = 0;
+ while (nameEnum.hasMoreElements()) {
+ count++;
+ assertTrue(keyList.contains(nameEnum.nextElement()));
+ }
+ assertEquals(keys.length, count);
+ }
+
+ /**
+ * @tests {@link java.util.Properties#stringPropertyNames()}
+ * @since 1.6
+ */
+ public void test_stringPropertyNames_scenario2() {
+ String[] defaultKeys = new String[] { "defaultKey1", "defaultKey2",
+ "defaultKey3", "defaultKey4", "defaultKey5", "defaultKey6" };
+ String[] defaultValues = new String[] { "defaultValue1",
+ "defaultValue2", "defaultValue3", "defaultValue4",
+ "defaultValue5", "defaultValue6" };
+ List<String> keyList = new ArrayList<String>();
+ Properties defaults = new Properties();
+ for (int index = 0; index < 3; index++) {
+ defaults.setProperty(defaultKeys[index], defaultValues[index]);
+ keyList.add(defaultKeys[index]);
+ }
+
+ String[] keys = new String[] { "key1", "key2", "key3" };
+ String[] values = new String[] { "value1", "value2", "value3" };
+ Properties properties = new Properties(defaults);
+ for (int index = 0; index < keys.length; index++) {
+ properties.setProperty(keys[index], values[index]);
+ keyList.add(keys[index]);
+ }
+
+ Set<String> nameSet = properties.stringPropertyNames();
+ assertEquals(keyList.size(), nameSet.size());
+ Iterator<String> iterator = nameSet.iterator();
+ while (iterator.hasNext()) {
+ assertTrue(keyList.contains(iterator.next()));
+ }
+
+ Enumeration<?> nameEnum = properties.propertyNames();
+ int count = 0;
+ while (nameEnum.hasMoreElements()) {
+ count++;
+ assertTrue(keyList.contains(nameEnum.nextElement()));
+ }
+ assertEquals(keyList.size(), count);
+
+ for (int index = 3; index < defaultKeys.length; index++) {
+ defaults.setProperty(defaultKeys[index], defaultValues[index]);
+ keyList.add(defaultKeys[index]);
+ }
+
+ nameSet = properties.stringPropertyNames();
+ assertEquals(keyList.size(), nameSet.size());
+ iterator = nameSet.iterator();
+ while (iterator.hasNext()) {
+ assertTrue(keyList.contains(iterator.next()));
+ }
+
+ nameEnum = properties.propertyNames();
+ count = 0;
+ while (nameEnum.hasMoreElements()) {
+ count++;
+ assertTrue(keyList.contains(nameEnum.nextElement()));
+ }
+ assertEquals(keyList.size(), count);
+ }
+
+ /**
* @tests java.util.Properties#save(java.io.OutputStream, java.lang.String)
*/
public void test_saveLjava_io_OutputStreamLjava_lang_String() {