Path: blob/master/test/jdk/java/io/etc/FailingFlushAndClose.java
41149 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;2425/**26* @test27* @bug 7015589 805456528* @summary Test that buffering streams are considered closed even when the29* close or flush from the underlying stream fails.30*/3132public class FailingFlushAndClose {3334static int failed;3536static void fail(String msg) {37System.err.println("FAIL: " + msg);38failed++;39}4041static void failWithIOE(String msg) throws IOException {42fail(msg);43throw new IOException(msg);44}4546static class FailingCloseInputStream extends InputStream {47boolean closed;48@Override49public int read()throws IOException {50if (closed)51failWithIOE("input stream is closed");52return 1;53}54@Override55public void close() throws IOException {56if (!closed) {57closed = true;58throw new IOException("close failed");59}60}61}6263static class FailingCloseOutputStream extends OutputStream {64boolean closed;65@Override66public void write(int b) throws IOException {67if (closed)68failWithIOE("output stream is closed");69}70@Override71public void flush() throws IOException {72if (closed)73failWithIOE("output stream is closed");74}75@Override76public void close() throws IOException {77if (!closed) {78closed = true;79throw new IOException("close failed");80}81}82}8384static class FailingFlushOutputStream extends OutputStream {85boolean closed;86@Override87public void write(int b) throws IOException {88if (closed)89failWithIOE("output stream is closed");90}91@Override92public void flush() throws IOException {93if (closed) {94failWithIOE("output stream is closed");95} else {96throw new IOException("flush failed");97}98}99@Override100public void close() throws IOException {101closed = true;102}103}104105static class FailingCloseReader extends Reader {106boolean closed;107@Override108public int read(char[] cbuf, int off, int len) throws IOException {109if (closed)110failWithIOE("reader is closed");111return 1;112}113@Override114public void close() throws IOException {115if (!closed) {116closed = true;117throw new IOException("close failed");118}119}120}121122static class FailingCloseWriter extends Writer {123boolean closed;124@Override125public void write(char[] cbuf, int off, int len) throws IOException {126if (closed)127failWithIOE("writer is closed");128}129@Override130public void flush() throws IOException {131if (closed)132failWithIOE("writer is closed");133}134@Override135public void close() throws IOException {136if (!closed) {137closed = true;138throw new IOException("close failed");139}140}141}142143static class FailingFlushWriter extends Writer {144boolean closed;145@Override146public void write(char[] cbuf, int off, int len) throws IOException {147if (closed)148failWithIOE("writer is closed");149}150@Override151public void flush() throws IOException {152if (closed) {153failWithIOE("writer is closed");154} else {155throw new IOException("flush failed");156}157}158@Override159public void close() throws IOException {160if (!closed) {161closed = true;162throw new IOException("close failed");163}164}165}166167static InputStream testFailingClose(InputStream in) throws IOException {168System.out.println(in.getClass());169in.read(new byte[100]);170try {171in.close();172fail("close did not fail");173} catch (IOException expected) { }174try {175in.read(new byte[100]);176fail("read did not fail");177} catch (IOException expected) { }178return in;179}180181static OutputStream testFailingClose(OutputStream out) throws IOException {182System.out.println(out.getClass());183out.write(1);184try {185out.close();186fail("close did not fail");187} catch (IOException expected) { }188try {189out.write(1);190if (!(out instanceof BufferedOutputStream))191fail("write did not fail");192} catch (IOException expected) { }193return out;194}195196static OutputStream testFailingFlush(OutputStream out) throws IOException {197System.out.println(out.getClass());198out.write(1);199try {200out.flush();201fail("flush did not fail");202} catch (IOException expected) { }203if (out instanceof BufferedOutputStream) {204out.write(1);205try {206out.close();207fail("close did not fail");208} catch (IOException expected) { }209}210return out;211}212213static void closeAgain(InputStream in) throws IOException {214// assert the given stream should already be closed.215try {216in.close();217} catch (IOException expected) {218fail("unexpected IOException from subsequent close");219}220}221static void closeAgain(OutputStream out) throws IOException {222// assert the given stream should already be closed.223try {224out.close();225} catch (IOException expected) {226fail("unexpected IOException from subsequent close");227}228}229230static Reader testFailingClose(Reader r) throws IOException {231System.out.println(r.getClass());232r.read(new char[100]);233try {234r.close();235fail("close did not fail");236} catch (IOException expected) { }237try {238r.read(new char[100]);239fail("read did not fail");240} catch (IOException expected) { }241return r;242}243244static Writer testFailingClose(Writer w) throws IOException {245System.out.println(w.getClass());246w.write("message");247try {248w.close();249fail("close did not fail");250} catch (IOException expected) { }251try {252w.write("another message");253fail("write did not fail");254} catch (IOException expected) { }255return w;256}257258static Writer testFailingFlush(Writer w) throws IOException {259System.out.println(w.getClass());260w.write("message");261try {262w.flush();263fail("flush did not fail");264} catch (IOException expected) { }265if (w instanceof BufferedWriter) {266// assume this message will be buffered267w.write("another message");268try {269w.close();270fail("close did not fail");271} catch (IOException expected) { }272}273return w;274}275276static Reader closeAgain(Reader r) throws IOException {277// assert the given stream should already be closed.278try {279r.close();280} catch (IOException expected) {281fail("unexpected IOException from subsequent close");282}283return r;284}285static Writer closeAgain(Writer w) throws IOException {286// assert the given stream should already be closed.287try {288w.close();289} catch (IOException expected) {290fail("unexpected IOException from subsequent close");291}292return w;293}294295public static void main(String[] args) throws IOException {296297closeAgain(testFailingClose(new BufferedInputStream(new FailingCloseInputStream())));298closeAgain(testFailingClose(new BufferedOutputStream(new FailingCloseOutputStream())));299300closeAgain(testFailingClose(new BufferedReader(new FailingCloseReader())));301closeAgain(testFailingClose(new BufferedWriter(new FailingCloseWriter())));302303closeAgain(testFailingFlush(new BufferedOutputStream(new FailingFlushOutputStream())));304closeAgain(testFailingFlush(new BufferedWriter(new FailingFlushWriter())));305306if (failed > 0)307throw new RuntimeException(failed + " test(s) failed - see log for details");308}309}310311312