Path: blob/master/test/jdk/java/util/PriorityQueue/NoNulls.java
41152 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.Collection;41import java.util.Comparator;42import java.util.PriorityQueue;43import java.util.SortedSet;44import java.util.TreeSet;45import java.util.concurrent.ArrayBlockingQueue;46import java.util.concurrent.LinkedBlockingDeque;47import java.util.concurrent.LinkedBlockingQueue;48import java.util.concurrent.PriorityBlockingQueue;4950public class NoNulls {51void test(String[] args) throws Throwable {52final Comparator<String> nullTolerantComparator53= new Comparator<>() {54public int compare(String x, String y) {55return (x == null ? -1 :56y == null ? 1 :57x.compareTo(y));58}};5960final SortedSet<String> nullSortedSet61= new TreeSet<>(nullTolerantComparator);62nullSortedSet.add(null);6364final PriorityQueue<String> nullPriorityQueue65= new PriorityQueue<>() {66public Object[] toArray() { return new Object[] { null };}};6768final Collection<String> nullCollection = new ArrayList<>();69nullCollection.add(null);7071THROWS(NullPointerException.class,72new F() { void f() {73new PriorityQueue<String>(nullCollection);74}},75new F() { void f() {76new PriorityBlockingQueue<String>(nullCollection);77}},78new F() { void f() {79new ArrayBlockingQueue<String>(10, false, nullCollection);80}},81new F() { void f() {82new ArrayBlockingQueue<String>(10, true, nullCollection);83}},84new F() { void f() {85new LinkedBlockingQueue<String>(nullCollection);86}},87new F() { void f() {88new LinkedBlockingDeque<String>(nullCollection);89}},9091new F() { void f() {92new PriorityQueue<String>((Collection<String>) nullPriorityQueue);93}},94new F() { void f() {95new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);96}},9798new F() { void f() {99new PriorityQueue<String>(nullSortedSet);100}},101new F() { void f() {102new PriorityBlockingQueue<String>(nullSortedSet);103}},104105new F() { void f() {106new PriorityQueue<String>((Collection<String>) nullSortedSet);107}},108new F() { void f() {109new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);110}},111112new F() { void f() {113new PriorityQueue<String>(nullPriorityQueue);114}},115new F() { void f() {116new PriorityBlockingQueue<String>(nullPriorityQueue);117}},118119new F() { void f() {120new PriorityQueue<String>().add(null);121}},122new F() { void f() {123new PriorityBlockingQueue<String>().add(null);124}},125new F() { void f() {126new ArrayBlockingQueue<String>(10, false).add(null);127}},128new F() { void f() {129new ArrayBlockingQueue<String>(10, true).add(null);130}},131new F() { void f() {132new LinkedBlockingQueue<String>().add(null);133}},134new F() { void f() {135new LinkedBlockingDeque<String>().add(null);136}},137138new F() { void f() {139new PriorityQueue<String>().offer(null);140}},141new F() { void f() {142new PriorityBlockingQueue<String>().offer(null);143}});144145nullSortedSet.add("foo");146nullCollection.add("foo");147THROWS(NullPointerException.class,148new F() { void f() {149new PriorityQueue<String>(nullCollection);150}},151new F() { void f() {152new PriorityBlockingQueue<String>(nullCollection);153}},154155new F() { void f() {156new PriorityQueue<String>((Collection<String>) nullPriorityQueue);157}},158new F() { void f() {159new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);160}},161162new F() { void f() {163new PriorityQueue<String>(nullSortedSet);164}},165new F() { void f() {166new PriorityBlockingQueue<String>(nullSortedSet);167}},168169new F() { void f() {170new PriorityQueue<String>((Collection<String>) nullSortedSet);171}},172new F() { void f() {173new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);174}});175176}177178//--------------------- Infrastructure ---------------------------179volatile int passed = 0, failed = 0;180void pass() {passed++;}181void fail() {failed++; Thread.dumpStack();}182void fail(String msg) {System.err.println(msg); fail();}183void unexpected(Throwable t) {failed++; t.printStackTrace();}184void check(boolean cond) {if (cond) pass(); else fail();}185void equal(Object x, Object y) {186if (x == null ? y == null : x.equals(y)) pass();187else fail(x + " not equal to " + y);}188public static void main(String[] args) throws Throwable {189new NoNulls().instanceMain(args);}190public void instanceMain(String[] args) throws Throwable {191try {test(args);} catch (Throwable t) {unexpected(t);}192System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);193if (failed > 0) throw new AssertionError("Some tests failed");}194abstract class F {abstract void f() throws Throwable;}195void THROWS(Class<? extends Throwable> k, F... fs) {196for (F f : fs)197try {f.f(); fail("Expected " + k.getName() + " not thrown");}198catch (Throwable t) {199if (k.isAssignableFrom(t.getClass())) pass();200else unexpected(t);}}201}202203204