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