ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/PriorityQueue/NoNulls.java
Revision: 1.5
Committed: Sat Dec 21 17:44:27 2013 UTC (10 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +2 -2 lines
Log Message:
sync with openjdk9

File Contents

# User Rev Content
1 jsr166 1.1 /*
2     * Written by Martin Buchholz with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.2 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.1 */
6    
7     /*
8     * @test
9     * @bug 6950540
10     * @summary Attempt to add a null throws NullPointerException
11     */
12    
13     import java.util.ArrayList;
14     import java.util.Arrays;
15     import java.util.Comparator;
16     import java.util.Collection;
17     import java.util.Collections;
18     import java.util.PriorityQueue;
19     import java.util.SortedSet;
20     import java.util.TreeSet;
21     import java.util.concurrent.ArrayBlockingQueue;
22     import java.util.concurrent.LinkedBlockingDeque;
23     import java.util.concurrent.LinkedBlockingQueue;
24     import java.util.concurrent.PriorityBlockingQueue;
25    
26     public class NoNulls {
27     void test(String[] args) throws Throwable {
28     final Comparator<String> nullTolerantComparator
29 jsr166 1.3 = new Comparator<String>() {
30 jsr166 1.1 public int compare(String x, String y) {
31     return (x == null ? -1 :
32     y == null ? 1 :
33     x.compareTo(y));
34     }};
35    
36     final SortedSet<String> nullSortedSet
37 jsr166 1.5 = new TreeSet<>(nullTolerantComparator);
38 jsr166 1.1 nullSortedSet.add(null);
39    
40     final PriorityQueue<String> nullPriorityQueue
41 jsr166 1.3 = new PriorityQueue<String>() {
42 jsr166 1.1 public Object[] toArray() { return new Object[] { null };}};
43    
44 jsr166 1.5 final Collection<String> nullCollection = new ArrayList<>();
45 jsr166 1.1 nullCollection.add(null);
46    
47     THROWS(NullPointerException.class,
48     new F() { void f() {
49     new PriorityQueue<String>(nullCollection);
50     }},
51     new F() { void f() {
52     new PriorityBlockingQueue<String>(nullCollection);
53     }},
54     new F() { void f() {
55     new ArrayBlockingQueue<String>(10, false, nullCollection);
56     }},
57     new F() { void f() {
58     new ArrayBlockingQueue<String>(10, true, nullCollection);
59     }},
60     new F() { void f() {
61     new LinkedBlockingQueue<String>(nullCollection);
62     }},
63     new F() { void f() {
64     new LinkedBlockingDeque<String>(nullCollection);
65     }},
66    
67     new F() { void f() {
68     new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
69     }},
70     new F() { void f() {
71     new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
72     }},
73    
74     new F() { void f() {
75     new PriorityQueue<String>(nullSortedSet);
76     }},
77     new F() { void f() {
78     new PriorityBlockingQueue<String>(nullSortedSet);
79     }},
80    
81     new F() { void f() {
82     new PriorityQueue<String>((Collection<String>) nullSortedSet);
83     }},
84     new F() { void f() {
85     new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
86     }},
87    
88     new F() { void f() {
89     new PriorityQueue<String>(nullPriorityQueue);
90     }},
91     new F() { void f() {
92     new PriorityBlockingQueue<String>(nullPriorityQueue);
93     }},
94    
95     new F() { void f() {
96     new PriorityQueue<String>().add(null);
97     }},
98     new F() { void f() {
99     new PriorityBlockingQueue<String>().add(null);
100     }},
101     new F() { void f() {
102     new ArrayBlockingQueue<String>(10, false).add(null);
103     }},
104     new F() { void f() {
105     new ArrayBlockingQueue<String>(10, true).add(null);
106     }},
107     new F() { void f() {
108     new LinkedBlockingQueue<String>().add(null);
109     }},
110     new F() { void f() {
111     new LinkedBlockingDeque<String>().add(null);
112     }},
113    
114     new F() { void f() {
115     new PriorityQueue<String>().offer(null);
116     }},
117     new F() { void f() {
118     new PriorityBlockingQueue<String>().offer(null);
119     }});
120    
121     nullSortedSet.add("foo");
122     nullCollection.add("foo");
123     THROWS(NullPointerException.class,
124     new F() { void f() {
125     new PriorityQueue<String>(nullCollection);
126     }},
127     new F() { void f() {
128     new PriorityBlockingQueue<String>(nullCollection);
129     }},
130    
131     new F() { void f() {
132     new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
133     }},
134     new F() { void f() {
135     new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
136     }},
137    
138     new F() { void f() {
139     new PriorityQueue<String>(nullSortedSet);
140     }},
141     new F() { void f() {
142     new PriorityBlockingQueue<String>(nullSortedSet);
143     }},
144    
145     new F() { void f() {
146     new PriorityQueue<String>((Collection<String>) nullSortedSet);
147     }},
148     new F() { void f() {
149     new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
150     }});
151    
152     }
153    
154     //--------------------- Infrastructure ---------------------------
155     volatile int passed = 0, failed = 0;
156     void pass() {passed++;}
157     void fail() {failed++; Thread.dumpStack();}
158     void fail(String msg) {System.err.println(msg); fail();}
159     void unexpected(Throwable t) {failed++; t.printStackTrace();}
160     void check(boolean cond) {if (cond) pass(); else fail();}
161     void equal(Object x, Object y) {
162     if (x == null ? y == null : x.equals(y)) pass();
163     else fail(x + " not equal to " + y);}
164     public static void main(String[] args) throws Throwable {
165     new NoNulls().instanceMain(args);}
166     public void instanceMain(String[] args) throws Throwable {
167     try {test(args);} catch (Throwable t) {unexpected(t);}
168     System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
169     if (failed > 0) throw new AssertionError("Some tests failed");}
170     abstract class F {abstract void f() throws Throwable;}
171     void THROWS(Class<? extends Throwable> k, F... fs) {
172     for (F f : fs)
173     try {f.f(); fail("Expected " + k.getName() + " not thrown");}
174     catch (Throwable t) {
175     if (k.isAssignableFrom(t.getClass())) pass();
176     else unexpected(t);}}
177     }