You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by re...@apache.org on 2009/04/24 08:33:56 UTC
svn commit: r768167 - 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: regisxu
Date: Fri Apr 24 06:33:56 2009
New Revision: 768167
URL: http://svn.apache.org/viewvc?rev=768167&view=rev
Log:
Apply fix for HARMONY-6162: [classlib][luni][java6] java.util.Properties#load(java.io.InputStream) should throw IllegalArgumentException if the given InputStream contains an Invalid Unicode sequence
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=768167&r1=768166&r2=768167&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 Fri Apr 24 06:33:56 2009
@@ -269,7 +269,21 @@
while (true) {
intVal = bis.read();
- if (intVal == -1) break;
+ if (intVal == -1) {
+ // if mode is UNICODE but has less than 4 hex digits, should
+ // throw an IllegalArgumentException
+ // luni.08=Invalid Unicode sequence: expected format \\uxxxx
+ if (mode == UNICODE && count < 4) {
+ throw new IllegalArgumentException(Messages
+ .getString("luni.08")); //$NON-NLS-1$
+ }
+ // if mode is SLASH and no data is read, should append '\u0000'
+ // to buf
+ if (mode == SLASH) {
+ buf[offset++] = '\u0000';
+ }
+ break;
+ }
nextChar = (char) (intVal & 0xff);
if (offset == buf.length) {
@@ -284,8 +298,10 @@
if (++count < 4) {
continue;
}
- } else {
- throw new IllegalArgumentException();
+ } else if (count <= 4) {
+ // luni.09=Invalid Unicode sequence: illegal character
+ throw new IllegalArgumentException(Messages
+ .getString("luni.09")); //$NON-NLS-1$
}
mode = NONE;
buf[offset++] = (char) unicode;
@@ -419,6 +435,9 @@
* @since 1.6
*/
public synchronized void load(Reader reader) throws IOException {
+ if (reader == null) {
+ throw new NullPointerException();
+ }
int mode = NONE, unicode = 0, count = 0;
char nextChar, buf[] = new char[40];
int offset = 0, keyLength = -1, intVal;
@@ -427,7 +446,21 @@
while (true) {
intVal = br.read();
- if (intVal == -1) break;
+ if (intVal == -1) {
+ // if mode is UNICODE but has less than 4 hex digits, should
+ // throw an IllegalArgumentException
+ // luni.08=Invalid Unicode sequence: expected format \\uxxxx
+ if (mode == UNICODE && count < 4) {
+ throw new IllegalArgumentException(Messages
+ .getString("luni.08")); //$NON-NLS-1$
+ }
+ // if mode is SLASH and no data is read, should append '\u0000'
+ // to buf
+ if (mode == SLASH) {
+ buf[offset++] = '\u0000';
+ }
+ break;
+ }
nextChar = (char) intVal;
if (offset == buf.length) {
@@ -560,21 +593,12 @@
}
buf[offset++] = nextChar;
}
- if (mode == UNICODE && count <= 4) {
- // luni.08=Invalid Unicode sequence: expected format \\uxxxx
- throw new IllegalArgumentException(Messages.getString("luni.08"));
- }
if (keyLength == -1 && offset > 0) {
keyLength = offset;
}
if (keyLength >= 0) {
String temp = new String(buf, 0, offset);
- String key = temp.substring(0, keyLength);
- String value = temp.substring(keyLength);
- if (mode == SLASH) {
- value += "\u0000";
- }
- put(key, value);
+ put(temp.substring(0, keyLength), temp.substring(keyLength));
}
}
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=768167&r1=768166&r2=768167&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 Fri Apr 24 06:33:56 2009
@@ -25,10 +25,10 @@
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.util.Enumeration;
-import java.util.InvalidPropertiesFormatException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Properties;
@@ -47,48 +47,54 @@
* @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);
+ assertTrue("Created incorrect Properties", p.isEmpty());
}
-
- public void test_loadsave() throws Exception{
+
+ public void test_loadLjava_io_InputStream_NPE() throws Exception {
Properties p = new Properties();
try {
p.load((InputStream) null);
- fail("should throw NPE");
+ fail("should throw NullPointerException");
+ } catch (NullPointerException e) {
+ // Expected
+ }
+ }
+
+ public void test_loadLjava_io_Reader_NPE() throws Exception {
+ Properties p = new Properties();
+ try {
+ p.load((Reader) null);
+ fail("should throw NullPointerException");
} catch (NullPointerException npe) {
- // expected
- }
+ // 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) {
- 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);
- }
+ Properties systemProperties = System.getProperties();
+ Properties properties = new Properties(systemProperties);
+ Enumeration<?> propertyNames = systemProperties.propertyNames();
+ String propertyName = null;
+ while (propertyNames.hasMoreElements()) {
+ propertyName = (String) propertyNames.nextElement();
+ assertEquals("failed to construct correct properties",
+ systemProperties.get(propertyName), properties
+ .getProperty(propertyName));
}
-
}
/**
* @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",
- ((String) tProps.getProperty("test.prop")));
+ tProps.getProperty("test.prop"));
}
/**
@@ -96,12 +102,10 @@
* 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",
- ((String) tProps.getProperty("test.prop", "Blarg")));
- assertEquals("Did not return default value", "Gabba", ((String) tProps
- .getProperty("notInThere.prop", "Gabba")));
+ tProps.getProperty("test.prop", "Blarg"));
+ assertEquals("Did not return default value", "Gabba", tProps
+ .getProperty("notInThere.prop", "Gabba"));
}
/**
@@ -134,159 +138,157 @@
* @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();
- String propList;
myProps.setProperty("Abba", "Cadabra");
myProps.setProperty("Open", "Sesame");
myProps.list(ps);
ps.flush();
- propList = baos.toString();
- assertTrue("Property list innacurate", (propList
- .indexOf("Abba=Cadabra") >= 0)
- && (propList.indexOf("Open=Sesame") >= 0));
+ String propList = baos.toString();
+ assertTrue("Property list innacurate",
+ propList.indexOf("Abba=Cadabra") >= 0);
+ assertTrue("Property list innacurate",
+ propList.indexOf("Open=Sesame") >= 0);
}
/**
* @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();
- String propList;
myProps.setProperty("Abba", "Cadabra");
myProps.setProperty("Open", "Sesame");
myProps.list(pw);
pw.flush();
- propList = baos.toString();
- assertTrue("Property list innacurate", (propList
- .indexOf("Abba=Cadabra") >= 0)
- && (propList.indexOf("Open=Sesame") >= 0));
+ String propList = baos.toString();
+ assertTrue("Property list innacurate",
+ propList.indexOf("Abba=Cadabra") >= 0);
+ assertTrue("Property list innacurate",
+ propList.indexOf("Open=Sesame") >= 0);
}
/**
* @tests java.util.Properties#load(java.io.InputStream)
*/
- 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());
- }
+ public void test_loadLjava_io_InputStream() throws Exception {
+ InputStream is = new ByteArrayInputStream(writeProperties());
+ Properties prop = new Properties();
+ prop.load(is);
+ is.close();
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.load(new ByteArrayInputStream("=".getBytes()));
+ assertEquals("Failed to add empty key", "", prop.get(""));
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");
- }
+ prop.load(new ByteArrayInputStream(" = ".getBytes()));
+ assertEquals("Failed to add empty key2", "", prop.get(""));
+
+ prop = new Properties();
+ prop.load(new ByteArrayInputStream(" a= b".getBytes()));
+ assertEquals("Failed to ignore whitespace", "b", prop.get("a"));
+
+ prop = new Properties();
+ prop.load(new ByteArrayInputStream(" a b".getBytes()));
+ assertEquals("Failed to interpret whitespace as =", "b", prop.get("a"));
+
+ prop = new Properties();
+ prop.load(new ByteArrayInputStream("#comment\na=value"
+ .getBytes("UTF-8")));
+ assertEquals("value", prop.getProperty("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 = new Properties();
+ prop.load(new ByteArrayInputStream(
+ "#properties file\r\nfred=1\r\n#last comment"
+ .getBytes("ISO8859_1")));
assertEquals("Failed to load when last line contains a comment", "1",
prop.get("fred"));
+
+ // Regression tests for HARMONY-5414
+ prop = new Properties();
+ prop.load(new ByteArrayInputStream("a=\\u1234z".getBytes()));
+
+ prop = new Properties();
+ try {
+ prop.load(new ByteArrayInputStream("a=\\u123".getBytes()));
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
+
+ prop = new Properties();
+ try {
+ prop.load(new ByteArrayInputStream("a=\\u123z".getBytes()));
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+ // Expected
+ }
+
+ prop = new Properties();
+ Properties expected = new Properties();
+ expected.put("a", "\u0000");
+ prop.load(new ByteArrayInputStream("a=\\".getBytes()));
+ assertEquals("Failed to read trailing slash value", expected, prop);
+
+ prop = new Properties();
+ expected = new Properties();
+ expected.put("a", "\u1234\u0000");
+ prop.load(new ByteArrayInputStream("a=\\u1234\\".getBytes()));
+ assertEquals("Failed to read trailing slash value #2", expected, prop);
+
+ prop = new Properties();
+ expected = new Properties();
+ expected.put("a", "q");
+ prop.load(new ByteArrayInputStream("a=\\q".getBytes()));
+ assertEquals("Failed to read slash value #3", expected, prop);
}
/**
- * @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);
- }
+ * @tests java.util.Properties#load(java.io.InputStream)
+ */
+ public void test_loadLjava_io_InputStream_subtest0() throws Exception {
+ 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"));
}
/**
- * @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());
- }
+ public void test_loadLjava_io_Reader() throws Exception {
+ InputStream is = new ByteArrayInputStream(writeProperties());
+ Properties prop = new Properties();
+ prop.load(new InputStreamReader(is));
+ is.close();
assertEquals("Failed to load correct properties", "harmony.tests", prop
.getProperty("test.pkg"));
assertNull("Load failed to parse incorrectly", prop
@@ -294,11 +296,11 @@
prop = new Properties();
prop.load(new StringReader("="));
- assertTrue("Failed to add empty key", prop.get("").equals(""));
+ assertEquals("Failed to add empty key", "", prop.get(""));
prop = new Properties();
prop.load(new StringReader(" = "));
- assertTrue("Failed to add empty key2", prop.get("").equals(""));
+ assertEquals("Failed to add empty key2", "", prop.get(""));
prop = new Properties();
prop.load(new StringReader(" a= b"));
@@ -316,43 +318,44 @@
prop = new Properties();
prop.load(new InputStreamReader(new ByteArrayInputStream(
"#\u008d\u00d2\na=\u008d\u00d3".getBytes("UTF-8"))));
+
+ prop = new Properties();
try {
prop
.load(new InputStreamReader(new ByteArrayInputStream(
"#\u008d\u00d2\na=\\\\u008d\\\\\\uu00d3"
.getBytes("UTF-8"))));
- fail("Should throw IllegalArgumentException");
+ fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
- // expected
+ // 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");
- }
+ prop.load(new InputStreamReader(new ByteArrayInputStream(
+ "#properties file\r\nfred=1\r\n#last comment"
+ .getBytes("ISO8859_1"))));
assertEquals("Failed to load when last line contains a comment", "1",
prop.get("fred"));
// Regression tests for HARMONY-5414
prop = new Properties();
- prop.load(new ByteArrayInputStream("a=\\u1234z".getBytes()));
+ prop.load(new InputStreamReader(new ByteArrayInputStream("a=\\u1234z"
+ .getBytes())));
prop = new Properties();
try {
- prop.load(new ByteArrayInputStream("a=\\u123".getBytes()));
- fail("Expected IllegalArgumentException due to invalid Unicode sequence");
+ prop.load(new InputStreamReader(new ByteArrayInputStream("a=\\u123"
+ .getBytes())));
+ fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected
}
prop = new Properties();
try {
- prop.load(new ByteArrayInputStream("a=\\u123z".getBytes()));
- fail("Expected IllegalArgumentException due to invalid unicode sequence");
+ prop.load(new InputStreamReader(new ByteArrayInputStream(
+ "a=\\u123z".getBytes())));
+ fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
// Expected
}
@@ -360,24 +363,26 @@
prop = new Properties();
Properties expected = new Properties();
expected.put("a", "\u0000");
- prop.load(new ByteArrayInputStream("a=\\".getBytes()));
+ prop.load(new InputStreamReader((new ByteArrayInputStream("a=\\"
+ .getBytes()))));
assertEquals("Failed to read trailing slash value", expected, prop);
prop = new Properties();
expected = new Properties();
expected.put("a", "\u1234\u0000");
- prop.load(new ByteArrayInputStream("a=\\u1234\\".getBytes()));
+ prop.load(new InputStreamReader(new ByteArrayInputStream("a=\\u1234\\"
+ .getBytes())));
assertEquals("Failed to read trailing slash value #2", expected, prop);
prop = new Properties();
expected = new Properties();
expected.put("a", "q");
- prop.load(new ByteArrayInputStream("a=\\q".getBytes()));
+ prop.load(new InputStreamReader(new ByteArrayInputStream("a=\\q"
+ .getBytes())));
assertEquals("Failed to read slash value #3", expected, prop);
}
/**
- * @throws IOException
* @tests java.util.Properties#load(java.io.Reader)
* @since 1.6
*/
@@ -406,13 +411,8 @@
* @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"));
@@ -470,45 +470,36 @@
set.add("another");
fail("Should throw UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
- // expected
+ // Expected
}
}
/**
* @tests java.util.Properties#save(java.io.OutputStream, java.lang.String)
*/
- public void test_saveLjava_io_OutputStreamLjava_lang_String() {
- // Test for method void java.util.Properties.save(java.io.OutputStream,
- // java.lang.String)
+ public void test_saveLjava_io_OutputStreamLjava_lang_String()
+ throws Exception {
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");
- 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());
- }
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ myProps.save(out, "A Header");
+ out.close();
- e = myProps.propertyNames();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Properties myProps2 = new Properties();
+ myProps2.load(in);
+ in.close();
+
+ Enumeration e = myProps.propertyNames();
+ String nextKey;
while (e.hasMoreElements()) {
nextKey = (String) e.nextElement();
- assertTrue("Stored property list not equal to original", myProps2
- .getProperty(nextKey).equals(myProps.getProperty(nextKey)));
+ assertEquals("Stored property list not equal to original", myProps
+ .getProperty(nextKey), myProps2.getProperty(nextKey));
}
-
}
/**
@@ -516,8 +507,6 @@
* 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
@@ -530,51 +519,37 @@
/**
* @tests java.util.Properties#store(java.io.OutputStream, java.lang.String)
*/
- public void test_storeLjava_io_OutputStreamLjava_lang_String() {
- // Test for method void java.util.Properties.store(java.io.OutputStream,
- // java.lang.String)
+ public void test_storeLjava_io_OutputStreamLjava_lang_String()
+ throws Exception {
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());
- }
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ myProps.store(out, "A Header");
+ out.close();
- e = myProps.propertyNames();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Properties myProps2 = new Properties();
+ myProps2.load(in);
+ in.close();
+
+ Enumeration e = myProps.propertyNames();
+ String nextKey;
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");
@@ -586,12 +561,14 @@
assertTrue(scanner.nextLine().startsWith("#"));
out.close();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Properties myProps2 = new Properties();
myProps2.load(in);
in.close();
- e = myProps.propertyNames();
- while (e.hasMoreElements()) {
- nextKey = (String) e.nextElement();
+ Enumeration propertyNames = myProps.propertyNames();
+ String nextKey;
+ while (propertyNames.hasMoreElements()) {
+ nextKey = (String) propertyNames.nextElement();
assertTrue("Stored property list not equal to original", myProps2
.getProperty(nextKey).equals(myProps.getProperty(nextKey)));
}
@@ -599,8 +576,8 @@
try {
myProps.store((Writer) null, "some comments");
fail("Should throw NullPointerException");
- } catch (NullPointerException e1) {
- // expected
+ } catch (NullPointerException e) {
+ // Expected
}
myProps.put(String.class, "wrong type");
@@ -608,9 +585,10 @@
myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
"some comments");
fail("Should throw ClassCastException");
- } catch (ClassCastException e1) {
- // expected
+ } catch (ClassCastException e) {
+ // Expected
}
+
myProps.remove(String.class);
myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
"some comments");
@@ -620,41 +598,30 @@
myProps.store(new OutputStreamWriter(new ByteArrayOutputStream()),
"some comments");
fail("Should throw ClassCastException");
- } catch (ClassCastException e1) {
- // expected
+ } catch (ClassCastException e) {
+ // Expected
}
}
/**
* @tests java.util.Properties#loadFromXML(java.io.InputStream)
*/
- 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());
- }
+ public void test_loadFromXMLLjava_io_InputStream() throws Exception {
+ InputStream is = new ByteArrayInputStream(writePropertiesXML("UTF-8"));
+ Properties prop = new Properties();
+ prop.loadFromXML(is);
+ is.close();
+
assertEquals("Failed to load correct properties", "value3", prop
.getProperty("key3"));
assertEquals("Failed to load correct properties", "value1", prop
.getProperty("key1"));
- 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());
- }
+ is = new ByteArrayInputStream(writePropertiesXML("ISO-8859-1"));
+ prop = new Properties();
+ prop.loadFromXML(is = new ByteArrayInputStream(
+ writePropertiesXML("ISO-8859-1")));
+ is.close();
assertEquals("Failed to load correct properties", "value2", prop
.getProperty("key2"));
assertEquals("Failed to load correct properties", "value1", prop
@@ -665,15 +632,9 @@
* @tests java.util.Properties#storeToXML(java.io.OutputStream,
* java.lang.String, java.lang.String)
*/
- 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)
+ public void test_storeToXMLLjava_io_OutputStreamLjava_lang_StringLjava_lang_String()
+ throws Exception {
Properties myProps = new Properties();
- Properties myProps2 = new Properties();
- Enumeration e;
- String nextKey;
-
myProps.setProperty("key1", "value1");
myProps.setProperty("key2", "value2");
myProps.setProperty("key3", "value3");
@@ -689,49 +650,34 @@
myProps.setProperty("<a>&key13<</a>",
"&&value13<b>&</b>");
- try {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
- // 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());
- }
+ // store in UTF-8 encoding
+ myProps.storeToXML(out, "comment");
+ out.close();
- e = myProps.propertyNames();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Properties myProps2 = new Properties();
+ myProps2.loadFromXML(in);
+ in.close();
+
+ Enumeration e = myProps.propertyNames();
+ String nextKey;
while (e.hasMoreElements()) {
nextKey = (String) e.nextElement();
assertTrue("Stored property list not equal to original", myProps2
.getProperty(nextKey).equals(myProps.getProperty(nextKey)));
}
- try {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
+ // store in ISO-8859-1 encoding
+ out = new ByteArrayOutputStream();
+ myProps.storeToXML(out, "comment", "ISO-8859-1");
+ out.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());
- }
+ in = new ByteArrayInputStream(out.toByteArray());
+ myProps2 = new Properties();
+ myProps2.loadFromXML(in);
+ in.close();
e = myProps.propertyNames();
while (e.hasMoreElements()) {
@@ -740,12 +686,12 @@
.getProperty(nextKey).equals(myProps.getProperty(nextKey)));
}
}
-
+
/**
- * if loading from single line like "hello" without "\n\r" neither "=", it should be same
- * as loading from "hello="
+ * if loading from single line like "hello" without "\n\r" neither "=", it
+ * should be same as loading from "hello="
*/
- public void testLoadSingleLine() throws Exception{
+ public void testLoadSingleLine() throws Exception {
Properties props = new Properties();
InputStream sr = new ByteArrayInputStream("hello".getBytes());
props.load(sr);
@@ -757,7 +703,6 @@
* is called before a test is executed.
*/
protected void setUp() {
-
tProps = new Properties();
tProps.put("test.prop", "this is a test property");
tProps.put("bogus.prop", "bogus");
@@ -768,6 +713,7 @@
* method is called after a test is executed.
*/
protected void tearDown() {
+ tProps = null;
}
/**
@@ -775,9 +721,8 @@
* method is called after a test is executed.
*/
protected byte[] writeProperties() throws IOException {
- PrintStream ps = null;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
- ps = new PrintStream(bout);
+ PrintStream ps = new PrintStream(bout);
ps.println("#commented.entry=Bogus");
ps.println("test.pkg=harmony.tests");
ps.println("test.proj=Automated Tests");
@@ -785,29 +730,10 @@
return bout.toByteArray();
}
- 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;
+ protected byte[] writePropertiesXML(String encoding) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
- ps = new PrintStream(bout, true, "ISO-8859-1");
- ps.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
+ PrintStream ps = new PrintStream(bout, true, encoding);
+ ps.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
ps
.println("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">");
ps.println("<properties>");