1 /* 2 * Copyright (c) 2005, 2010, 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 { 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 public BinaryTreeTest left; 79 public BinaryTreeTest right; 80 public int id; 81 public int level; 82 83 private static int count = 0; 84 85 public BinaryTreeTest(int l) { 86 id = count++; 87 level = l; 88 if (l > 0) { 89 left = new BinaryTreeTest(l-1); 90 right = new BinaryTreeTest(l-1); 91 } 92 } 93 94 public void print(int levels) { 95 for (int i = 0; i < level; i++) { 96 System.out.print(" "); 97 } 98 System.err.println("node " + id); 99 100 if (level <= levels && left != null) { 101 left.print(levels); 102 } 103 104 if (level <= levels && right != null) { 105 right.print(levels); 106 } 107 } 108 109 public boolean equals(BinaryTreeTest other) { 110 if (other == null) { 111 return false; 112 } 113 114 if (id != other.id) { 115 return false; 116 } 117 118 if (left != null && !left.equals(other.left)) { 119 return false; 120 } 121 122 if (right != null && !right.equals(other.right)) { 123 return false; 124 } 125 126 return true; 127 } 128 }