/* * Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @bug 8000354 8000685 8004371 8043119 8276207 * @summary Basic test of storeToXML and loadToXML * @run main/othervm -Djava.security.manager=allow LoadAndStoreXML
*/
/** * Simple policy implementation that grants a set of permissions to * all code sources and protection domains.
*/ staticclass SimplePolicy extends Policy { privatefinal Permissions perms;
public SimplePolicy(Permission...permissions) {
perms = new Permissions(); for (Permission permission : permissions)
perms.add(permission);
}
@Override public PermissionCollection getPermissions(CodeSource cs) { return perms;
}
@Override public PermissionCollection getPermissions(ProtectionDomain pd) { return perms;
}
/** * A {@code ByteArrayInputStream} that allows testing if the * {@code close} method has been invoked.
*/ staticclass TestInputStream extends ByteArrayInputStream { privateboolean closed;
/** * A {@code ByteArrayOutputStream} that allows testing if the * {@code close} method has been invoked.
*/ staticclass TestOutputStream extends ByteArrayOutputStream { privateboolean closed;
/** * Sanity test that properties saved with Properties#storeToXML can be * read with Properties#loadFromXML.
*/ staticvoid testLoadAndStore(String encoding, boolean appendBOM) throws IOException {
System.out.println("testLoadAndStore, encoding=" + encoding);
TestOutputStream out = new TestOutputStream();
props.storeToXML(out, null, encoding); if (!out.isOpen()) thrownew RuntimeException("OutputStream closed by storeToXML");
Properties p = new Properties();
TestInputStream in; if (appendBOM) { byte[] byteOrderMark = bomChar.getBytes(Charset.forName(encoding)); byte[] outArray = out.toByteArray(); byte[] inputArray = newbyte[byteOrderMark.length + outArray.length];
System.arraycopy(byteOrderMark, 0, inputArray, 0, byteOrderMark.length);
System.arraycopy(outArray, 0, inputArray, byteOrderMark.length, outArray.length);
in = new TestInputStream(inputArray);
} else {
in = new TestInputStream(out.toByteArray());
}
p.loadFromXML(in); if (in.isOpen()) thrownew RuntimeException("InputStream not closed by loadFromXML");
/** * Test loadFromXML with a document that does not have an encoding declaration
*/ staticvoid testLoadWithoutEncoding() throws IOException {
System.out.println("testLoadWithoutEncoding");
Properties expected = new Properties();
expected.put("foo", "bar");
String s = "1.0\"?>" + "http://java.sun.com/dtd/properties.dtd\">" + "" + "foo\">bar" + "";
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
Properties props = new Properties();
props.loadFromXML(in);
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 ist noch experimentell.