1/*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 5045147
27 * @summary Test handling of null with empty Map
28 * @author Mike Duigou
29 */
30
31import java.util.*;
32import java.util.concurrent.*;
33import java.util.concurrent.atomic.*;
34import java.lang.reflect.*;
35
36public class EmptyMapAndNulls {
37
38    @SuppressWarnings("rawtypes")
39    static void realMain(String[] args) throws Throwable {
40        // No comparator
41        Map<String,String> comparable = new TreeMap<>();
42
43        // insert null into empty map (5045147 failure)
44        try {
45            comparable.put(null, "anything");
46            fail("null shouldn't be accepted");
47        } catch (NullPointerException failed) {
48            pass();
49        }
50
51        // insert non-null into empty map
52        try {
53            comparable.put("test", "anything");
54            pass();
55        } catch (NullPointerException failed) {
56            fail();
57        }
58
59        // insert null into non-empty map
60        try {
61            comparable.put(null, "anything");
62            fail("null shouldn't be accepted");
63        } catch (NullPointerException failed) {
64            pass();
65        }
66
67        // Comparator (String.CASE_INSENSITIVE_ORDER). Intentionally a raw type.
68        Map comparator = new TreeMap(String.CASE_INSENSITIVE_ORDER);
69
70        // insert null into empty map (5045147 failure)
71        try {
72            comparator.put(null, "anything");
73            fail("null shouldn't be accepted");
74        } catch (NullPointerException failed) {
75            pass();
76        }
77
78        // insert non-null into empty map
79        try {
80            comparator.put("test", "anything");
81            pass();
82        } catch (NullPointerException failed) {
83            fail();
84        }
85
86        // insert null into non-empty map
87        try {
88            comparator.put(null, "anything");
89            fail("null shouldn't be accepted");
90        } catch (NullPointerException failed) {
91            pass();
92        }
93
94        comparator.clear();
95
96        // insert non-String into empty map (5045147 failure)
97        try {
98            comparator.put(new Object(), "anything");
99            fail("Object shouldn't be accepted");
100        } catch (ClassCastException failed) {
101            pass();
102        }
103
104    }
105
106    //--------------------- Infrastructure ---------------------------
107    static volatile int passed = 0, failed = 0;
108    static void pass() {passed++;}
109    static void fail() {failed++; Thread.dumpStack();}
110    static void fail(String msg) {System.out.println(msg); fail();}
111    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
112    static void check(boolean cond) {if (cond) pass(); else fail();}
113    static void equal(Object x, Object y) {
114        if (x == null ? y == null : x.equals(y)) pass();
115        else fail(x + " not equal to " + y);}
116    public static void main(String[] args) throws Throwable {
117        try {realMain(args);} catch (Throwable t) {unexpected(t);}
118        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
119        if (failed > 0) throw new AssertionError("Some tests failed");}
120}
121