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