1 /*
2  * Copyright (c) 1999, 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  * @bug 4189641
27  * @summary Wrapping a null collection/array should blow up sooner
28  *          rather than later
29  */
30 
31 package test.java.util.Collections;
32 
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.SortedMap;
40 import java.util.SortedSet;
41 import java.util.TreeMap;
42 import java.util.TreeSet;
43 
44 public class WrappedNull {
main(String[] args)45     public static void main(String[] args) throws Exception {
46         boolean testSucceeded = false;
47         try {
48             List l = Arrays.asList(null);
49         }
50         catch (NullPointerException e) {
51             testSucceeded = true;
52         }
53         if (!testSucceeded)
54             throw new Exception("Arrays.asList");
55 
56         testSucceeded = false;
57         try {
58             Collection c = Collections.unmodifiableCollection(null);
59         }
60         catch (NullPointerException e) {
61             testSucceeded = true;
62         }
63         if (!testSucceeded)
64             throw new Exception("unmodifiableCollection");
65 
66         testSucceeded = false;
67         try {
68             Set c = Collections.unmodifiableSet(null);
69         }
70         catch (NullPointerException e) {
71             testSucceeded = true;
72         }
73         if (!testSucceeded)
74             throw new Exception("unmodifiableSet");
75 
76         testSucceeded = false;
77         try {
78             List c = Collections.unmodifiableList(null);
79         }
80         catch (NullPointerException e) {
81             testSucceeded = true;
82         }
83         if (!testSucceeded)
84             throw new Exception("unmodifiableList");
85 
86         testSucceeded = false;
87         try {
88             Map c = Collections.unmodifiableMap(null);
89         }
90         catch (NullPointerException e) {
91             testSucceeded = true;
92         }
93         if (!testSucceeded)
94             throw new Exception("unmodifiableMap");
95 
96         testSucceeded = false;
97         try {
98             SortedSet c = Collections.unmodifiableSortedSet(null);
99         }
100         catch (NullPointerException e) {
101             testSucceeded = true;
102         }
103         if (!testSucceeded)
104             throw new Exception("unmodifiableSortedSet");
105 
106         testSucceeded = false;
107         try {
108             SortedMap c = Collections.unmodifiableSortedMap(null);
109         }
110         catch (NullPointerException e) {
111             testSucceeded = true;
112         }
113         if (!testSucceeded)
114             throw new Exception("unmodifiableSortedMap");
115 
116         testSucceeded = false;
117         try {
118             Collection c = Collections.synchronizedCollection(null);
119         }
120         catch (NullPointerException e) {
121             testSucceeded = true;
122         }
123         if (!testSucceeded)
124             throw new Exception("synchronizedCollection");
125 
126         testSucceeded = false;
127         try {
128             Set c = Collections.synchronizedSet(null);
129         }
130         catch (NullPointerException e) {
131             testSucceeded = true;
132         }
133         if (!testSucceeded)
134             throw new Exception("synchronizedSet");
135 
136         testSucceeded = false;
137         try {
138             List c = Collections.synchronizedList(null);
139         }
140         catch (NullPointerException e) {
141             testSucceeded = true;
142         }
143         if (!testSucceeded)
144             throw new Exception("synchronizedList");
145 
146         testSucceeded = false;
147         try {
148             Map c = Collections.synchronizedMap(null);
149         }
150         catch (NullPointerException e) {
151             testSucceeded = true;
152         }
153         if (!testSucceeded)
154             throw new Exception("synchronizedMap");
155 
156         testSucceeded = false;
157         try {
158             SortedSet c = Collections.synchronizedSortedSet(null);
159         }
160         catch (NullPointerException e) {
161             testSucceeded = true;
162         }
163         if (!testSucceeded)
164             throw new Exception("synchronizedSortedSet");
165 
166         testSucceeded = false;
167         try {
168             SortedMap c = Collections.synchronizedSortedMap(null);
169         }
170         catch (NullPointerException e) {
171             testSucceeded = true;
172         }
173         if (!testSucceeded)
174             throw new Exception("synchronizedSortedMap");
175 
176         // Make sure that non-null arguments don't throw exc.
177         List l = Arrays.asList(new Object[0]);
178         Collection c = Collections.unmodifiableCollection(
179                 Collections.EMPTY_SET);
180         Set s = Collections.unmodifiableSet(Collections.EMPTY_SET);
181         l = Collections.unmodifiableList(Collections.EMPTY_LIST);
182         Map m = Collections.unmodifiableMap(Collections.EMPTY_MAP);
183         SortedSet ss = Collections.unmodifiableSortedSet(new TreeSet());
184         SortedMap sm = Collections.unmodifiableSortedMap(new TreeMap());
185 
186         c = Collections.synchronizedCollection(Collections.EMPTY_SET);
187         s = Collections.synchronizedSet(Collections.EMPTY_SET);
188         l = Collections.synchronizedList(Collections.EMPTY_LIST);
189         m = Collections.synchronizedMap(Collections.EMPTY_MAP);
190         ss = Collections.synchronizedSortedSet(new TreeSet());
191         sm = Collections.synchronizedSortedMap(new TreeMap());
192     }
193 }
194