< prev index next >
test/jdk/java/nio/file/Files/ReadWriteString.java
Print this page
@@ -22,39 +22,40 @@
*/
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 java.nio.file.StandardOpenOption;
import static java.nio.file.StandardOpenOption.APPEND;
+import static java.nio.file.StandardOpenOption.CREATE;
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
+ * @bug 8201276 8205058
* @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 {
- private static final OpenOption OPTION_CREATE = StandardOpenOption.CREATE;
// data for text files
private static final String EN_STRING = "The quick brown fox jumps over the lazy dog";
private static final String JA_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217";
// malformed input: a high surrogate without the low surrogate
static char[] illChars = {
@@ -72,11 +73,12 @@
baos.write(str1.getBytes());
baos.write(0xFA);
baos.write(str2.getBytes());
return baos.toByteArray();
} catch (IOException ex) {
- return null; //shouldn't happen
+ // in case it happens, fail the test
+ throw new RuntimeException(ex);
}
}
// file used by most tests
private Path tmpfile;
@@ -123,24 +125,24 @@
/**
* Verifies that NPE is thrown when one of the parameters is null.
*/
@Test
public void testNulls() {
- Path path = Paths.get(".");
+ 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, OPTION_CREATE));
- checkNullPointerException(() -> Files.writeString(path, (CharSequence) null, OPTION_CREATE));
+ 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, OPTION_CREATE));
- checkNullPointerException(() -> Files.writeString(path, (CharSequence) null, UTF_8, OPTION_CREATE));
- checkNullPointerException(() -> Files.writeString(path, s, (Charset) null, OPTION_CREATE));
+ 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
@@ -166,17 +168,17 @@
* @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 = IOException.class)
+ @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, OPTION_CREATE);
+ Files.writeString(path, s, CREATE);
} else {
- Files.writeString(path, s, cs, OPTION_CREATE);
+ Files.writeString(path, s, cs, CREATE);
}
}
/**
* Verifies that IOException is thrown when reading a file using the wrong
@@ -186,15 +188,15 @@
* @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 = IOException.class)
+ @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, OPTION_CREATE);
+ Files.writeString(path, temp, csWrite, CREATE);
String s;
if (csRead == null) {
s = Files.readString(path);
} else {
s = Files.readString(path, csRead);
@@ -210,11 +212,10 @@
fail(e + " not expected");
}
}
private void testReadWrite(int size, Charset cs, boolean append) throws IOException {
- StringBuilder sb = new StringBuilder(size);
String expected;
String str = generateString(size);
Path result;
if (cs == null) {
result = Files.writeString(tmpfile, str);
@@ -233,30 +234,27 @@
assertTrue(Files.size(tmpfile) == size * 2);
}
if (append) {
- sb.append(str).append(str);
- expected = sb.toString();
+ expected = str + str;
} else {
expected = str;
}
String read;
if (cs == null) {
read = Files.readString(result);
} else {
read = Files.readString(result, cs);
}
- //System.out.println("chars read: " + read.length());
- //System.out.println(read);
- //System.out.println("---end---");
+
assertTrue(read.equals(expected), "String read not the same as written");
}
static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz \r\n".toCharArray();
- StringBuilder sb = new StringBuilder(512);
+ 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++) {
< prev index next >