1 /*
2  * Copyright (c) 2020, 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.testng.annotations.Test;
26 
27 import java.util.*;
28 import java.util.stream.*;
29 
30 import org.openjdk.testlib.java.util.stream.DefaultMethodStreams;
31 import org.openjdk.testlib.java.util.stream.OpTestCase;
32 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
33 import org.openjdk.testlib.java.util.stream.TestData;
34 
35 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*;
36 import static org.testng.Assert.assertEquals;
37 import static org.testng.Assert.assertFalse;
38 import static org.testng.Assert.assertTrue;
39 import static org.testng.Assert.fail;
40 
41 
42 /**
43  * ToListOpTest
44  */
45 @Test
46 public class ToListOpTest extends OpTestCase {
47 
testToList()48     public void testToList() {
49         assertCountSum(countTo(0).stream().toList(), 0, 0);
50         assertCountSum(countTo(10).stream().toList(), 10, 55);
51     }
52 
checkUnmodifiable(List<Integer> list)53     private void checkUnmodifiable(List<Integer> list) {
54         try {
55             list.add(Integer.MIN_VALUE);
56             fail("List.add did not throw UnsupportedOperationException");
57         } catch (UnsupportedOperationException ignore) { }
58 
59         if (list.size() > 0) {
60             try {
61                 list.set(0, Integer.MAX_VALUE);
62                 fail("List.set did not throw UnsupportedOperationException");
63             } catch (UnsupportedOperationException ignore) { }
64         }
65     }
66 
67     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOps(String name, TestData.OfRef<Integer> data)68     public void testOps(String name, TestData.OfRef<Integer> data) {
69         List<Integer> objects = exerciseTerminalOps(data, s -> s.toList());
70         checkUnmodifiable(objects);
71         assertFalse(objects.contains(null));
72     }
73 
74     @Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOpsWithNull(String name, TestData.OfRef<Integer> data)75     public void testOpsWithNull(String name, TestData.OfRef<Integer> data) {
76         List<Integer> objects = exerciseTerminalOps(data, s -> s.toList());
77         checkUnmodifiable(objects);
78         assertTrue(objects.contains(null));
79     }
80 
81     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testDefaultOps(String name, TestData.OfRef<Integer> data)82     public void testDefaultOps(String name, TestData.OfRef<Integer> data) {
83         List<Integer> objects = exerciseTerminalOps(data, s -> DefaultMethodStreams.delegateTo(s).toList());
84         checkUnmodifiable(objects);
85         assertFalse(objects.contains(null));
86     }
87 
88     @Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testDefaultOpsWithNull(String name, TestData.OfRef<Integer> data)89     public void testDefaultOpsWithNull(String name, TestData.OfRef<Integer> data) {
90         List<Integer> objects = exerciseTerminalOps(data, s -> DefaultMethodStreams.delegateTo(s).toList());
91         checkUnmodifiable(objects);
92         assertTrue(objects.contains(null));
93     }
94 
95 }
96