Path: blob/master/test/jdk/java/util/HashSet/Serialization.java
41149 views
/*1* Copyright (c) 2013, 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.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.io.ObjectInputStream;27import java.io.ObjectOutputStream;28import java.util.HashSet;29import java.util.Random;30import java.util.concurrent.ThreadLocalRandom;3132/*33* @test34* @bug 801625235* @summary Verify that a serialized HashSet may successfully be deserialized.36* @key randomness37*/38public class Serialization {3940private static final int NUM_SETS = 43;41private static final int MAX_CAPACITY = 257;42private static final float MAX_LOAD_FACTOR = 100.0F;4344private static final Random rnd = ThreadLocalRandom.current();4546private static HashSet<Integer> createHashSet() {47int capacity = rnd.nextInt(MAX_CAPACITY);48float loadFactor = Float.MIN_VALUE + rnd.nextFloat()*MAX_LOAD_FACTOR;49HashSet<Integer> hashSet = new HashSet<Integer>(capacity, loadFactor);50float multiplier = 2*rnd.nextFloat(); // range [0,2]51int size = (int)(capacity*loadFactor*multiplier);52for (int i = 0; i < size; i++) {53hashSet.add(rnd.nextInt());54}55return hashSet;56}5758private static HashSet<Integer> serDeser(HashSet<Integer> hashSet) throws IOException, ClassNotFoundException {59ByteArrayOutputStream baos = new ByteArrayOutputStream();60ObjectOutputStream oos = new ObjectOutputStream(baos);61oos.writeObject(hashSet);62oos.flush();6364ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());65ObjectInputStream ois = new ObjectInputStream(bais);66HashSet<Integer> result = (HashSet<Integer>)ois.readObject();6768oos.close();69ois.close();7071return result;72}7374private static void printHashSet(HashSet<Integer> hashSet) {75System.err.println("Size: "+hashSet.size());76for (Object o : hashSet) {77System.err.println(o);78}79}8081public static void main(String[] args) {82int failures = 0;8384for (int i = 0; i < NUM_SETS; i++) {85HashSet<Integer> hashSet = createHashSet();8687HashSet<Integer> result = null;88try {89result = serDeser(hashSet);90} catch (IOException ioe) {91System.err.println(ioe);92failures++;93} catch (ClassNotFoundException cnfe) {94System.err.println(cnfe);95failures++;96}9798if (!hashSet.equals(result)) {99System.err.println("Unequal HashSets!");100printHashSet(hashSet);101System.err.println();102failures++;103}104}105106if (failures != 0) {107throw new RuntimeException("HashSet/Serialzation failed with "+108failures+" failures!");109}110}111}112113114