< prev index next >
test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamCloseTest.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
--- 1,7 ----
/*
! * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*** 23,32 ****
--- 23,33 ----
/*
* @test
* @summary close handlers and closing streams
* @bug 8044047
+ * @bug 8147505
*/
package org.openjdk.tests.java.util.stream;
import java.util.Arrays;
*** 35,44 ****
--- 36,46 ----
import org.testng.annotations.Test;
import static java.util.stream.LambdaTestHelpers.countTo;
import static java.util.stream.ThowableHelper.checkNPE;
+ import static java.util.stream.ThowableHelper.checkISE;
@Test(groups = { "serialization-hostile" })
public class StreamCloseTest extends OpTestCase {
public void testNullCloseHandler() {
checkNPE(() -> Stream.of(1).onClose(null));
*** 168,173 ****
--- 170,192 ----
assertTrue(e.getMessage().equals("1"));
assertTrue(e.getSuppressed().length == n - 1);
for (int i=0; i<n-1; i++)
assertTrue(e.getSuppressed()[i].getMessage().equals(String.valueOf(i + 2)));
}
+
+ public void testConsumed() {
+ try(Stream<Integer> s = countTo(100).stream()) {
+ s.forEach(i -> {});
+ // Adding onClose handler when stream is consumed is illegal
+ // handler must not be registered
+ checkISE(() -> s.onClose(() -> fail("1")));
+ }
+
+ // close() must be idempotent:
+ // second close() invoked at the end of try-with-resources must have no effect
+ try(Stream<Integer> s = countTo(100).stream()) {
+ s.close();
+ // Adding onClose handler when stream is closed is also illegal
+ checkISE(() -> s.onClose(() -> fail("3")));
+ }
+ }
}
< prev index next >