1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  */
22 
23 /*
24  * This file is available under and governed by the GNU General Public
25  * License version 2 only, as published by the Free Software Foundation.
26  * However, the following notice accompanied the original version of this
27  * file:
28  *
29  * Written by Martin Buchholz with assistance from members of JCP JSR-166
30  * Expert Group and released to the public domain, as explained at
31  * http://creativecommons.org/publicdomain/zero/1.0/
32  */
33 
34 /*
35  * @test
36  * @bug 6950540
37  * @summary Attempt to add a null throws NullPointerException
38  */
39 
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Comparator;
43 import java.util.PriorityQueue;
44 import java.util.SortedSet;
45 import java.util.TreeSet;
46 import java.util.concurrent.ArrayBlockingQueue;
47 import java.util.concurrent.LinkedBlockingDeque;
48 import java.util.concurrent.LinkedBlockingQueue;
49 import java.util.concurrent.PriorityBlockingQueue;
50 
51 public class NoNulls {
test(String[] args)52     void test(String[] args) throws Throwable {
53         final Comparator<String> nullTolerantComparator
54             = new Comparator<>() {
55             public int compare(String x, String y) {
56                 return (x == null ? -1 :
57                         y == null ? 1 :
58                         x.compareTo(y));
59             }};
60 
61         final SortedSet<String> nullSortedSet
62             = new TreeSet<>(nullTolerantComparator);
63         nullSortedSet.add(null);
64 
65         final PriorityQueue<String> nullPriorityQueue
66             = new PriorityQueue<>() {
67             public Object[] toArray() { return new Object[] { null };}};
68 
69         final Collection<String> nullCollection = new ArrayList<>();
70         nullCollection.add(null);
71 
72         THROWS(NullPointerException.class,
73                new F() { void f() {
74                    new PriorityQueue<String>(nullCollection);
75                }},
76                new F() { void f() {
77                    new PriorityBlockingQueue<String>(nullCollection);
78                }},
79                new F() { void f() {
80                    new ArrayBlockingQueue<String>(10, false, nullCollection);
81                }},
82                new F() { void f() {
83                    new ArrayBlockingQueue<String>(10, true, nullCollection);
84                }},
85                new F() { void f() {
86                    new LinkedBlockingQueue<String>(nullCollection);
87                }},
88                new F() { void f() {
89                    new LinkedBlockingDeque<String>(nullCollection);
90                }},
91 
92                new F() { void f() {
93                    new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
94                }},
95                new F() { void f() {
96                    new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
97                }},
98 
99                new F() { void f() {
100                    new PriorityQueue<String>(nullSortedSet);
101                }},
102                new F() { void f() {
103                    new PriorityBlockingQueue<String>(nullSortedSet);
104                }},
105 
106                new F() { void f() {
107                    new PriorityQueue<String>((Collection<String>) nullSortedSet);
108                }},
109                new F() { void f() {
110                    new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
111                }},
112 
113                new F() { void f() {
114                    new PriorityQueue<String>(nullPriorityQueue);
115                }},
116                new F() { void f() {
117                    new PriorityBlockingQueue<String>(nullPriorityQueue);
118                }},
119 
120                new F() { void f() {
121                    new PriorityQueue<String>().add(null);
122                }},
123                new F() { void f() {
124                    new PriorityBlockingQueue<String>().add(null);
125                }},
126                new F() { void f() {
127                    new ArrayBlockingQueue<String>(10, false).add(null);
128                }},
129                new F() { void f() {
130                    new ArrayBlockingQueue<String>(10, true).add(null);
131                }},
132                new F() { void f() {
133                    new LinkedBlockingQueue<String>().add(null);
134                }},
135                new F() { void f() {
136                    new LinkedBlockingDeque<String>().add(null);
137                }},
138 
139                new F() { void f() {
140                    new PriorityQueue<String>().offer(null);
141                }},
142                new F() { void f() {
143                    new PriorityBlockingQueue<String>().offer(null);
144                }});
145 
146         nullSortedSet.add("foo");
147         nullCollection.add("foo");
148         THROWS(NullPointerException.class,
149                new F() { void f() {
150                    new PriorityQueue<String>(nullCollection);
151                }},
152                new F() { void f() {
153                    new PriorityBlockingQueue<String>(nullCollection);
154                }},
155 
156                new F() { void f() {
157                    new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
158                }},
159                new F() { void f() {
160                    new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
161                }},
162 
163                new F() { void f() {
164                    new PriorityQueue<String>(nullSortedSet);
165                }},
166                new F() { void f() {
167                    new PriorityBlockingQueue<String>(nullSortedSet);
168                }},
169 
170                new F() { void f() {
171                    new PriorityQueue<String>((Collection<String>) nullSortedSet);
172                }},
173                new F() { void f() {
174                    new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
175                }});
176 
177     }
178 
179     //--------------------- Infrastructure ---------------------------
180     volatile int passed = 0, failed = 0;
pass()181     void pass() {passed++;}
fail()182     void fail() {failed++; Thread.dumpStack();}
fail(String msg)183     void fail(String msg) {System.err.println(msg); fail();}
unexpected(Throwable t)184     void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)185     void check(boolean cond) {if (cond) pass(); else fail();}
equal(Object x, Object y)186     void equal(Object x, Object y) {
187         if (x == null ? y == null : x.equals(y)) pass();
188         else fail(x + " not equal to " + y);}
main(String[] args)189     public static void main(String[] args) throws Throwable {
190         new NoNulls().instanceMain(args);}
instanceMain(String[] args)191     public void instanceMain(String[] args) throws Throwable {
192         try {test(args);} catch (Throwable t) {unexpected(t);}
193         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
194         if (failed > 0) throw new AssertionError("Some tests failed");}
f()195     abstract class F {abstract void f() throws Throwable;}
THROWS(Class<? extends Throwable> k, F... fs)196     void THROWS(Class<? extends Throwable> k, F... fs) {
197         for (F f : fs)
198             try {f.f(); fail("Expected " + k.getName() + " not thrown");}
199             catch (Throwable t) {
200                 if (k.isAssignableFrom(t.getClass())) pass();
201                 else unexpected(t);}}
202 }
203