privatestaticvoid BlankLines() throws Exception { // write a single space to the output stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(' '); // test empty properties
Properties prop1 = new Properties();
// now load the file we just created, into a properties object. // the properties object should have no elements, but due to a // bug, it has an empty key/value. key = "", value = ""
Properties prop2 = new Properties();
prop2.load(getInputStream(baos.toByteArray()));
check(prop1, prop2);
// test reader
prop2 = new Properties();
prop2.load(getReader(baos.toByteArray(), "UTF-8"));
check(prop1, prop2);
report("BlinkLine");
}
privatestaticvoid EscapeSpace() throws Exception {
String propsCases = "key1=\\ \\ Value\\u4e001, has leading and trailing spaces\\ \n" + "key2=Value\\u4e002,\\ does not have\\ leading or trailing\\ spaces\n" + "key3=Value\\u4e003,has,no,spaces\n" + "key4=Value\\u4e004, does not have leading spaces\\ \n" + "key5=\\t\\ \\ Value\\u4e005, has leading tab and no trailing spaces\n" + "key6=\\ \\ Value\\u4e006,doesnothaveembeddedspaces\\ \\ \n" + "\\ key1\\ test\\ =key1, has leading and trailing spaces \n" + "key2\\ test=key2, does not have leading or trailing spaces\n" + "key3test=key3,has,no,spaces\n" + "key4\\ test\\ =key4, does not have leading spaces \n" + "\\t\\ key5\\ test=key5, has leading tab and no trailing spaces\n" + "\\ \\ key6\\ \\ =\\ key6,doesnothaveembeddedspaces ";
// Put the same properties, but without the escape char for space in // value part.
Properties props = new Properties();
props.put("key1", " Value\u4e001, has leading and trailing spaces ");
props.put("key2", "Value\u4e002, does not have leading or trailing spaces");
props.put("key3", "Value\u4e003,has,no,spaces");
props.put("key4", "Value\u4e004, does not have leading spaces ");
props.put("key5", "\t Value\u4e005, has leading tab and no trailing spaces");
props.put("key6", " Value\u4e006,doesnothaveembeddedspaces ");
props.put(" key1 test ", "key1, has leading and trailing spaces ");
props.put("key2 test", "key2, does not have leading or trailing spaces");
props.put("key3test", "key3,has,no,spaces");
props.put("key4 test ", "key4, does not have leading spaces ");
props.put("\t key5 test", "key5, has leading tab and no trailing spaces");
props.put(" key6 ", " key6,doesnothaveembeddedspaces ");
// Load properties with escape character '\' before space characters
Properties props1 = new Properties();
props1.load(getInputStream(propsCases));
check(props1, props);
// Test Reader
props1 = new Properties();
props1.load(getReader(propsCases, "UTF-8"));
check(props1, props);
// Also store the new properties to a storage
ByteArrayOutputStream baos = new ByteArrayOutputStream();
props.store(baos, "EscapeSpace Test");
props1 = new Properties();
props1.load(getInputStream(baos.toByteArray()));
check(props1, props);
// Reader should work as well
props1 = new Properties();
props1.load(getReader(baos.toByteArray(), "UTF-8"));
check(props1, props);
props1 = new Properties();
props1.load(getReader(baos.toByteArray(), "UTF-8"));
check(props1, props);
report("EscapeSpace");
}
privatestaticvoid LoadParsing() throws Exception {
File f = new File(System.getProperty("test.src", "."), "input.txt");
FileInputStream myIn = new FileInputStream(f);
Properties myProps = new Properties(); int size = 0; try {
myProps.load(myIn);
} finally {
myIn.close();
}
if (!myProps.getProperty("key1").equals("value1") || // comment
!myProps.getProperty("key2").equals("abc\\defg\\") || // slash
!myProps.getProperty("key3").equals("value3") || // spaces in key
!myProps.getProperty("key4").equals(":value4") || // separator // Does not recognize comments with leading spaces
(myProps.getProperty("#") != null) || // Wrong number of keys in Properties
((size=myProps.size()) != 4))
failCount++;
report("LoadParsing");
}
privatestaticvoid SaveEncoding() throws Exception { // Create a properties object to save
Properties props = new Properties();
props.put("signal", "val\u0019");
props.put("ABC 10", "value0");
props.put("\uff10test", "value\u0020");
props.put("key with spaces", "value with spaces");
props.put("key with space and Chinese_\u4e00", "valueWithChinese_\u4e00");
props.put(" special#=key ", "value3");
// Save the properties and check output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
props.store(baos,"A test");
// Read properties file and verify \u0019
BufferedReader in = new BufferedReader( new InputStreamReader( new ByteArrayInputStream(
baos.toByteArray())));
String firstLine = "foo"; while (!firstLine.startsWith("signal"))
firstLine = in.readLine(); if (firstLine.length() != 16)
failCount++;
// Load the properties set
Properties newProps = new Properties();
newProps.load(getInputStream(baos.toByteArray()));
check(newProps, props);
// Store(Writer)/Load(Reader)
baos = new ByteArrayOutputStream();
props.store(new OutputStreamWriter(baos, "EUC_JP"), "A test");
newProps = new Properties();
newProps.load(getReader(baos.toByteArray(), "EUC_JP"));
check(newProps, props);
/* Note: this regression test only detects incorrect line *separatoronplatformrunningthetest
*/ privatestaticvoid SaveSeparator() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Properties props = new Properties();
props.store(baos, "A test");
// Examine the result to verify that line.separator was used
String theSeparator = System.getProperty("line.separator");
String content = baos.toString(); if (!content.endsWith(theSeparator))
failCount++;
// store(Writer)
baos = new ByteArrayOutputStream();
props.store(new OutputStreamWriter(baos, "UTF-8"), "A test");
content = baos.toString(); if (!content.endsWith(theSeparator))
failCount++;
report("SaveSeparator");
}
// Ensure that the save method doesn't close its output stream privatestaticvoid SaveClose() throws Exception {
Properties p = new Properties();
p.put("Foo", "Bar"); class MyOS extends ByteArrayOutputStream { boolean closed = false; publicvoid close() throws IOException { this.closed = true;
}
}
MyOS myos = new MyOS();
p.store(myos, "Test"); if (myos.closed)
failCount++;
p.store(new OutputStreamWriter(myos, "UTF-8"), "Test"); if (myos.closed)
failCount++;
privatestaticvoid SaveComments() throws Exception {
String ls = System.getProperty("line.separator");
String[] input = new String[] { "Comments with \u4e2d\u6587\u6c49\u5b57 included", "Comments with \n Second comments line", "Comments with \n# Second comments line", "Comments with \n! Second comments line", "Comments with last character is \n", "Comments with last character is \r\n", "Comments with last two characters are \n\n", "Comments with last four characters are \r\n\r\n", "Comments with \nkey4=value4", "Comments with \n#key4=value4"};
String[] output = new String[] { "#Comments with \\u4E2D\\u6587\\u6C49\\u5B57 included" + ls, "#Comments with " + ls + "# Second comments line" + ls, "#Comments with " + ls + "# Second comments line" + ls, "#Comments with " + ls + "! Second comments line" + ls, "#Comments with last character is " + ls+"#"+ls, "#Comments with last character is " + ls+"#"+ls, "#Comments with last two characters are " + ls+"#"+ls+"#"+ls, "#Comments with last four characters are " + ls+"#"+ls+"#"+ls};
Properties props = new Properties();
ByteArrayOutputStream baos; int i = 0; for (i = 0; i < output.length; i++) {
baos = new ByteArrayOutputStream();
props.store(baos, input[i]);
String result = baos.toString("iso8859-1"); if (result.indexOf(output[i]) == -1) {
failCount++;
}
}
props.put("key1", "value1");
props.put("key2", "value2");
props.put("key3", "value3"); for (; i < input.length; i++) {
baos = new ByteArrayOutputStream();
props.store(baos, input[i]);
Properties propsNew = new Properties();
propsNew.load(getInputStream(baos.toByteArray()));
check(propsNew, props);
baos = new ByteArrayOutputStream();
props.store(new OutputStreamWriter(baos, "UTF-8"),
input[i]);
propsNew = new Properties();
propsNew.load(getReader(baos.toByteArray(), "UTF-8"));
check(propsNew, props);
}
report("SaveComments");
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-09-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.