Path: blob/master/test/jdk/java/util/Collections/WrappedNull.java
41149 views
/*1* Copyright (c) 1999, 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 418964126* @summary Wrapping a null collection/array should blow up sooner27* rather than later28*/2930import java.util.Arrays;31import java.util.Collection;32import java.util.Collections;33import java.util.List;34import java.util.Map;35import java.util.Set;36import java.util.SortedMap;37import java.util.SortedSet;38import java.util.TreeMap;39import java.util.TreeSet;4041public class WrappedNull {42public static void main(String[] args) throws Exception {43boolean testSucceeded = false;44try {45List l = Arrays.asList(null);46}47catch (NullPointerException e) {48testSucceeded = true;49}50if (!testSucceeded)51throw new Exception("Arrays.asList");5253testSucceeded = false;54try {55Collection c = Collections.unmodifiableCollection(null);56}57catch (NullPointerException e) {58testSucceeded = true;59}60if (!testSucceeded)61throw new Exception("unmodifiableCollection");6263testSucceeded = false;64try {65Set c = Collections.unmodifiableSet(null);66}67catch (NullPointerException e) {68testSucceeded = true;69}70if (!testSucceeded)71throw new Exception("unmodifiableSet");7273testSucceeded = false;74try {75List c = Collections.unmodifiableList(null);76}77catch (NullPointerException e) {78testSucceeded = true;79}80if (!testSucceeded)81throw new Exception("unmodifiableList");8283testSucceeded = false;84try {85Map c = Collections.unmodifiableMap(null);86}87catch (NullPointerException e) {88testSucceeded = true;89}90if (!testSucceeded)91throw new Exception("unmodifiableMap");9293testSucceeded = false;94try {95SortedSet c = Collections.unmodifiableSortedSet(null);96}97catch (NullPointerException e) {98testSucceeded = true;99}100if (!testSucceeded)101throw new Exception("unmodifiableSortedSet");102103testSucceeded = false;104try {105SortedMap c = Collections.unmodifiableSortedMap(null);106}107catch (NullPointerException e) {108testSucceeded = true;109}110if (!testSucceeded)111throw new Exception("unmodifiableSortedMap");112113testSucceeded = false;114try {115Collection c = Collections.synchronizedCollection(null);116}117catch (NullPointerException e) {118testSucceeded = true;119}120if (!testSucceeded)121throw new Exception("synchronizedCollection");122123testSucceeded = false;124try {125Set c = Collections.synchronizedSet(null);126}127catch (NullPointerException e) {128testSucceeded = true;129}130if (!testSucceeded)131throw new Exception("synchronizedSet");132133testSucceeded = false;134try {135List c = Collections.synchronizedList(null);136}137catch (NullPointerException e) {138testSucceeded = true;139}140if (!testSucceeded)141throw new Exception("synchronizedList");142143testSucceeded = false;144try {145Map c = Collections.synchronizedMap(null);146}147catch (NullPointerException e) {148testSucceeded = true;149}150if (!testSucceeded)151throw new Exception("synchronizedMap");152153testSucceeded = false;154try {155SortedSet c = Collections.synchronizedSortedSet(null);156}157catch (NullPointerException e) {158testSucceeded = true;159}160if (!testSucceeded)161throw new Exception("synchronizedSortedSet");162163testSucceeded = false;164try {165SortedMap c = Collections.synchronizedSortedMap(null);166}167catch (NullPointerException e) {168testSucceeded = true;169}170if (!testSucceeded)171throw new Exception("synchronizedSortedMap");172173// Make sure that non-null arguments don't throw exc.174List l = Arrays.asList(new Object[0]);175Collection c = Collections.unmodifiableCollection(176Collections.EMPTY_SET);177Set s = Collections.unmodifiableSet(Collections.EMPTY_SET);178l = Collections.unmodifiableList(Collections.EMPTY_LIST);179Map m = Collections.unmodifiableMap(Collections.EMPTY_MAP);180SortedSet ss = Collections.unmodifiableSortedSet(new TreeSet());181SortedMap sm = Collections.unmodifiableSortedMap(new TreeMap());182183c = Collections.synchronizedCollection(Collections.EMPTY_SET);184s = Collections.synchronizedSet(Collections.EMPTY_SET);185l = Collections.synchronizedList(Collections.EMPTY_LIST);186m = Collections.synchronizedMap(Collections.EMPTY_MAP);187ss = Collections.synchronizedSortedSet(new TreeSet());188sm = Collections.synchronizedSortedMap(new TreeMap());189}190}191192193