5051N/A/*
5051N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5051N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5051N/A *
5051N/A * This code is free software; you can redistribute it and/or modify it
5051N/A * under the terms of the GNU General Public License version 2 only, as
5051N/A * published by the Free Software Foundation.
5051N/A *
5051N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5051N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5051N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5051N/A * version 2 for more details (a copy is included in the LICENSE file that
5051N/A * accompanied this code).
5051N/A *
5051N/A * You should have received a copy of the GNU General Public License version
5051N/A * 2 along with this work; if not, write to the Free Software Foundation,
5051N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5051N/A *
5051N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5051N/A * or visit www.oracle.com if you need additional information or have any
5051N/A * questions.
5051N/A */
5051N/A
5051N/A/**
5051N/A * @test
5051N/A * @bug 7173432
5051N/A * @summary If the key to be inserted into a HashMap is null and the table
5051N/A * needs to be resized as part of the insertion then addEntry will try to
5051N/A * recalculate the hash of a null key. This will fail with an NPE.
5051N/A */
5051N/A
5051N/Aimport java.util.*;
5051N/A
5051N/Apublic class NullKeyAtResize {
5051N/A public static void main(String[] args) throws Exception {
5051N/A List<Object> old_order = new ArrayList<>();
5051N/A Map<Object,Object> m = new HashMap<>(16);
5051N/A int number = 0;
5051N/A while(number < 100000) {
5051N/A m.put(null,null); // try to put in null. This may cause resize.
5051N/A m.remove(null); // remove it.
5051N/A Integer adding = (number += 100);
5051N/A m.put(adding, null); // try to put in a number. This wont cause resize.
5051N/A List<Object> new_order = new ArrayList<>();
5051N/A new_order.addAll(m.keySet());
5051N/A new_order.remove(adding);
5051N/A if(!old_order.equals(new_order)) {
5051N/A // we resized and didn't crash.
5051N/A System.out.println("Encountered resize after " + (number / 100) + " iterations");
5051N/A break;
5051N/A }
5051N/A // remember this order for the next time around.
5051N/A old_order.clear();
5051N/A old_order.addAll(m.keySet());
5051N/A }
5051N/A if(number == 100000) {
5051N/A throw new Error("Resize never occurred");
5051N/A }
5051N/A }
5051N/A}