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