1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 8078891 26 * @summary Ensure close() closes all component streams 27 */ 28 29 import java.io.ByteArrayInputStream; 30 import java.io.IOException; 31 import java.io.SequenceInputStream; 32 import java.util.Collections; 33 import java.util.Enumeration; 34 import java.util.List; 35 36 public class Close { 37 38 public static void main(String[] argv) { 39 byte[] buf = new byte[42]; 40 41 List<CloseableBAOS> list = List.of(new CloseableBAOS(buf), 42 new CloseableBAOS(buf), new CloseableBAOS(buf), 43 new CloseableBAOS(buf)); 44 45 Enumeration<CloseableBAOS> enumeration = Collections.enumeration(list); 46 47 SequenceInputStream sequence = new SequenceInputStream(enumeration); 48 try { 49 sequence.close(); 50 throw new RuntimeException("Expected IOException not thrown"); 51 } catch (IOException e) { 52 for (CloseableBAOS c : list) { 53 if (!c.isClosed()) { 54 throw new RuntimeException("Component stream not closed"); 55 } 56 } 57 Throwable[] suppressed = e.getSuppressed(); 58 if (suppressed == null) { 59 throw new RuntimeException("No suppressed exceptions"); 60 } else if (suppressed.length != list.size() - 1) { 61 throw new RuntimeException("Expected " + (list.size() - 1) + 62 " suppressed exceptions but got " + suppressed.length); 63 } 64 for (Throwable t : suppressed) { 65 if (!(t instanceof IOException)) { 66 throw new RuntimeException("Expected IOException but got " + 67 t.getClass().getName()); 68 } 69 } 70 } 71 } 72 73 static class CloseableBAOS extends ByteArrayInputStream{ 74 private boolean closed; 75 76 CloseableBAOS(byte[] buf) { 77 super(buf); 78 } 79 80 @Override 81 public void close() throws IOException { 82 closed = true; 83 throw new IOException(); 84 } 85 86 public boolean isClosed() { 87 return closed; 88 } 89 } 90 }