undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
63
test/jdk/java/io/Reader/NullLock.java
Normal file
63
test/jdk/java/io/Reader/NullLock.java
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 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 4152455
|
||||
@summary A null lock in the create must throw an exception
|
||||
*/
|
||||
|
||||
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class NullLock {
|
||||
public static void main(String argv[]) throws Exception {
|
||||
if (!testBufferedReader())
|
||||
throw new Exception("Buffered Reader constructor: "
|
||||
+ "Null argument must throw an exception");
|
||||
if (!testBufferedWriter())
|
||||
throw new Exception("BufferedWriter constructor: "
|
||||
+ " Null arg must throw an exception");
|
||||
}
|
||||
|
||||
static boolean testBufferedReader(){
|
||||
try {
|
||||
InputStreamReader isr = null;
|
||||
BufferedReader br = new BufferedReader(isr);
|
||||
} catch(NullPointerException e) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean testBufferedWriter(){
|
||||
try {
|
||||
OutputStreamWriter isr = null;
|
||||
BufferedWriter br = new BufferedWriter(isr);
|
||||
} catch(NullPointerException e) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
151
test/jdk/java/io/Reader/NullReader.java
Normal file
151
test/jdk/java/io/Reader/NullReader.java
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
* Copyright (c) 2018, 2026, 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.Reader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.ReadOnlyBufferException;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8196298 8204930
|
||||
* @run junit NullReader
|
||||
* @summary Check for expected behavior of Reader.nullReader().
|
||||
*/
|
||||
public class NullReader {
|
||||
private static Reader openReader;
|
||||
private static Reader closedReader;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() throws IOException {
|
||||
openReader = Reader.nullReader();
|
||||
closedReader = Reader.nullReader();
|
||||
closedReader.close();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void closeStream() throws IOException {
|
||||
openReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpen() {
|
||||
assertNotNull(openReader, "Reader.nullReader() returned null");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRead() throws IOException {
|
||||
assertEquals(-1, openReader.read(), "read() != -1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBII() throws IOException {
|
||||
assertEquals(-1, openReader.read(new char[1], 0, 1),
|
||||
"read(char[],int,int) != -1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBIILenZero() throws IOException {
|
||||
assertEquals(0, openReader.read(new char[1], 0, 0),
|
||||
"read(char[],int,int) != 0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadCharBuffer() throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(1);
|
||||
assertEquals(-1, openReader.read(charBuffer),
|
||||
"read(CharBuffer) != -1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadCharBufferZeroRemaining() throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(0);
|
||||
assertEquals(0, openReader.read(charBuffer),
|
||||
"read(CharBuffer) != 0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReady() throws IOException {
|
||||
assertFalse(openReader.ready());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkip() throws IOException {
|
||||
assertEquals(0, openReader.skip(1), "skip() != 0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransferTo() throws IOException {
|
||||
assertEquals(0, openReader.transferTo(new StringWriter(7)),
|
||||
"transferTo() != 0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadClosed() throws IOException {
|
||||
assertThrows(IOException.class, () -> closedReader.read());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadBIIClosed() throws IOException {
|
||||
assertThrows(IOException.class,
|
||||
() -> closedReader.read(new char[1], 0, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadCharBufferClosed() throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(0);
|
||||
assertThrows(IOException.class, () -> closedReader.read(charBuffer));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadCharBufferZeroRemainingClosed() throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(0);
|
||||
assertThrows(IOException.class, () -> closedReader.read(charBuffer));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadyClosed() throws IOException {
|
||||
assertThrows(IOException.class, () -> closedReader.ready());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipClosed() throws IOException {
|
||||
assertThrows(IOException.class, () -> closedReader.skip(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransferToClosed() throws IOException {
|
||||
assertThrows(IOException.class,
|
||||
() -> closedReader.transferTo(new StringWriter(7)));
|
||||
}
|
||||
}
|
||||
236
test/jdk/java/io/Reader/Of.java
Normal file
236
test/jdk/java/io/Reader/Of.java
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 2026, 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.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.ReadOnlyBufferException;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8341566
|
||||
* @summary Check for expected behavior of Reader.of().
|
||||
* @run junit Of
|
||||
*/
|
||||
public class Of {
|
||||
final static String CONTENT = "Some Reader Test";
|
||||
|
||||
/*
|
||||
* Readers to be tested.
|
||||
*/
|
||||
public static Reader[] readers() {
|
||||
return new Reader[] {
|
||||
new StringReader(CONTENT),
|
||||
Reader.of(CONTENT),
|
||||
Reader.of(new StringBuffer(CONTENT)),
|
||||
Reader.of(new StringBuilder(CONTENT)),
|
||||
Reader.of(ByteBuffer.allocateDirect(CONTENT.length() * 2)
|
||||
.asCharBuffer().put(CONTENT).flip()),
|
||||
Reader.of(CharBuffer.wrap(CONTENT.toCharArray())),
|
||||
Reader.of(new CharSequence() {
|
||||
@Override
|
||||
public char charAt(int index) {
|
||||
return CONTENT.charAt(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return CONTENT.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence subSequence(int start, int end) {
|
||||
// unused by Reader.Of's result
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// Reader.Of's result SHALL NOT convert to String
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testRead(Reader reader) throws IOException {
|
||||
String s = "";
|
||||
for (int c; (c = reader.read()) != -1; s += (char) c);
|
||||
assertEquals(CONTENT, s, "read() returned wrong value");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadBII(Reader reader) throws IOException {
|
||||
char[] c = new char[16];
|
||||
assertEquals(8, reader.read(c, 8, 8),
|
||||
"read(char[],int,int) does not respect given start or end");
|
||||
assertEquals(8, reader.read(c, 0, 16),
|
||||
"read(char[],int,int) does not respect end of stream");
|
||||
assertEquals(CONTENT.substring(8, 16) + CONTENT.substring(0, 8),
|
||||
new String(c),
|
||||
"read(char[],int,int) provides wrong content");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadBIILenZero(Reader reader) throws IOException {
|
||||
assertEquals(0, reader.read(new char[1], 0, 0),
|
||||
"read(char[],int,int) != 0");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadDirectCharBuffer(Reader reader) throws IOException {
|
||||
CharBuffer charBuffer = ByteBuffer.allocateDirect(32).asCharBuffer();
|
||||
charBuffer.position(8);
|
||||
assertEquals(8, reader.read(charBuffer),
|
||||
"read(CharBuffer) does not respect position or limit");
|
||||
charBuffer.rewind();
|
||||
assertEquals(8, reader.read(charBuffer),
|
||||
"read(CharBuffer) does not respect end of stream");
|
||||
charBuffer.rewind();
|
||||
// last part first proves that copy loops correctly stopped
|
||||
assertEquals(CONTENT.substring(8, 16) + CONTENT.substring(0, 8),
|
||||
charBuffer.toString(),
|
||||
"read(CharBuffer) provides wrong content");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadNonDirectCharBuffer(Reader reader) throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(16);
|
||||
charBuffer.position(8);
|
||||
assertEquals(8, reader.read(charBuffer),
|
||||
"read(CharBuffer) does not respect position or limit");
|
||||
charBuffer.rewind();
|
||||
assertEquals(8, reader.read(charBuffer),
|
||||
"read(CharBuffer) does not respect end of stream");
|
||||
charBuffer.rewind();
|
||||
assertEquals(CONTENT.substring(8, 16) + CONTENT.substring(0, 8),
|
||||
charBuffer.toString(),
|
||||
"read(CharBuffer) provides wrong content");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadCharBufferZeroRemaining(Reader reader) throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(0);
|
||||
assertEquals(0, reader.read(charBuffer), "read(CharBuffer) != 0");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReady(Reader reader) throws IOException {
|
||||
assertTrue(reader.ready());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testSkip(Reader reader) throws IOException {
|
||||
assertEquals(8, reader.skip(8), "skip() does not respect limit");
|
||||
assertEquals(8, reader.skip(9), "skip() does not respect end of stream");
|
||||
assertEquals(0, reader.skip(1), "skip() does not respect empty stream");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testTransferTo(Reader reader) throws IOException {
|
||||
StringWriter sw = new StringWriter(16);
|
||||
assertEquals(16, reader.transferTo(sw), "transferTo() != 16");
|
||||
assertEquals(0, reader.transferTo(sw),
|
||||
"transferTo() does not respect empty stream");
|
||||
assertEquals(CONTENT, sw.toString(),
|
||||
"transferTo() provides wrong content");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadClosed(Reader reader) throws IOException {
|
||||
reader.close();
|
||||
assertThrows(IOException.class, () -> {reader.read();});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadBIIClosed(Reader reader) throws IOException {
|
||||
reader.close();
|
||||
assertThrows(IOException.class, () -> reader.read(new char[1], 0, 1));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadCharBufferClosed(Reader reader) throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(1);
|
||||
reader.close();
|
||||
assertThrows(IOException.class, () -> reader.read(charBuffer));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadCharBufferZeroRemainingClosed(Reader reader) throws IOException {
|
||||
CharBuffer charBuffer = CharBuffer.allocate(0);
|
||||
reader.close();
|
||||
assertThrows(IOException.class, () -> reader.read(charBuffer));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testReadyClosed(Reader reader) throws IOException {
|
||||
reader.close();
|
||||
assertThrows(IOException.class, () -> reader.ready());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testSkipClosed(Reader reader) throws IOException {
|
||||
reader.close();
|
||||
assertThrows(IOException.class, () -> reader.skip(1));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testTransferToClosed(Reader reader) throws IOException {
|
||||
reader.close();
|
||||
assertThrows(IOException.class, () -> reader.transferTo(new StringWriter(1)));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void testCloseClosed(Reader reader) throws IOException {
|
||||
reader.close();
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
207
test/jdk/java/io/Reader/OpsAfterClose.java
Normal file
207
test/jdk/java/io/Reader/OpsAfterClose.java
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 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 5085148 4143651
|
||||
* @summary Test if Reader methods will check if the stream
|
||||
* has been closed.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public enum OpsAfterClose {
|
||||
|
||||
READ { boolean check(Reader r) {
|
||||
try {
|
||||
r.read();
|
||||
} catch (IOException io) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
|
||||
READ_BUF { boolean check(Reader r) {
|
||||
try {
|
||||
char buf[] = new char[2];
|
||||
int len = 1;
|
||||
r.read(buf, 0, len);
|
||||
} catch (IOException io) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
READY { boolean check(Reader r) {
|
||||
try {
|
||||
r.ready();
|
||||
} catch (IOException io) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
MARK { boolean check(Reader r) {
|
||||
try {
|
||||
r.mark(1);
|
||||
} catch (IOException io) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
SKIP { boolean check(Reader r) {
|
||||
try {
|
||||
r.skip(1);
|
||||
} catch (IOException io) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
RESET { boolean check(Reader r) {
|
||||
try {
|
||||
r.reset();
|
||||
} catch (IOException io) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} },
|
||||
CLOSE { boolean check(Reader r) {
|
||||
try {
|
||||
r.close();
|
||||
} catch (IOException io) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} };
|
||||
|
||||
abstract boolean check(Reader r);
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
|
||||
boolean failed = false;
|
||||
|
||||
BufferedReader br = new BufferedReader(
|
||||
new StringReader("abc def ghi"));
|
||||
if (testReader(br)) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
CharArrayReader car = new CharArrayReader(new char[2]);
|
||||
if (testReader(car)) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
PushbackReader pbr = new PushbackReader(
|
||||
new CharArrayReader(new char[2]));
|
||||
if (testReader(pbr)) {
|
||||
failed = true;
|
||||
}
|
||||
if (testPushbackReader(pbr)) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
StringReader sr = new StringReader("abc def ghi");
|
||||
if (testReader(sr)) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
InputStreamReader isr = new InputStreamReader(
|
||||
new ByteArrayInputStream("abc".getBytes()));
|
||||
if (testReader(isr)) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
LineNumberReader lnr = new LineNumberReader(
|
||||
new StringReader("abc def ghi"));
|
||||
if (testReader(lnr)) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
File f = new File(System.getProperty("test.dir", "."),
|
||||
"NewFile");
|
||||
f.createNewFile();
|
||||
f.deleteOnExit();
|
||||
|
||||
FileReader fr = new FileReader(f);
|
||||
if (testReader(fr)) {
|
||||
failed = true;
|
||||
}
|
||||
|
||||
PipedWriter pw = new PipedWriter();
|
||||
PipedReader pr = new PipedReader(pw);
|
||||
if (testReader(pr)) {
|
||||
failed = true;
|
||||
}
|
||||
if (failed) {
|
||||
throw new Exception("Test failed for some of the operation{s}" +
|
||||
" on some of the reader{s}, check the messages");
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean testReader(Reader r) throws Exception {
|
||||
r.close();
|
||||
boolean failed = false;
|
||||
boolean result;
|
||||
System.out.println("Testing reader:" + r);
|
||||
for (OpsAfterClose op : OpsAfterClose.values()) {
|
||||
result = op.check(r);
|
||||
if (!result) {
|
||||
failed = true;
|
||||
}
|
||||
System.out.println(op + ":" + result);
|
||||
}
|
||||
if (failed) {
|
||||
System.out.println("Test failed for the failed operation{s}" +
|
||||
" above for the Reader:" + r);
|
||||
}
|
||||
return failed;
|
||||
}
|
||||
|
||||
private static boolean testPushbackReader(PushbackReader pr)
|
||||
throws Exception {
|
||||
boolean failed = false;
|
||||
try {
|
||||
pr.unread(1);
|
||||
System.out.println("Test failed for unread(int):" + pr);
|
||||
failed = true;
|
||||
} catch (IOException io) {
|
||||
System.out.println("UNREAD(int):true");
|
||||
}
|
||||
|
||||
char buf[] = new char[2];
|
||||
try {
|
||||
pr.unread(buf, 0, 2);
|
||||
System.out.println("Test failed for unread(buf, offset, len):" +
|
||||
pr);
|
||||
failed = true;
|
||||
} catch (IOException io) {
|
||||
System.out.println("UNREAD(buf, offset, len):true");
|
||||
}
|
||||
try {
|
||||
pr.unread(buf);
|
||||
System.out.println("Test failed for unread(char[] buf):" + pr);
|
||||
failed = true;
|
||||
} catch (IOException io) {
|
||||
System.out.println("UNREAD(buf):true");
|
||||
}
|
||||
return failed;
|
||||
}
|
||||
}
|
||||
194
test/jdk/java/io/Reader/ReadAll.java
Normal file
194
test/jdk/java/io/Reader/ReadAll.java
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
/*
|
||||
* Copyright (c) 2025, 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 8354724
|
||||
* @summary Test Reader readAllLines and readAllAstring methods
|
||||
* @library .. /test/lib
|
||||
* @build jdk.test.lib.Platform jdk.test.lib.RandomFactory
|
||||
* @run junit ReadAll
|
||||
* @key randomness
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import jdk.test.lib.RandomFactory;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ReadAll {
|
||||
private static final String PHRASE =
|
||||
"Ange plein de gaieté, connaissez-vous l'angoisse";
|
||||
|
||||
private static File file;
|
||||
private static Path path;
|
||||
private static Random rnd;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() throws IOException {
|
||||
path = Files.createTempFile(Path.of("."), "foo", "bar");
|
||||
file = path.toFile();
|
||||
|
||||
rnd = RandomFactory.getRandom();
|
||||
int size = rnd.nextInt(2, 16386);
|
||||
|
||||
int plen = PHRASE.length();
|
||||
StringBuilder sb = new StringBuilder(plen);
|
||||
List<String> strings = new ArrayList<>(size);
|
||||
while (strings.size() < size) {
|
||||
int fromIndex = rnd.nextInt(0, plen / 2);
|
||||
int toIndex = rnd.nextInt(fromIndex, plen);
|
||||
String s = PHRASE.substring(fromIndex, toIndex);
|
||||
sb.append(s);
|
||||
int bound = toIndex - fromIndex;
|
||||
if (bound > 0) {
|
||||
int offset = bound/2;
|
||||
int n = rnd.nextInt(0, bound);
|
||||
for (int i = 0; i < n; i++) {
|
||||
String f = null;
|
||||
switch (rnd.nextInt(7)) {
|
||||
case 0 -> f = "";
|
||||
case 1 -> f = "\r";
|
||||
case 2 -> f = "\n";
|
||||
case 3 -> f = "\r\n";
|
||||
case 4 -> f = "\r\r";
|
||||
case 5 -> f = "\n\n";
|
||||
case 6 -> f = " ";
|
||||
}
|
||||
sb.insert(offset, f);
|
||||
}
|
||||
}
|
||||
strings.add(sb.toString());
|
||||
sb.setLength(0);
|
||||
}
|
||||
|
||||
String p4096 = PHRASE.repeat((4096 + plen - 1)/plen);
|
||||
String p8192 = PHRASE.repeat((8192 + plen - 1)/plen);
|
||||
String p16384 = PHRASE.repeat((16384 + plen - 1)/plen);
|
||||
|
||||
for (int i = 0; i < 64; i++) {
|
||||
for (int j = 0; j < 32; j++) {
|
||||
switch (rnd.nextInt(8)) {
|
||||
case 0 -> sb.append("");
|
||||
case 1 -> sb.append(" ");
|
||||
case 2 -> sb.append("\n");
|
||||
case 3 -> sb.append(PHRASE);
|
||||
case 5 -> sb.append(p4096);
|
||||
case 6 -> sb.append(p8192);
|
||||
case 7 -> sb.append(p16384);
|
||||
}
|
||||
}
|
||||
strings.add(sb.toString());
|
||||
sb.setLength(0);
|
||||
}
|
||||
|
||||
Files.write(path, strings);
|
||||
System.out.println(strings.size() + " lines written");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void cleanup() throws IOException {
|
||||
if (file != null)
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAllLines() throws IOException {
|
||||
// Reader implementation
|
||||
System.out.println("Reader implementation");
|
||||
List<String> lines;
|
||||
try (FileReader fr = new FileReader(file)) {
|
||||
lines = fr.readAllLines();
|
||||
}
|
||||
System.out.println(lines.size() + " lines read");
|
||||
|
||||
List<String> linesExpected = Files.readAllLines(path);
|
||||
int count = linesExpected.size();
|
||||
if (lines.size() != count)
|
||||
throw new RuntimeException("Size mismatch: " + lines.size() + " != " + count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
String expected = linesExpected.get(i);
|
||||
String actual = lines.get(i);
|
||||
if (!actual.equals(expected)) {
|
||||
String msg = String.format("%d: \"%s\" != \"%s\"",
|
||||
i, actual, expected);
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// Reader.of implementation
|
||||
System.out.println("Reader.of implementation");
|
||||
String stringExpected = Files.readString(path);
|
||||
int n = rnd.nextInt(stringExpected.length()/2);
|
||||
String substringExpected = stringExpected.substring(n);
|
||||
linesExpected = substringExpected.lines().toList();
|
||||
try (Reader r = new StringReader(stringExpected)) {
|
||||
r.skip(n);
|
||||
lines = r.readAllLines();
|
||||
}
|
||||
count = linesExpected.size();
|
||||
if (lines.size() != count)
|
||||
throw new RuntimeException("Size mismatch: " + lines.size() + " != " + count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
String expected = linesExpected.get(i);
|
||||
String actual = lines.get(i);
|
||||
if (!actual.equals(expected)) {
|
||||
String msg = String.format("%d: \"%s\" != \"%s\"",
|
||||
i, actual, expected);
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readAllAsString() throws IOException {
|
||||
// Reader implementation
|
||||
String string;
|
||||
try (FileReader fr = new FileReader(file)) {
|
||||
string = fr.readAllAsString();
|
||||
}
|
||||
String stringExpected = Files.readString(path);
|
||||
assertEquals(stringExpected, string);
|
||||
|
||||
// Reader.of implementation
|
||||
int n = rnd.nextInt(stringExpected.length()/2);
|
||||
try (Reader r = Reader.of(stringExpected)) {
|
||||
r.skip(n);
|
||||
string = r.readAllAsString();
|
||||
}
|
||||
assertEquals(stringExpected.substring(n), string);
|
||||
}
|
||||
}
|
||||
158
test/jdk/java/io/Reader/ReadCharBuffer.java
Normal file
158
test/jdk/java/io/Reader/ReadCharBuffer.java
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2026, 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 4926314 8266014
|
||||
* @summary Test for Reader#read(CharBuffer).
|
||||
* @run junit ReadCharBuffer
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.CharArrayReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ReadCharBuffer {
|
||||
|
||||
private static final int BUFFER_SIZE = 8 + 8192 + 2;
|
||||
|
||||
public static Stream<CharBuffer> buffers() {
|
||||
// test both on-heap and off-heap buffers as they make use different code paths
|
||||
return Stream.of(CharBuffer.allocate(BUFFER_SIZE),
|
||||
ByteBuffer.allocateDirect(BUFFER_SIZE * 2).asCharBuffer());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("buffers")
|
||||
public void read(CharBuffer buffer) throws IOException {
|
||||
fillBuffer(buffer);
|
||||
|
||||
StringBuilder input = new StringBuilder(BUFFER_SIZE - 2 + 1);
|
||||
input.append("ABCDEF");
|
||||
for (int i = 0; i < 8192; i++) {
|
||||
input.append('y');
|
||||
}
|
||||
input.append("GH");
|
||||
|
||||
try (Reader reader = new UnoptimizedStringReader(input.toString())) {
|
||||
// put only between position and limit in the target buffer
|
||||
int limit = 1 + 6;
|
||||
buffer.limit(limit);
|
||||
buffer.position(1);
|
||||
assertEquals(6, reader.read(buffer));
|
||||
assertEquals(limit, buffer.position());
|
||||
assertEquals(limit, buffer.limit());
|
||||
|
||||
// read the full temporary buffer
|
||||
// and then accurately reduce the next #read call
|
||||
limit = 8 + 8192 + 1;
|
||||
buffer.limit(8 + 8192 + 1);
|
||||
buffer.position(8);
|
||||
assertEquals(8192 + 1, reader.read(buffer));
|
||||
assertEquals(limit, buffer.position());
|
||||
assertEquals(limit, buffer.limit());
|
||||
|
||||
assertEquals('H', reader.read());
|
||||
assertEquals(-1, reader.read());
|
||||
}
|
||||
|
||||
buffer.clear();
|
||||
StringBuilder expected = new StringBuilder(BUFFER_SIZE);
|
||||
expected.append("xABCDEFx");
|
||||
for (int i = 0; i < 8192; i++) {
|
||||
expected.append('y');
|
||||
}
|
||||
expected.append("Gx");
|
||||
assertEquals(expected.toString(), buffer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readZeroLength() throws IOException {
|
||||
char[] buf = new char[] {1, 2, 3};
|
||||
BufferedReader r = new BufferedReader(new CharArrayReader(buf));
|
||||
int n = r.read(CharBuffer.allocate(0));
|
||||
assertEquals(0, n);
|
||||
}
|
||||
|
||||
private void fillBuffer(CharBuffer buffer) {
|
||||
char[] filler = new char[buffer.remaining()];
|
||||
Arrays.fill(filler, 'x');
|
||||
buffer.put(filler);
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unoptimized version of StringReader in case StringReader overrides
|
||||
* #read(CharBuffer)
|
||||
*/
|
||||
static final class UnoptimizedStringReader extends Reader {
|
||||
|
||||
private String str;
|
||||
private int next = 0;
|
||||
|
||||
UnoptimizedStringReader(String s) {
|
||||
this.str = s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
synchronized (lock) {
|
||||
if (next >= str.length())
|
||||
return -1;
|
||||
return str.charAt(next++);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(char cbuf[], int off, int len) throws IOException {
|
||||
synchronized (lock) {
|
||||
Objects.checkFromIndexSize(off, len, cbuf.length);
|
||||
if (next >= str.length())
|
||||
return -1;
|
||||
int n = Math.min(str.length() - next, len);
|
||||
str.getChars(next, next + n, cbuf, off);
|
||||
next += n;
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
67
test/jdk/java/io/Reader/ReadIntoReadOnlyBuffer.java
Normal file
67
test/jdk/java/io/Reader/ReadIntoReadOnlyBuffer.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 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.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.ReadOnlyBufferException;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8266078
|
||||
* @summary Tests that attempting to read into a read-only CharBuffer
|
||||
* does not advance the Reader position
|
||||
* @run main ReadIntoReadOnlyBuffer
|
||||
*/
|
||||
public class ReadIntoReadOnlyBuffer {
|
||||
private static final String THE_STRING = "123";
|
||||
public static void main(String[] args) throws Exception {
|
||||
CharBuffer buf = CharBuffer.allocate(8).asReadOnlyBuffer();
|
||||
StringReader r = new StringReader(THE_STRING);
|
||||
read(r, buf);
|
||||
buf = ByteBuffer.allocateDirect(16).asCharBuffer().asReadOnlyBuffer();
|
||||
r = new StringReader(THE_STRING);
|
||||
read(r, buf);
|
||||
}
|
||||
|
||||
private static void read(Reader r, CharBuffer b) throws IOException {
|
||||
try {
|
||||
r.read(b);
|
||||
throw new RuntimeException("ReadOnlyBufferException expected");
|
||||
} catch (ReadOnlyBufferException expected) {
|
||||
}
|
||||
|
||||
char[] c = new char[3];
|
||||
int n = r.read(c);
|
||||
if (n != c.length) {
|
||||
throw new RuntimeException("Expected " + c.length + ", got " + n);
|
||||
}
|
||||
|
||||
String s = new String(c);
|
||||
if (!s.equals(THE_STRING)) {
|
||||
throw new RuntimeException("Expected " + THE_STRING + ", got " + s);
|
||||
}
|
||||
}
|
||||
}
|
||||
85
test/jdk/java/io/Reader/ReadIntoZeroLengthArray.java
Normal file
85
test/jdk/java/io/Reader/ReadIntoZeroLengthArray.java
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright (c) 2020, 2026, 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.CharArrayReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.PushbackReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8248383
|
||||
* @summary Ensure that zero is returned for read into zero length array
|
||||
* @run junit ReadIntoZeroLengthArray
|
||||
*/
|
||||
public class ReadIntoZeroLengthArray {
|
||||
private static File file;
|
||||
|
||||
private static char[] cbuf0;
|
||||
private static char[] cbuf1;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() throws IOException {
|
||||
file = File.createTempFile("foo", "bar", new File("."));
|
||||
|
||||
cbuf0 = new char[0];
|
||||
cbuf1 = new char[1];
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void teardown() throws IOException {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
public static Stream<Reader> readers() throws IOException {
|
||||
return Stream.of(new LineNumberReader(new FileReader(file)),
|
||||
new CharArrayReader(new char[] {27}),
|
||||
new PushbackReader(new FileReader(file)),
|
||||
new FileReader(file),
|
||||
new StringReader(new String(new byte[] {(byte)42})));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
void test0(Reader r) throws IOException {
|
||||
assertEquals(0, r.read(cbuf0));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
void test1(Reader r) throws IOException {
|
||||
assertEquals(0, r.read(cbuf1, 0, 0));
|
||||
}
|
||||
}
|
||||
99
test/jdk/java/io/Reader/ReadParams.java
Normal file
99
test/jdk/java/io/Reader/ReadParams.java
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 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 4127657
|
||||
* @summary Check for correct handling of parameters to
|
||||
* XXXXReader.read(b, off, len).
|
||||
*
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class ReadParams {
|
||||
static int values[] = {Integer.MIN_VALUE, -1, 0, 1, 4, 16, 31,
|
||||
32, 33, Integer.MAX_VALUE};
|
||||
static char b[][] = {null, new char[32]};
|
||||
|
||||
static void test(Reader rdr) throws Exception {
|
||||
int i = 0, j = 0, k = 0;
|
||||
boolean nullPtr = false, indexOutBnd = false;
|
||||
|
||||
for (i = 0; i < b.length; i++) {
|
||||
for ( j = 0; j < values.length; j++) {
|
||||
for ( k = 0; k < values.length; k++) {
|
||||
|
||||
nullPtr = (b[i] == null);
|
||||
|
||||
int bufLen = nullPtr ? 0 : b[i].length;
|
||||
indexOutBnd = ((values[j] + values[k]) < 0)
|
||||
||(values[j] < 0)
|
||||
|| (values[j] > bufLen)
|
||||
|| (values[k] < 0)
|
||||
|| ((values[j] + values[k]) > bufLen);
|
||||
|
||||
try {
|
||||
rdr.read(b[i], values[j], values[k]);
|
||||
} catch (NullPointerException e) {
|
||||
if (!nullPtr) {
|
||||
throw new Exception
|
||||
("should not throw NullPointerException" + i + " " +j + " " + k);
|
||||
}
|
||||
continue;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
if (!indexOutBnd) {
|
||||
throw new Exception
|
||||
("should not throw IndexOutOfBoundsException");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (nullPtr || indexOutBnd) {
|
||||
throw new Exception("Should have thrown an exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception{
|
||||
StringReader sr = new StringReader(new String(new byte[512]));
|
||||
test(sr);
|
||||
|
||||
test(new BufferedReader(sr));
|
||||
|
||||
test(new CharArrayReader(new char[8]));
|
||||
|
||||
InputStreamReader ir = new InputStreamReader
|
||||
(new ByteArrayInputStream(new byte[512]));
|
||||
test(ir);
|
||||
|
||||
test(new PushbackReader(sr, 2));
|
||||
|
||||
PipedWriter pw = new PipedWriter();
|
||||
PipedReader pir = new PipedReader(pw);
|
||||
pw.write(new char[512], 0, 512);
|
||||
test(pir);
|
||||
}
|
||||
}
|
||||
171
test/jdk/java/io/Reader/ReaderBulkReadContract.java
Normal file
171
test/jdk/java/io/Reader/ReaderBulkReadContract.java
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
* Copyright (c) 2015, 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.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.CharArrayReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.PipedReader;
|
||||
import java.io.PipedWriter;
|
||||
import java.io.PushbackReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8029689
|
||||
* @summary checks the bounds part of the contract of java.io.Reader.read(char[], int, int):
|
||||
*
|
||||
* 0 <= off <= off+len <= cbuf.length
|
||||
*
|
||||
* for publicly exported subtypes of java.io.Reader
|
||||
*/
|
||||
public class ReaderBulkReadContract {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
ReaderBulkReadContract t = new ReaderBulkReadContract();
|
||||
t.test();
|
||||
}
|
||||
|
||||
private void test() throws IOException {
|
||||
Iterator<Object[]> args = args();
|
||||
while (args.hasNext()) {
|
||||
Object[] a = args.next();
|
||||
Reader r = (Reader) a[0];
|
||||
int size = (int) a[1];
|
||||
int off = (int) a[2];
|
||||
int len = (int) a[3];
|
||||
try {
|
||||
read(r, size, off, len);
|
||||
} finally {
|
||||
r.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Iterator<Object[]> args() {
|
||||
|
||||
Integer[] lens = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};
|
||||
Integer[] offs = {Integer.MIN_VALUE, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, Integer.MAX_VALUE};
|
||||
Integer[] sizes = {0, 1, 2, 3, 4, 5};
|
||||
String[] contents = {"", "a", "ab"};
|
||||
|
||||
List<Function<String, Reader>> fs = Arrays.asList(
|
||||
(String s) -> new BufferedReader(new StringReader(s)),
|
||||
(String s) -> new LineNumberReader(new StringReader(s)),
|
||||
(String s) -> new CharArrayReader(s.toCharArray()),
|
||||
(String s) -> new InputStreamReader(new ByteArrayInputStream(s.getBytes())),
|
||||
(String s) -> newFileReader(s),
|
||||
(String s) -> new PushbackReader(new StringReader(s)),
|
||||
(String s) -> newPipedReader(s),
|
||||
(String s) -> new StringReader(s)
|
||||
);
|
||||
|
||||
// The easiest way to produce a cartesian product from a small fixed number of sets
|
||||
List<Object[]> tuples = Collections.synchronizedList(new LinkedList<>());
|
||||
for (Integer len : lens)
|
||||
for (Integer off : offs)
|
||||
for (String s : contents)
|
||||
for (Integer size : sizes)
|
||||
for (Function<String, Reader> f : fs)
|
||||
tuples.add(new Object[]{f.apply(s), size, off, len});
|
||||
|
||||
return tuples.iterator();
|
||||
}
|
||||
|
||||
private void read(Reader r, int size, int off, int len) throws IOException {
|
||||
IndexOutOfBoundsException ex = null;
|
||||
try {
|
||||
r.read(new char[size], off, len);
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
ex = e;
|
||||
}
|
||||
|
||||
boolean incorrectBounds = off < 0 || len < 0 || len > size - off;
|
||||
boolean exceptionThrown = ex != null;
|
||||
|
||||
if (incorrectBounds != exceptionThrown) { // incorrectBounds iff exceptionThrown
|
||||
throw new AssertionError(format("r=%s, size=%s, off=%s, len=%s, incorrectBounds=%s, exceptionThrown=%s",
|
||||
r, size, off, len, incorrectBounds, exceptionThrown));
|
||||
}
|
||||
}
|
||||
|
||||
private static PipedReader newPipedReader(String contents) {
|
||||
try (PipedWriter w = new PipedWriter()) {
|
||||
PipedReader r = new PipedReader(w);
|
||||
w.write(contents);
|
||||
return r;
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private FileReader newFileReader(String contents) {
|
||||
try {
|
||||
// To not create an enormous amount of files
|
||||
File f = cache.computeIfAbsent(contents,
|
||||
ReaderBulkReadContract::createTempFileWithContents);
|
||||
return new FileReader(f);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static File createTempFileWithContents(String contents) {
|
||||
try {
|
||||
File testDir = new File(System.getProperty("test.dir", "."));
|
||||
File file = File.createTempFile("ReaderContract", "", testDir);
|
||||
try (FileWriter w = new FileWriter(file)) {
|
||||
w.write(contents);
|
||||
}
|
||||
return file;
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// To avoid myriads of tiny files a cache is used.
|
||||
// ConcurrentHashMap.computeIfAbsent promises a crucial thing:
|
||||
//
|
||||
// ...The entire method invocation is performed atomically, so the
|
||||
// function is applied at most once per key...
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
private final ConcurrentHashMap<String, File> cache = new ConcurrentHashMap<>();
|
||||
}
|
||||
110
test/jdk/java/io/Reader/Skip.java
Normal file
110
test/jdk/java/io/Reader/Skip.java
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2026, 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 4134311 8247918
|
||||
* @summary Test if skip works correctly
|
||||
* @run junit Skip
|
||||
*/
|
||||
|
||||
import java.io.CharArrayReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.PushbackReader;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class Skip {
|
||||
private static String FILENAME =
|
||||
System.getProperty("test.src", ".") + File.separator + "SkipInput.txt";
|
||||
private static File file = new File(FILENAME);
|
||||
|
||||
@Test
|
||||
public void skip() throws IOException {
|
||||
try (FileReader fr = new FileReader(file)) {
|
||||
long nchars = 8200;
|
||||
long actual = fr.skip(nchars);
|
||||
|
||||
assertFalse(actual > nchars,
|
||||
"Should skip " + nchars + ", but skipped " +actual+" chars");
|
||||
}
|
||||
}
|
||||
|
||||
public static Reader[] readers() throws IOException {
|
||||
return new Reader[] {
|
||||
new LineNumberReader(new FileReader(file)),
|
||||
new CharArrayReader(new char[] {27}),
|
||||
new PushbackReader(new FileReader(file)),
|
||||
new FileReader(file),
|
||||
new StringReader(new String(new byte[] {(byte)42}))
|
||||
};
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("readers")
|
||||
public void eof(Reader r) throws IOException {
|
||||
r.skip(Long.MAX_VALUE);
|
||||
assertEquals(0, r.skip(1));
|
||||
assertEquals(-1, r.read());
|
||||
}
|
||||
|
||||
public static Reader[] skipIAE() throws IOException {
|
||||
return new Reader[] {
|
||||
new LineNumberReader(new FileReader(file)),
|
||||
new PushbackReader(new FileReader(file)),
|
||||
new FileReader(file)
|
||||
};
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("skipIAE")
|
||||
public void testThrowsIAE(Reader r) throws IOException {
|
||||
assertThrows(IllegalArgumentException.class, () -> r.skip(-1));
|
||||
}
|
||||
|
||||
public static Reader[] skipNoIAE() throws IOException {
|
||||
return new Reader[] {
|
||||
new CharArrayReader(new char[] {27}),
|
||||
new StringReader(new String(new byte[] {(byte)42}))
|
||||
};
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("skipNoIAE")
|
||||
public void testNoIAE(Reader r) throws IOException {
|
||||
r.skip(-1);
|
||||
}
|
||||
}
|
||||
400
test/jdk/java/io/Reader/SkipInput.txt
Normal file
400
test/jdk/java/io/Reader/SkipInput.txt
Normal file
|
|
@ -0,0 +1,400 @@
|
|||
This line of text is 50 characters in length 0
|
||||
This line of text is 50 characters in length 1
|
||||
This line of text is 50 characters in length 2
|
||||
This line of text is 50 characters in length 3
|
||||
This line of text is 50 characters in length 4
|
||||
This line of text is 50 characters in length 5
|
||||
This line of text is 50 characters in length 6
|
||||
This line of text is 50 characters in length 7
|
||||
This line of text is 50 characters in length 8
|
||||
This line of text is 50 characters in length 9
|
||||
This line of text is 50 characters in length 10
|
||||
This line of text is 50 characters in length 11
|
||||
This line of text is 50 characters in length 12
|
||||
This line of text is 50 characters in length 13
|
||||
This line of text is 50 characters in length 14
|
||||
This line of text is 50 characters in length 15
|
||||
This line of text is 50 characters in length 16
|
||||
This line of text is 50 characters in length 17
|
||||
This line of text is 50 characters in length 18
|
||||
This line of text is 50 characters in length 19
|
||||
This line of text is 50 characters in length 20
|
||||
This line of text is 50 characters in length 21
|
||||
This line of text is 50 characters in length 22
|
||||
This line of text is 50 characters in length 23
|
||||
This line of text is 50 characters in length 24
|
||||
This line of text is 50 characters in length 25
|
||||
This line of text is 50 characters in length 26
|
||||
This line of text is 50 characters in length 27
|
||||
This line of text is 50 characters in length 28
|
||||
This line of text is 50 characters in length 29
|
||||
This line of text is 50 characters in length 30
|
||||
This line of text is 50 characters in length 31
|
||||
This line of text is 50 characters in length 32
|
||||
This line of text is 50 characters in length 33
|
||||
This line of text is 50 characters in length 34
|
||||
This line of text is 50 characters in length 35
|
||||
This line of text is 50 characters in length 36
|
||||
This line of text is 50 characters in length 37
|
||||
This line of text is 50 characters in length 38
|
||||
This line of text is 50 characters in length 39
|
||||
This line of text is 50 characters in length 40
|
||||
This line of text is 50 characters in length 41
|
||||
This line of text is 50 characters in length 42
|
||||
This line of text is 50 characters in length 43
|
||||
This line of text is 50 characters in length 44
|
||||
This line of text is 50 characters in length 45
|
||||
This line of text is 50 characters in length 46
|
||||
This line of text is 50 characters in length 47
|
||||
This line of text is 50 characters in length 48
|
||||
This line of text is 50 characters in length 49
|
||||
This line of text is 50 characters in length 50
|
||||
This line of text is 50 characters in length 51
|
||||
This line of text is 50 characters in length 52
|
||||
This line of text is 50 characters in length 53
|
||||
This line of text is 50 characters in length 54
|
||||
This line of text is 50 characters in length 55
|
||||
This line of text is 50 characters in length 56
|
||||
This line of text is 50 characters in length 57
|
||||
This line of text is 50 characters in length 58
|
||||
This line of text is 50 characters in length 59
|
||||
This line of text is 50 characters in length 60
|
||||
This line of text is 50 characters in length 61
|
||||
This line of text is 50 characters in length 62
|
||||
This line of text is 50 characters in length 63
|
||||
This line of text is 50 characters in length 64
|
||||
This line of text is 50 characters in length 65
|
||||
This line of text is 50 characters in length 66
|
||||
This line of text is 50 characters in length 67
|
||||
This line of text is 50 characters in length 68
|
||||
This line of text is 50 characters in length 69
|
||||
This line of text is 50 characters in length 70
|
||||
This line of text is 50 characters in length 71
|
||||
This line of text is 50 characters in length 72
|
||||
This line of text is 50 characters in length 73
|
||||
This line of text is 50 characters in length 74
|
||||
This line of text is 50 characters in length 75
|
||||
This line of text is 50 characters in length 76
|
||||
This line of text is 50 characters in length 77
|
||||
This line of text is 50 characters in length 78
|
||||
This line of text is 50 characters in length 79
|
||||
This line of text is 50 characters in length 80
|
||||
This line of text is 50 characters in length 81
|
||||
This line of text is 50 characters in length 82
|
||||
This line of text is 50 characters in length 83
|
||||
This line of text is 50 characters in length 84
|
||||
This line of text is 50 characters in length 85
|
||||
This line of text is 50 characters in length 86
|
||||
This line of text is 50 characters in length 87
|
||||
This line of text is 50 characters in length 88
|
||||
This line of text is 50 characters in length 89
|
||||
This line of text is 50 characters in length 90
|
||||
This line of text is 50 characters in length 91
|
||||
This line of text is 50 characters in length 92
|
||||
This line of text is 50 characters in length 93
|
||||
This line of text is 50 characters in length 94
|
||||
This line of text is 50 characters in length 95
|
||||
This line of text is 50 characters in length 96
|
||||
This line of text is 50 characters in length 97
|
||||
This line of text is 50 characters in length 98
|
||||
This line of text is 50 characters in length 99
|
||||
This line of text is 50 characters in length 100
|
||||
This line of text is 50 characters in length 101
|
||||
This line of text is 50 characters in length 102
|
||||
This line of text is 50 characters in length 103
|
||||
This line of text is 50 characters in length 104
|
||||
This line of text is 50 characters in length 105
|
||||
This line of text is 50 characters in length 106
|
||||
This line of text is 50 characters in length 107
|
||||
This line of text is 50 characters in length 108
|
||||
This line of text is 50 characters in length 109
|
||||
This line of text is 50 characters in length 110
|
||||
This line of text is 50 characters in length 111
|
||||
This line of text is 50 characters in length 112
|
||||
This line of text is 50 characters in length 113
|
||||
This line of text is 50 characters in length 114
|
||||
This line of text is 50 characters in length 115
|
||||
This line of text is 50 characters in length 116
|
||||
This line of text is 50 characters in length 117
|
||||
This line of text is 50 characters in length 118
|
||||
This line of text is 50 characters in length 119
|
||||
This line of text is 50 characters in length 120
|
||||
This line of text is 50 characters in length 121
|
||||
This line of text is 50 characters in length 122
|
||||
This line of text is 50 characters in length 123
|
||||
This line of text is 50 characters in length 124
|
||||
This line of text is 50 characters in length 125
|
||||
This line of text is 50 characters in length 126
|
||||
This line of text is 50 characters in length 127
|
||||
This line of text is 50 characters in length 128
|
||||
This line of text is 50 characters in length 129
|
||||
This line of text is 50 characters in length 130
|
||||
This line of text is 50 characters in length 131
|
||||
This line of text is 50 characters in length 132
|
||||
This line of text is 50 characters in length 133
|
||||
This line of text is 50 characters in length 134
|
||||
This line of text is 50 characters in length 135
|
||||
This line of text is 50 characters in length 136
|
||||
This line of text is 50 characters in length 137
|
||||
This line of text is 50 characters in length 138
|
||||
This line of text is 50 characters in length 139
|
||||
This line of text is 50 characters in length 140
|
||||
This line of text is 50 characters in length 141
|
||||
This line of text is 50 characters in length 142
|
||||
This line of text is 50 characters in length 143
|
||||
This line of text is 50 characters in length 144
|
||||
This line of text is 50 characters in length 145
|
||||
This line of text is 50 characters in length 146
|
||||
This line of text is 50 characters in length 147
|
||||
This line of text is 50 characters in length 148
|
||||
This line of text is 50 characters in length 149
|
||||
This line of text is 50 characters in length 150
|
||||
This line of text is 50 characters in length 151
|
||||
This line of text is 50 characters in length 152
|
||||
This line of text is 50 characters in length 153
|
||||
This line of text is 50 characters in length 154
|
||||
This line of text is 50 characters in length 155
|
||||
This line of text is 50 characters in length 156
|
||||
This line of text is 50 characters in length 157
|
||||
This line of text is 50 characters in length 158
|
||||
This line of text is 50 characters in length 159
|
||||
This line of text is 50 characters in length 160
|
||||
This line of text is 50 characters in length 161
|
||||
This line of text is 50 characters in length 162
|
||||
This line of text is 50 characters in length 163
|
||||
This line of text is 50 characters in length 164
|
||||
This line of text is 50 characters in length 165
|
||||
This line of text is 50 characters in length 166
|
||||
This line of text is 50 characters in length 167
|
||||
This line of text is 50 characters in length 168
|
||||
This line of text is 50 characters in length 169
|
||||
This line of text is 50 characters in length 170
|
||||
This line of text is 50 characters in length 171
|
||||
This line of text is 50 characters in length 172
|
||||
This line of text is 50 characters in length 173
|
||||
This line of text is 50 characters in length 174
|
||||
This line of text is 50 characters in length 175
|
||||
This line of text is 50 characters in length 176
|
||||
This line of text is 50 characters in length 177
|
||||
This line of text is 50 characters in length 178
|
||||
This line of text is 50 characters in length 179
|
||||
This line of text is 50 characters in length 180
|
||||
This line of text is 50 characters in length 181
|
||||
This line of text is 50 characters in length 182
|
||||
This line of text is 50 characters in length 183
|
||||
This line of text is 50 characters in length 184
|
||||
This line of text is 50 characters in length 185
|
||||
This line of text is 50 characters in length 186
|
||||
This line of text is 50 characters in length 187
|
||||
This line of text is 50 characters in length 188
|
||||
This line of text is 50 characters in length 189
|
||||
This line of text is 50 characters in length 190
|
||||
This line of text is 50 characters in length 191
|
||||
This line of text is 50 characters in length 192
|
||||
This line of text is 50 characters in length 193
|
||||
This line of text is 50 characters in length 194
|
||||
This line of text is 50 characters in length 195
|
||||
This line of text is 50 characters in length 196
|
||||
This line of text is 50 characters in length 197
|
||||
This line of text is 50 characters in length 198
|
||||
This line of text is 50 characters in length 199
|
||||
This line of text is 50 characters in length 200
|
||||
This line of text is 50 characters in length 201
|
||||
This line of text is 50 characters in length 202
|
||||
This line of text is 50 characters in length 203
|
||||
This line of text is 50 characters in length 204
|
||||
This line of text is 50 characters in length 205
|
||||
This line of text is 50 characters in length 206
|
||||
This line of text is 50 characters in length 207
|
||||
This line of text is 50 characters in length 208
|
||||
This line of text is 50 characters in length 209
|
||||
This line of text is 50 characters in length 210
|
||||
This line of text is 50 characters in length 211
|
||||
This line of text is 50 characters in length 212
|
||||
This line of text is 50 characters in length 213
|
||||
This line of text is 50 characters in length 214
|
||||
This line of text is 50 characters in length 215
|
||||
This line of text is 50 characters in length 216
|
||||
This line of text is 50 characters in length 217
|
||||
This line of text is 50 characters in length 218
|
||||
This line of text is 50 characters in length 219
|
||||
This line of text is 50 characters in length 220
|
||||
This line of text is 50 characters in length 221
|
||||
This line of text is 50 characters in length 222
|
||||
This line of text is 50 characters in length 223
|
||||
This line of text is 50 characters in length 224
|
||||
This line of text is 50 characters in length 225
|
||||
This line of text is 50 characters in length 226
|
||||
This line of text is 50 characters in length 227
|
||||
This line of text is 50 characters in length 228
|
||||
This line of text is 50 characters in length 229
|
||||
This line of text is 50 characters in length 230
|
||||
This line of text is 50 characters in length 231
|
||||
This line of text is 50 characters in length 232
|
||||
This line of text is 50 characters in length 233
|
||||
This line of text is 50 characters in length 234
|
||||
This line of text is 50 characters in length 235
|
||||
This line of text is 50 characters in length 236
|
||||
This line of text is 50 characters in length 237
|
||||
This line of text is 50 characters in length 238
|
||||
This line of text is 50 characters in length 239
|
||||
This line of text is 50 characters in length 240
|
||||
This line of text is 50 characters in length 241
|
||||
This line of text is 50 characters in length 242
|
||||
This line of text is 50 characters in length 243
|
||||
This line of text is 50 characters in length 244
|
||||
This line of text is 50 characters in length 245
|
||||
This line of text is 50 characters in length 246
|
||||
This line of text is 50 characters in length 247
|
||||
This line of text is 50 characters in length 248
|
||||
This line of text is 50 characters in length 249
|
||||
This line of text is 50 characters in length 250
|
||||
This line of text is 50 characters in length 251
|
||||
This line of text is 50 characters in length 252
|
||||
This line of text is 50 characters in length 253
|
||||
This line of text is 50 characters in length 254
|
||||
This line of text is 50 characters in length 255
|
||||
This line of text is 50 characters in length 256
|
||||
This line of text is 50 characters in length 257
|
||||
This line of text is 50 characters in length 258
|
||||
This line of text is 50 characters in length 259
|
||||
This line of text is 50 characters in length 260
|
||||
This line of text is 50 characters in length 261
|
||||
This line of text is 50 characters in length 262
|
||||
This line of text is 50 characters in length 263
|
||||
This line of text is 50 characters in length 264
|
||||
This line of text is 50 characters in length 265
|
||||
This line of text is 50 characters in length 266
|
||||
This line of text is 50 characters in length 267
|
||||
This line of text is 50 characters in length 268
|
||||
This line of text is 50 characters in length 269
|
||||
This line of text is 50 characters in length 270
|
||||
This line of text is 50 characters in length 271
|
||||
This line of text is 50 characters in length 272
|
||||
This line of text is 50 characters in length 273
|
||||
This line of text is 50 characters in length 274
|
||||
This line of text is 50 characters in length 275
|
||||
This line of text is 50 characters in length 276
|
||||
This line of text is 50 characters in length 277
|
||||
This line of text is 50 characters in length 278
|
||||
This line of text is 50 characters in length 279
|
||||
This line of text is 50 characters in length 280
|
||||
This line of text is 50 characters in length 281
|
||||
This line of text is 50 characters in length 282
|
||||
This line of text is 50 characters in length 283
|
||||
This line of text is 50 characters in length 284
|
||||
This line of text is 50 characters in length 285
|
||||
This line of text is 50 characters in length 286
|
||||
This line of text is 50 characters in length 287
|
||||
This line of text is 50 characters in length 288
|
||||
This line of text is 50 characters in length 289
|
||||
This line of text is 50 characters in length 290
|
||||
This line of text is 50 characters in length 291
|
||||
This line of text is 50 characters in length 292
|
||||
This line of text is 50 characters in length 293
|
||||
This line of text is 50 characters in length 294
|
||||
This line of text is 50 characters in length 295
|
||||
This line of text is 50 characters in length 296
|
||||
This line of text is 50 characters in length 297
|
||||
This line of text is 50 characters in length 298
|
||||
This line of text is 50 characters in length 299
|
||||
This line of text is 50 characters in length 300
|
||||
This line of text is 50 characters in length 301
|
||||
This line of text is 50 characters in length 302
|
||||
This line of text is 50 characters in length 303
|
||||
This line of text is 50 characters in length 304
|
||||
This line of text is 50 characters in length 305
|
||||
This line of text is 50 characters in length 306
|
||||
This line of text is 50 characters in length 307
|
||||
This line of text is 50 characters in length 308
|
||||
This line of text is 50 characters in length 309
|
||||
This line of text is 50 characters in length 310
|
||||
This line of text is 50 characters in length 311
|
||||
This line of text is 50 characters in length 312
|
||||
This line of text is 50 characters in length 313
|
||||
This line of text is 50 characters in length 314
|
||||
This line of text is 50 characters in length 315
|
||||
This line of text is 50 characters in length 316
|
||||
This line of text is 50 characters in length 317
|
||||
This line of text is 50 characters in length 318
|
||||
This line of text is 50 characters in length 319
|
||||
This line of text is 50 characters in length 320
|
||||
This line of text is 50 characters in length 321
|
||||
This line of text is 50 characters in length 322
|
||||
This line of text is 50 characters in length 323
|
||||
This line of text is 50 characters in length 324
|
||||
This line of text is 50 characters in length 325
|
||||
This line of text is 50 characters in length 326
|
||||
This line of text is 50 characters in length 327
|
||||
This line of text is 50 characters in length 328
|
||||
This line of text is 50 characters in length 329
|
||||
This line of text is 50 characters in length 330
|
||||
This line of text is 50 characters in length 331
|
||||
This line of text is 50 characters in length 332
|
||||
This line of text is 50 characters in length 333
|
||||
This line of text is 50 characters in length 334
|
||||
This line of text is 50 characters in length 335
|
||||
This line of text is 50 characters in length 336
|
||||
This line of text is 50 characters in length 337
|
||||
This line of text is 50 characters in length 338
|
||||
This line of text is 50 characters in length 339
|
||||
This line of text is 50 characters in length 340
|
||||
This line of text is 50 characters in length 341
|
||||
This line of text is 50 characters in length 342
|
||||
This line of text is 50 characters in length 343
|
||||
This line of text is 50 characters in length 344
|
||||
This line of text is 50 characters in length 345
|
||||
This line of text is 50 characters in length 346
|
||||
This line of text is 50 characters in length 347
|
||||
This line of text is 50 characters in length 348
|
||||
This line of text is 50 characters in length 349
|
||||
This line of text is 50 characters in length 350
|
||||
This line of text is 50 characters in length 351
|
||||
This line of text is 50 characters in length 352
|
||||
This line of text is 50 characters in length 353
|
||||
This line of text is 50 characters in length 354
|
||||
This line of text is 50 characters in length 355
|
||||
This line of text is 50 characters in length 356
|
||||
This line of text is 50 characters in length 357
|
||||
This line of text is 50 characters in length 358
|
||||
This line of text is 50 characters in length 359
|
||||
This line of text is 50 characters in length 360
|
||||
This line of text is 50 characters in length 361
|
||||
This line of text is 50 characters in length 362
|
||||
This line of text is 50 characters in length 363
|
||||
This line of text is 50 characters in length 364
|
||||
This line of text is 50 characters in length 365
|
||||
This line of text is 50 characters in length 366
|
||||
This line of text is 50 characters in length 367
|
||||
This line of text is 50 characters in length 368
|
||||
This line of text is 50 characters in length 369
|
||||
This line of text is 50 characters in length 370
|
||||
This line of text is 50 characters in length 371
|
||||
This line of text is 50 characters in length 372
|
||||
This line of text is 50 characters in length 373
|
||||
This line of text is 50 characters in length 374
|
||||
This line of text is 50 characters in length 375
|
||||
This line of text is 50 characters in length 376
|
||||
This line of text is 50 characters in length 377
|
||||
This line of text is 50 characters in length 378
|
||||
This line of text is 50 characters in length 379
|
||||
This line of text is 50 characters in length 380
|
||||
This line of text is 50 characters in length 381
|
||||
This line of text is 50 characters in length 382
|
||||
This line of text is 50 characters in length 383
|
||||
This line of text is 50 characters in length 384
|
||||
This line of text is 50 characters in length 385
|
||||
This line of text is 50 characters in length 386
|
||||
This line of text is 50 characters in length 387
|
||||
This line of text is 50 characters in length 388
|
||||
This line of text is 50 characters in length 389
|
||||
This line of text is 50 characters in length 390
|
||||
This line of text is 50 characters in length 391
|
||||
This line of text is 50 characters in length 392
|
||||
This line of text is 50 characters in length 393
|
||||
This line of text is 50 characters in length 394
|
||||
This line of text is 50 characters in length 395
|
||||
This line of text is 50 characters in length 396
|
||||
This line of text is 50 characters in length 397
|
||||
This line of text is 50 characters in length 398
|
||||
This line of text is 50 characters in length 399
|
||||
49
test/jdk/java/io/Reader/SkipNegative.java
Normal file
49
test/jdk/java/io/Reader/SkipNegative.java
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2010, 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 4150442
|
||||
@summary Skip must throw an exception for negative args
|
||||
*/
|
||||
|
||||
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class SkipNegative {
|
||||
public static void main(String argv[]) throws Exception {
|
||||
File f = new File(System.getProperty("test.src", "."),
|
||||
"SkipInput.txt");
|
||||
FileReader fr = new FileReader(f);
|
||||
long nchars = -1L;
|
||||
try {
|
||||
long actual = fr.skip(nchars);
|
||||
} catch(IllegalArgumentException e){
|
||||
// Negative argument caught
|
||||
return;
|
||||
} finally {
|
||||
fr.close();
|
||||
}
|
||||
throw new Exception("Skip should not accept negative values");
|
||||
}
|
||||
}
|
||||
353
test/jdk/java/io/Reader/TransferTo.java
Normal file
353
test/jdk/java/io/Reader/TransferTo.java
Normal file
|
|
@ -0,0 +1,353 @@
|
|||
/*
|
||||
* Copyright (c) 2017, 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.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import jdk.test.lib.RandomFactory;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8191706
|
||||
* @summary tests whether java.io.Reader.transferTo conforms to its
|
||||
* contract defined source the javadoc
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.RandomFactory
|
||||
* @run main TransferTo
|
||||
* @key randomness
|
||||
* @author Patrick Reinhart
|
||||
*/
|
||||
public class TransferTo {
|
||||
|
||||
private static Random generator = RandomFactory.getRandom();
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
ifOutIsNullThenNpeIsThrown();
|
||||
ifExceptionInInputNeitherStreamIsClosed();
|
||||
ifExceptionInOutputNeitherStreamIsClosed();
|
||||
onReturnNeitherStreamIsClosed();
|
||||
onReturnInputIsAtEnd();
|
||||
contents();
|
||||
}
|
||||
|
||||
private static void ifOutIsNullThenNpeIsThrown() throws IOException {
|
||||
try (Reader in = input()) {
|
||||
assertThrowsNPE(() -> in.transferTo(null), "out");
|
||||
}
|
||||
|
||||
try (Reader in = input((char) 1)) {
|
||||
assertThrowsNPE(() -> in.transferTo(null), "out");
|
||||
}
|
||||
|
||||
try (Reader in = input((char) 1, (char) 2)) {
|
||||
assertThrowsNPE(() -> in.transferTo(null), "out");
|
||||
}
|
||||
|
||||
Reader in = null;
|
||||
try {
|
||||
Reader fin = in = new ThrowingReader();
|
||||
// null check should precede everything else:
|
||||
// Reader shouldn't be touched if Writer is null
|
||||
assertThrowsNPE(() -> fin.transferTo(null), "out");
|
||||
} finally {
|
||||
if (in != null)
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException ignored) { }
|
||||
}
|
||||
}
|
||||
|
||||
private static void ifExceptionInInputNeitherStreamIsClosed()
|
||||
throws IOException {
|
||||
transferToThenCheckIfAnyClosed(input(0, new char[]{1, 2, 3}), output());
|
||||
transferToThenCheckIfAnyClosed(input(1, new char[]{1, 2, 3}), output());
|
||||
transferToThenCheckIfAnyClosed(input(2, new char[]{1, 2, 3}), output());
|
||||
}
|
||||
|
||||
private static void ifExceptionInOutputNeitherStreamIsClosed()
|
||||
throws IOException {
|
||||
transferToThenCheckIfAnyClosed(input(new char[]{1, 2, 3}), output(0));
|
||||
transferToThenCheckIfAnyClosed(input(new char[]{1, 2, 3}), output(1));
|
||||
transferToThenCheckIfAnyClosed(input(new char[]{1, 2, 3}), output(2));
|
||||
}
|
||||
|
||||
private static void transferToThenCheckIfAnyClosed(Reader input,
|
||||
Writer output)
|
||||
throws IOException {
|
||||
try (CloseLoggingReader in = new CloseLoggingReader(input);
|
||||
CloseLoggingWriter out =
|
||||
new CloseLoggingWriter(output)) {
|
||||
boolean thrown = false;
|
||||
try {
|
||||
in.transferTo(out);
|
||||
} catch (IOException ignored) {
|
||||
thrown = true;
|
||||
}
|
||||
if (!thrown)
|
||||
throw new AssertionError();
|
||||
|
||||
if (in.wasClosed() || out.wasClosed()) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void onReturnNeitherStreamIsClosed()
|
||||
throws IOException {
|
||||
try (CloseLoggingReader in =
|
||||
new CloseLoggingReader(input(new char[]{1, 2, 3}));
|
||||
CloseLoggingWriter out =
|
||||
new CloseLoggingWriter(output())) {
|
||||
|
||||
in.transferTo(out);
|
||||
|
||||
if (in.wasClosed() || out.wasClosed()) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void onReturnInputIsAtEnd() throws IOException {
|
||||
try (Reader in = input(new char[]{1, 2, 3});
|
||||
Writer out = output()) {
|
||||
|
||||
in.transferTo(out);
|
||||
|
||||
if (in.read() != -1) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void contents() throws IOException {
|
||||
checkTransferredContents(new char[0]);
|
||||
checkTransferredContents(createRandomChars(1024, 4096));
|
||||
// to span through several batches
|
||||
checkTransferredContents(createRandomChars(16384, 16384));
|
||||
}
|
||||
|
||||
private static void checkTransferredContents(char[] chars)
|
||||
throws IOException {
|
||||
try (Reader in = input(chars);
|
||||
StringWriter out = new StringWriter()) {
|
||||
in.transferTo(out);
|
||||
|
||||
char[] outChars = out.toString().toCharArray();
|
||||
if (!Arrays.equals(chars, outChars)) {
|
||||
throw new AssertionError(
|
||||
format("chars.length=%s, outChars.length=%s",
|
||||
chars.length, outChars.length));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static char[] createRandomChars(int min, int maxRandomAdditive) {
|
||||
char[] chars = new char[min + generator.nextInt(maxRandomAdditive)];
|
||||
for (int index=0; index<chars.length; index++) {
|
||||
chars[index] = (char)generator.nextInt();
|
||||
}
|
||||
return chars;
|
||||
}
|
||||
|
||||
private static Writer output() {
|
||||
return output(-1);
|
||||
}
|
||||
|
||||
private static Writer output(int exceptionPosition) {
|
||||
return new Writer() {
|
||||
|
||||
int pos;
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
if (pos++ == exceptionPosition)
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(char[] chars, int off, int len) throws IOException {
|
||||
for (int i=0; i<len; i++) {
|
||||
write(chars[off + i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Writer append(CharSequence csq, int start, int end) throws IOException {
|
||||
for (int i = start; i < end; i++) {
|
||||
write(csq.charAt(i));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static Reader input(char... chars) {
|
||||
return input(-1, chars);
|
||||
}
|
||||
|
||||
private static Reader input(int exceptionPosition, char... chars) {
|
||||
return new Reader() {
|
||||
|
||||
int pos;
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (pos == exceptionPosition) {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
if (pos >= chars.length)
|
||||
return -1;
|
||||
return chars[pos++];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(char[] cbuf, int off, int len) throws IOException {
|
||||
int c = read();
|
||||
if (c == -1) {
|
||||
return -1;
|
||||
}
|
||||
cbuf[off] = (char)c;
|
||||
|
||||
int i = 1;
|
||||
for (; i < len ; i++) {
|
||||
c = read();
|
||||
if (c == -1) {
|
||||
break;
|
||||
}
|
||||
cbuf[off + i] = (char)c;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static class ThrowingReader extends Reader {
|
||||
|
||||
boolean closed;
|
||||
|
||||
@Override
|
||||
public int read(char[] b, int off, int len) throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (!closed) {
|
||||
closed = true;
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
|
||||
private static class CloseLoggingReader extends FilterReader {
|
||||
|
||||
boolean closed;
|
||||
|
||||
CloseLoggingReader(Reader in) {
|
||||
super(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closed = true;
|
||||
super.close();
|
||||
}
|
||||
|
||||
boolean wasClosed() {
|
||||
return closed;
|
||||
}
|
||||
}
|
||||
|
||||
private static class CloseLoggingWriter extends FilterWriter {
|
||||
|
||||
boolean closed;
|
||||
|
||||
CloseLoggingWriter(Writer out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closed = true;
|
||||
super.close();
|
||||
}
|
||||
|
||||
boolean wasClosed() {
|
||||
return closed;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Thrower {
|
||||
public void run() throws Throwable;
|
||||
}
|
||||
|
||||
public static void assertThrowsNPE(Thrower thrower, String message) {
|
||||
assertThrows(thrower, NullPointerException.class, message);
|
||||
}
|
||||
|
||||
public static <T extends Throwable> void assertThrows(Thrower thrower,
|
||||
Class<T> throwable,
|
||||
String message) {
|
||||
Throwable thrown;
|
||||
try {
|
||||
thrower.run();
|
||||
thrown = null;
|
||||
} catch (Throwable caught) {
|
||||
thrown = caught;
|
||||
}
|
||||
|
||||
if (!throwable.isInstance(thrown)) {
|
||||
String caught = thrown == null ?
|
||||
"nothing" : thrown.getClass().getCanonicalName();
|
||||
throw new AssertionError(
|
||||
format("Expected to catch %s, but caught %s",
|
||||
throwable, caught), thrown);
|
||||
}
|
||||
|
||||
if (thrown != null && !message.equals(thrown.getMessage())) {
|
||||
throw new AssertionError(
|
||||
format("Expected exception message to be '%s', but it's '%s'",
|
||||
message, thrown.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue