Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Properties/LoadAndStoreXML.java
41149 views
1
/*
2
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8000354 8000685 8004371 8043119
27
* @summary Basic test of storeToXML and loadToXML
28
* @run main/othervm -Djava.security.manager=allow LoadAndStoreXML
29
*/
30
31
import java.io.ByteArrayInputStream;
32
import java.io.ByteArrayOutputStream;
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.io.UnsupportedEncodingException;
36
import java.nio.charset.Charset;
37
import java.nio.file.DirectoryStream;
38
import java.nio.file.Files;
39
import java.nio.file.Path;
40
import java.nio.file.Paths;
41
import java.security.CodeSource;
42
import java.security.Permission;
43
import java.security.PermissionCollection;
44
import java.security.Permissions;
45
import java.security.Policy;
46
import java.security.ProtectionDomain;
47
import java.util.InvalidPropertiesFormatException;
48
import java.util.Properties;
49
import java.util.PropertyPermission;
50
51
public class LoadAndStoreXML {
52
static final String bomChar = "\uFEFF";
53
54
/**
55
* Simple policy implementation that grants a set of permissions to
56
* all code sources and protection domains.
57
*/
58
static class SimplePolicy extends Policy {
59
private final Permissions perms;
60
61
public SimplePolicy(Permission...permissions) {
62
perms = new Permissions();
63
for (Permission permission : permissions)
64
perms.add(permission);
65
}
66
67
@Override
68
public PermissionCollection getPermissions(CodeSource cs) {
69
return perms;
70
}
71
72
@Override
73
public PermissionCollection getPermissions(ProtectionDomain pd) {
74
return perms;
75
}
76
77
@Override
78
public boolean implies(ProtectionDomain pd, Permission p) {
79
return perms.implies(p);
80
}
81
}
82
83
/**
84
* A {@code ByteArrayInputStream} that allows testing if the
85
* {@code close} method has been invoked.
86
*/
87
static class TestInputStream extends ByteArrayInputStream {
88
private boolean closed;
89
90
TestInputStream(byte[] buf) {
91
super(buf);
92
}
93
94
boolean isOpen() {
95
return !closed;
96
}
97
98
public void close() throws IOException {
99
try {
100
super.close();
101
} finally {
102
closed = true;
103
}
104
}
105
}
106
107
/**
108
* A {@code ByteArrayOutputStream} that allows testing if the
109
* {@code close} method has been invoked.
110
*/
111
static class TestOutputStream extends ByteArrayOutputStream {
112
private boolean closed;
113
114
boolean isOpen() {
115
return !closed;
116
}
117
118
public void close() throws IOException {
119
try {
120
super.close();
121
} finally {
122
closed = true;
123
}
124
}
125
}
126
127
/**
128
* Sanity test that properties saved with Properties#storeToXML can be
129
* read with Properties#loadFromXML.
130
*/
131
static void testLoadAndStore(String encoding, boolean appendBOM) throws IOException {
132
System.out.println("testLoadAndStore, encoding=" + encoding);
133
134
Properties props = new Properties();
135
props.put("k0", "\u6C34");
136
props.put("k1", "foo");
137
props.put("k2", "bar");
138
props.put("k3", "\u0020\u0391\u0392\u0393\u0394\u0395\u0396\u0397");
139
props.put("k4", "\u7532\u9aa8\u6587");
140
props.put("k5", "<java.home>/conf/jaxp.properties");
141
142
TestOutputStream out = new TestOutputStream();
143
props.storeToXML(out, null, encoding);
144
if (!out.isOpen())
145
throw new RuntimeException("OutputStream closed by storeToXML");
146
147
Properties p = new Properties();
148
TestInputStream in;
149
if (appendBOM) {
150
byte[] byteOrderMark = bomChar.getBytes(Charset.forName(encoding));
151
byte[] outArray = out.toByteArray();
152
byte[] inputArray = new byte[byteOrderMark.length + outArray.length];
153
System.arraycopy(byteOrderMark, 0, inputArray, 0, byteOrderMark.length);
154
System.arraycopy(outArray, 0, inputArray, byteOrderMark.length, outArray.length);
155
in = new TestInputStream(inputArray);
156
} else {
157
in = new TestInputStream(out.toByteArray());
158
}
159
p.loadFromXML(in);
160
if (in.isOpen())
161
throw new RuntimeException("InputStream not closed by loadFromXML");
162
163
if (!p.equals(props)) {
164
System.err.println("stored: " + props);
165
System.err.println("loaded: " + p);
166
throw new RuntimeException("Test failed");
167
}
168
}
169
170
/**
171
* Test loadFromXML with a document that does not have an encoding declaration
172
*/
173
static void testLoadWithoutEncoding() throws IOException {
174
System.out.println("testLoadWithoutEncoding");
175
176
Properties expected = new Properties();
177
expected.put("foo", "bar");
178
179
String s = "<?xml version=\"1.0\"?>" +
180
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
181
"<properties>" +
182
"<entry key=\"foo\">bar</entry>" +
183
"</properties>";
184
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
185
Properties props = new Properties();
186
props.loadFromXML(in);
187
188
if (!props.equals(expected)) {
189
System.err.println("loaded: " + props + ", expected: " + expected);
190
throw new RuntimeException("Test failed");
191
}
192
}
193
194
/**
195
* Test loadFromXML with unsupported encoding
196
*/
197
static void testLoadWithBadEncoding() throws IOException {
198
System.out.println("testLoadWithBadEncoding");
199
String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
200
"<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
201
"<properties>" +
202
"<entry key=\"foo\">bar</entry>" +
203
"</properties>";
204
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
205
Properties props = new Properties();
206
try {
207
props.loadFromXML(in);
208
throw new RuntimeException("UnsupportedEncodingException expected");
209
} catch (UnsupportedEncodingException expected) { }
210
}
211
212
/**
213
* Test storeToXML with unsupported encoding
214
*/
215
static void testStoreWithBadEncoding() throws IOException {
216
System.out.println("testStoreWithBadEncoding");
217
Properties props = new Properties();
218
props.put("foo", "bar");
219
ByteArrayOutputStream out = new ByteArrayOutputStream();
220
try {
221
props.storeToXML(out, null, "BAD");
222
throw new RuntimeException("UnsupportedEncodingException expected");
223
} catch (UnsupportedEncodingException expected) { }
224
}
225
226
/**
227
* Test loadFromXML with malformed documents
228
*/
229
static void testLoadWithMalformedDoc(Path dir) throws IOException {
230
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) {
231
for (Path file: stream) {
232
System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName());
233
try (InputStream in = Files.newInputStream(file)) {
234
Properties props = new Properties();
235
try {
236
props.loadFromXML(in);
237
throw new RuntimeException("InvalidPropertiesFormatException not thrown");
238
} catch (InvalidPropertiesFormatException x) {
239
System.out.println(x);
240
}
241
}
242
}
243
}
244
}
245
246
public static void main(String[] args) throws IOException {
247
248
testLoadAndStore("UTF-8", false);
249
testLoadAndStore("UTF-16", false);
250
testLoadAndStore("UTF-16BE", false);
251
testLoadAndStore("UTF-16LE", false);
252
testLoadAndStore("UTF-16BE", true);
253
testLoadAndStore("UTF-16LE", true);
254
testLoadWithoutEncoding();
255
testLoadWithBadEncoding();
256
testStoreWithBadEncoding();
257
258
// malformed documents
259
String src = System.getProperty("test.src");
260
String subdir = "invalidxml";
261
Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir);
262
testLoadWithMalformedDoc(dir);
263
264
// re-run sanity test with security manager
265
Policy orig = Policy.getPolicy();
266
Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
267
new PropertyPermission("line.separator", "read"));
268
Policy.setPolicy(p);
269
System.setSecurityManager(new SecurityManager());
270
try {
271
testLoadAndStore("UTF-8", false);
272
} finally {
273
// turn off security manager and restore policy
274
System.setSecurityManager(null);
275
Policy.setPolicy(orig);
276
}
277
278
}
279
}
280
281