Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PriorityQueue/NoNulls.java
38812 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*/2122/*23* This file is available under and governed by the GNU General Public24* License version 2 only, as published by the Free Software Foundation.25* However, the following notice accompanied the original version of this26* file:27*28* Written by Martin Buchholz with assistance from members of JCP JSR-16629* Expert Group and released to the public domain, as explained at30* http://creativecommons.org/publicdomain/zero/1.0/31*/3233/*34* @test35* @bug 695054036* @summary Attempt to add a null throws NullPointerException37*/3839import java.util.ArrayList;40import java.util.Arrays;41import java.util.Comparator;42import java.util.Collection;43import java.util.Collections;44import java.util.PriorityQueue;45import java.util.SortedSet;46import java.util.TreeSet;47import java.util.concurrent.ArrayBlockingQueue;48import java.util.concurrent.LinkedBlockingDeque;49import java.util.concurrent.LinkedBlockingQueue;50import java.util.concurrent.PriorityBlockingQueue;5152public class NoNulls {53void test(String[] args) throws Throwable {54final Comparator<String> nullTolerantComparator55= new Comparator<String>() {56public int compare(String x, String y) {57return (x == null ? -1 :58y == null ? 1 :59x.compareTo(y));60}};6162final SortedSet<String> nullSortedSet63= new TreeSet<>(nullTolerantComparator);64nullSortedSet.add(null);6566final PriorityQueue<String> nullPriorityQueue67= new PriorityQueue<String>() {68public Object[] toArray() { return new Object[] { null };}};6970final Collection<String> nullCollection = new ArrayList<>();71nullCollection.add(null);7273THROWS(NullPointerException.class,74new F() { void f() {75new PriorityQueue<String>(nullCollection);76}},77new F() { void f() {78new PriorityBlockingQueue<String>(nullCollection);79}},80new F() { void f() {81new ArrayBlockingQueue<String>(10, false, nullCollection);82}},83new F() { void f() {84new ArrayBlockingQueue<String>(10, true, nullCollection);85}},86new F() { void f() {87new LinkedBlockingQueue<String>(nullCollection);88}},89new F() { void f() {90new LinkedBlockingDeque<String>(nullCollection);91}},9293new F() { void f() {94new PriorityQueue<String>((Collection<String>) nullPriorityQueue);95}},96new F() { void f() {97new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);98}},99100new F() { void f() {101new PriorityQueue<String>(nullSortedSet);102}},103new F() { void f() {104new PriorityBlockingQueue<String>(nullSortedSet);105}},106107new F() { void f() {108new PriorityQueue<String>((Collection<String>) nullSortedSet);109}},110new F() { void f() {111new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);112}},113114new F() { void f() {115new PriorityQueue<String>(nullPriorityQueue);116}},117new F() { void f() {118new PriorityBlockingQueue<String>(nullPriorityQueue);119}},120121new F() { void f() {122new PriorityQueue<String>().add(null);123}},124new F() { void f() {125new PriorityBlockingQueue<String>().add(null);126}},127new F() { void f() {128new ArrayBlockingQueue<String>(10, false).add(null);129}},130new F() { void f() {131new ArrayBlockingQueue<String>(10, true).add(null);132}},133new F() { void f() {134new LinkedBlockingQueue<String>().add(null);135}},136new F() { void f() {137new LinkedBlockingDeque<String>().add(null);138}},139140new F() { void f() {141new PriorityQueue<String>().offer(null);142}},143new F() { void f() {144new PriorityBlockingQueue<String>().offer(null);145}});146147nullSortedSet.add("foo");148nullCollection.add("foo");149THROWS(NullPointerException.class,150new F() { void f() {151new PriorityQueue<String>(nullCollection);152}},153new F() { void f() {154new PriorityBlockingQueue<String>(nullCollection);155}},156157new F() { void f() {158new PriorityQueue<String>((Collection<String>) nullPriorityQueue);159}},160new F() { void f() {161new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);162}},163164new F() { void f() {165new PriorityQueue<String>(nullSortedSet);166}},167new F() { void f() {168new PriorityBlockingQueue<String>(nullSortedSet);169}},170171new F() { void f() {172new PriorityQueue<String>((Collection<String>) nullSortedSet);173}},174new F() { void f() {175new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);176}});177178}179180//--------------------- Infrastructure ---------------------------181volatile int passed = 0, failed = 0;182void pass() {passed++;}183void fail() {failed++; Thread.dumpStack();}184void fail(String msg) {System.err.println(msg); fail();}185void unexpected(Throwable t) {failed++; t.printStackTrace();}186void check(boolean cond) {if (cond) pass(); else fail();}187void equal(Object x, Object y) {188if (x == null ? y == null : x.equals(y)) pass();189else fail(x + " not equal to " + y);}190public static void main(String[] args) throws Throwable {191new NoNulls().instanceMain(args);}192public void instanceMain(String[] args) throws Throwable {193try {test(args);} catch (Throwable t) {unexpected(t);}194System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);195if (failed > 0) throw new AssertionError("Some tests failed");}196abstract class F {abstract void f() throws Throwable;}197void THROWS(Class<? extends Throwable> k, F... fs) {198for (F f : fs)199try {f.f(); fail("Expected " + k.getName() + " not thrown");}200catch (Throwable t) {201if (k.isAssignableFrom(t.getClass())) pass();202else unexpected(t);}}203}204205206