jdk/test/java/util/HashSet/Serialization.java
changeset 20750 330bf22c39fa
child 30046 cf2c86e1819e
equal deleted inserted replaced
20749:7891146ae141 20750:330bf22c39fa
       
     1 /*
       
     2  * Copyright (c) 2013, 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 import java.io.ByteArrayInputStream;
       
    25 import java.io.ByteArrayOutputStream;
       
    26 import java.io.IOException;
       
    27 import java.io.ObjectInputStream;
       
    28 import java.io.ObjectOutputStream;
       
    29 import java.util.HashSet;
       
    30 import java.util.Random;
       
    31 import java.util.concurrent.ThreadLocalRandom;
       
    32 
       
    33 /*
       
    34  * @test
       
    35  * @bug 8016252
       
    36  * @summary Verify that a serialized HashSet may successfully be deserialized.
       
    37  */
       
    38 public class Serialization {
       
    39 
       
    40     private static final int NUM_SETS = 43;
       
    41     private static final int MAX_CAPACITY = 257;
       
    42     private static final float MAX_LOAD_FACTOR = 100.0F;
       
    43 
       
    44     private static final Random rnd = ThreadLocalRandom.current();
       
    45 
       
    46     private static HashSet<Integer> createHashSet() {
       
    47         int capacity = rnd.nextInt(MAX_CAPACITY);
       
    48         float loadFactor = Float.MIN_VALUE + rnd.nextFloat()*MAX_LOAD_FACTOR;
       
    49         HashSet<Integer> hashSet = new HashSet<Integer>(capacity, loadFactor);
       
    50         float multiplier = 2*rnd.nextFloat(); // range [0,2]
       
    51         int size = (int)(capacity*loadFactor*multiplier);
       
    52         for (int i = 0; i < size; i++) {
       
    53             hashSet.add(rnd.nextInt());
       
    54         }
       
    55         return hashSet;
       
    56     }
       
    57 
       
    58     private static HashSet<Integer> serDeser(HashSet<Integer> hashSet) throws IOException, ClassNotFoundException {
       
    59         ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
    60         ObjectOutputStream oos = new ObjectOutputStream(baos);
       
    61         oos.writeObject(hashSet);
       
    62         oos.flush();
       
    63 
       
    64         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
       
    65         ObjectInputStream ois = new ObjectInputStream(bais);
       
    66         HashSet<Integer> result = (HashSet<Integer>)ois.readObject();
       
    67 
       
    68         oos.close();
       
    69         ois.close();
       
    70 
       
    71         return result;
       
    72     }
       
    73 
       
    74     private static void printHashSet(HashSet<Integer> hashSet) {
       
    75         System.err.println("Size: "+hashSet.size());
       
    76         for (Object o : hashSet) {
       
    77             System.err.println(o);
       
    78         }
       
    79     }
       
    80 
       
    81     public static void main(String[] args) {
       
    82         int failures = 0;
       
    83 
       
    84         for (int i = 0; i < NUM_SETS; i++) {
       
    85             HashSet<Integer> hashSet = createHashSet();
       
    86 
       
    87             HashSet<Integer> result = null;
       
    88             try {
       
    89                 result = serDeser(hashSet);
       
    90             } catch (IOException ioe) {
       
    91                 System.err.println(ioe);
       
    92                 failures++;
       
    93             } catch (ClassNotFoundException cnfe) {
       
    94                 System.err.println(cnfe);
       
    95                 failures++;
       
    96             }
       
    97 
       
    98             if (!hashSet.equals(result)) {
       
    99                 System.err.println("Unequal HashSets!");
       
   100                 printHashSet(hashSet);
       
   101                 System.err.println();
       
   102                 failures++;
       
   103             }
       
   104         }
       
   105 
       
   106         if (failures != 0) {
       
   107             throw new RuntimeException("HashSet/Serialzation failed with "+
       
   108                     failures+" failures!");
       
   109         }
       
   110     }
       
   111 }