Path: blob/master/test/jdk/java/io/SequenceInputStream/Close.java
41152 views
/*1* Copyright (c) 2019, 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*/2223/* @test24* @bug 807889125* @summary Ensure close() closes all component streams26*/2728import java.io.ByteArrayInputStream;29import java.io.IOException;30import java.io.SequenceInputStream;31import java.util.Collections;32import java.util.Enumeration;33import java.util.List;3435public class Close {3637public static void main(String[] argv) {38byte[] buf = new byte[42];3940List<CloseableBAOS> list = List.of(new CloseableBAOS(buf),41new CloseableBAOS(buf), new CloseableBAOS(buf),42new CloseableBAOS(buf));4344Enumeration<CloseableBAOS> enumeration = Collections.enumeration(list);4546SequenceInputStream sequence = new SequenceInputStream(enumeration);47try {48sequence.close();49throw new RuntimeException("Expected IOException not thrown");50} catch (IOException e) {51for (CloseableBAOS c : list) {52if (!c.isClosed()) {53throw new RuntimeException("Component stream not closed");54}55}56Throwable[] suppressed = e.getSuppressed();57if (suppressed == null) {58throw new RuntimeException("No suppressed exceptions");59} else if (suppressed.length != list.size() - 1) {60throw new RuntimeException("Expected " + (list.size() - 1) +61" suppressed exceptions but got " + suppressed.length);62}63for (Throwable t : suppressed) {64if (!(t instanceof IOException)) {65throw new RuntimeException("Expected IOException but got " +66t.getClass().getName());67}68}69}70}7172static class CloseableBAOS extends ByteArrayInputStream{73private boolean closed;7475CloseableBAOS(byte[] buf) {76super(buf);77}7879@Override80public void close() throws IOException {81closed = true;82throw new IOException();83}8485public boolean isClosed() {86return closed;87}88}89}909192