Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Files/ReadWriteString.java
41153 views
1
/*
2
* Copyright (c) 2018, 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
import java.io.ByteArrayOutputStream;
25
import java.io.IOException;
26
import java.nio.charset.Charset;
27
import java.nio.charset.MalformedInputException;
28
import java.nio.charset.UnmappableCharacterException;
29
import static java.nio.charset.StandardCharsets.US_ASCII;
30
import static java.nio.charset.StandardCharsets.ISO_8859_1;
31
import static java.nio.charset.StandardCharsets.UTF_8;
32
import java.nio.file.Files;
33
import java.nio.file.OpenOption;
34
import java.nio.file.Path;
35
import java.nio.file.Paths;
36
import static java.nio.file.StandardOpenOption.APPEND;
37
import static java.nio.file.StandardOpenOption.CREATE;
38
import java.util.Arrays;
39
import java.util.Random;
40
import java.util.concurrent.Callable;
41
import static org.testng.Assert.assertTrue;
42
import static org.testng.Assert.fail;
43
import org.testng.annotations.AfterClass;
44
import org.testng.annotations.BeforeClass;
45
import org.testng.annotations.DataProvider;
46
import org.testng.annotations.Test;
47
48
/* @test
49
* @bug 8201276 8205058 8209576
50
* @build ReadWriteString PassThroughFileSystem
51
* @run testng ReadWriteString
52
* @summary Unit test for methods for Files readString and write methods.
53
* @key randomness
54
*/
55
@Test(groups = "readwrite")
56
public class ReadWriteString {
57
58
// data for text files
59
final String TEXT_UNICODE = "\u201CHello\u201D";
60
final String TEXT_ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n abcdefghijklmnopqrstuvwxyz\n 1234567890\n";
61
private static final String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217";
62
63
// malformed input: a high surrogate without the low surrogate
64
static char[] illChars = {
65
'\u00fa', '\ud800'
66
};
67
68
static byte[] data = getData();
69
70
static byte[] getData() {
71
try {
72
String str1 = "A string that contains ";
73
String str2 = " , an invalid character for UTF-8.";
74
75
ByteArrayOutputStream baos = new ByteArrayOutputStream();
76
baos.write(str1.getBytes());
77
baos.write(0xFA);
78
baos.write(str2.getBytes());
79
return baos.toByteArray();
80
} catch (IOException ex) {
81
// in case it happens, fail the test
82
throw new RuntimeException(ex);
83
}
84
}
85
86
// file used by testReadWrite, testReadString and testWriteString
87
private Path[] testFiles = new Path[3];
88
89
/*
90
* DataProvider for malformed write test. Provides the following fields:
91
* file path, malformed input string, charset
92
*/
93
@DataProvider(name = "malformedWrite")
94
public Object[][] getMalformedWrite() throws IOException {
95
Path path = Files.createTempFile("malformedWrite", null);
96
return new Object[][]{
97
{path, "\ud800", null}, //the default Charset is UTF_8
98
{path, "\u00A0\u00A1", US_ASCII},
99
{path, "\ud800", UTF_8},
100
{path, JA_STRING, ISO_8859_1},
101
};
102
}
103
104
/*
105
* DataProvider for illegal input test
106
* Writes the data in ISO8859 and reads with UTF_8, expects MalformedInputException
107
*/
108
@DataProvider(name = "illegalInput")
109
public Object[][] getIllegalInput() throws IOException {
110
Path path = Files.createTempFile("illegalInput", null);
111
return new Object[][]{
112
{path, data, ISO_8859_1, null},
113
{path, data, ISO_8859_1, UTF_8}
114
};
115
}
116
117
/*
118
* DataProvider for writeString test
119
* Writes the data using both the existing and new method and compares the results.
120
*/
121
@DataProvider(name = "testWriteString")
122
public Object[][] getWriteString() throws IOException {
123
124
return new Object[][]{
125
{testFiles[1], testFiles[2], TEXT_ASCII, US_ASCII, null},
126
{testFiles[1], testFiles[2], TEXT_ASCII, US_ASCII, US_ASCII},
127
{testFiles[1], testFiles[2], TEXT_UNICODE, UTF_8, null},
128
{testFiles[1], testFiles[2], TEXT_UNICODE, UTF_8, UTF_8}
129
};
130
}
131
132
/*
133
* DataProvider for readString test
134
* Reads the file using both the existing and new method and compares the results.
135
*/
136
@DataProvider(name = "testReadString")
137
public Object[][] getReadString() throws IOException {
138
Path path = Files.createTempFile("readString_file1", null);
139
return new Object[][]{
140
{testFiles[1], TEXT_ASCII, US_ASCII, US_ASCII},
141
{testFiles[1], TEXT_ASCII, US_ASCII, UTF_8},
142
{testFiles[1], TEXT_UNICODE, UTF_8, null},
143
{testFiles[1], TEXT_UNICODE, UTF_8, UTF_8}
144
};
145
}
146
147
@BeforeClass
148
void setup() throws IOException {
149
testFiles[0] = Files.createTempFile("readWriteString", null);
150
testFiles[1] = Files.createTempFile("writeString_file1", null);
151
testFiles[2] = Files.createTempFile("writeString_file2", null);
152
}
153
154
@AfterClass
155
void cleanup() throws IOException {
156
for (Path path : testFiles) {
157
Files.deleteIfExists(path);
158
}
159
}
160
161
/**
162
* Verifies that NPE is thrown when one of the parameters is null.
163
*/
164
@Test
165
public void testNulls() {
166
Path path = Paths.get("foo");
167
String s = "abc";
168
169
checkNullPointerException(() -> Files.readString((Path) null));
170
checkNullPointerException(() -> Files.readString((Path) null, UTF_8));
171
checkNullPointerException(() -> Files.readString(path, (Charset) null));
172
173
checkNullPointerException(() -> Files.writeString((Path) null, s, CREATE));
174
checkNullPointerException(() -> Files.writeString(path, (CharSequence) null, CREATE));
175
checkNullPointerException(() -> Files.writeString(path, s, (OpenOption[]) null));
176
177
checkNullPointerException(() -> Files.writeString((Path) null, s, UTF_8, CREATE));
178
checkNullPointerException(() -> Files.writeString(path, (CharSequence) null, UTF_8, CREATE));
179
checkNullPointerException(() -> Files.writeString(path, s, (Charset) null, CREATE));
180
checkNullPointerException(() -> Files.writeString(path, s, UTF_8, (OpenOption[]) null));
181
}
182
183
/**
184
* Verifies the readString and write String methods. Writes to files Strings
185
* of various sizes, with/without specifying the Charset, and then compares
186
* the result of reading the files.
187
*/
188
@Test
189
public void testReadWrite() throws IOException {
190
int size = 0;
191
while (size < 16 * 1024) {
192
testReadWrite(size, null, false);
193
testReadWrite(size, null, true);
194
testReadWrite(size, UTF_8, false);
195
testReadWrite(size, UTF_8, true);
196
size += 1024;
197
}
198
}
199
200
/**
201
* Verifies fix for @bug 8209576 that the writeString method converts the
202
* bytes properly.
203
* This method compares the results written by the existing write method and
204
* the writeString method added since 11.
205
*/
206
@Test(dataProvider = "testWriteString")
207
public void testWriteString(Path path, Path path2, String text, Charset cs, Charset cs2) throws IOException {
208
Files.write(path, text.getBytes(cs));
209
210
// writeString @since 11
211
if (cs2 == null) {
212
Files.writeString(path2, text);
213
} else {
214
Files.writeString(path2, text, cs2);
215
}
216
byte[] bytes = Files.readAllBytes(path);
217
byte[] bytes2 = Files.readAllBytes(path2);
218
assertTrue((Arrays.compare(bytes, bytes2) == 0), "The bytes should be the same");
219
}
220
221
/**
222
* Verifies that the readString method added since 11 behaves the same as
223
* constructing a string from the existing readAllBytes method.
224
*/
225
@Test(dataProvider = "testReadString")
226
public void testReadString(Path path, String text, Charset cs, Charset cs2) throws IOException {
227
Files.write(path, text.getBytes(cs));
228
String str = new String(Files.readAllBytes(path), cs);
229
230
// readString @since 11
231
String str2 = (cs2 == null) ? Files.readString(path) :
232
Files.readString(path, cs2);
233
assertTrue((str.equals(str2)), "The strings should be the same");
234
}
235
236
/**
237
* Verifies that IOException is thrown (as specified) when giving a malformed
238
* string input.
239
*
240
* @param path the path to write
241
* @param s the string
242
* @param cs the Charset
243
* @throws IOException if the input is malformed
244
*/
245
@Test(dataProvider = "malformedWrite", expectedExceptions = UnmappableCharacterException.class)
246
public void testMalformedWrite(Path path, String s, Charset cs) throws IOException {
247
path.toFile().deleteOnExit();
248
if (cs == null) {
249
Files.writeString(path, s, CREATE);
250
} else {
251
Files.writeString(path, s, cs, CREATE);
252
}
253
}
254
255
/**
256
* Verifies that IOException is thrown when reading a file using the wrong
257
* Charset.
258
*
259
* @param path the path to write and read
260
* @param data the data used for the test
261
* @param csWrite the Charset to use for writing the test file
262
* @param csRead the Charset to use for reading the file
263
* @throws IOException when the Charset used for reading the file is incorrect
264
*/
265
@Test(dataProvider = "illegalInput", expectedExceptions = MalformedInputException.class)
266
public void testMalformedRead(Path path, byte[] data, Charset csWrite, Charset csRead) throws IOException {
267
path.toFile().deleteOnExit();
268
String temp = new String(data, csWrite);
269
Files.writeString(path, temp, csWrite, CREATE);
270
String s;
271
if (csRead == null) {
272
s = Files.readString(path);
273
} else {
274
s = Files.readString(path, csRead);
275
}
276
}
277
278
private void checkNullPointerException(Callable<?> c) {
279
try {
280
c.call();
281
fail("NullPointerException expected");
282
} catch (NullPointerException ignore) {
283
} catch (Exception e) {
284
fail(e + " not expected");
285
}
286
}
287
288
private void testReadWrite(int size, Charset cs, boolean append) throws IOException {
289
String expected;
290
String str = generateString(size);
291
Path result;
292
if (cs == null) {
293
result = Files.writeString(testFiles[0], str);
294
} else {
295
result = Files.writeString(testFiles[0], str, cs);
296
}
297
298
//System.out.println(result.toUri().toASCIIString());
299
assertTrue(result == testFiles[0]);
300
if (append) {
301
if (cs == null) {
302
Files.writeString(testFiles[0], str, APPEND);
303
} else {
304
Files.writeString(testFiles[0], str, cs, APPEND);
305
}
306
assertTrue(Files.size(testFiles[0]) == size * 2);
307
}
308
309
310
if (append) {
311
expected = str + str;
312
} else {
313
expected = str;
314
}
315
316
String read;
317
if (cs == null) {
318
read = Files.readString(result);
319
} else {
320
read = Files.readString(result, cs);
321
}
322
323
assertTrue(read.equals(expected), "String read not the same as written");
324
}
325
326
static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz \r\n".toCharArray();
327
StringBuilder sb = new StringBuilder(1024 << 4);
328
Random random = new Random();
329
330
private String generateString(int size) {
331
sb.setLength(0);
332
for (int i = 0; i < size; i++) {
333
char c = CHARS[random.nextInt(CHARS.length)];
334
sb.append(c);
335
}
336
337
return sb.toString();
338
}
339
}
340
341