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