Path: blob/master/test/jdk/java/util/Hashtable/DeserializedLength.java
41149 views
/*1* Copyright (c) 2014, 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*/2223import java.io.*;24import java.lang.reflect.Field;25import java.util.Hashtable;2627/**28* @test29* @bug 806842730* @summary Hashtable deserialization reconstitutes table with wrong capacity31* @modules java.base/java.util:open32*/33public class DeserializedLength {3435static boolean testDeserializedLength(int elements, float loadFactor) throws Exception {3637// construct Hashtable with minimal initial capacity and given loadFactor38Hashtable<Integer, Integer> ht1 = new Hashtable<>(1, loadFactor);3940// add given number of unique elements41for (int i = 0; i < elements; i++) {42ht1.put(i, i);43}4445// serialize and deserialize into a deep clone46Hashtable<Integer, Integer> ht2 = serialClone(ht1);4748// compare lengths of internal tables49Object[] table1 = (Object[]) hashtableTableField.get(ht1);50Object[] table2 = (Object[]) hashtableTableField.get(ht2);51assert table1 != null;52assert table2 != null;5354int minLength = (int) (ht1.size() / loadFactor) + 1;55int maxLength = minLength * 2;5657boolean ok = (table2.length >= minLength && table2.length <= maxLength);5859System.out.printf(60"%7d %5.2f %7d %7d %7d...%7d %s\n",61ht1.size(), loadFactor,62table1.length, table2.length,63minLength, maxLength,64(ok ? "OK" : "NOT-OK")65);6667return ok;68}6970static <T> T serialClone(T o) throws IOException, ClassNotFoundException {71ByteArrayOutputStream bos = new ByteArrayOutputStream();72try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {73oos.writeObject(o);74}75@SuppressWarnings("unchecked")76T clone = (T) new ObjectInputStream(77new ByteArrayInputStream(bos.toByteArray())).readObject();78return clone;79}8081private static final Field hashtableTableField;8283static {84try {85hashtableTableField = Hashtable.class.getDeclaredField("table");86hashtableTableField.setAccessible(true);87} catch (NoSuchFieldException e) {88throw new Error(e);89}90}9192public static void main(String[] args) throws Exception {93boolean ok = true;9495System.out.printf("Results:\n" +96" ser. deser.\n" +97" size load lentgh length valid range ok?\n" +98"------- ----- ------- ------- ----------------- ------\n"99);100101for (int elements : new int[]{10, 50, 500, 5000}) {102for (float loadFactor : new float[]{0.15f, 0.5f, 0.75f, 1.0f, 2.5f}) {103ok &= testDeserializedLength(elements, loadFactor);104}105}106if (!ok) {107throw new AssertionError("Test failed.");108}109}110}111112113