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  * @key randomness
38  */
39 public class Serialization {
40 
41     private static final int NUM_SETS = 43;
42     private static final int MAX_CAPACITY = 257;
43     private static final float MAX_LOAD_FACTOR = 100.0F;
44 
45     private static final Random rnd = ThreadLocalRandom.current();
46 
createHashSet()47     private static HashSet<Integer> createHashSet() {
48         int capacity = rnd.nextInt(MAX_CAPACITY);
49         float loadFactor = Float.MIN_VALUE + rnd.nextFloat()*MAX_LOAD_FACTOR;
50         HashSet<Integer> hashSet = new HashSet<Integer>(capacity, loadFactor);
51         float multiplier = 2*rnd.nextFloat(); // range [0,2]
52         int size = (int)(capacity*loadFactor*multiplier);
53         for (int i = 0; i < size; i++) {
54             hashSet.add(rnd.nextInt());
55         }
56         return hashSet;
57     }
58 
serDeser(HashSet<Integer> hashSet)59     private static HashSet<Integer> serDeser(HashSet<Integer> hashSet) throws IOException, ClassNotFoundException {
60         ByteArrayOutputStream baos = new ByteArrayOutputStream();
61         ObjectOutputStream oos = new ObjectOutputStream(baos);
62         oos.writeObject(hashSet);
63         oos.flush();
64 
65         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
66         ObjectInputStream ois = new ObjectInputStream(bais);
67         HashSet<Integer> result = (HashSet<Integer>)ois.readObject();
68 
69         oos.close();
70         ois.close();
71 
72         return result;
73     }
74 
printHashSet(HashSet<Integer> hashSet)75     private static void printHashSet(HashSet<Integer> hashSet) {
76         System.err.println("Size: "+hashSet.size());
77         for (Object o : hashSet) {
78             System.err.println(o);
79         }
80     }
81 
main(String[] args)82     public static void main(String[] args) {
83         int failures = 0;
84 
85         for (int i = 0; i < NUM_SETS; i++) {
86             HashSet<Integer> hashSet = createHashSet();
87 
88             HashSet<Integer> result = null;
89             try {
90                 result = serDeser(hashSet);
91             } catch (IOException ioe) {
92                 System.err.println(ioe);
93                 failures++;
94             } catch (ClassNotFoundException cnfe) {
95                 System.err.println(cnfe);
96                 failures++;
97             }
98 
99             if (!hashSet.equals(result)) {
100                 System.err.println("Unequal HashSets!");
101                 printHashSet(hashSet);
102                 System.err.println();
103                 failures++;
104             }
105         }
106 
107         if (failures != 0) {
108             throw new RuntimeException("HashSet/Serialzation failed with "+
109                     failures+" failures!");
110         }
111     }
112 }
113