1 /*
2  * Copyright (c) 2012, 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 org.openjdk.testlib.java.util.stream.DoubleStreamTestDataProvider;
26 import org.openjdk.testlib.java.util.stream.IntStreamTestDataProvider;
27 import org.openjdk.testlib.java.util.stream.LambdaTestHelpers;
28 import org.openjdk.testlib.java.util.stream.LongStreamTestDataProvider;
29 import org.openjdk.testlib.java.util.stream.OpTestCase;
30 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
31 import org.openjdk.testlib.java.util.stream.TestData;
32 
33 import java.util.function.Consumer;
34 import java.util.function.DoubleConsumer;
35 import java.util.function.IntConsumer;
36 import java.util.function.LongConsumer;
37 
38 import org.testng.annotations.Test;
39 
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43 
44 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*;
45 
46 /**
47  * TeeOpTest
48  */
49 @Test(groups = { "serialization-hostile" })
50 public class TeeOpTest extends OpTestCase {
51 
testTee()52     public void testTee() {
53         List<Integer> copy = new ArrayList<>();
54 
55         assertCountSum(countTo(0).stream().peek(copy::add), 0, 0);
56         assertCountSum(copy.iterator(), 0, 0);
57 
58         copy.clear();
59         assertCountSum(countTo(10).stream().peek(copy::add), 10, 55);
60         assertCountSum(copy.iterator(), 10, 55);
61 
62         copy.clear();
63         assertCountSum(countTo(10).stream().map(mDoubler).peek(copy::add), 10, 110);
64         assertCountSum(copy.iterator(), 10, 110);
65     }
66 
67     static class AbstractRecordingConsumer<T> {
68         List<T> list;
69 
before(TestData<T, ?> td)70         void before(TestData<T, ?> td) {
71             // Tee block can be called concurrently
72             list = Collections.synchronizedList(new ArrayList<>());
73         }
74 
after(TestData<T, ?> td)75         void after(TestData<T, ?> td) {
76             // No guarantees in parallel tests that calls to tee block will
77             // be in the encounter order, if defined, of the data
78             // @@@ Consider passing more meta-data about evaluation
79             assertContentsUnordered(list, td.into(new ArrayList<T>()));
80         }
81     }
82 
83     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOps(String name, final TestData.OfRef<Integer> data)84     public void testOps(String name, final TestData.OfRef<Integer> data) {
85         class RecordingConsumer extends AbstractRecordingConsumer<Integer> implements Consumer<Integer> {
86             public void accept(Integer t) {
87                 list.add(t);
88             }
89         }
90         final RecordingConsumer b = new RecordingConsumer();
91 
92         withData(data)
93                 .stream(s -> s.peek(b))
94                 .before(b::before)
95                 .after(b::after)
96                 .exercise();
97     }
98 
99     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testIntOps(String name, final TestData.OfInt data)100     public void testIntOps(String name, final TestData.OfInt data) {
101         class RecordingConsumer extends AbstractRecordingConsumer<Integer> implements IntConsumer {
102             public void accept(int t) {
103                 list.add(t);
104             }
105         }
106         final RecordingConsumer b = new RecordingConsumer();
107 
108         withData(data)
109                 .stream(s -> s.peek(b))
110                 .before(b::before)
111                 .after(b::after)
112                 .exercise();
113     }
114 
115     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
testLongOps(String name, final TestData.OfLong data)116     public void testLongOps(String name, final TestData.OfLong data) {
117         class RecordingConsumer extends AbstractRecordingConsumer<Long> implements LongConsumer {
118             public void accept(long t) {
119                 list.add(t);
120             }
121         }
122         final RecordingConsumer b = new RecordingConsumer();
123 
124         withData(data)
125                 .stream(s -> s.peek(b))
126                 .before(b::before)
127                 .after(b::after)
128                 .exercise();
129     }
130 
131     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
testDoubleOps(String name, final TestData.OfDouble data)132     public void testDoubleOps(String name, final TestData.OfDouble data) {
133         class RecordingConsumer extends AbstractRecordingConsumer<Double> implements DoubleConsumer {
134             public void accept(double t) {
135                 list.add(t);
136             }
137         }
138         final RecordingConsumer b = new RecordingConsumer();
139 
140         withData(data)
141                 .stream(s -> s.peek(b))
142                 .before(b::before)
143                 .after(b::after)
144                 .exercise();
145     }
146 }
147