Path: blob/master/test/jdk/java/util/Properties/LoadAndStoreXML.java
41149 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 8004371 804311926* @summary Basic test of storeToXML and loadToXML27* @run main/othervm -Djava.security.manager=allow LoadAndStoreXML28*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.IOException;33import java.io.InputStream;34import java.io.UnsupportedEncodingException;35import java.nio.charset.Charset;36import java.nio.file.DirectoryStream;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.security.CodeSource;41import java.security.Permission;42import java.security.PermissionCollection;43import java.security.Permissions;44import java.security.Policy;45import java.security.ProtectionDomain;46import java.util.InvalidPropertiesFormatException;47import java.util.Properties;48import java.util.PropertyPermission;4950public class LoadAndStoreXML {51static final String bomChar = "\uFEFF";5253/**54* Simple policy implementation that grants a set of permissions to55* all code sources and protection domains.56*/57static class SimplePolicy extends Policy {58private final Permissions perms;5960public SimplePolicy(Permission...permissions) {61perms = new Permissions();62for (Permission permission : permissions)63perms.add(permission);64}6566@Override67public PermissionCollection getPermissions(CodeSource cs) {68return perms;69}7071@Override72public PermissionCollection getPermissions(ProtectionDomain pd) {73return perms;74}7576@Override77public boolean implies(ProtectionDomain pd, Permission p) {78return perms.implies(p);79}80}8182/**83* A {@code ByteArrayInputStream} that allows testing if the84* {@code close} method has been invoked.85*/86static class TestInputStream extends ByteArrayInputStream {87private boolean closed;8889TestInputStream(byte[] buf) {90super(buf);91}9293boolean isOpen() {94return !closed;95}9697public void close() throws IOException {98try {99super.close();100} finally {101closed = true;102}103}104}105106/**107* A {@code ByteArrayOutputStream} that allows testing if the108* {@code close} method has been invoked.109*/110static class TestOutputStream extends ByteArrayOutputStream {111private boolean closed;112113boolean isOpen() {114return !closed;115}116117public void close() throws IOException {118try {119super.close();120} finally {121closed = true;122}123}124}125126/**127* Sanity test that properties saved with Properties#storeToXML can be128* read with Properties#loadFromXML.129*/130static void testLoadAndStore(String encoding, boolean appendBOM) throws IOException {131System.out.println("testLoadAndStore, encoding=" + encoding);132133Properties props = new Properties();134props.put("k0", "\u6C34");135props.put("k1", "foo");136props.put("k2", "bar");137props.put("k3", "\u0020\u0391\u0392\u0393\u0394\u0395\u0396\u0397");138props.put("k4", "\u7532\u9aa8\u6587");139props.put("k5", "<java.home>/conf/jaxp.properties");140141TestOutputStream out = new TestOutputStream();142props.storeToXML(out, null, encoding);143if (!out.isOpen())144throw new RuntimeException("OutputStream closed by storeToXML");145146Properties p = new Properties();147TestInputStream in;148if (appendBOM) {149byte[] byteOrderMark = bomChar.getBytes(Charset.forName(encoding));150byte[] outArray = out.toByteArray();151byte[] inputArray = new byte[byteOrderMark.length + outArray.length];152System.arraycopy(byteOrderMark, 0, inputArray, 0, byteOrderMark.length);153System.arraycopy(outArray, 0, inputArray, byteOrderMark.length, outArray.length);154in = new TestInputStream(inputArray);155} else {156in = new TestInputStream(out.toByteArray());157}158p.loadFromXML(in);159if (in.isOpen())160throw new RuntimeException("InputStream not closed by loadFromXML");161162if (!p.equals(props)) {163System.err.println("stored: " + props);164System.err.println("loaded: " + p);165throw new RuntimeException("Test failed");166}167}168169/**170* Test loadFromXML with a document that does not have an encoding declaration171*/172static void testLoadWithoutEncoding() throws IOException {173System.out.println("testLoadWithoutEncoding");174175Properties expected = new Properties();176expected.put("foo", "bar");177178String s = "<?xml version=\"1.0\"?>" +179"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +180"<properties>" +181"<entry key=\"foo\">bar</entry>" +182"</properties>";183ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));184Properties props = new Properties();185props.loadFromXML(in);186187if (!props.equals(expected)) {188System.err.println("loaded: " + props + ", expected: " + expected);189throw new RuntimeException("Test failed");190}191}192193/**194* Test loadFromXML with unsupported encoding195*/196static void testLoadWithBadEncoding() throws IOException {197System.out.println("testLoadWithBadEncoding");198String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +199"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +200"<properties>" +201"<entry key=\"foo\">bar</entry>" +202"</properties>";203ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));204Properties props = new Properties();205try {206props.loadFromXML(in);207throw new RuntimeException("UnsupportedEncodingException expected");208} catch (UnsupportedEncodingException expected) { }209}210211/**212* Test storeToXML with unsupported encoding213*/214static void testStoreWithBadEncoding() throws IOException {215System.out.println("testStoreWithBadEncoding");216Properties props = new Properties();217props.put("foo", "bar");218ByteArrayOutputStream out = new ByteArrayOutputStream();219try {220props.storeToXML(out, null, "BAD");221throw new RuntimeException("UnsupportedEncodingException expected");222} catch (UnsupportedEncodingException expected) { }223}224225/**226* Test loadFromXML with malformed documents227*/228static void testLoadWithMalformedDoc(Path dir) throws IOException {229try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) {230for (Path file: stream) {231System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName());232try (InputStream in = Files.newInputStream(file)) {233Properties props = new Properties();234try {235props.loadFromXML(in);236throw new RuntimeException("InvalidPropertiesFormatException not thrown");237} catch (InvalidPropertiesFormatException x) {238System.out.println(x);239}240}241}242}243}244245public static void main(String[] args) throws IOException {246247testLoadAndStore("UTF-8", false);248testLoadAndStore("UTF-16", false);249testLoadAndStore("UTF-16BE", false);250testLoadAndStore("UTF-16LE", false);251testLoadAndStore("UTF-16BE", true);252testLoadAndStore("UTF-16LE", true);253testLoadWithoutEncoding();254testLoadWithBadEncoding();255testStoreWithBadEncoding();256257// malformed documents258String src = System.getProperty("test.src");259String subdir = "invalidxml";260Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir);261testLoadWithMalformedDoc(dir);262263// re-run sanity test with security manager264Policy orig = Policy.getPolicy();265Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),266new PropertyPermission("line.separator", "read"));267Policy.setPolicy(p);268System.setSecurityManager(new SecurityManager());269try {270testLoadAndStore("UTF-8", false);271} finally {272// turn off security manager and restore policy273System.setSecurityManager(null);274Policy.setPolicy(orig);275}276277}278}279280281