2351N/A/*
2351N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2351N/A *
2351N/A * This code is free software; you can redistribute it and/or modify it
2351N/A * under the terms of the GNU General Public License version 2 only, as
2351N/A * published by the Free Software Foundation.
2351N/A *
2351N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2351N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2351N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2351N/A * version 2 for more details (a copy is included in the LICENSE file that
2351N/A * accompanied this code).
2351N/A *
2351N/A * You should have received a copy of the GNU General Public License version
2351N/A * 2 along with this work; if not, write to the Free Software Foundation,
2351N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2351N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2351N/A */
2351N/A
2351N/A/*
2351N/A * This file is available under and governed by the GNU General Public
2351N/A * License version 2 only, as published by the Free Software Foundation.
2351N/A * However, the following notice accompanied the original version of this
2351N/A * file:
2351N/A *
2351N/A * Written by Martin Buchholz with assistance from members of JCP JSR-166
2351N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
2351N/A */
2351N/A
2351N/A/*
2351N/A * @test
2351N/A * @bug 6950540
2351N/A * @summary Attempt to add a null throws NullPointerException
2351N/A */
2351N/A
2351N/Aimport java.util.ArrayList;
2351N/Aimport java.util.Arrays;
2351N/Aimport java.util.Comparator;
2351N/Aimport java.util.Collection;
2351N/Aimport java.util.Collections;
2351N/Aimport java.util.PriorityQueue;
2351N/Aimport java.util.SortedSet;
2351N/Aimport java.util.TreeSet;
2351N/Aimport java.util.concurrent.ArrayBlockingQueue;
2351N/Aimport java.util.concurrent.LinkedBlockingDeque;
2351N/Aimport java.util.concurrent.LinkedBlockingQueue;
2351N/Aimport java.util.concurrent.PriorityBlockingQueue;
2351N/A
2351N/Apublic class NoNulls {
2351N/A void test(String[] args) throws Throwable {
2351N/A final Comparator<String> nullTolerantComparator
3683N/A = new Comparator<String>() {
2351N/A public int compare(String x, String y) {
2351N/A return (x == null ? -1 :
2351N/A y == null ? 1 :
2351N/A x.compareTo(y));
2351N/A }};
2351N/A
2351N/A final SortedSet<String> nullSortedSet
2351N/A = new TreeSet<>(nullTolerantComparator);
2351N/A nullSortedSet.add(null);
2351N/A
2351N/A final PriorityQueue<String> nullPriorityQueue
3683N/A = new PriorityQueue<String>() {
2351N/A public Object[] toArray() { return new Object[] { null };}};
2351N/A
2351N/A final Collection<String> nullCollection = new ArrayList<>();
2351N/A nullCollection.add(null);
2351N/A
2351N/A THROWS(NullPointerException.class,
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>(nullCollection);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>(nullCollection);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new ArrayBlockingQueue<String>(10, false, nullCollection);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new ArrayBlockingQueue<String>(10, true, nullCollection);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new LinkedBlockingQueue<String>(nullCollection);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new LinkedBlockingDeque<String>(nullCollection);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>(nullSortedSet);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>(nullSortedSet);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>((Collection<String>) nullSortedSet);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>(nullPriorityQueue);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>(nullPriorityQueue);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>().add(null);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>().add(null);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new ArrayBlockingQueue<String>(10, false).add(null);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new ArrayBlockingQueue<String>(10, true).add(null);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new LinkedBlockingQueue<String>().add(null);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new LinkedBlockingDeque<String>().add(null);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>().offer(null);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>().offer(null);
2351N/A }});
2351N/A
2351N/A nullSortedSet.add("foo");
2351N/A nullCollection.add("foo");
2351N/A THROWS(NullPointerException.class,
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>(nullCollection);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>(nullCollection);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>((Collection<String>) nullPriorityQueue);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>((Collection<String>) nullPriorityQueue);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>(nullSortedSet);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>(nullSortedSet);
2351N/A }},
2351N/A
2351N/A new F() { void f() {
2351N/A new PriorityQueue<String>((Collection<String>) nullSortedSet);
2351N/A }},
2351N/A new F() { void f() {
2351N/A new PriorityBlockingQueue<String>((Collection<String>) nullSortedSet);
2351N/A }});
2351N/A
2351N/A }
2351N/A
2351N/A //--------------------- Infrastructure ---------------------------
2351N/A volatile int passed = 0, failed = 0;
2351N/A void pass() {passed++;}
2351N/A void fail() {failed++; Thread.dumpStack();}
2351N/A void fail(String msg) {System.err.println(msg); fail();}
2351N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
2351N/A void check(boolean cond) {if (cond) pass(); else fail();}
2351N/A void equal(Object x, Object y) {
2351N/A if (x == null ? y == null : x.equals(y)) pass();
2351N/A else fail(x + " not equal to " + y);}
2351N/A public static void main(String[] args) throws Throwable {
2351N/A new NoNulls().instanceMain(args);}
2351N/A public void instanceMain(String[] args) throws Throwable {
2351N/A try {test(args);} catch (Throwable t) {unexpected(t);}
2351N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
2351N/A if (failed > 0) throw new AssertionError("Some tests failed");}
2351N/A abstract class F {abstract void f() throws Throwable;}
2351N/A void THROWS(Class<? extends Throwable> k, F... fs) {
2351N/A for (F f : fs)
2351N/A try {f.f(); fail("Expected " + k.getName() + " not thrown");}
2351N/A catch (Throwable t) {
2351N/A if (k.isAssignableFrom(t.getClass())) pass();
2351N/A else unexpected(t);}}
2351N/A}