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