1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Copyright (c) 2015, 2016, 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 8066070 |
27 |
|
|
* @run testng AddNonComparable |
28 |
|
|
*/ |
29 |
|
|
|
30 |
jsr166 |
1.3 |
import org.testng.annotations.Test; |
31 |
|
|
|
32 |
jsr166 |
1.1 |
import java.util.PriorityQueue; |
33 |
|
|
import java.util.Queue; |
34 |
|
|
import java.util.SortedMap; |
35 |
|
|
import java.util.SortedSet; |
36 |
|
|
import java.util.TreeMap; |
37 |
|
|
import java.util.TreeSet; |
38 |
|
|
import java.util.concurrent.ConcurrentSkipListMap; |
39 |
|
|
import java.util.concurrent.ConcurrentSkipListSet; |
40 |
|
|
import java.util.concurrent.PriorityBlockingQueue; |
41 |
|
|
import java.util.function.BiConsumer; |
42 |
|
|
import java.util.function.Supplier; |
43 |
|
|
|
44 |
jsr166 |
1.3 |
import static org.testng.Assert.assertEquals; |
45 |
jsr166 |
1.5 |
import static org.testng.Assert.assertNull; |
46 |
jsr166 |
1.3 |
import static org.testng.Assert.assertTrue; |
47 |
jsr166 |
1.1 |
|
48 |
|
|
public class AddNonComparable { |
49 |
|
|
|
50 |
|
|
static <E> void test(Queue<E> queue, Supplier<E> supplier, |
51 |
|
|
BiConsumer<? super Queue<E>, Throwable> checker) { |
52 |
|
|
Throwable x = null; |
53 |
|
|
try { queue.add(supplier.get()); } |
54 |
|
|
catch (Throwable e) { x = e; } |
55 |
|
|
checker.accept(queue, x); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
@Test |
59 |
|
|
public void queues() { |
60 |
|
|
test(new PriorityQueue<>(), NonComparable::new, |
61 |
|
|
(q, e) -> { |
62 |
|
|
assertEquals(q.size(), 0); |
63 |
|
|
assertTrue(e instanceof ClassCastException); |
64 |
|
|
}); |
65 |
|
|
test(new PriorityQueue<>(), AComparable::new, |
66 |
|
|
(q, e) -> { |
67 |
|
|
assertEquals(q.size(), 1); |
68 |
jsr166 |
1.4 |
assertNull(e); |
69 |
jsr166 |
1.1 |
}); |
70 |
|
|
|
71 |
|
|
test(new PriorityBlockingQueue<>(), NonComparable::new, |
72 |
|
|
(q, e) -> { |
73 |
|
|
assertEquals(q.size(), 0); |
74 |
|
|
assertTrue(e instanceof ClassCastException); |
75 |
|
|
}); |
76 |
|
|
test(new PriorityBlockingQueue<>(), AComparable::new, |
77 |
|
|
(q, e) -> { |
78 |
|
|
assertEquals(q.size(), 1); |
79 |
jsr166 |
1.4 |
assertNull(e); |
80 |
jsr166 |
1.1 |
}); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
static <E> void test(SortedSet<E> set, Supplier<E> supplier, |
84 |
|
|
BiConsumer<? super SortedSet<E>, Throwable> checker) { |
85 |
|
|
Throwable x = null; |
86 |
|
|
try { set.add(supplier.get()); } |
87 |
|
|
catch (Throwable e) { x = e; } |
88 |
|
|
checker.accept(set, x); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
|
92 |
|
|
@Test |
93 |
|
|
public void sets() { |
94 |
|
|
test(new TreeSet<>(), NonComparable::new, |
95 |
|
|
(s, e) -> { |
96 |
|
|
assertEquals(s.size(), 0); |
97 |
|
|
assertTrue(e instanceof ClassCastException); |
98 |
|
|
}); |
99 |
|
|
test(new TreeSet<>(), AComparable::new, |
100 |
|
|
(s, e) -> { |
101 |
|
|
assertEquals(s.size(), 1); |
102 |
jsr166 |
1.4 |
assertNull(e); |
103 |
jsr166 |
1.1 |
}); |
104 |
|
|
|
105 |
jsr166 |
1.2 |
test(new ConcurrentSkipListSet<>(), NonComparable::new, |
106 |
|
|
(s, e) -> { |
107 |
|
|
assertEquals(s.size(), 0); |
108 |
|
|
assertTrue(e instanceof ClassCastException); |
109 |
|
|
}); |
110 |
jsr166 |
1.1 |
test(new ConcurrentSkipListSet<>(), AComparable::new, |
111 |
|
|
(s, e) -> { |
112 |
|
|
assertEquals(s.size(), 1); |
113 |
jsr166 |
1.4 |
assertNull(e); |
114 |
jsr166 |
1.1 |
}); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
static <K> void test(SortedMap<K,Boolean> map, Supplier<K> supplier, |
118 |
|
|
BiConsumer<? super SortedMap<K,Boolean>, Throwable> checker) { |
119 |
|
|
Throwable x = null; |
120 |
|
|
try { map.put(supplier.get(), Boolean.TRUE); } |
121 |
|
|
catch (Throwable e) { x = e; } |
122 |
|
|
checker.accept(map, x); |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
@Test |
126 |
|
|
public void maps() { |
127 |
|
|
test(new TreeMap<>(), NonComparable::new, |
128 |
|
|
(m, e) -> { |
129 |
|
|
assertEquals(m.size(), 0); |
130 |
|
|
assertTrue(e instanceof ClassCastException); |
131 |
|
|
}); |
132 |
|
|
test(new TreeMap<>(), AComparable::new, |
133 |
|
|
(m, e) -> { |
134 |
|
|
assertEquals(m.size(), 1); |
135 |
jsr166 |
1.4 |
assertNull(e); |
136 |
jsr166 |
1.1 |
}); |
137 |
|
|
|
138 |
jsr166 |
1.2 |
test(new ConcurrentSkipListMap<>(), NonComparable::new, |
139 |
|
|
(s, e) -> { |
140 |
|
|
assertEquals(s.size(), 0); |
141 |
|
|
assertTrue(e instanceof ClassCastException); |
142 |
|
|
}); |
143 |
jsr166 |
1.1 |
test(new ConcurrentSkipListMap<>(), AComparable::new, |
144 |
|
|
(s, e) -> { |
145 |
|
|
assertEquals(s.size(), 1); |
146 |
jsr166 |
1.4 |
assertNull(e); |
147 |
jsr166 |
1.1 |
}); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
static class NonComparable { } |
151 |
|
|
|
152 |
|
|
static class AComparable implements Comparable<AComparable> { |
153 |
|
|
@Override public int compareTo(AComparable v) { return 0; } |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
} |