1 /* 2 * Copyright (c) 2012, 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 flat-map operations 27 * @bug 8044047 8076458 28 */ 29 30 package org.openjdk.tests.java.util.stream; 31 32 import org.testng.annotations.Test; 33 34 import java.util.Arrays; 35 import java.util.Collection; 36 import java.util.concurrent.atomic.AtomicInteger; 37 import java.util.function.Function; 38 import java.util.function.Supplier; 39 import java.util.stream.DoubleStream; 40 import java.util.stream.IntStream; 41 import java.util.stream.LongStream; 42 import java.util.stream.OpTestCase; 43 import java.util.stream.Stream; 44 import java.util.stream.StreamTestDataProvider; 45 import java.util.stream.TestData; 46 47 import static java.util.stream.LambdaTestHelpers.*; 48 import static java.util.stream.ThowableHelper.checkNPE; 49 50 @Test 51 public class FlatMapOpTest extends OpTestCase { 52 53 public void testNullMapper() { 54 checkNPE(() -> Stream.of(1).flatMap(null)); 55 checkNPE(() -> IntStream.of(1).flatMap(null)); 56 checkNPE(() -> LongStream.of(1).flatMap(null)); 57 checkNPE(() -> DoubleStream.of(1).flatMap(null)); 58 } 59 60 static final Function<Integer, Stream<Integer>> integerRangeMapper 61 = e -> IntStream.range(0, e).boxed(); 62 63 public void testFlatMap() { 64 String[] stringsArray = {"hello", "there", "", "yada"}; 65 Stream<String> strings = Arrays.asList(stringsArray).stream(); 66 assertConcat(strings.flatMap(flattenChars).iterator(), "hellothereyada"); 67 68 assertCountSum(countTo(10).stream().flatMap(mfId), 10, 55); 69 assertCountSum(countTo(10).stream().flatMap(mfNull), 0, 0); 70 assertCountSum(countTo(3).stream().flatMap(mfLt), 6, 4); 71 72 exerciseOps(TestData.Factory.ofArray("stringsArray", stringsArray), s -> s.flatMap(flattenChars)); 73 exerciseOps(TestData.Factory.ofArray("LONG_STRING", new String[] {LONG_STRING}), s -> s.flatMap(flattenChars)); 74 } 75 76 @Test 77 public void testClose() { 78 AtomicInteger before = new AtomicInteger(); 79 AtomicInteger onClose = new AtomicInteger(); 80 81 Supplier<Stream<Integer>> s = () -> { 82 before.set(0); onClose.set(0); 83 return Stream.of(1, 2).peek(e -> before.getAndIncrement()); 84 }; 85 86 s.get().flatMap(i -> Stream.of(i, i).onClose(onClose::getAndIncrement)).count(); 87 assertEquals(before.get(), onClose.get()); 88 89 s.get().flatMapToInt(i -> IntStream.of(i, i).onClose(onClose::getAndIncrement)).count(); 90 assertEquals(before.get(), onClose.get()); 91 92 s.get().flatMapToLong(i -> LongStream.of(i, i).onClose(onClose::getAndIncrement)).count(); 93 assertEquals(before.get(), onClose.get()); 94 95 s.get().flatMapToDouble(i -> DoubleStream.of(i, i).onClose(onClose::getAndIncrement)).count(); 96 assertEquals(before.get(), onClose.get()); 97 } 98 99 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 100 public void testOps(String name, TestData.OfRef<Integer> data) { 101 Collection<Integer> result = exerciseOps(data, s -> s.flatMap(mfId)); 102 assertEquals(data.size(), result.size()); 103 104 result = exerciseOps(data, s -> s.flatMap(mfNull)); 105 assertEquals(0, result.size()); 106 107 result = exerciseOps(data, s-> s.flatMap(e -> Stream.empty())); 108 assertEquals(0, result.size()); 109 110 exerciseOps(data, s -> s.flatMap(mfLt)); 111 exerciseOps(data, s -> s.flatMap(integerRangeMapper)); 112 exerciseOps(data, s -> s.flatMap((Integer e) -> IntStream.range(0, e).boxed().limit(10))); 113 } 114 }