test/java/io/etc/FailingFlushAndClose.java
Print this page
*** 23,33 ****
import java.io.*;
/**
* @test
! * @bug 7015589
* @summary Test that buffering streams are considered closed even when the
* close or flush from the underlying stream fails.
*/
public class FailingFlushAndClose {
--- 23,33 ----
import java.io.*;
/**
* @test
! * @bug 7015589 8054565
* @summary Test that buffering streams are considered closed even when the
* close or flush from the underlying stream fails.
*/
public class FailingFlushAndClose {
*** 163,186 ****
throw new IOException("close failed");
}
}
}
! static void testFailingClose(InputStream in) throws IOException {
System.out.println(in.getClass());
in.read(new byte[100]);
try {
in.close();
fail("close did not fail");
} catch (IOException expected) { }
try {
in.read(new byte[100]);
fail("read did not fail");
} catch (IOException expected) { }
}
! static void testFailingClose(OutputStream out) throws IOException {
System.out.println(out.getClass());
out.write(1);
try {
out.close();
fail("close did not fail");
--- 163,187 ----
throw new IOException("close failed");
}
}
}
! static InputStream testFailingClose(InputStream in) throws IOException {
System.out.println(in.getClass());
in.read(new byte[100]);
try {
in.close();
fail("close did not fail");
} catch (IOException expected) { }
try {
in.read(new byte[100]);
fail("read did not fail");
} catch (IOException expected) { }
+ return in;
}
! static OutputStream testFailingClose(OutputStream out) throws IOException {
System.out.println(out.getClass());
out.write(1);
try {
out.close();
fail("close did not fail");
*** 188,200 ****
try {
out.write(1);
if (!(out instanceof BufferedOutputStream))
fail("write did not fail");
} catch (IOException expected) { }
}
! static void testFailingFlush(OutputStream out) throws IOException {
System.out.println(out.getClass());
out.write(1);
try {
out.flush();
fail("flush did not fail");
--- 189,202 ----
try {
out.write(1);
if (!(out instanceof BufferedOutputStream))
fail("write did not fail");
} catch (IOException expected) { }
+ return out;
}
! static OutputStream testFailingFlush(OutputStream out) throws IOException {
System.out.println(out.getClass());
out.write(1);
try {
out.flush();
fail("flush did not fail");
*** 204,242 ****
try {
out.close();
fail("close did not fail");
} catch (IOException expected) { }
}
}
! static void testFailingClose(Reader r) throws IOException {
System.out.println(r.getClass());
r.read(new char[100]);
try {
r.close();
fail("close did not fail");
} catch (IOException expected) { }
try {
r.read(new char[100]);
fail("read did not fail");
} catch (IOException expected) { }
}
! static void testFailingClose(Writer w) throws IOException {
System.out.println(w.getClass());
w.write("message");
try {
w.close();
fail("close did not fail");
} catch (IOException expected) { }
try {
w.write("another message");
fail("write did not fail");
} catch (IOException expected) { }
}
! static void testFailingFlush(Writer w) throws IOException {
System.out.println(w.getClass());
w.write("message");
try {
w.flush();
fail("flush did not fail");
--- 206,264 ----
try {
out.close();
fail("close did not fail");
} catch (IOException expected) { }
}
+ return out;
}
! static void closeAgain(InputStream in) throws IOException {
! // assert the given stream should already be closed.
! try {
! in.close();
! } catch (IOException expected) {
! fail("unexpected IOException from subsequent close");
! }
! }
! static void closeAgain(OutputStream out) throws IOException {
! // assert the given stream should already be closed.
! try {
! out.close();
! } catch (IOException expected) {
! fail("unexpected IOException from subsequent close");
! }
! }
!
! static Reader testFailingClose(Reader r) throws IOException {
System.out.println(r.getClass());
r.read(new char[100]);
try {
r.close();
fail("close did not fail");
} catch (IOException expected) { }
try {
r.read(new char[100]);
fail("read did not fail");
} catch (IOException expected) { }
+ return r;
}
! static Writer testFailingClose(Writer w) throws IOException {
System.out.println(w.getClass());
w.write("message");
try {
w.close();
fail("close did not fail");
} catch (IOException expected) { }
try {
w.write("another message");
fail("write did not fail");
} catch (IOException expected) { }
+ return w;
}
! static Writer testFailingFlush(Writer w) throws IOException {
System.out.println(w.getClass());
w.write("message");
try {
w.flush();
fail("flush did not fail");
*** 247,268 ****
try {
w.close();
fail("close did not fail");
} catch (IOException expected) { }
}
}
public static void main(String[] args) throws IOException {
! testFailingClose(new BufferedInputStream(new FailingCloseInputStream()));
! testFailingClose(new BufferedOutputStream(new FailingCloseOutputStream()));
! testFailingClose(new BufferedReader(new FailingCloseReader()));
! testFailingClose(new BufferedWriter(new FailingCloseWriter()));
! testFailingFlush(new BufferedOutputStream(new FailingFlushOutputStream()));
! testFailingFlush(new BufferedWriter(new FailingFlushWriter()));
if (failed > 0)
throw new RuntimeException(failed + " test(s) failed - see log for details");
}
}
--- 269,310 ----
try {
w.close();
fail("close did not fail");
} catch (IOException expected) { }
}
+ return w;
+ }
+
+ static Reader closeAgain(Reader r) throws IOException {
+ // assert the given stream should already be closed.
+ try {
+ r.close();
+ } catch (IOException expected) {
+ fail("unexpected IOException from subsequent close");
+ }
+ return r;
+ }
+ static Writer closeAgain(Writer w) throws IOException {
+ // assert the given stream should already be closed.
+ try {
+ w.close();
+ } catch (IOException expected) {
+ fail("unexpected IOException from subsequent close");
+ }
+ return w;
}
public static void main(String[] args) throws IOException {
! closeAgain(testFailingClose(new BufferedInputStream(new FailingCloseInputStream())));
! closeAgain(testFailingClose(new BufferedOutputStream(new FailingCloseOutputStream())));
! closeAgain(testFailingClose(new BufferedReader(new FailingCloseReader())));
! closeAgain(testFailingClose(new BufferedWriter(new FailingCloseWriter())));
! closeAgain(testFailingFlush(new BufferedOutputStream(new FailingFlushOutputStream())));
! closeAgain(testFailingFlush(new BufferedWriter(new FailingFlushWriter())));
if (failed > 0)
throw new RuntimeException(failed + " test(s) failed - see log for details");
}
}