Path: blob/master/test/jdk/java/io/Serializable/oldTests/CheckingEquality.java
41153 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary it is new version of old test which was25* /src/share/test/serialization/psiotest.java26* Test pickling and unpickling an object with derived classes27* and using a read special to serialize the "middle" class.28*29*/3031import java.io.*;3233public class CheckingEquality {34public static void main (String argv[]) {35System.err.println("\nRegression test of " +36"serialization/deserialization of " +37"complex objects\n");3839FileInputStream istream = null;40try {4142Thirdpsio objout = new Thirdpsio();43objout.init();4445FileOutputStream ostream = new FileOutputStream("psiotest1.tmp");46ObjectOutputStream p = new ObjectOutputStream(ostream);4748p.writeObject(objout);4950p.flush();51ostream.close();5253istream = new FileInputStream("psiotest1.tmp");54ObjectInputStream q = new ObjectInputStream(istream);5556Thirdpsio objin = (Thirdpsio)q.readObject();5758if (!objout.equals(objin)) {59System.err.println(60"\nTEST FAILED: Original and read objects not equal.");61throw new Error();62}63istream.close();64System.err.println("\nTEST PASSED");65} catch (Exception e) {66System.err.print("TEST FAILED: ");67e.printStackTrace();6869System.err.println("\nInput remaining");70int ch;71try {72while ((ch = istream.read()) != -1) {73System.out.print(Integer.toString(ch, 16) + " ");74}75System.err.println("\n");76} catch (Exception f) {77throw new Error();78}79throw new Error();80}81}82}8384class Firstpsio implements java.io.Serializable {85private static final long serialVersionUID = 1L;8687String one;88int two;89float three[];9091void init() { /* called only before writing */92one = "one";93two = 2;94three = new float[5];95float f = 3.0f;96for (int i=0; i<5 ; i++) {97f += 0.11;98three[i] = f;99}100}101102/* Compare two first objects */103boolean equals(Firstpsio other) {104boolean ret = true;105106if (!one.equals(other.one)) {107System.err.println("\nfirstpsio: expected " + one +108" actual " + other.one);109ret = false;110}111if (two != other.two) {112System.err.println("\nfirstpsio: expected " + two +113" actual " + other.two);114ret = false;115}116117for (int i = 0; i < three.length; i++ ) {118if (three[i] != other.three[i]) {119System.err.println("\nfirstpsio: three[" + i + "] expected " +120three[i] + " actual " + other.three[i]);121ret = false;122}123}124return ret;125}126}127128class Secondpsio extends Firstpsio {129private static final long serialVersionUID = 1L;130131String quatre;132int cinq;133134private void writeObject(ObjectOutputStream pw) throws IOException {135pw.writeObject(quatre);136pw.writeInt(cinq);137}138139private void readObject(ObjectInputStream pr)140throws StreamCorruptedException, IOException, ClassNotFoundException141{142if (one == null) {143System.err.println(144"\nTEST FAILED: Superclass not serialized when " +145"it should have been");146throw new StreamCorruptedException("Superclass not serialized " +147"when it should have been");148}149150quatre = (String)pr.readObject();151cinq = pr.readInt();152}153154boolean equals(Secondpsio other) {155boolean ret = super.equals(other);156157if (!quatre.equals(other.quatre)) {158System.err.println("\nsecondpsio: quatre expected " + quatre +159" actual " + other.quatre);160ret = false;161}162if (cinq != other.cinq) {163System.err.println("\nsecondpsio: cinq expected " + cinq +164" actual " + other.cinq);165ret = false;166}167return ret;168}169170/* called only before writing */171void init() {172quatre = "4444";173cinq = 5;174super.init();175}176}177178class Thirdpsio extends Secondpsio {179private static final long serialVersionUID = 1L;180181static String ign = "ignored";182transient Object oh;183184int six;185186private static int seven[];187protected byte eight = (byte)9;188static final byte dcare = (byte) 128;189private short nine = 8888;190long ten;191@SuppressWarnings("serial") /* Incorrect declarations are being tested */192java.util.Enumeration<?> zero;193194195boolean equals(Thirdpsio other) {196boolean ret = super.equals(other);197198if (six != other.six) {199System.err.println("\nthirdpsio six " + six +200" actual " + other.six);201ret = false;202}203if (eight != other.eight) {204System.err.println("\nthirdpsio eight - expected " + eight +205" actual " + other.eight);206ret = false;207}208if (nine != other.nine) {209System.err.println("\nthirdpsio nine - expected " + nine +210" actual " + other.nine);211ret = false;212}213if (ten != other.ten) {214System.err.println("\nthirdpsio ten - expected " + ten +215" actual " + other.ten);216ret = false;217}218if (zero != other.zero) {219System.err.println("\nthirdpsio zero - expected " + zero +220" actual " + other.zero);221ret = false;222}223return ret;224}225226/* called only before writing */227void init() {228six = 666;229230int s7[] = { 7, 7, 7 };231seven = s7;232eight = (byte)8;233nine = (short)9;234ten = (long)100000;235java.util.Enumeration<?> em = null; /* default */236237super.init();238}239}240241242