1 /*
   2  * Copyright (c) 2013, 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 package org.openjdk.tests.java.util.stream;
  24 
  25 import java.util.Arrays;
  26 import java.util.stream.OpTestCase;
  27 import java.util.stream.Stream;
  28 
  29 import org.testng.annotations.Test;
  30 
  31 import static java.util.stream.LambdaTestHelpers.countTo;
  32 
  33 /**
  34  * StreamCloseTest
  35  *
  36  * @author Brian Goetz
  37  */
  38 @Test(groups = { "serialization-hostile" })
  39 public class StreamCloseTest extends OpTestCase {
  40     public void testEmptyCloseHandler() {
  41         try (Stream<Integer> ints = countTo(100).stream()) {
  42             ints.forEach(i -> {});
  43         }
  44     }
  45 
  46     public void testOneCloseHandler() {
  47         final boolean[] holder = new boolean[1];
  48         Runnable closer = () -> { holder[0] = true; };
  49 
  50         try (Stream<Integer> ints = countTo(100).stream()) {
  51             ints.onClose(closer);
  52             ints.forEach(i -> {});
  53         }
  54         assertTrue(holder[0]);
  55 
  56         Arrays.fill(holder, false);
  57         try (Stream<Integer> ints = countTo(100).stream().onClose(closer)) {
  58             ints.forEach(i -> {});
  59         }
  60         assertTrue(holder[0]);
  61 
  62         Arrays.fill(holder, false);
  63         try (Stream<Integer> ints = countTo(100).stream().filter(e -> true).onClose(closer)) {
  64             ints.forEach(i -> {});
  65         }
  66         assertTrue(holder[0]);
  67 
  68         Arrays.fill(holder, false);
  69         try (Stream<Integer> ints = countTo(100).stream().filter(e -> true).onClose(closer).filter(e -> true)) {
  70             ints.forEach(i -> {});
  71         }
  72         assertTrue(holder[0]);
  73     }
  74 
  75     public void testTwoCloseHandlers() {
  76         final boolean[] holder = new boolean[2];
  77         Runnable close1 = () -> { holder[0] = true; };
  78         Runnable close2 = () -> { holder[1] = true; };
  79 
  80         try (Stream<Integer> ints = countTo(100).stream()) {
  81             ints.onClose(close1).onClose(close2);
  82             ints.forEach(i -> {});
  83         }
  84         assertTrue(holder[0] && holder[1]);
  85 
  86         Arrays.fill(holder, false);
  87         try (Stream<Integer> ints = countTo(100).stream().onClose(close1).onClose(close2)) {
  88             ints.forEach(i -> {});
  89         }
  90         assertTrue(holder[0] && holder[1]);
  91 
  92         Arrays.fill(holder, false);
  93         try (Stream<Integer> ints = countTo(100).stream().filter(e -> true).onClose(close1).onClose(close2)) {
  94             ints.forEach(i -> {});
  95         }
  96         assertTrue(holder[0] && holder[1]);
  97 
  98         Arrays.fill(holder, false);
  99         try (Stream<Integer> ints = countTo(100).stream().filter(e -> true).onClose(close1).onClose(close2).filter(e -> true)) {
 100             ints.forEach(i -> {});
 101         }
 102         assertTrue(holder[0] && holder[1]);
 103     }
 104 
 105     public void testCascadedExceptions() {
 106         final boolean[] holder = new boolean[3];
 107         boolean caught = false;
 108         Runnable close1 = () -> { holder[0] = true; throw new RuntimeException("1"); };
 109         Runnable close2 = () -> { holder[1] = true; throw new RuntimeException("2"); };
 110         Runnable close3 = () -> { holder[2] = true; throw new RuntimeException("3"); };
 111 
 112         try (Stream<Integer> ints = countTo(100).stream()) {
 113             ints.onClose(close1).onClose(close2).onClose(close3);
 114             ints.forEach(i -> {});
 115         }
 116         catch (RuntimeException e) {
 117             assertCascaded(e, 3);
 118             assertTrue(holder[0] && holder[1] && holder[2]);
 119             caught = true;
 120         }
 121         assertTrue(caught);
 122 
 123         Arrays.fill(holder, false);
 124         caught = false;
 125         try (Stream<Integer> ints = countTo(100).stream().onClose(close1).onClose(close2).onClose(close3)) {
 126             ints.forEach(i -> {});
 127         }
 128         catch (RuntimeException e) {
 129             assertCascaded(e, 3);
 130             assertTrue(holder[0] && holder[1] && holder[2]);
 131             caught = true;
 132         }
 133         assertTrue(caught);
 134 
 135         caught = false;
 136         Arrays.fill(holder, false);
 137         try (Stream<Integer> ints = countTo(100).stream().filter(e -> true).onClose(close1).onClose(close2).onClose(close3)) {
 138             ints.forEach(i -> {});
 139         }
 140         catch (RuntimeException e) {
 141             assertCascaded(e, 3);
 142             assertTrue(holder[0] && holder[1] && holder[2]);
 143             caught = true;
 144         }
 145         assertTrue(caught);
 146 
 147         caught = false;
 148         Arrays.fill(holder, false);
 149         try (Stream<Integer> ints = countTo(100).stream().filter(e -> true).onClose(close1).onClose(close2).filter(e -> true).onClose(close3)) {
 150             ints.forEach(i -> {});
 151         }
 152         catch (RuntimeException e) {
 153             assertCascaded(e, 3);
 154             assertTrue(holder[0] && holder[1] && holder[2]);
 155             caught = true;
 156         }
 157         assertTrue(caught);
 158     }
 159 
 160     private void assertCascaded(RuntimeException e, int n) {
 161         assertTrue(e.getMessage().equals("1"));
 162         assertTrue(e.getSuppressed().length == n - 1);
 163         for (int i=0; i<n-1; i++)
 164         assertTrue(e.getSuppressed()[i].getMessage().equals(String.valueOf(i + 2)));
 165     }
 166 }