Path: blob/master/test/jdk/java/io/Serializable/oldTests/BinaryTree.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/piotest.java26* Test of serialization/deserialization of27* objects with BinaryTree types28*/2930import java.io.*;3132public class BinaryTree {33public static void main (String argv[]) {34System.err.println("\nRegression test for testing of " +35"serialization/deserialization of objects " +36"with BinaryTree types \n");3738try {39BinaryTreeTest base = new BinaryTreeTest(2);40FileOutputStream ostream = new FileOutputStream("piotest3.tmp");41try {42ObjectOutputStream p = new ObjectOutputStream(ostream);43p.writeObject(null);44p.writeObject(base);45p.flush();46} finally {47ostream.close();48}4950FileInputStream istream = new FileInputStream("piotest3.tmp");51try {52ObjectInputStream q = new ObjectInputStream(istream);53Object n = q.readObject();54if (n != null) {55System.err.println("\nnull read as " + n);56}57BinaryTreeTest nbase = (BinaryTreeTest)q.readObject();58if (!base.equals(nbase)) {59System.err.println("\nTEST FAILED: BinaryTree read " +60"incorrectly.");61throw new Error();62}63} finally {64istream.close();65}6667System.err.println("\nTEST PASSED");68} catch (Exception e) {69System.err.print("TEST FAILED: ");70e.printStackTrace();71throw new Error();72}73}74}7576class BinaryTreeTest implements java.io.Serializable {77private static final long serialVersionUID = 1L;7879public BinaryTreeTest left;80public BinaryTreeTest right;81public int id;82public int level;8384private static int count = 0;8586public BinaryTreeTest(int l) {87id = count++;88level = l;89if (l > 0) {90left = new BinaryTreeTest(l-1);91right = new BinaryTreeTest(l-1);92}93}9495public void print(int levels) {96for (int i = 0; i < level; i++) {97System.out.print(" ");98}99System.err.println("node " + id);100101if (level <= levels && left != null) {102left.print(levels);103}104105if (level <= levels && right != null) {106right.print(levels);107}108}109110public boolean equals(BinaryTreeTest other) {111if (other == null) {112return false;113}114115if (id != other.id) {116return false;117}118119if (left != null && !left.equals(other.left)) {120return false;121}122123if (right != null && !right.equals(other.right)) {124return false;125}126127return true;128}129}130131132