test/java/util/Properties/LoadAndStoreXML.java
Print this page
@@ -21,17 +21,20 @@
* questions.
*/
/*
* @test
- * @bug 8000354 8000685
+ * @bug 8000354 8000685 8004371
* @summary Basic test of storeToXML and loadToXML
+ * @run main LoadAndStoreXML
+ * @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider LoadAndStoreXML
*/
import java.io.*;
import java.util.*;
import java.security.*;
+import java.nio.file.*;
public class LoadAndStoreXML {
/**
* Simple policy implementation that grants a set of permissions to
@@ -65,10 +68,12 @@
/**
* Sanity test that properties saved with Properties#storeToXML can be
* read with Properties#loadFromXML.
*/
static void testLoadAndStore(String encoding) throws IOException {
+ System.out.println("testLoadAndStore, encoding=" + encoding);
+
Properties props = new Properties();
props.put("k1", "foo");
props.put("k2", "bar");
ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -87,10 +92,12 @@
/**
* Test loadFromXML with a document that does not have an encoding declaration
*/
static void testLoadWithoutEncoding() throws IOException {
+ System.out.println("testLoadWithoutEncoding");
+
Properties expected = new Properties();
expected.put("foo", "bar");
String s = "<?xml version=\"1.0\"?>" +
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
@@ -109,10 +116,11 @@
/**
* Test loadFromXML with unsupported encoding
*/
static void testLoadWithBadEncoding() throws IOException {
+ System.out.println("testLoadWithBadEncoding");
String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
"<properties>" +
"<entry key=\"foo\">bar</entry>" +
"</properties>";
@@ -126,27 +134,54 @@
/**
* Test storeToXML with unsupported encoding
*/
static void testStoreWithBadEncoding() throws IOException {
+ System.out.println("testStoreWithBadEncoding");
Properties props = new Properties();
props.put("foo", "bar");
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
props.storeToXML(out, null, "BAD");
throw new RuntimeException("UnsupportedEncodingException expected");
} catch (UnsupportedEncodingException expected) { }
}
+ /**
+ * Test loadFromXML with malformed documents
+ */
+ static void testLoadWithMalformedDoc(Path dir) throws IOException {
+ try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) {
+ for (Path file: stream) {
+ System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName());
+ try (InputStream in = Files.newInputStream(file)) {
+ Properties props = new Properties();
+ try {
+ props.loadFromXML(in);
+ throw new RuntimeException("InvalidPropertiesFormatException not thrown");
+ } catch (InvalidPropertiesFormatException x) {
+ System.out.println(x);
+ }
+ }
+ }
+ }
+ }
+
public static void main(String[] args) throws IOException {
testLoadAndStore("UTF-8");
testLoadAndStore("UTF-16");
testLoadWithoutEncoding();
testLoadWithBadEncoding();
testStoreWithBadEncoding();
+ // malformed documents
+ String src = System.getProperty("test.src");
+ String subdir = "invalidxml";
+ Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir);
+ testLoadWithMalformedDoc(dir);
+
// re-run sanity test with security manager
Policy orig = Policy.getPolicy();
Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
new PropertyPermission("line.separator", "read"));
Policy.setPolicy(p);