Path: blob/master/test/jdk/java/util/PriorityQueue/AddNonComparable.java
41149 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 806607026* @run testng AddNonComparable27*/2829import org.testng.annotations.Test;3031import java.util.PriorityQueue;32import java.util.Queue;33import java.util.SortedMap;34import java.util.SortedSet;35import java.util.TreeMap;36import java.util.TreeSet;37import java.util.concurrent.ConcurrentSkipListMap;38import java.util.concurrent.ConcurrentSkipListSet;39import java.util.concurrent.PriorityBlockingQueue;40import java.util.function.BiConsumer;41import java.util.function.Supplier;4243import static org.testng.Assert.assertEquals;44import static org.testng.Assert.assertNull;45import static org.testng.Assert.assertTrue;4647public class AddNonComparable {4849static <E> void test(Queue<E> queue, Supplier<E> supplier,50BiConsumer<? super Queue<E>, Throwable> checker) {51Throwable x = null;52try { queue.add(supplier.get()); }53catch (Throwable e) { x = e; }54checker.accept(queue, x);55}5657@Test58public void queues() {59test(new PriorityQueue<>(), NonComparable::new,60(q, e) -> {61assertEquals(q.size(), 0);62assertTrue(e instanceof ClassCastException);63});64test(new PriorityQueue<>(), AComparable::new,65(q, e) -> {66assertEquals(q.size(), 1);67assertNull(e);68});6970test(new PriorityBlockingQueue<>(), NonComparable::new,71(q, e) -> {72assertEquals(q.size(), 0);73assertTrue(e instanceof ClassCastException);74});75test(new PriorityBlockingQueue<>(), AComparable::new,76(q, e) -> {77assertEquals(q.size(), 1);78assertNull(e);79});80}8182static <E> void test(SortedSet<E> set, Supplier<E> supplier,83BiConsumer<? super SortedSet<E>, Throwable> checker) {84Throwable x = null;85try { set.add(supplier.get()); }86catch (Throwable e) { x = e; }87checker.accept(set, x);88}899091@Test92public void sets() {93test(new TreeSet<>(), NonComparable::new,94(s, e) -> {95assertEquals(s.size(), 0);96assertTrue(e instanceof ClassCastException);97});98test(new TreeSet<>(), AComparable::new,99(s, e) -> {100assertEquals(s.size(), 1);101assertNull(e);102});103104test(new ConcurrentSkipListSet<>(), NonComparable::new,105(s, e) -> {106assertEquals(s.size(), 0);107assertTrue(e instanceof ClassCastException);108});109test(new ConcurrentSkipListSet<>(), AComparable::new,110(s, e) -> {111assertEquals(s.size(), 1);112assertNull(e);113});114}115116static <K> void test(SortedMap<K,Boolean> map, Supplier<K> supplier,117BiConsumer<? super SortedMap<K,Boolean>, Throwable> checker) {118Throwable x = null;119try { map.put(supplier.get(), Boolean.TRUE); }120catch (Throwable e) { x = e; }121checker.accept(map, x);122}123124@Test125public void maps() {126test(new TreeMap<>(), NonComparable::new,127(m, e) -> {128assertEquals(m.size(), 0);129assertTrue(e instanceof ClassCastException);130});131test(new TreeMap<>(), AComparable::new,132(m, e) -> {133assertEquals(m.size(), 1);134assertNull(e);135});136137test(new ConcurrentSkipListMap<>(), NonComparable::new,138(s, e) -> {139assertEquals(s.size(), 0);140assertTrue(e instanceof ClassCastException);141});142test(new ConcurrentSkipListMap<>(), AComparable::new,143(s, e) -> {144assertEquals(s.size(), 1);145assertNull(e);146});147}148149static class NonComparable { }150151static class AComparable implements Comparable<AComparable> {152@Override public int compareTo(AComparable v) { return 0; }153}154155}156157158