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 2007/07/10 12:02:41 UTC
svn commit: r554894 - in
/harmony/enhanced/classlib/branches/java6/modules/luni/src:
main/java/java/util/Properties.java
test/api/common/tests/api/java/util/PropertiesTest.java
Author: tellison
Date: Tue Jul 10 03:02:33 2007
New Revision: 554894
URL: http://svn.apache.org/viewvc?view=rev&rev=554894
Log:
Apply patch HARMONY-4204 ([classlib][luni][java6] New methods in java.util.Properties for java6)
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/tests/api/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?view=diff&rev=554894&r1=554893&r2=554894
==============================================================================
--- 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 Tue Jul 10 03:02:33 2007
@@ -24,7 +24,9 @@
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
+import java.io.Reader;
import java.io.StringReader;
+import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
@@ -34,18 +36,16 @@
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+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.util.PriviAction;
-
/**
* Properties is a Hashtable where the keys and values must be Strings. Each
* Properties can have a default Properties which specifies the default values
@@ -97,7 +97,8 @@
defaults = properties;
}
- private void dumpString(StringBuilder buffer, String string, boolean key) {
+ @SuppressWarnings("nls")
+ private void dumpString(StringBuilder buffer, String string, boolean key) {
int i = 0;
if (!key && i < string.length() && string.charAt(i) == ' ') {
buffer.append("\\ "); //$NON-NLS-1$
@@ -255,6 +256,9 @@
* @throws IOException
*/
public synchronized void load(InputStream in) throws IOException {
+ if (in == null) {
+ throw new NullPointerException();
+ }
int mode = NONE, unicode = 0, count = 0;
char nextChar, buf[] = new char[40];
int offset = 0, keyLength = -1;
@@ -283,7 +287,9 @@
if (++count < 4) {
continue;
}
- }
+ } else {
+ throw new IllegalArgumentException();
+ }
mode = NONE;
buf[offset++] = (char) unicode;
if (nextChar != '\n') {
@@ -406,12 +412,177 @@
put(temp.substring(0, keyLength), temp.substring(keyLength));
}
}
+
+ /**
+ * Loads properties from the specified InputStream. The properties are of
+ * the form <code>key=value</code>, one property per line. It may be not
+ * encode as 'ISO-8859-1'.
+ *
+ * @param reader
+ * the input reader
+ * @throws IOException
+ * @since 1.6
+ */
+ public synchronized void load(Reader reader) throws IOException {
+ int mode = NONE, unicode = 0, count = 0;
+ char nextChar, buf[] = new char[40];
+ int offset = 0, keyLength = -1;
+ boolean firstChar = true;
+ char[] inbuf = new char[256];
+ int inbufCount = 0, inbufPos = 0;
+
+ while (true) {
+ if (inbufPos == inbufCount) {
+ if ((inbufCount = reader.read(inbuf)) == -1) {
+ break;
+ }
+ inbufPos = 0;
+ }
+ nextChar = inbuf[inbufPos++];
+
+ if (offset == buf.length) {
+ char[] newBuf = new char[buf.length * 2];
+ System.arraycopy(buf, 0, newBuf, 0, offset);
+ buf = newBuf;
+ }
+ if (mode == UNICODE) {
+ int digit = Character.digit(nextChar, 16);
+ if (digit >= 0) {
+ unicode = (unicode << 4) + digit;
+ if (++count < 4) {
+ continue;
+ }
+ } else {
+ throw new IllegalArgumentException();
+ }
+ mode = NONE;
+ buf[offset++] = (char) unicode;
+ if (nextChar != '\n') {
+ continue;
+ }
+ }
+ if (mode == SLASH) {
+ mode = NONE;
+ switch (nextChar) {
+ case '\r':
+ mode = CONTINUE; // Look for a following \n
+ continue;
+ case '\n':
+ mode = IGNORE; // Ignore whitespace on the next line
+ continue;
+ case 'b':
+ nextChar = '\b';
+ break;
+ case 'f':
+ nextChar = '\f';
+ break;
+ case 'n':
+ nextChar = '\n';
+ break;
+ case 'r':
+ nextChar = '\r';
+ break;
+ case 't':
+ nextChar = '\t';
+ break;
+ case 'u':
+ mode = UNICODE;
+ unicode = count = 0;
+ continue;
+ }
+ } else {
+ switch (nextChar) {
+ case '#':
+ case '!':
+ if (firstChar) {
+ while (true) {
+ if (inbufPos == inbufCount) {
+ if ((inbufCount = reader.read(inbuf)) == -1) {
+ inbufPos = -1;
+ break;
+ }
+ inbufPos = 0;
+ }
+ nextChar = (char) inbuf[inbufPos++]; // & 0xff
+ // not
+ // required
+ if (nextChar == '\r' || nextChar == '\n') {
+ break;
+ }
+ }
+ continue;
+ }
+ break;
+ case '\n':
+ if (mode == CONTINUE) { // Part of a \r\n sequence
+ mode = IGNORE; // Ignore whitespace on the next line
+ continue;
+ }
+ // fall into the next case
+ case '\r':
+ mode = NONE;
+ firstChar = true;
+ if (offset > 0) {
+ if (keyLength == -1) {
+ keyLength = offset;
+ }
+ String temp = new String(buf, 0, offset);
+ put(temp.substring(0, keyLength), temp
+ .substring(keyLength));
+ }
+ keyLength = -1;
+ offset = 0;
+ continue;
+ case '\\':
+ if (mode == KEY_DONE) {
+ keyLength = offset;
+ }
+ mode = SLASH;
+ continue;
+ case ':':
+ case '=':
+ if (keyLength == -1) { // if parsing the key
+ mode = NONE;
+ keyLength = offset;
+ continue;
+ }
+ break;
+ }
+ if (Character.isWhitespace(nextChar)) {
+ if (mode == CONTINUE) {
+ mode = IGNORE;
+ }
+ // if key length == 0 or value length == 0
+ if (offset == 0 || offset == keyLength || mode == IGNORE) {
+ continue;
+ }
+ if (keyLength == -1) { // if parsing the key
+ mode = KEY_DONE;
+ continue;
+ }
+ }
+ if (mode == IGNORE || mode == CONTINUE) {
+ mode = NONE;
+ }
+ }
+ firstChar = false;
+ if (mode == KEY_DONE) {
+ keyLength = offset;
+ mode = NONE;
+ }
+ buf[offset++] = nextChar;
+ }
+ if (keyLength >= 0) {
+ String temp = new String(buf, 0, offset);
+ put(temp.substring(0, keyLength), temp.substring(keyLength));
+ }
+ }
/**
- * Answers all of the property names that this Properties contains.
- *
- * @return an Enumeration containing the names of all properties
- */
+ * Answers all of the property names that this Properties contains.
+ *
+ * @return an Enumeration containing the names of all properties
+ */
public Enumeration<?> propertyNames() {
if (defaults == null) {
return keys();
@@ -428,6 +599,32 @@
}
return set.keys();
}
+
+ /**
+ * Answers a set of keys in this property list whoes key and value are
+ * strings.
+ *
+ * @return a set of keys in the property list
+ *
+ * @since 1.6
+ */
+ public Set<String> stringPropertyNames(){
+ HashSet<String> set = new HashSet<String>();
+ Enumeration<?> keys = propertyNames();
+ while (keys.hasMoreElements()) {
+ Object key = keys.nextElement();
+ if (key instanceof String) {
+ Object value = this.get(key);
+ if (value == null){
+ value = this.defaults.get(key);
+ }
+ if (value instanceof String){
+ set.add((String)key);
+ }
+ }
+ }
+ return Collections.unmodifiableSet(set);
+ }
/**
* Saves the mappings in this Properties to the specified OutputStream,
@@ -482,7 +679,7 @@
* @exception ClassCastException
* when the key or value of a mapping is not a String
*/
- public synchronized void store(OutputStream out, String comment)
+ public synchronized void store(OutputStream out, String comments)
throws IOException {
if (lineSeparator == null) {
lineSeparator = AccessController
@@ -491,9 +688,9 @@
StringBuilder buffer = new StringBuilder(200);
OutputStreamWriter writer = new OutputStreamWriter(out, "ISO8859_1"); //$NON-NLS-1$
- if (comment != null) {
+ if (comments != null) {
writer.write("#"); //$NON-NLS-1$
- writer.write(comment);
+ writer.write(comments);
writer.write(lineSeparator);
}
writer.write("#"); //$NON-NLS-1$
@@ -511,6 +708,46 @@
}
writer.flush();
}
+
+ /**
+ * Stores the mappings in this Properties to the specified OutputStream,
+ * putting the specified comment at the beginning. The output from this
+ * method is suitable for being read by the load() method.
+ *
+ * @param writer
+ * the writer
+ * @param comments
+ * the comment
+ * @throws IOException
+ * if any I/O exception occurs
+ * @since 1.6
+ */
+ public synchronized void store(Writer writer, String comments) throws IOException {
+ if (lineSeparator == null) {
+ lineSeparator = AccessController
+ .doPrivileged(new PriviAction<String>("line.separator")); //$NON-NLS-1$
+ }
+ StringBuilder buffer = new StringBuilder(200);
+ if (comments != null) {
+ writer.write("#"); //$NON-NLS-1$
+ writer.write(comments);
+ writer.write(lineSeparator);
+ }
+ writer.write("#"); //$NON-NLS-1$
+ writer.write(new Date().toString());
+ writer.write(lineSeparator);
+
+ for (Map.Entry<Object, Object> entry : entrySet()) {
+ String key = (String) entry.getKey();
+ dumpString(buffer, key, true);
+ buffer.append('=');
+ dumpString(buffer, (String) entry.getValue(), false);
+ buffer.append(lineSeparator);
+ writer.write(buffer.toString());
+ buffer.setLength(0);
+ }
+ writer.flush();
+ }
public synchronized void loadFromXML(InputStream in)
throws IOException, InvalidPropertiesFormatException {
Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PropertiesTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PropertiesTest.java?view=diff&rev=554894&r1=554893&r2=554894
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PropertiesTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/api/common/tests/api/java/util/PropertiesTest.java Tue Jul 10 03:02:33 2007
@@ -21,10 +21,17 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
+import java.io.StringReader;
+import java.io.Writer;
import java.util.Enumeration;
+import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
+import java.util.Scanner;
+import java.util.Set;
import tests.support.resource.Support_Resources;
@@ -38,28 +45,48 @@
* @tests java.util.Properties#Properties()
*/
public void test_Constructor() {
+ // Test for method java.util.Properties()
Properties p = new Properties();
// do something to avoid getting a variable unused warning
p.clear();
+ assertTrue("Created incorrect Properties", true);
+ }
+
+ public void test_loadsave() throws Exception{
+ Properties p = new Properties();
+ try {
+ p.load((InputStream) null);
+ fail("should throw NPE");
+ } catch (NullPointerException npe) {
+ // expected
+ }
}
/**
* @tests java.util.Properties#Properties(java.util.Properties)
*/
public void test_ConstructorLjava_util_Properties() {
+ // Test for method java.util.Properties(java.util.Properties)
if (System.getProperty("java.vendor") != null) {
- Properties p = new Properties(System.getProperties());
- assertNotNull("failed to construct correct properties", p
- .getProperty("java.vendor"));
+ try {
+ Properties p = new Properties(System.getProperties());
+ assertNotNull("failed to construct correct properties", p
+ .getProperty("java.vendor"));
+ } catch (Exception e) {
+ fail("exception occured while creating construcotr" + e);
+ }
}
+
}
/**
* @tests java.util.Properties#getProperty(java.lang.String)
*/
public void test_getPropertyLjava_lang_String() {
+ // Test for method java.lang.String
+ // java.util.Properties.getProperty(java.lang.String)
assertEquals("Did not retrieve property", "this is a test property",
- tProps.getProperty("test.prop"));
+ ((String) tProps.getProperty("test.prop")));
}
/**
@@ -67,10 +94,12 @@
* java.lang.String)
*/
public void test_getPropertyLjava_lang_StringLjava_lang_String() {
+ // Test for method java.lang.String
+ // java.util.Properties.getProperty(java.lang.String, java.lang.String)
assertEquals("Did not retrieve property", "this is a test property",
- tProps.getProperty("test.prop", "Blarg"));
- assertEquals("Did not return default value", "Gabba", tProps
- .getProperty("notInThere.prop", "Gabba"));
+ ((String) tProps.getProperty("test.prop", "Blarg")));
+ assertEquals("Did not return default value", "Gabba", ((String) tProps
+ .getProperty("notInThere.prop", "Gabba")));
}
/**
@@ -103,6 +132,7 @@
* @tests java.util.Properties#list(java.io.PrintStream)
*/
public void test_listLjava_io_PrintStream() {
+ // Test for method void java.util.Properties.list(java.io.PrintStream)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
Properties myProps = new Properties();
@@ -121,6 +151,7 @@
* @tests java.util.Properties#list(java.io.PrintWriter)
*/
public void test_listLjava_io_PrintWriter() {
+ // Test for method void java.util.Properties.list(java.io.PrintWriter)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
Properties myProps = new Properties();
@@ -136,58 +167,185 @@
}
/**
- * @throws IOException
* @tests java.util.Properties#load(java.io.InputStream)
*/
- public void test_loadLjava_io_InputStream() throws IOException {
- Properties prop = new Properties();
- InputStream is = new ByteArrayInputStream(writeProperties());
- prop.load(is);
- is.close();
+ public void test_loadLjava_io_InputStream() {
+ // Test for method void java.util.Properties.load(java.io.InputStream)
+ Properties prop = null;
+ try {
+ InputStream is;
+ prop = new Properties();
+ prop.load(is = new ByteArrayInputStream(writeProperties()));
+ is.close();
+ } catch (Exception e) {
+ fail("Exception during load test : " + e.getMessage());
+ }
+ assertEquals("Failed to load correct properties", "harmony.tests", prop
+ .getProperty("test.pkg"));
+ assertNull("Load failed to parse incorrectly", prop
+ .getProperty("commented.entry"));
+
+ prop = new Properties();
+ try {
+ prop.load(new ByteArrayInputStream("=".getBytes()));
+ } catch (IOException e) {
+ // expected
+ }
+ assertTrue("Failed to add empty key", prop.get("").equals(""));
+
+ prop = new Properties();
+ try {
+ prop.load(new ByteArrayInputStream(" = ".getBytes()));
+ } catch (IOException e) {
+ // expected
+ }
+ assertTrue("Failed to add empty key2", prop.get("").equals(""));
+
+ prop = new Properties();
+ try {
+ prop.load(new ByteArrayInputStream(" a= b".getBytes()));
+ } catch (IOException e) {
+ // expected
+ }
+ assertEquals("Failed to ignore whitespace", "b", prop.get("a"));
+
+ prop = new Properties();
+ try {
+ prop.load(new ByteArrayInputStream(" a b".getBytes()));
+ } catch (IOException e) {
+ // expected
+ }
+ assertEquals("Failed to interpret whitespace as =", "b", prop.get("a"));
+
+ prop = new Properties();
+ try {
+ prop.load(new ByteArrayInputStream("#\u008d\u00d2\na=\u008d\u00d3"
+ .getBytes("ISO8859_1")));
+ } catch (IOException e) {
+ // expected
+ }
+ assertEquals("Failed to parse chars >= 0x80", "\u008d\u00d3", prop
+ .get("a"));
+
+ prop = new Properties();
+ try {
+ prop.load(new ByteArrayInputStream(
+ "#properties file\r\nfred=1\r\n#last comment"
+ .getBytes("ISO8859_1")));
+ } catch (IOException e) {
+ // expected
+ } catch (IndexOutOfBoundsException e) {
+ fail("IndexOutOfBoundsException when last line is a comment with no line terminator");
+ }
+ assertEquals("Failed to load when last line contains a comment", "1",
+ prop.get("fred"));
+ }
+
+ /**
+ * @tests java.util.Properties#load(java.io.InputStream)
+ */
+ public void test_loadLjava_io_InputStream_subtest0() {
+ try {
+ InputStream is = Support_Resources
+ .getStream("hyts_PropertiesTest.properties");
+ Properties props = new Properties();
+ props.load(is);
+ is.close();
+ assertEquals("1", "\n \t \f", props.getProperty(" \r"));
+ assertEquals("2", "a", props.getProperty("a"));
+ assertEquals("3", "bb as,dn ", props.getProperty("b"));
+ assertEquals("4", ":: cu", props.getProperty("c\r \t\nu"));
+ assertEquals("5", "bu", props.getProperty("bu"));
+ assertEquals("6", "d\r\ne=e", props.getProperty("d"));
+ assertEquals("7", "fff", props.getProperty("f"));
+ assertEquals("8", "g", props.getProperty("g"));
+ assertEquals("9", "", props.getProperty("h h"));
+ assertEquals("10", "i=i", props.getProperty(" "));
+ assertEquals("11", " j", props.getProperty("j"));
+ assertEquals("12", " c", props.getProperty("space"));
+ assertEquals("13", "\\", props.getProperty("dblbackslash"));
+ } catch (IOException e) {
+ fail("Unexpected: " + e);
+ }
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Properties#load(java.io.Reader)
+ * @since 1.6
+ */
+ public void test_loadLjava_io_Reader() throws IOException {
+ Properties prop = null;
+ try {
+ InputStream is;
+ prop = new Properties();
+ is = new ByteArrayInputStream(writeProperties());
+ prop.load(new InputStreamReader(is));
+ is.close();
+ } catch (Exception e) {
+ fail("Exception during load test : " + e.getMessage());
+ }
assertEquals("Failed to load correct properties", "harmony.tests", prop
.getProperty("test.pkg"));
assertNull("Load failed to parse incorrectly", prop
.getProperty("commented.entry"));
prop = new Properties();
- prop.load(new ByteArrayInputStream("=".getBytes()));
+ prop.load(new StringReader("="));
assertTrue("Failed to add empty key", prop.get("").equals(""));
prop = new Properties();
- prop.load(new ByteArrayInputStream(" = ".getBytes()));
+ prop.load(new StringReader(" = "));
assertTrue("Failed to add empty key2", prop.get("").equals(""));
prop = new Properties();
- prop.load(new ByteArrayInputStream(" a= b".getBytes()));
+ prop.load(new StringReader(" a= b"));
assertEquals("Failed to ignore whitespace", "b", prop.get("a"));
prop = new Properties();
- prop.load(new ByteArrayInputStream(" a b".getBytes()));
+ prop.load(new StringReader(" a b"));
assertEquals("Failed to interpret whitespace as =", "b", prop.get("a"));
prop = new Properties();
- prop.load(new ByteArrayInputStream("#\u008d\u00d2\na=\u008d\u00d3"
- .getBytes("ISO8859_1")));
- assertEquals("Failed to parse chars >= 0x80", "\u008d\u00d3", prop
- .get("a"));
+ prop.load(new InputStreamReader(new ByteArrayInputStream(
+ "#comment\na=value".getBytes("UTF-8"))));
+ assertEquals("value", prop.getProperty("a"));
prop = new Properties();
- prop.load(new ByteArrayInputStream(
- "#properties file\r\nfred=1\r\n#last comment"
- .getBytes("ISO8859_1")));
+ prop.load(new InputStreamReader(new ByteArrayInputStream(
+ "#\u008d\u00d2\na=\u008d\u00d3".getBytes("UTF-8"))));
+ try {
+ prop
+ .load(new InputStreamReader(new ByteArrayInputStream(
+ "#\u008d\u00d2\na=\\\\u008d\\\\\\uu00d3"
+ .getBytes("UTF-8"))));
+ fail("Should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ prop = new Properties();
+ try {
+ prop.load(new InputStreamReader(new ByteArrayInputStream(
+ "#properties file\r\nfred=1\r\n#last comment"
+ .getBytes("ISO8859_1"))));
+ } catch (IndexOutOfBoundsException e) {
+ fail("IndexOutOfBoundsException when last line is a comment with no line terminator");
+ }
assertEquals("Failed to load when last line contains a comment", "1",
prop.get("fred"));
}
/**
* @throws IOException
- * @tests java.util.Properties#load(java.io.InputStream)
+ * @tests java.util.Properties#load(java.io.Reader)
+ * @since 1.6
*/
- public void test_loadLjava_io_InputStream_subtest0() throws IOException {
+ public void test_loadLjava_io_Reader_subtest0() throws IOException {
InputStream is = Support_Resources
.getStream("hyts_PropertiesTest.properties");
Properties props = new Properties();
- props.load(is);
+ props.load(new InputStreamReader(is));
is.close();
assertEquals("1", "\n \t \f", props.getProperty(" \r"));
assertEquals("2", "a", props.getProperty("a"));
@@ -208,8 +366,13 @@
* @tests java.util.Properties#propertyNames()
*/
public void test_propertyNames() {
+ // Test for method java.util.Enumeration
+ // java.util.Properties.propertyNames()
+
Enumeration names = tProps.propertyNames();
+ int i = 0;
while (names.hasMoreElements()) {
+ ++i;
String p = (String) names.nextElement();
assertTrue("Incorrect names returned", p.equals("test.prop")
|| p.equals("bogus.prop"));
@@ -217,31 +380,85 @@
}
/**
- * @throws IOException
+ * @tests {@link java.util.Properties#stringPropertyNames()}
+ * @since 1.6
+ */
+ public void test_stringPropertyNames() {
+ Set<String> set = tProps.stringPropertyNames();
+ assertEquals(2, set.size());
+ assertTrue(set.contains("test.prop"));
+ assertTrue(set.contains("bogus.prop"));
+ assertNotSame(set, tProps.stringPropertyNames());
+
+ set = new Properties().stringPropertyNames();
+ assertEquals(0, set.size());
+
+ set = new Properties(System.getProperties()).stringPropertyNames();
+ assertTrue(set.size() > 0);
+
+ tProps = new Properties(tProps);
+ tProps.put("test.prop", "anotherValue");
+ tProps.put("3rdKey", "3rdValue");
+ set = tProps.stringPropertyNames();
+ assertEquals(3, set.size());
+ assertTrue(set.contains("test.prop"));
+ assertTrue(set.contains("bogus.prop"));
+ assertTrue(set.contains("3rdKey"));
+
+ tProps.put(String.class, "valueOfNonStringKey");
+ set = tProps.stringPropertyNames();
+ assertEquals(3, set.size());
+ assertTrue(set.contains("test.prop"));
+ assertTrue(set.contains("bogus.prop"));
+ assertTrue(set.contains("3rdKey"));
+
+ tProps.put("4thKey", "4thValue");
+ assertEquals(4, tProps.size());
+ assertEquals(3, set.size());
+
+ try {
+ set.add("another");
+ fail("Should throw UnsupportedOperationException");
+ } catch (UnsupportedOperationException e) {
+ // expected
+ }
+ }
+
+ /**
* @tests java.util.Properties#save(java.io.OutputStream, java.lang.String)
*/
- public void test_saveLjava_io_OutputStreamLjava_lang_String()
- throws IOException {
+ public void test_saveLjava_io_OutputStreamLjava_lang_String() {
+ // Test for method void java.util.Properties.save(java.io.OutputStream,
+ // java.lang.String)
Properties myProps = new Properties();
+ Properties myProps2 = new Properties();
+ Enumeration e;
+ String nextKey;
+
myProps.setProperty("Property A", "aye");
myProps.setProperty("Property B", "bee");
myProps.setProperty("Property C", "see");
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- myProps.save(out, "A Header");
- out.close();
-
- Properties myProps2 = new Properties();
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- myProps2.load(in);
- in.close();
+ try {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ myProps.save(out, "A Header");
+ out.close();
+ ByteArrayInputStream in = new ByteArrayInputStream(out
+ .toByteArray());
+ myProps2.load(in);
+ in.close();
+ } catch (IOException ioe) {
+ fail("IOException occurred reading/writing file : "
+ + ioe.getMessage());
+ }
- Enumeration e = myProps.propertyNames();
+ e = myProps.propertyNames();
while (e.hasMoreElements()) {
- String nextKey = (String) e.nextElement();
+ nextKey = (String) e.nextElement();
assertTrue("Stored property list not equal to original", myProps2
.getProperty(nextKey).equals(myProps.getProperty(nextKey)));
}
+
}
/**
@@ -249,6 +466,8 @@
* java.lang.String)
*/
public void test_setPropertyLjava_lang_StringLjava_lang_String() {
+ // Test for method java.lang.Object
+ // java.util.Properties.setProperty(java.lang.String, java.lang.String)
Properties myProps = new Properties();
myProps.setProperty("Yoink", "Yabba");
assertEquals("Failed to set property", "Yabba", myProps
@@ -259,53 +478,133 @@
}
/**
- * @throws IOException
* @tests java.util.Properties#store(java.io.OutputStream, java.lang.String)
*/
- public void test_storeLjava_io_OutputStreamLjava_lang_String()
- throws IOException {
+ public void test_storeLjava_io_OutputStreamLjava_lang_String() {
+ // Test for method void java.util.Properties.store(java.io.OutputStream,
+ // java.lang.String)
Properties myProps = new Properties();
+ Properties myProps2 = new Properties();
+ Enumeration e;
+ String nextKey;
+
myProps.put("Property A", " aye\\\f\t\n\r\b");
myProps.put("Property B", "b ee#!=:");
myProps.put("Property C", "see");
+ try {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ myProps.store(out, "A Header");
+ out.close();
+ ByteArrayInputStream in = new ByteArrayInputStream(out
+ .toByteArray());
+ myProps2.load(in);
+ in.close();
+ } catch (IOException ioe) {
+ fail("IOException occurred reading/writing file : "
+ + ioe.getMessage());
+ }
+
+ e = myProps.propertyNames();
+ while (e.hasMoreElements()) {
+ nextKey = (String) e.nextElement();
+ assertTrue("Stored property list not equal to original", myProps2
+ .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
+ }
+
+ }
+
+ /**
+ * @throws IOException
+ * @tests java.util.Properties#store(java.io.Writer, java.lang.String)
+ * @since 1.6
+ */
+ public void test_storeLjava_io_WriterLjava_lang_String() throws IOException {
+ Properties myProps = new Properties();
Properties myProps2 = new Properties();
+ Enumeration e;
+ String nextKey;
+
+ myProps.put("Property A", " aye\\\f\t\n\r\b");
+ myProps.put("Property B", "b ee#!=:");
+ myProps.put("Property C", "see");
+
ByteArrayOutputStream out = new ByteArrayOutputStream();
- myProps.store(out, "A Header");
+ myProps.store(new OutputStreamWriter(out), "A Header");
+ Scanner scanner = new Scanner(out.toString());
+ assertTrue(scanner.nextLine().startsWith("#A Header"));
+ assertTrue(scanner.nextLine().startsWith("#"));
out.close();
-
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
myProps2.load(in);
in.close();
- Enumeration e = myProps.propertyNames();
+ e = myProps.propertyNames();
while (e.hasMoreElements()) {
- String nextKey = (String) e.nextElement();
+ nextKey = (String) e.nextElement();
assertTrue("Stored property list not equal to original", myProps2
.getProperty(nextKey).equals(myProps.getProperty(nextKey)));
}
+
+ try {
+ myProps.store((Writer) null, "some comments");
+ fail("Should throw NullPointerException");
+ } catch (NullPointerException e1) {
+ // expected
+ }
+
+ myProps.put(String.class, "wrong type");
+ try {
+ myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
+ "some comments");
+ fail("Should throw ClassCastException");
+ } catch (ClassCastException e1) {
+ // expected
+ }
+ myProps.remove(String.class);
+ myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
+ "some comments");
+ // it is OK
+ myProps.put("wrong type", String.class);
+ try {
+ myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
+ "some comments");
+ fail("Should throw ClassCastException");
+ } catch (ClassCastException e1) {
+ // expected
+ }
}
/**
- * @throws IOException
* @tests java.util.Properties#loadFromXML(java.io.InputStream)
*/
- public void test_loadFromXMLLjava_io_InputStream() throws IOException {
- Properties prop = new Properties();
- InputStream is = new ByteArrayInputStream(writePropertiesXML("UTF-8"));
- prop.loadFromXML(is);
- is.close();
-
+ public void test_loadFromXMLLjava_io_InputStream() {
+ // Test for method void
+ // java.util.Properties.loadFromXML(java.io.InputStream)
+ Properties prop = null;
+ try {
+ InputStream is;
+ prop = new Properties();
+ prop.loadFromXML(is = new ByteArrayInputStream(
+ writePropertiesXMLUTF_8()));
+ is.close();
+ } catch (Exception e) {
+ fail("Exception during load test : " + e.getMessage());
+ }
assertEquals("Failed to load correct properties", "value3", prop
.getProperty("key3"));
assertEquals("Failed to load correct properties", "value1", prop
.getProperty("key1"));
- prop = new Properties();
- is = new ByteArrayInputStream(writePropertiesXML("ISO-8859-1"));
- prop.loadFromXML(is);
- is.close();
-
+ try {
+ InputStream is;
+ prop = new Properties();
+ prop.loadFromXML(is = new ByteArrayInputStream(
+ writePropertiesXMLISO_8859_1()));
+ is.close();
+ } catch (Exception e) {
+ fail("Exception during load test : " + e.getMessage());
+ }
assertEquals("Failed to load correct properties", "value2", prop
.getProperty("key2"));
assertEquals("Failed to load correct properties", "value1", prop
@@ -313,13 +612,18 @@
}
/**
- * @throws IOException
* @tests java.util.Properties#storeToXML(java.io.OutputStream,
* java.lang.String, java.lang.String)
*/
- public void test_storeToXMLLjava_io_OutputStreamLjava_lang_StringLjava_lang_String()
- throws IOException {
+ public void test_storeToXMLLjava_io_OutputStreamLjava_lang_StringLjava_lang_String() {
+ // Test for method void
+ // java.util.Properties.storeToXML(java.io.OutputStream,
+ // java.lang.String, java.lang.String)
Properties myProps = new Properties();
+ Properties myProps2 = new Properties();
+ Enumeration e;
+ String nextKey;
+
myProps.setProperty("key1", "value1");
myProps.setProperty("key2", "value2");
myProps.setProperty("key3", "value3");
@@ -335,36 +639,53 @@
myProps.setProperty("<a>&key13<</a>",
"&&value13<b>&</b>");
- // store in UTF-8 encoding
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- myProps.storeToXML(out, "comment");
- out.close();
+ try {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- Properties myProps2 = new Properties();
- myProps2.loadFromXML(in);
- in.close();
+ // store in UTF-8 encoding
+ myProps.storeToXML(out, "comment");
+ out.close();
+ ByteArrayInputStream in = new ByteArrayInputStream(out
+ .toByteArray());
+ myProps2.loadFromXML(in);
+ in.close();
+ } catch (InvalidPropertiesFormatException ipfe) {
+ fail("InvalidPropertiesFormatException occurred reading file: "
+ + ipfe.getMessage());
+ } catch (IOException ioe) {
+ fail("IOException occurred reading/writing file : "
+ + ioe.getMessage());
+ }
- Enumeration e = myProps.propertyNames();
+ e = myProps.propertyNames();
while (e.hasMoreElements()) {
- String nextKey = (String) e.nextElement();
+ nextKey = (String) e.nextElement();
assertTrue("Stored property list not equal to original", myProps2
.getProperty(nextKey).equals(myProps.getProperty(nextKey)));
}
- // store in ISO-8859-1 encoding
- out = new ByteArrayOutputStream();
- myProps.storeToXML(out, "comment", "ISO-8859-1");
- out.close();
+ try {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
- in = new ByteArrayInputStream(out.toByteArray());
- myProps2 = new Properties();
- myProps2.loadFromXML(in);
- in.close();
+ // store in ISO-8859-1 encoding
+ myProps.storeToXML(out, "comment", "ISO-8859-1");
+ out.close();
+ ByteArrayInputStream in = new ByteArrayInputStream(out
+ .toByteArray());
+ myProps2 = new Properties();
+ myProps2.loadFromXML(in);
+ in.close();
+ } catch (InvalidPropertiesFormatException ipfe) {
+ fail("InvalidPropertiesFormatException occurred reading file: "
+ + ipfe.getMessage());
+ } catch (IOException ioe) {
+ fail("IOException occurred reading/writing file : "
+ + ioe.getMessage());
+ }
e = myProps.propertyNames();
while (e.hasMoreElements()) {
- String nextKey = (String) e.nextElement();
+ nextKey = (String) e.nextElement();
assertTrue("Stored property list not equal to original", myProps2
.getProperty(nextKey).equals(myProps.getProperty(nextKey)));
}
@@ -374,8 +695,8 @@
* Sets up the fixture, for example, open a network connection. This method
* is called before a test is executed.
*/
- protected void setUp() throws Exception {
- super.setUp();
+ protected void setUp() {
+
tProps = new Properties();
tProps.put("test.prop", "this is a test property");
tProps.put("bogus.prop", "bogus");
@@ -385,14 +706,17 @@
* Tears down the fixture, for example, close a network connection. This
* method is called after a test is executed.
*/
- protected void tearDown() throws Exception {
- tProps = null;
- super.tearDown();
+ protected void tearDown() {
}
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
protected byte[] writeProperties() throws IOException {
+ PrintStream ps = null;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
- PrintStream ps = new PrintStream(bout);
+ ps = new PrintStream(bout);
ps.println("#commented.entry=Bogus");
ps.println("test.pkg=harmony.tests");
ps.println("test.proj=Automated Tests");
@@ -400,10 +724,29 @@
return bout.toByteArray();
}
- protected byte[] writePropertiesXML(String encoding) throws IOException {
+ protected byte[] writePropertiesXMLUTF_8() throws IOException {
+ PrintStream ps = null;
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ ps = new PrintStream(bout, true, "UTF-8");
+ ps.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+ ps
+ .println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
+ ps.println("<properties>");
+ ps.println("<comment>comment</comment>");
+ ps.println("<entry key=\"key4\">value4</entry>");
+ ps.println("<entry key=\"key3\">value3</entry>");
+ ps.println("<entry key=\"key2\">value2</entry>");
+ ps.println("<entry key=\"key1\"><!-- xml comment -->value1</entry>");
+ ps.println("</properties>");
+ ps.close();
+ return bout.toByteArray();
+ }
+
+ protected byte[] writePropertiesXMLISO_8859_1() throws IOException {
+ PrintStream ps = null;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
- PrintStream ps = new PrintStream(bout, true, encoding);
- ps.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
+ ps = new PrintStream(bout, true, "ISO-8859-1");
+ ps.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
ps
.println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
ps.println("<properties>");