< prev index next >

test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamCloseTest.java

Print this page


   1 /*
   2  * Copyright (c) 2014, 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 /*
  25  * @test
  26  * @summary close handlers and closing streams
  27  * @bug 8044047

  28  */
  29 
  30 package org.openjdk.tests.java.util.stream;
  31 
  32 import java.util.Arrays;
  33 import java.util.stream.OpTestCase;
  34 import java.util.stream.Stream;
  35 
  36 import org.testng.annotations.Test;
  37 
  38 import static java.util.stream.LambdaTestHelpers.countTo;
  39 import static java.util.stream.ThowableHelper.checkNPE;

  40 
  41 @Test(groups = { "serialization-hostile" })
  42 public class StreamCloseTest extends OpTestCase {
  43     public void testNullCloseHandler() {
  44         checkNPE(() -> Stream.of(1).onClose(null));
  45     }
  46 
  47     public void testEmptyCloseHandler() {
  48         try (Stream<Integer> ints = countTo(100).stream()) {
  49             ints.forEach(i -> {});
  50         }
  51     }
  52 
  53     public void testOneCloseHandler() {
  54         final boolean[] holder = new boolean[1];
  55         Runnable closer = () -> { holder[0] = true; };
  56 
  57         try (Stream<Integer> ints = countTo(100).stream()) {
  58             ints.onClose(closer);
  59             ints.forEach(i -> {});


 152         assertTrue(caught);
 153 
 154         caught = false;
 155         Arrays.fill(holder, false);
 156         try (Stream<Integer> ints = countTo(100).stream().filter(e -> true).onClose(close1).onClose(close2).filter(e -> true).onClose(close3)) {
 157             ints.forEach(i -> {});
 158         }
 159         catch (RuntimeException e) {
 160             assertCascaded(e, 3);
 161             assertTrue(holder[0] && holder[1] && holder[2]);
 162             caught = true;
 163         }
 164         assertTrue(caught);
 165     }
 166 
 167     private void assertCascaded(RuntimeException e, int n) {
 168         assertTrue(e.getMessage().equals("1"));
 169         assertTrue(e.getSuppressed().length == n - 1);
 170         for (int i=0; i<n-1; i++)
 171         assertTrue(e.getSuppressed()[i].getMessage().equals(String.valueOf(i + 2)));

















 172     }
 173 }
   1 /*
   2  * Copyright (c) 2014, 2016, 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 /*
  25  * @test
  26  * @summary close handlers and closing streams
  27  * @bug 8044047
  28  * @bug 8147505
  29  */
  30 
  31 package org.openjdk.tests.java.util.stream;
  32 
  33 import java.util.Arrays;
  34 import java.util.stream.OpTestCase;
  35 import java.util.stream.Stream;
  36 
  37 import org.testng.annotations.Test;
  38 
  39 import static java.util.stream.LambdaTestHelpers.countTo;
  40 import static java.util.stream.ThowableHelper.checkNPE;
  41 import static java.util.stream.ThowableHelper.checkISE;
  42 
  43 @Test(groups = { "serialization-hostile" })
  44 public class StreamCloseTest extends OpTestCase {
  45     public void testNullCloseHandler() {
  46         checkNPE(() -> Stream.of(1).onClose(null));
  47     }
  48 
  49     public void testEmptyCloseHandler() {
  50         try (Stream<Integer> ints = countTo(100).stream()) {
  51             ints.forEach(i -> {});
  52         }
  53     }
  54 
  55     public void testOneCloseHandler() {
  56         final boolean[] holder = new boolean[1];
  57         Runnable closer = () -> { holder[0] = true; };
  58 
  59         try (Stream<Integer> ints = countTo(100).stream()) {
  60             ints.onClose(closer);
  61             ints.forEach(i -> {});


 154         assertTrue(caught);
 155 
 156         caught = false;
 157         Arrays.fill(holder, false);
 158         try (Stream<Integer> ints = countTo(100).stream().filter(e -> true).onClose(close1).onClose(close2).filter(e -> true).onClose(close3)) {
 159             ints.forEach(i -> {});
 160         }
 161         catch (RuntimeException e) {
 162             assertCascaded(e, 3);
 163             assertTrue(holder[0] && holder[1] && holder[2]);
 164             caught = true;
 165         }
 166         assertTrue(caught);
 167     }
 168 
 169     private void assertCascaded(RuntimeException e, int n) {
 170         assertTrue(e.getMessage().equals("1"));
 171         assertTrue(e.getSuppressed().length == n - 1);
 172         for (int i=0; i<n-1; i++)
 173         assertTrue(e.getSuppressed()[i].getMessage().equals(String.valueOf(i + 2)));
 174     }
 175 
 176     public void testConsumed() {
 177         try(Stream<Integer> s = countTo(100).stream()) {
 178             s.forEach(i -> {});
 179             // Adding onClose handler when stream is consumed is illegal
 180             // handler must not be registered
 181             checkISE(() -> s.onClose(() -> fail("1")));
 182         }
 183 
 184         // close() must be idempotent:
 185         // second close() invoked at the end of try-with-resources must have no effect
 186         try(Stream<Integer> s = countTo(100).stream()) {
 187             s.close();
 188             // Adding onClose handler when stream is closed is also illegal
 189             checkISE(() -> s.onClose(() -> fail("3")));
 190         }
 191     }
 192 }
< prev index next >