From 66348935c281aea6466e502f4faf25c3742be58b Mon Sep 17 00:00:00 2001 From: psandoz Date: Wed, 16 Jul 2014 14:35:48 +0200 Subject: [PATCH] 8044047: Missing null pointer checks for streams Reviewed-by: dfuchs Contributed-by: paul.sandoz@oracle.com, ivan.gerasimov@oracle.com --- .../java/util/stream/AbstractPipeline.java | 3 +- .../classes/java/util/stream/BaseStream.java | 2 +- .../java/util/stream/DoublePipeline.java | 2 + .../java/util/stream/DoubleStream.java | 2 +- .../classes/java/util/stream/IntPipeline.java | 2 + .../classes/java/util/stream/IntStream.java | 2 +- .../java/util/stream/LongPipeline.java | 4 +- .../classes/java/util/stream/LongStream.java | 2 +- .../java/util/stream/PipelineHelper.java | 2 +- .../classes/java/util/stream/SliceOps.java | 2 +- .../classes/java/util/stream/Stream.java | 2 +- .../java/util/stream/StreamOpFlag.java | 2 +- .../java/util/stream/ThowableHelper.java | 49 ++++++ .../CollectAndSummaryStatisticsTest.java | 153 ++++++++++++++++++ ...{ExplodeOpTest.java => FlatMapOpTest.java} | 24 ++- .../java/util/stream/StreamBuilderTest.java | 20 +-- .../java/util/stream/StreamCloseTest.java | 19 ++- .../util/stream/SummaryStatisticsTest.java | 88 ---------- 18 files changed, 251 insertions(+), 129 deletions(-) create mode 100644 test/java/util/stream/bootlib/java/util/stream/ThowableHelper.java create mode 100644 test/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectAndSummaryStatisticsTest.java rename test/java/util/stream/test/org/openjdk/tests/java/util/stream/{ExplodeOpTest.java => FlatMapOpTest.java} (90%) delete mode 100644 test/java/util/stream/test/org/openjdk/tests/java/util/stream/SummaryStatisticsTest.java diff --git a/src/share/classes/java/util/stream/AbstractPipeline.java b/src/share/classes/java/util/stream/AbstractPipeline.java index d3ccdacbf..61ed7c89a 100644 --- a/src/share/classes/java/util/stream/AbstractPipeline.java +++ b/src/share/classes/java/util/stream/AbstractPipeline.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 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 @@ -327,6 +327,7 @@ abstract class AbstractPipeline> @Override @SuppressWarnings("unchecked") public S onClose(Runnable closeHandler) { + Objects.requireNonNull(closeHandler); Runnable existingHandler = sourceStage.sourceCloseAction; sourceStage.sourceCloseAction = (existingHandler == null) diff --git a/src/share/classes/java/util/stream/BaseStream.java b/src/share/classes/java/util/stream/BaseStream.java index 35d46e087..61e74867a 100644 --- a/src/share/classes/java/util/stream/BaseStream.java +++ b/src/share/classes/java/util/stream/BaseStream.java @@ -53,7 +53,7 @@ import java.util.function.Predicate; * parallelism, which governs the behavior of all stream types. * * @param the type of the stream elements - * @param the type of of the stream implementing {@code BaseStream} + * @param the type of the stream implementing {@code BaseStream} * @since 1.8 * @see Stream * @see IntStream diff --git a/src/share/classes/java/util/stream/DoublePipeline.java b/src/share/classes/java/util/stream/DoublePipeline.java index 3b6335b77..3e9cddbd3 100644 --- a/src/share/classes/java/util/stream/DoublePipeline.java +++ b/src/share/classes/java/util/stream/DoublePipeline.java @@ -254,6 +254,7 @@ abstract class DoublePipeline @Override public final DoubleStream flatMap(DoubleFunction mapper) { + Objects.requireNonNull(mapper); return new StatelessOp(this, StreamShape.DOUBLE_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { @Override @@ -469,6 +470,7 @@ abstract class DoublePipeline public final R collect(Supplier supplier, ObjDoubleConsumer accumulator, BiConsumer combiner) { + Objects.requireNonNull(combiner); BinaryOperator operator = (left, right) -> { combiner.accept(left, right); return left; diff --git a/src/share/classes/java/util/stream/DoubleStream.java b/src/share/classes/java/util/stream/DoubleStream.java index 4d5d23d49..cfc12ffde 100644 --- a/src/share/classes/java/util/stream/DoubleStream.java +++ b/src/share/classes/java/util/stream/DoubleStream.java @@ -768,7 +768,7 @@ public interface DoubleStream extends BaseStream { * to the element at position {@code n - 1}. * * @param seed the initial element - * @param f a function to be applied to to the previous element to produce + * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code DoubleStream} */ diff --git a/src/share/classes/java/util/stream/IntPipeline.java b/src/share/classes/java/util/stream/IntPipeline.java index 5c3a8f125..313045f96 100644 --- a/src/share/classes/java/util/stream/IntPipeline.java +++ b/src/share/classes/java/util/stream/IntPipeline.java @@ -290,6 +290,7 @@ abstract class IntPipeline @Override public final IntStream flatMap(IntFunction mapper) { + Objects.requireNonNull(mapper); return new StatelessOp(this, StreamShape.INT_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { @Override @@ -465,6 +466,7 @@ abstract class IntPipeline public final R collect(Supplier supplier, ObjIntConsumer accumulator, BiConsumer combiner) { + Objects.requireNonNull(combiner); BinaryOperator operator = (left, right) -> { combiner.accept(left, right); return left; diff --git a/src/share/classes/java/util/stream/IntStream.java b/src/share/classes/java/util/stream/IntStream.java index 94c2924fc..0a67d5a19 100644 --- a/src/share/classes/java/util/stream/IntStream.java +++ b/src/share/classes/java/util/stream/IntStream.java @@ -734,7 +734,7 @@ public interface IntStream extends BaseStream { * element at position {@code n - 1}. * * @param seed the initial element - * @param f a function to be applied to to the previous element to produce + * @param f a function to be applied to the previous element to produce * a new element * @return A new sequential {@code IntStream} */ diff --git a/src/share/classes/java/util/stream/LongPipeline.java b/src/share/classes/java/util/stream/LongPipeline.java index 88e919e71..fab01a211 100644 --- a/src/share/classes/java/util/stream/LongPipeline.java +++ b/src/share/classes/java/util/stream/LongPipeline.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 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 @@ -271,6 +271,7 @@ abstract class LongPipeline @Override public final LongStream flatMap(LongFunction mapper) { + Objects.requireNonNull(mapper); return new StatelessOp(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { @Override @@ -447,6 +448,7 @@ abstract class LongPipeline public final R collect(Supplier supplier, ObjLongConsumer accumulator, BiConsumer combiner) { + Objects.requireNonNull(combiner); BinaryOperator operator = (left, right) -> { combiner.accept(left, right); return left; diff --git a/src/share/classes/java/util/stream/LongStream.java b/src/share/classes/java/util/stream/LongStream.java index a2d429e5a..78901b2b1 100644 --- a/src/share/classes/java/util/stream/LongStream.java +++ b/src/share/classes/java/util/stream/LongStream.java @@ -727,7 +727,7 @@ public interface LongStream extends BaseStream { * element at position {@code n - 1}. * * @param seed the initial element - * @param f a function to be applied to to the previous element to produce + * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code LongStream} */ diff --git a/src/share/classes/java/util/stream/PipelineHelper.java b/src/share/classes/java/util/stream/PipelineHelper.java index f510131d6..090469def 100644 --- a/src/share/classes/java/util/stream/PipelineHelper.java +++ b/src/share/classes/java/util/stream/PipelineHelper.java @@ -74,7 +74,7 @@ abstract class PipelineHelper { /** * Returns the exact output size of the portion of the output resulting from * applying the pipeline stages described by this {@code PipelineHelper} to - * the the portion of the input described by the provided + * the portion of the input described by the provided * {@code Spliterator}, if known. If not known or known infinite, will * return {@code -1}. * diff --git a/src/share/classes/java/util/stream/SliceOps.java b/src/share/classes/java/util/stream/SliceOps.java index 34d55309d..bfe053fca 100644 --- a/src/share/classes/java/util/stream/SliceOps.java +++ b/src/share/classes/java/util/stream/SliceOps.java @@ -693,7 +693,7 @@ final class SliceOps { * size. * * @param target the target size - * @return return the number of completed elements + * @return the number of completed elements */ private long completedSize(long target) { if (completed) diff --git a/src/share/classes/java/util/stream/Stream.java b/src/share/classes/java/util/stream/Stream.java index c35fc0568..bd9157962 100644 --- a/src/share/classes/java/util/stream/Stream.java +++ b/src/share/classes/java/util/stream/Stream.java @@ -1013,7 +1013,7 @@ public interface Stream extends BaseStream> { * * @param the type of stream elements * @param seed the initial element - * @param f a function to be applied to to the previous element to produce + * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code Stream} */ diff --git a/src/share/classes/java/util/stream/StreamOpFlag.java b/src/share/classes/java/util/stream/StreamOpFlag.java index 8fecfed4d..e4ad988ea 100644 --- a/src/share/classes/java/util/stream/StreamOpFlag.java +++ b/src/share/classes/java/util/stream/StreamOpFlag.java @@ -115,7 +115,7 @@ import java.util.Spliterator; * characteristics that stream has; when describing a stream operation, one need * describe whether the operation preserves, injects, or clears that * characteristic. Accordingly, two bits are used for each flag, so as to allow - * representing not only the presence of of a characteristic, but how an + * representing not only the presence of a characteristic, but how an * operation modifies that characteristic. There are two common forms in which * flag bits are combined into an {@code int} bit set. Stream flags * are a unioned bit set constructed by ORing the enum characteristic values of diff --git a/test/java/util/stream/bootlib/java/util/stream/ThowableHelper.java b/test/java/util/stream/bootlib/java/util/stream/ThowableHelper.java new file mode 100644 index 000000000..fc7807f6d --- /dev/null +++ b/test/java/util/stream/bootlib/java/util/stream/ThowableHelper.java @@ -0,0 +1,49 @@ +/* + * 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. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util.stream; + +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +public final class ThowableHelper { + + public static void checkException(Class ce, Runnable r) { + Exception caught = null; + try { + r.run(); + } catch (Exception e) { + caught = e; + } + + assertNotNull(caught); + assertTrue(ce.isInstance(caught)); + } + + public static void checkNPE(Runnable r) { + checkException(NullPointerException.class, r); + } + + public static void checkISE(Runnable r) { + checkException(IllegalStateException.class, r); + } +} diff --git a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectAndSummaryStatisticsTest.java b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectAndSummaryStatisticsTest.java new file mode 100644 index 000000000..a869cdf41 --- /dev/null +++ b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/CollectAndSummaryStatisticsTest.java @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2012, 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. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary primtive stream collection with summary statistics + * @bug 8044047 + */ + +package org.openjdk.tests.java.util.stream; + +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.DoubleSummaryStatistics; +import java.util.IntSummaryStatistics; +import java.util.List; +import java.util.LongSummaryStatistics; +import java.util.stream.Collectors; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.OpTestCase; + +import static java.util.stream.LambdaTestHelpers.countTo; +import static java.util.stream.ThowableHelper.checkNPE; + +@Test +public class CollectAndSummaryStatisticsTest extends OpTestCase { + + public void testIntCollectNull() { + checkNPE(() -> IntStream.of(1).collect(null, + IntSummaryStatistics::accept, + IntSummaryStatistics::combine)); + checkNPE(() -> IntStream.of(1).collect(IntSummaryStatistics::new, + null, + IntSummaryStatistics::combine)); + checkNPE(() -> IntStream.of(1).collect(IntSummaryStatistics::new, + IntSummaryStatistics::accept, + null)); + } + + public void testLongCollectNull() { + checkNPE(() -> LongStream.of(1).collect(null, + LongSummaryStatistics::accept, + LongSummaryStatistics::combine)); + checkNPE(() -> LongStream.of(1).collect(LongSummaryStatistics::new, + null, + LongSummaryStatistics::combine)); + checkNPE(() -> LongStream.of(1).collect(LongSummaryStatistics::new, + LongSummaryStatistics::accept, + null)); + } + + public void testDoubleCollectNull() { + checkNPE(() -> DoubleStream.of(1).collect(null, + DoubleSummaryStatistics::accept, + DoubleSummaryStatistics::combine)); + checkNPE(() -> DoubleStream.of(1).collect(DoubleSummaryStatistics::new, + null, + DoubleSummaryStatistics::combine)); + checkNPE(() -> DoubleStream.of(1).collect(DoubleSummaryStatistics::new, + DoubleSummaryStatistics::accept, + null)); + } + + public void testIntStatistics() { + List instances = new ArrayList<>(); + instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i))); + instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics()); + instances.add(countTo(1000).stream().mapToInt(i -> i).collect(IntSummaryStatistics::new, + IntSummaryStatistics::accept, + IntSummaryStatistics::combine)); + instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i))); + instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics()); + instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(IntSummaryStatistics::new, + IntSummaryStatistics::accept, + IntSummaryStatistics::combine)); + + for (IntSummaryStatistics stats : instances) { + assertEquals(stats.getCount(), 1000); + assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum()); + assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount()); + assertEquals(stats.getMax(), 1000); + assertEquals(stats.getMin(), 1); + } + } + + + public void testLongStatistics() { + List instances = new ArrayList<>(); + instances.add(countTo(1000).stream().collect(Collectors.summarizingLong(i -> i))); + instances.add(countTo(1000).stream().mapToLong(i -> i).summaryStatistics()); + instances.add(countTo(1000).stream().mapToLong(i -> i).collect(LongSummaryStatistics::new, + LongSummaryStatistics::accept, + LongSummaryStatistics::combine)); + instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingLong(i -> i))); + instances.add(countTo(1000).parallelStream().mapToLong(i -> i).summaryStatistics()); + instances.add(countTo(1000).parallelStream().mapToLong(i -> i).collect(LongSummaryStatistics::new, + LongSummaryStatistics::accept, + LongSummaryStatistics::combine)); + + for (LongSummaryStatistics stats : instances) { + assertEquals(stats.getCount(), 1000); + assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum()); + assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount()); + assertEquals(stats.getMax(), 1000L); + assertEquals(stats.getMin(), 1L); + } + } + + public void testDoubleStatistics() { + List instances = new ArrayList<>(); + instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i))); + instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics()); + instances.add(countTo(1000).stream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new, + DoubleSummaryStatistics::accept, + DoubleSummaryStatistics::combine)); + instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i))); + instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics()); + instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new, + DoubleSummaryStatistics::accept, + DoubleSummaryStatistics::combine)); + + for (DoubleSummaryStatistics stats : instances) { + assertEquals(stats.getCount(), 1000); + assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum()); + assertEquals(stats.getAverage(), stats.getSum() / stats.getCount()); + assertEquals(stats.getMax(), 1000.0); + assertEquals(stats.getMin(), 1.0); + } + } +} diff --git a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/ExplodeOpTest.java b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/FlatMapOpTest.java similarity index 90% rename from test/java/util/stream/test/org/openjdk/tests/java/util/stream/ExplodeOpTest.java rename to test/java/util/stream/test/org/openjdk/tests/java/util/stream/FlatMapOpTest.java index cbd9534aa..787b6b8fe 100644 --- a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/ExplodeOpTest.java +++ b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/FlatMapOpTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 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 @@ -20,6 +20,13 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + +/* + * @test + * @summary flat-map operations + * @bug 8044047 + */ + package org.openjdk.tests.java.util.stream; import org.testng.annotations.Test; @@ -31,14 +38,17 @@ import java.util.function.Function; import java.util.stream.*; import static java.util.stream.LambdaTestHelpers.*; +import static java.util.stream.ThowableHelper.checkNPE; -/** - * ExplodeOpTest - * - * @author Brian Goetz - */ @Test -public class ExplodeOpTest extends OpTestCase { +public class FlatMapOpTest extends OpTestCase { + + public void testNullMapper() { + checkNPE(() -> Stream.of(1).flatMap(null)); + checkNPE(() -> IntStream.of(1).flatMap(null)); + checkNPE(() -> LongStream.of(1).flatMap(null)); + checkNPE(() -> DoubleStream.of(1).flatMap(null)); + } static final Function> integerRangeMapper = e -> IntStream.range(0, e).boxed(); diff --git a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamBuilderTest.java b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamBuilderTest.java index d8d46fd63..352f3cc70 100644 --- a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamBuilderTest.java +++ b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamBuilderTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 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 @@ -38,6 +38,7 @@ import java.util.stream.Stream; import java.util.stream.TestData; import static java.util.stream.Collectors.toList; +import static java.util.stream.ThowableHelper.checkISE; @Test public class StreamBuilderTest extends OpTestCase { @@ -52,23 +53,6 @@ public class StreamBuilderTest extends OpTestCase { return sizes.stream().map(i -> new Object[] { i }).toArray(Object[][]::new); } - private void checkException(Class ce, Runnable r) { - Exception caught = null; - try { - r.run(); - } catch (Exception e) { - caught = e; - } - - assertNotNull(caught); - assertTrue(ce.isInstance(caught)); - } - - private void checkISE(Runnable r) { - checkException(IllegalStateException.class, r); - } - - // @Test public void testSingleton() { diff --git a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamCloseTest.java b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamCloseTest.java index 51ffd4b90..6fd24a981 100644 --- a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamCloseTest.java +++ b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamCloseTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -20,6 +20,13 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + +/* + * @test + * @summary close handlers and closing streams + * @bug 8044047 + */ + package org.openjdk.tests.java.util.stream; import java.util.Arrays; @@ -29,14 +36,14 @@ import java.util.stream.Stream; import org.testng.annotations.Test; import static java.util.stream.LambdaTestHelpers.countTo; +import static java.util.stream.ThowableHelper.checkNPE; -/** - * StreamCloseTest - * - * @author Brian Goetz - */ @Test(groups = { "serialization-hostile" }) public class StreamCloseTest extends OpTestCase { + public void testNullCloseHandler() { + checkNPE(() -> Stream.of(1).onClose(null)); + } + public void testEmptyCloseHandler() { try (Stream ints = countTo(100).stream()) { ints.forEach(i -> {}); diff --git a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/SummaryStatisticsTest.java b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/SummaryStatisticsTest.java deleted file mode 100644 index 3850b8388..000000000 --- a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/SummaryStatisticsTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2012, 2013, 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. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package org.openjdk.tests.java.util.stream; - -import java.util.ArrayList; -import java.util.DoubleSummaryStatistics; -import java.util.IntSummaryStatistics; -import java.util.List; -import java.util.LongSummaryStatistics; -import java.util.stream.Collectors; -import java.util.stream.OpTestCase; - -import org.testng.annotations.Test; - -import static java.util.stream.LambdaTestHelpers.countTo; - -/** - * TestSummaryStatistics - * - * @author Brian Goetz - */ -@Test -public class SummaryStatisticsTest extends OpTestCase { - public void testIntStatistics() { - List instances = new ArrayList<>(); - instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i))); - instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics()); - instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i))); - instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics()); - - for (IntSummaryStatistics stats : instances) { - assertEquals(stats.getCount(), 1000); - assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum()); - assertEquals(stats.getMax(), 1000); - assertEquals(stats.getMin(), 1); - } - } - - public void testLongStatistics() { - List instances = new ArrayList<>(); - instances.add(countTo(1000).stream().collect(Collectors.summarizingLong(i -> i))); - instances.add(countTo(1000).stream().mapToLong(i -> i).summaryStatistics()); - instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingLong(i -> i))); - instances.add(countTo(1000).parallelStream().mapToLong(i -> i).summaryStatistics()); - - for (LongSummaryStatistics stats : instances) { - assertEquals(stats.getCount(), 1000); - assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum()); - assertEquals(stats.getMax(), 1000L); - assertEquals(stats.getMin(), 1L); - } - } - - public void testDoubleStatistics() { - List instances = new ArrayList<>(); - instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i))); - instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics()); - instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i))); - instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics()); - - for (DoubleSummaryStatistics stats : instances) { - assertEquals(stats.getCount(), 1000); - assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum()); - assertEquals(stats.getMax(), 1000.0); - assertEquals(stats.getMin(), 1.0); - } - } -} -- GitLab