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