3773N/A/*
3781N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3773N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3773N/A *
3773N/A * This code is free software; you can redistribute it and/or modify it
3773N/A * under the terms of the GNU General Public License version 2 only, as
3773N/A * published by the Free Software Foundation.
3773N/A *
3773N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3773N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3773N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3773N/A * version 2 for more details (a copy is included in the LICENSE file that
3773N/A * accompanied this code).
3773N/A *
3773N/A * You should have received a copy of the GNU General Public License version
3773N/A * 2 along with this work; if not, write to the Free Software Foundation,
3773N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3773N/A *
3773N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3773N/A * or visit www.oracle.com if you need additional information or have any
3773N/A * questions.
3773N/A */
3773N/A
3773N/A/*
3773N/A * @test
3773N/A * @bug 5045147
3773N/A * @summary Test handling of null with empty Map
3773N/A * @author Mike Duigou
3773N/A */
3773N/A
3773N/Aimport java.util.*;
3773N/Aimport java.util.concurrent.*;
3773N/Aimport java.util.concurrent.atomic.*;
3773N/Aimport java.lang.reflect.*;
3773N/A
3781N/Apublic class EmptyMapAndNulls {
3773N/A
3781N/A @SuppressWarnings("rawtypes")
3773N/A static void realMain(String[] args) throws Throwable {
3773N/A // No comparator
3773N/A Map<String,String> comparable = new TreeMap<>();
3773N/A
3781N/A // insert null into empty map (5045147 failure)
3773N/A try {
3773N/A comparable.put(null, "anything");
3773N/A fail("null shouldn't be accepted");
3781N/A } catch (NullPointerException failed) {
3773N/A pass();
3773N/A }
3773N/A
3773N/A // insert non-null into empty map
3773N/A try {
3773N/A comparable.put("test", "anything");
3773N/A pass();
3781N/A } catch (NullPointerException failed) {
3773N/A fail();
3773N/A }
3773N/A
3773N/A // insert null into non-empty map
3773N/A try {
3773N/A comparable.put(null, "anything");
3773N/A fail("null shouldn't be accepted");
3781N/A } catch (NullPointerException failed) {
3773N/A pass();
3773N/A }
3773N/A
3781N/A // Comparator (String.CASE_INSENSITIVE_ORDER). Intentionally a raw type.
3781N/A Map comparator = new TreeMap(String.CASE_INSENSITIVE_ORDER);
3773N/A
3781N/A // insert null into empty map (5045147 failure)
3773N/A try {
3773N/A comparator.put(null, "anything");
3773N/A fail("null shouldn't be accepted");
3781N/A } catch (NullPointerException failed) {
3773N/A pass();
3773N/A }
3773N/A
3773N/A // insert non-null into empty map
3773N/A try {
3773N/A comparator.put("test", "anything");
3773N/A pass();
3781N/A } catch (NullPointerException failed) {
3773N/A fail();
3773N/A }
3773N/A
3773N/A // insert null into non-empty map
3773N/A try {
3773N/A comparator.put(null, "anything");
3773N/A fail("null shouldn't be accepted");
3781N/A } catch (NullPointerException failed) {
3773N/A pass();
3773N/A }
3773N/A
3773N/A comparator.clear();
3773N/A
3781N/A // insert non-String into empty map (5045147 failure)
3773N/A try {
3773N/A comparator.put(new Object(), "anything");
3773N/A fail("Object shouldn't be accepted");
3781N/A } catch (ClassCastException failed) {
3773N/A pass();
3773N/A }
3773N/A
3773N/A }
3773N/A
3773N/A //--------------------- Infrastructure ---------------------------
3773N/A static volatile int passed = 0, failed = 0;
3773N/A static void pass() {passed++;}
3773N/A static void fail() {failed++; Thread.dumpStack();}
3773N/A static void fail(String msg) {System.out.println(msg); fail();}
3773N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
3773N/A static void check(boolean cond) {if (cond) pass(); else fail();}
3773N/A static void equal(Object x, Object y) {
3773N/A if (x == null ? y == null : x.equals(y)) pass();
3773N/A else fail(x + " not equal to " + y);}
3773N/A public static void main(String[] args) throws Throwable {
3773N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
3773N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
3773N/A if (failed > 0) throw new AssertionError("Some tests failed");}
3773N/A}