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