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 package test.java.util.PriorityQueue;
31 
32 import org.testng.annotations.Test;
33 
34 import java.util.PriorityQueue;
35 import java.util.Queue;
36 import java.util.SortedMap;
37 import java.util.SortedSet;
38 import java.util.TreeMap;
39 import java.util.TreeSet;
40 import java.util.concurrent.ConcurrentSkipListMap;
41 import java.util.concurrent.ConcurrentSkipListSet;
42 import java.util.concurrent.PriorityBlockingQueue;
43 import java.util.function.BiConsumer;
44 import java.util.function.Supplier;
45 
46 import static org.testng.Assert.assertEquals;
47 import static org.testng.Assert.assertTrue;
48 
49 import android.compat.Compatibility;
50 
51 import dalvik.annotation.compat.VersionCodes;
52 import dalvik.system.VMRuntime;
53 
54 public class AddNonComparable {
55 
test(Queue<E> queue, Supplier<E> supplier, BiConsumer<? super Queue<E>, Throwable> checker)56     static <E> void test(Queue<E> queue, Supplier<E> supplier,
57                          BiConsumer<? super Queue<E>, Throwable> checker) {
58         Throwable x = null;
59         try { queue.add(supplier.get()); }
60         catch (Throwable e) { x = e; }
61         checker.accept(queue, x);
62     }
63 
64     @Test
queues()65     public void queues() {
66         // Android-added: test old behavior in < U.
67         boolean testPreAndroidUBehavior = VMRuntime.getSdkVersion() < VersionCodes.UPSIDE_DOWN_CAKE
68             || !Compatibility.isChangeEnabled(PriorityQueue.PRIORITY_QUEUE_OFFER_NON_COMPARABLE_ONE_ELEMENT);
69 
70         if (testPreAndroidUBehavior) {
71             test(new PriorityQueue<>(), NonComparable::new,
72                  (q, e) -> {
73                      assertEquals(q.size(), 1);
74                      assertTrue(e == null);
75                  });
76         } else {
77             test(new PriorityQueue<>(), NonComparable::new,
78                  (q, e) -> {
79                      assertEquals(q.size(), 0);
80                      assertTrue(e instanceof ClassCastException);
81                  });
82         }
83         test(new PriorityQueue<>(), AComparable::new,
84              (q, e) -> {
85                  assertEquals(q.size(), 1);
86                  assertTrue(e == null);
87              });
88 
89         test(new PriorityBlockingQueue<>(), NonComparable::new,
90              (q, e) -> {
91                  assertEquals(q.size(), 0);
92                  assertTrue(e instanceof ClassCastException);
93              });
94         test(new PriorityBlockingQueue<>(), AComparable::new,
95              (q, e) -> {
96                  assertEquals(q.size(), 1);
97                  assertTrue(e == null);
98              });
99     }
100 
test(SortedSet<E> set, Supplier<E> supplier, BiConsumer<? super SortedSet<E>, Throwable> checker)101     static <E> void test(SortedSet<E> set, Supplier<E> supplier,
102                          BiConsumer<? super SortedSet<E>, Throwable> checker) {
103         Throwable x = null;
104         try { set.add(supplier.get()); }
105         catch (Throwable e) { x = e; }
106         checker.accept(set, x);
107     }
108 
109 
110     @Test
sets()111     public void sets() {
112         test(new TreeSet<>(), NonComparable::new,
113              (s, e) -> {
114                  assertEquals(s.size(), 0);
115                  assertTrue(e instanceof ClassCastException);
116              });
117         test(new TreeSet<>(), AComparable::new,
118              (s, e) -> {
119                  assertEquals(s.size(), 1);
120                  assertTrue(e == null);
121              });
122 
123         test(new ConcurrentSkipListSet<>(), NonComparable::new,
124              (s, e) -> {
125                  assertEquals(s.size(), 0);
126                  assertTrue(e instanceof ClassCastException);
127              });
128         test(new ConcurrentSkipListSet<>(), AComparable::new,
129              (s, e) -> {
130                  assertEquals(s.size(), 1);
131                  assertTrue(e == null);
132              });
133     }
134 
test(SortedMap<K,Boolean> map, Supplier<K> supplier, BiConsumer<? super SortedMap<K,Boolean>, Throwable> checker)135     static <K> void test(SortedMap<K,Boolean> map, Supplier<K> supplier,
136                          BiConsumer<? super SortedMap<K,Boolean>, Throwable> checker) {
137         Throwable x = null;
138         try { map.put(supplier.get(), Boolean.TRUE); }
139         catch (Throwable e) { x = e; }
140         checker.accept(map, x);
141     }
142 
143     @Test
maps()144     public void maps() {
145         test(new TreeMap<>(), NonComparable::new,
146              (m, e) -> {
147                  assertEquals(m.size(), 0);
148                  assertTrue(e instanceof ClassCastException);
149              });
150         test(new TreeMap<>(), AComparable::new,
151              (m, e) -> {
152                  assertEquals(m.size(), 1);
153                  assertTrue(e == null);
154              });
155 
156         test(new ConcurrentSkipListMap<>(), NonComparable::new,
157              (s, e) -> {
158                  assertEquals(s.size(), 0);
159                  assertTrue(e instanceof ClassCastException);
160              });
161         test(new ConcurrentSkipListMap<>(), AComparable::new,
162              (s, e) -> {
163                  assertEquals(s.size(), 1);
164                  assertTrue(e == null);
165              });
166     }
167 
168     static class NonComparable { }
169 
170     static class AComparable implements Comparable<AComparable> {
compareTo(AComparable v)171         @Override public int compareTo(AComparable v) { return 0; }
172     }
173 
174 }
175