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.InputStream;
  31 import java.io.IOException;
  32 import java.io.SequenceInputStream;
  33 import java.util.ArrayList;
  34 import java.util.Collections;
  35 import java.util.Enumeration;
  36 import java.util.List;
  37 
  38 public class Close {
  39 
  40     public static void main(String[] argv) throws IOException {
  41         byte[] buf = new byte[42];
  42 
  43         List<CloseableBAOS> list = List.of(new CloseableBAOS(buf),
  44             new CloseableBAOS(buf), new CloseableBAOS(buf),
  45             new CloseableBAOS(buf));
  46 
  47         Enumeration<CloseableBAOS> enumeration = Collections.enumeration(list);
  48 
  49         SequenceInputStream sequence = new SequenceInputStream(enumeration);
  50         try {
  51             sequence.close();
  52             throw new RuntimeException("Expected IOException not thrown");
  53         } catch (IOException e) {
  54             for (CloseableBAOS c : list) {
  55                 if (!c.isClosed()) {
  56                     throw new RuntimeException("Component stream not closed");
  57                 }
  58             }
  59             Throwable[] suppressed = e.getSuppressed();
  60             if (suppressed == null) {
  61                 throw new RuntimeException("No suppressed exceptions");
  62             } else if (suppressed.length != list.size() - 1) {
  63                 throw new RuntimeException("Expected " + (list.size() - 1) +
  64                     " suppressed exceptions but got " + suppressed.length);
  65             }
  66             for (Throwable t : suppressed) {
  67                 if (!(t instanceof IOException)) {
  68                     throw new RuntimeException("Expected IOException but got " +
  69                         t.getClass().getName());
  70                 }
  71             }
  72         }
  73     }
  74 
  75     static class CloseableBAOS extends ByteArrayInputStream{
  76         private boolean closed;
  77         private int numCloses = 0;
  78 
  79         CloseableBAOS(byte[] buf) {
  80             super(buf);
  81         }
  82 
  83         public void close() throws IOException {
  84             closed = true;
  85             throw new IOException();
  86         }
  87 
  88         public boolean isClosed() {
  89             return closed;
  90         }
  91     }
  92 }