1 /* 2 * Copyright (c) 2006, 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 31 import java.util.*; 32 import java.util.concurrent.*; 33 import java.util.concurrent.atomic.*; 34 import java.lang.reflect.*; 35 36 public class cr5045147 { 37 38 static void realMain(String[] args) throws Throwable { 39 // No comparator 40 Map<String,String> comparable = new TreeMap<>(); 41 42 // insert null into empty map (504517 failure) 43 try { 44 comparable.put(null, "anything"); 45 fail("null shouldn't be accepted"); 46 } catch(NullPointerException failed) { 47 pass(); 48 } 49 50 // insert non-null into empty map 51 try { 52 comparable.put("test", "anything"); 53 pass(); 54 } catch(NullPointerException failed) { 55 fail(); 56 } 57 58 // insert null into non-empty map 59 try { 60 comparable.put(null, "anything"); 61 fail("null shouldn't be accepted"); 62 } catch(NullPointerException failed) { 63 pass(); 64 } 65 66 // Comparator (String.CASE_INSENSITIVE_ORDER). Intentionally a raw type. 67 Map comparator = new TreeMap(String.CASE_INSENSITIVE_ORDER); 68 69 // insert null into empty map (504517 failure) 70 try { 71 comparator.put(null, "anything"); 72 fail("null shouldn't be accepted"); 73 } catch(NullPointerException failed) { 74 pass(); 75 } 76 77 // insert non-null into empty map 78 try { 79 comparator.put("test", "anything"); 80 pass(); 81 } catch(NullPointerException failed) { 82 fail(); 83 } 84 85 // insert null into non-empty map 86 try { 87 comparator.put(null, "anything"); 88 fail("null shouldn't be accepted"); 89 } catch(NullPointerException failed) { 90 pass(); 91 } 92 93 comparator.clear(); 94 95 // insert non-String into empty map (504517 failure) 96 try { 97 comparator.put(new Object(), "anything"); 98 fail("Object shouldn't be accepted"); 99 } catch(ClassCastException failed) { 100 pass(); 101 } 102 103 } 104 105 //--------------------- Infrastructure --------------------------- 106 static volatile int passed = 0, failed = 0; 107 static void pass() {passed++;} 108 static void fail() {failed++; Thread.dumpStack();} 109 static void fail(String msg) {System.out.println(msg); fail();} 110 static void unexpected(Throwable t) {failed++; t.printStackTrace();} 111 static void check(boolean cond) {if (cond) pass(); else fail();} 112 static void equal(Object x, Object y) { 113 if (x == null ? y == null : x.equals(y)) pass(); 114 else fail(x + " not equal to " + y);} 115 public static void main(String[] args) throws Throwable { 116 try {realMain(args);} catch (Throwable t) {unexpected(t);} 117 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 118 if (failed > 0) throw new AssertionError("Some tests failed");} 119 }