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 package test.java.util.PriorityQueue;
41 
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.Comparator;
45 import java.util.PriorityQueue;
46 import java.util.SortedSet;
47 import java.util.TreeSet;
48 import java.util.concurrent.ArrayBlockingQueue;
49 import java.util.concurrent.LinkedBlockingDeque;
50 import java.util.concurrent.LinkedBlockingQueue;
51 import java.util.concurrent.PriorityBlockingQueue;
52 
53 public class NoNulls {
54 
test(String[] args)55     void test(String[] args) throws Throwable {
56         final Comparator<String> nullTolerantComparator
57             = new Comparator<>() {
58             public int compare(String x, String y) {
59                 return (x == null ? -1 :
60                         y == null ? 1 :
61                         x.compareTo(y));
62             }};
63 
64         final SortedSet<String> nullSortedSet
65             = new TreeSet<>(nullTolerantComparator);
66         nullSortedSet.add(null);
67 
68         final PriorityQueue<String> nullPriorityQueue
69             = new PriorityQueue<>() {
70             public Object[] toArray() { return new Object[] { null };}};
71 
72         final Collection<String> nullCollection = new ArrayList<>();
73         nullCollection.add(null);
74 
75         THROWS(NullPointerException.class,
76                new F() { void f() {
77                    new PriorityQueue<String>(nullCollection);
78                }},
79                new F() { void f() {
80                    new PriorityBlockingQueue<String>(nullCollection);
81                }},
82                new F() { void f() {
83                    new ArrayBlockingQueue<String>(10, false, nullCollection);
84                }},
85                new F() { void f() {
86                    new ArrayBlockingQueue<String>(10, true, nullCollection);
87                }},
88                new F() { void f() {
89                    new LinkedBlockingQueue<String>(nullCollection);
90                }},
91                new F() { void f() {
92                    new LinkedBlockingDeque<String>(nullCollection);
93                }},
94 
95                new F() { void f() {
96                    new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
97                }},
98                new F() { void f() {
99                    new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
100                }},
101 
102                new F() { void f() {
103                    new PriorityQueue<String>(nullSortedSet);
104                }},
105                new F() { void f() {
106                    new PriorityBlockingQueue<String>(nullSortedSet);
107                }},
108 
109                new F() { void f() {
110                    new PriorityQueue<String>((Collection<String>) nullSortedSet);
111                }},
112                new F() { void f() {
113                    new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
114                }},
115 
116                new F() { void f() {
117                    new PriorityQueue<String>(nullPriorityQueue);
118                }},
119                new F() { void f() {
120                    new PriorityBlockingQueue<String>(nullPriorityQueue);
121                }},
122 
123                new F() { void f() {
124                    new PriorityQueue<String>().add(null);
125                }},
126                new F() { void f() {
127                    new PriorityBlockingQueue<String>().add(null);
128                }},
129                new F() { void f() {
130                    new ArrayBlockingQueue<String>(10, false).add(null);
131                }},
132                new F() { void f() {
133                    new ArrayBlockingQueue<String>(10, true).add(null);
134                }},
135                new F() { void f() {
136                    new LinkedBlockingQueue<String>().add(null);
137                }},
138                new F() { void f() {
139                    new LinkedBlockingDeque<String>().add(null);
140                }},
141 
142                new F() { void f() {
143                    new PriorityQueue<String>().offer(null);
144                }},
145                new F() { void f() {
146                    new PriorityBlockingQueue<String>().offer(null);
147                }});
148 
149         nullSortedSet.add("foo");
150         nullCollection.add("foo");
151         THROWS(NullPointerException.class,
152                new F() { void f() {
153                    new PriorityQueue<String>(nullCollection);
154                }},
155                new F() { void f() {
156                    new PriorityBlockingQueue<String>(nullCollection);
157                }},
158 
159                new F() { void f() {
160                    new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
161                }},
162                new F() { void f() {
163                    new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
164                }},
165 
166                new F() { void f() {
167                    new PriorityQueue<String>(nullSortedSet);
168                }},
169                new F() { void f() {
170                    new PriorityBlockingQueue<String>(nullSortedSet);
171                }},
172 
173                new F() { void f() {
174                    new PriorityQueue<String>((Collection<String>) nullSortedSet);
175                }},
176                new F() { void f() {
177                    new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
178                }});
179 
180     }
181 
182     //--------------------- Infrastructure ---------------------------
183     volatile int passed = 0, failed = 0;
pass()184     void pass() {passed++;}
fail()185     void fail() {failed++; Thread.dumpStack();}
fail(String msg)186     void fail(String msg) {System.err.println(msg); fail();}
unexpected(Throwable t)187     void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)188     void check(boolean cond) {if (cond) pass(); else fail();}
equal(Object x, Object y)189     void equal(Object x, Object y) {
190         if (x == null ? y == null : x.equals(y)) pass();
191         else fail(x + " not equal to " + y);}
main(String[] args)192     public static void main(String[] args) throws Throwable {
193         new NoNulls().instanceMain(args);}
instanceMain(String[] args)194     public void instanceMain(String[] args) throws Throwable {
195         try {test(args);} catch (Throwable t) {unexpected(t);}
196         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
197         if (failed > 0) throw new AssertionError("Some tests failed");}
f()198     abstract class F {abstract void f() throws Throwable;}
THROWS(Class<? extends Throwable> k, F... fs)199     void THROWS(Class<? extends Throwable> k, F... fs) {
200         for (F f : fs)
201             try {f.f(); fail("Expected " + k.getName() + " not thrown");}
202             catch (Throwable t) {
203                 if (k.isAssignableFrom(t.getClass())) pass();
204                 else unexpected(t);}}
205 }
206