1 /* 2 * Copyright (c) 2001, 2019, 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 /* @test 25 * @summary Basic sanity check to test if default (de)serialization is 26 * transmitting values properly. 27 * @key randomness 28 */ 29 30 import java.io.*; 31 import java.util.*; 32 33 class Item implements Serializable { 34 private static final long serialVersionUID = 1L; 35 36 static final int ARRAYLEN = 1000; 37 static final int STRLEN = 1000; 38 static Random rand = new Random(System.currentTimeMillis()); 39 40 boolean z; 41 byte b; 42 char c; 43 short s; 44 int i; 45 float f; 46 long j; 47 double d; 48 49 boolean[] zary; 50 byte[] bary; 51 char[] cary; 52 short[] sary; 53 int[] iary; 54 float[] fary; 55 long[] jary; 56 double[] dary; 57 58 String str; 59 @SuppressWarnings("serial") /* Incorrect declarations are being tested */ 60 Object[] oary; 61 Item()62 Item() { 63 z = rand.nextBoolean(); 64 b = (byte) rand.nextInt(); 65 c = (char) rand.nextInt(); 66 s = (short) rand.nextInt(); 67 i = rand.nextInt(); 68 f = rand.nextFloat(); 69 j = rand.nextLong(); 70 d = rand.nextDouble(); 71 72 zary = new boolean[ARRAYLEN]; 73 bary = new byte[ARRAYLEN]; 74 cary = new char[ARRAYLEN]; 75 sary = new short[ARRAYLEN]; 76 iary = new int[ARRAYLEN]; 77 fary = new float[ARRAYLEN]; 78 jary = new long[ARRAYLEN]; 79 dary = new double[ARRAYLEN]; 80 oary = new Object[ARRAYLEN]; 81 82 for (int i = 0; i < ARRAYLEN; i++) { 83 zary[i] = rand.nextBoolean(); 84 bary[i] = (byte) rand.nextInt(); 85 cary[i] = (char) rand.nextInt(); 86 sary[i] = (short) rand.nextInt(); 87 iary[i] = rand.nextInt(); 88 fary[i] = rand.nextFloat(); 89 jary[i] = rand.nextLong(); 90 dary[i] = rand.nextDouble(); 91 oary[i] = rand.nextInt(); 92 } 93 94 char[] strChars = new char[STRLEN]; 95 for (int i = 0; i < STRLEN; i++) { 96 strChars[i] = (char) rand.nextInt(); 97 } 98 str = new String(strChars); 99 } 100 equals(Object obj)101 public boolean equals(Object obj) { 102 if (!(obj instanceof Item)) { 103 return false; 104 } 105 Item other = (Item) obj; 106 107 if ((z != other.z) || (b != other.b) || (c != other.c) || 108 (s != other.s) || (i != other.i) || (f != other.f) || 109 (j != other.j) || (d != other.d)) 110 { 111 return false; 112 } 113 114 for (int i = 0; i < ARRAYLEN; i++) { 115 if ((zary[i] != other.zary[i]) || (bary[i] != other.bary[i]) || 116 (cary[i] != other.cary[i]) || (sary[i] != other.sary[i]) || 117 (iary[i] != other.iary[i]) || (fary[i] != other.fary[i]) || 118 (jary[i] != other.jary[i]) || (dary[i] != other.dary[i]) || 119 !oary[i].equals(other.oary[i])) 120 { 121 return false; 122 } 123 } 124 125 if (!str.equals(other.str)) { 126 return false; 127 } 128 129 return true; 130 } 131 132 @Override hashCode()133 public int hashCode() { 134 return Objects.hash(i, j); 135 } 136 } 137 138 public class SanityCheck { main(String[] args)139 public static void main(String[] args) throws Exception { 140 for (int i = 0; i < 20; i++) { 141 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 142 ObjectOutputStream oout = new ObjectOutputStream(bout); 143 Item item = new Item(); 144 oout.writeObject(item); 145 oout.close(); 146 147 ObjectInputStream oin = new ObjectInputStream( 148 new ByteArrayInputStream(bout.toByteArray())); 149 Item itemcopy = (Item) oin.readObject(); 150 151 if (! item.equals(itemcopy)) { 152 throw new Error(); 153 } 154 } 155 } 156 } 157