1 /* 2 * Copyright (c) 2005, 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 it is new version of old test which was 26 * /src/share/test/serialization/piotest.java 27 * Test of serialization/deserialization of 28 * objects with BinaryTree types 29 */ 30 31 import java.io.*; 32 33 public class BinaryTree { main(String argv[])34 public static void main (String argv[]) { 35 System.err.println("\nRegression test for testing of " + 36 "serialization/deserialization of objects " + 37 "with BinaryTree types \n"); 38 39 try { 40 BinaryTreeTest base = new BinaryTreeTest(2); 41 FileOutputStream ostream = new FileOutputStream("piotest3.tmp"); 42 try { 43 ObjectOutputStream p = new ObjectOutputStream(ostream); 44 p.writeObject(null); 45 p.writeObject(base); 46 p.flush(); 47 } finally { 48 ostream.close(); 49 } 50 51 FileInputStream istream = new FileInputStream("piotest3.tmp"); 52 try { 53 ObjectInputStream q = new ObjectInputStream(istream); 54 Object n = q.readObject(); 55 if (n != null) { 56 System.err.println("\nnull read as " + n); 57 } 58 BinaryTreeTest nbase = (BinaryTreeTest)q.readObject(); 59 if (!base.equals(nbase)) { 60 System.err.println("\nTEST FAILED: BinaryTree read " + 61 "incorrectly."); 62 throw new Error(); 63 } 64 } finally { 65 istream.close(); 66 } 67 68 System.err.println("\nTEST PASSED"); 69 } catch (Exception e) { 70 System.err.print("TEST FAILED: "); 71 e.printStackTrace(); 72 throw new Error(); 73 } 74 } 75 } 76 77 class BinaryTreeTest implements java.io.Serializable { 78 private static final long serialVersionUID = 1L; 79 80 public BinaryTreeTest left; 81 public BinaryTreeTest right; 82 public int id; 83 public int level; 84 85 private static int count = 0; 86 BinaryTreeTest(int l)87 public BinaryTreeTest(int l) { 88 id = count++; 89 level = l; 90 if (l > 0) { 91 left = new BinaryTreeTest(l-1); 92 right = new BinaryTreeTest(l-1); 93 } 94 } 95 print(int levels)96 public void print(int levels) { 97 for (int i = 0; i < level; i++) { 98 System.out.print(" "); 99 } 100 System.err.println("node " + id); 101 102 if (level <= levels && left != null) { 103 left.print(levels); 104 } 105 106 if (level <= levels && right != null) { 107 right.print(levels); 108 } 109 } 110 equals(BinaryTreeTest other)111 public boolean equals(BinaryTreeTest other) { 112 if (other == null) { 113 return false; 114 } 115 116 if (id != other.id) { 117 return false; 118 } 119 120 if (left != null && !left.equals(other.left)) { 121 return false; 122 } 123 124 if (right != null && !right.equals(other.right)) { 125 return false; 126 } 127 128 return true; 129 } 130 } 131