/* * Copyright (c) 2018, 2022, 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 8201276 8205058 8209576 8287541 8288589 * @build ReadWriteString PassThroughFileSystem * @run testng ReadWriteString * @summary Unit test for methods for Files readString and write methods. * @key randomness * @modules jdk.charsets
*/
@Test(groups = "readwrite") publicclass ReadWriteString {
// data for text files final String TEXT_UNICODE = "\u201CHello\u201D"; final String TEXT_ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n abcdefghijklmnopqrstuvwxyz\n 1234567890\n"; privatestaticfinal String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217"; privatestaticfinal Charset WINDOWS_1252 = Charset.forName("windows-1252"); privatestaticfinal Charset WINDOWS_31J = Charset.forName("windows-31j");
staticbyte[] data = getData();
staticbyte[] getData() { try {
String str1 = "A string that contains ";
String str2 = " , an invalid character for UTF-8.";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(str1.getBytes());
baos.write(0xFA);
baos.write(str2.getBytes()); return baos.toByteArray();
} catch (IOException ex) { // in case it happens, fail the test thrownew RuntimeException(ex);
}
}
// file used by testReadWrite, testReadString and testWriteString private Path[] testFiles = new Path[3];
/* * DataProvider for malformed write test. Provides the following fields: * file path, malformed input string, charset
*/
@DataProvider(name = "malformedWrite") public Object[][] getMalformedWrite() throws IOException {
Path path = Files.createFile(Path.of("malformedWrite")); returnnew Object[][]{
{path, "\ud800", null}, //the default Charset is UTF_8
{path, "\u00A0\u00A1", US_ASCII},
{path, "\ud800", UTF_8},
{path, JA_STRING, ISO_8859_1},
{path, "\u041e", WINDOWS_1252}, // cyrillic capital letter O
{path, "\u091c", WINDOWS_31J}, // devanagari letter ja
};
}
/* * DataProvider for illegal input test * Writes the data in ISO8859 and reads with UTF_8, expects MalformedInputException
*/
@DataProvider(name = "illegalInput") public Object[][] getIllegalInput() throws IOException {
Path path = Files.createFile(Path.of("illegalInput")); returnnew Object[][]{
{path, data, ISO_8859_1, null},
{path, data, ISO_8859_1, UTF_8}
};
}
/* * DataProvider for writeString test * Writes the data using both the existing and new method and compares the results.
*/
@DataProvider(name = "testWriteString") public Object[][] getWriteString() {
/* * DataProvider for readString test * Reads the file using both the existing and new method and compares the results.
*/
@DataProvider(name = "testReadString") public Object[][] getReadString() { returnnew Object[][]{
{testFiles[1], TEXT_ASCII, US_ASCII, US_ASCII},
{testFiles[1], TEXT_ASCII, US_ASCII, UTF_8},
{testFiles[1], TEXT_UNICODE, UTF_8, null},
{testFiles[1], TEXT_UNICODE, UTF_8, UTF_8}
};
}
/** * Verifies that NPE is thrown when one of the parameters is null.
*/
@Test publicvoid testNulls() {
Path path = Paths.get("foo");
String s = "abc";
/** * Verifies the readString and write String methods. Writes to files Strings * of various sizes, with/without specifying the Charset, and then compares * the result of reading the files.
*/
@Test publicvoid testReadWrite() throws IOException { int size = 0; while (size < 16 * 1024) {
testReadWrite(size, null, false);
testReadWrite(size, null, true);
testReadWrite(size, UTF_8, false);
testReadWrite(size, UTF_8, true);
size += 1024;
}
}
/** * Verifies fix for @bug 8209576 that the writeString method converts the * bytes properly. * This method compares the results written by the existing write method and * the writeString method added since 11.
*/
@Test(dataProvider = "testWriteString") publicvoid testWriteString(Path path, Path path2, String text, Charset cs, Charset cs2) throws IOException {
Files.write(path, text.getBytes(cs));
// writeString @since 11 if (cs2 == null) {
Files.writeString(path2, text);
} else {
Files.writeString(path2, text, cs2);
} byte[] bytes = Files.readAllBytes(path); byte[] bytes2 = Files.readAllBytes(path2);
assertTrue((Arrays.compare(bytes, bytes2) == 0), "The bytes should be the same");
}
/** * Verifies that the readString method added since 11 behaves the same as * constructing a string from the existing readAllBytes method.
*/
@Test(dataProvider = "testReadString") publicvoid testReadString(Path path, String text, Charset cs, Charset cs2) throws IOException {
Files.write(path, text.getBytes(cs));
String str = new String(Files.readAllBytes(path), cs);
// readString @since 11
String str2 = (cs2 == null) ? Files.readString(path) :
Files.readString(path, cs2);
assertTrue((str.equals(str2)), "The strings should be the same");
}
/** * Verifies that IOException is thrown (as specified) when giving a malformed * string input. * * @param path the path to write * @param s the string * @param cs the Charset * @throws IOException if the input is malformed
*/
@Test(dataProvider = "malformedWrite", expectedExceptions = UnmappableCharacterException.class) publicvoid testMalformedWrite(Path path, String s, Charset cs) throws IOException { if (cs == null) {
Files.writeString(path, s);
} else {
Files.writeString(path, s, cs);
}
}
/** * Verifies that IOException is thrown when reading a file using the wrong * Charset. * * @param path the path to write and read * @param data the data used for the test * @param csWrite the Charset to use for writing the test file * @param csRead the Charset to use for reading the file * @throws IOException when the Charset used for reading the file is incorrect
*/
@Test(dataProvider = "illegalInput", expectedExceptions = MalformedInputException.class) publicvoid testMalformedRead(Path path, byte[] data, Charset csWrite, Charset csRead) throws IOException {
String temp = new String(data, csWrite);
Files.writeString(path, temp, csWrite); if (csRead == null) {
Files.readString(path);
} else {
Files.readString(path, csRead);
}
}
/** * Verifies that IOException is thrown when reading a file containing * illegal bytes * * @param data the data used for the test * @param csRead the Charset to use for reading the file * @param expected exception class * @throws IOException when the Charset used for reading the file is incorrect
*/
@Test(dataProvider = "illegalInputBytes") publicvoid testMalformedReadBytes(byte[] data, Charset csRead, Class<CharacterCodingException> expected) throws IOException {
Path path = Path.of("illegalInputBytes");
Files.write(path, data); try {
Files.readString(path, csRead);
} catch (MalformedInputException | UnmappableCharacterException e) { if (expected.isInstance(e)) { // success return;
}
} thrownew RuntimeException("An instance of " + expected + " should be thrown");
}
assertTrue(read.equals(expected), "String read not the same as written");
}
staticfinalchar[] CHARS = "abcdefghijklmnopqrstuvwxyz \r\n".toCharArray();
StringBuilder sb = new StringBuilder(1024 << 4);
Random random = new Random();
private String generateString(int size) {
sb.setLength(0); for (int i = 0; i < size; i++) { char c = CHARS[random.nextInt(CHARS.length)];
sb.append(c);
}
return sb.toString();
}
}
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet)
¤
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.