Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Properties/LoadAndStoreXML.java
47182 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8000354 8000685 800437126* @summary Basic test of storeToXML and loadToXML27* @run main LoadAndStoreXML28* @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider LoadAndStoreXML29*/3031import java.io.*;32import java.util.*;33import java.security.*;34import java.nio.file.*;3536public class LoadAndStoreXML {3738/**39* Simple policy implementation that grants a set of permissions to40* all code sources and protection domains.41*/42static class SimplePolicy extends Policy {43private final Permissions perms;4445public SimplePolicy(Permission...permissions) {46perms = new Permissions();47for (Permission permission : permissions)48perms.add(permission);49}5051@Override52public PermissionCollection getPermissions(CodeSource cs) {53return perms;54}5556@Override57public PermissionCollection getPermissions(ProtectionDomain pd) {58return perms;59}6061@Override62public boolean implies(ProtectionDomain pd, Permission p) {63return perms.implies(p);64}65}6667/**68* Sanity test that properties saved with Properties#storeToXML can be69* read with Properties#loadFromXML.70*/71static void testLoadAndStore(String encoding) throws IOException {72System.out.println("testLoadAndStore, encoding=" + encoding);7374Properties props = new Properties();75props.put("k1", "foo");76props.put("k2", "bar");77props.put("k3", "\\u0020\\u0391\\u0392\\u0393\\u0394\\u0395\\u0396\\u0397");78props.put("k4", "\u7532\u9aa8\u6587");79props.put("k5", "<java.home>/lib/jaxp.properties");8081ByteArrayOutputStream out = new ByteArrayOutputStream();82props.storeToXML(out, null, encoding);8384Properties p = new Properties();85ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());86p.loadFromXML(in);8788if (!p.equals(props)) {89System.err.println("stored: " + props);90System.err.println("loaded: " + p);91throw new RuntimeException("Test failed");92}93}9495/**96* Test loadFromXML with a document that does not have an encoding declaration97*/98static void testLoadWithoutEncoding() throws IOException {99System.out.println("testLoadWithoutEncoding");100101Properties expected = new Properties();102expected.put("foo", "bar");103104String s = "<?xml version=\"1.0\"?>" +105"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +106"<properties>" +107"<entry key=\"foo\">bar</entry>" +108"</properties>";109ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));110Properties props = new Properties();111props.loadFromXML(in);112113if (!props.equals(expected)) {114System.err.println("loaded: " + props + ", expected: " + expected);115throw new RuntimeException("Test failed");116}117}118119/**120* Test loadFromXML with unsupported encoding121*/122static void testLoadWithBadEncoding() throws IOException {123System.out.println("testLoadWithBadEncoding");124String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +125"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +126"<properties>" +127"<entry key=\"foo\">bar</entry>" +128"</properties>";129ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));130Properties props = new Properties();131try {132props.loadFromXML(in);133throw new RuntimeException("UnsupportedEncodingException expected");134} catch (UnsupportedEncodingException expected) { }135}136137/**138* Test storeToXML with unsupported encoding139*/140static void testStoreWithBadEncoding() throws IOException {141System.out.println("testStoreWithBadEncoding");142Properties props = new Properties();143props.put("foo", "bar");144ByteArrayOutputStream out = new ByteArrayOutputStream();145try {146props.storeToXML(out, null, "BAD");147throw new RuntimeException("UnsupportedEncodingException expected");148} catch (UnsupportedEncodingException expected) { }149}150151/**152* Test loadFromXML with malformed documents153*/154static void testLoadWithMalformedDoc(Path dir) throws IOException {155try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) {156for (Path file: stream) {157System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName());158try (InputStream in = Files.newInputStream(file)) {159Properties props = new Properties();160try {161props.loadFromXML(in);162throw new RuntimeException("InvalidPropertiesFormatException not thrown");163} catch (InvalidPropertiesFormatException x) {164System.out.println(x);165}166}167}168}169}170171public static void main(String[] args) throws IOException {172173testLoadAndStore("UTF-8");174testLoadAndStore("UTF-16");175testLoadWithoutEncoding();176testLoadWithBadEncoding();177testStoreWithBadEncoding();178179// malformed documents180String src = System.getProperty("test.src");181String subdir = "invalidxml";182Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir);183testLoadWithMalformedDoc(dir);184185// re-run sanity test with security manager186Policy orig = Policy.getPolicy();187Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),188new PropertyPermission("line.separator", "read"));189Policy.setPolicy(p);190System.setSecurityManager(new SecurityManager());191try {192testLoadAndStore("UTF-8");193} finally {194// turn off security manager and restore policy195System.setSecurityManager(null);196Policy.setPolicy(orig);197}198199}200}201202203