J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | FileOutputStream ostream = new FileOutputStream("piotest3.tmp"); |
| 41 | ObjectOutputStream p = new ObjectOutputStream(ostream); |
| 42 | |
| 43 | BinaryTreeTest base = new BinaryTreeTest(2); |
| 44 | p.writeObject(null); |
| 45 | p.writeObject(base); |
| 46 | p.flush(); |
| 47 | ostream.close(); |
| 48 | |
| 49 | FileInputStream istream = new FileInputStream("piotest3.tmp"); |
| 50 | ObjectInputStream q = new ObjectInputStream(istream); |
| 51 | |
| 52 | Object n = q.readObject(); |
| 53 | if (n != null) { |
| 54 | System.err.println("\nnull read as " + n); |
| 55 | } |
| 56 | BinaryTreeTest nbase = (BinaryTreeTest)q.readObject(); |
| 57 | if (!base.equals(nbase)) { |
| 58 | System.err.println("\nTEST FAILED: BinaryTree read " + |
| 59 | "incorrectly."); |
| 60 | throw new Error(); |
| 61 | } |
| 62 | System.err.println("\nTEST PASSED"); |
| 63 | } catch (Exception e) { |
| 64 | System.err.print("TEST FAILED: "); |
| 65 | e.printStackTrace(); |
| 66 | throw new Error(); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | class BinaryTreeTest implements java.io.Serializable { |
| 72 | public BinaryTreeTest left; |
| 73 | public BinaryTreeTest right; |
| 74 | public int id; |
| 75 | public int level; |
| 76 | |
| 77 | private static int count = 0; |
| 78 | |
| 79 | public BinaryTreeTest(int l) { |
| 80 | id = count++; |
| 81 | level = l; |
| 82 | if (l > 0) { |
| 83 | left = new BinaryTreeTest(l-1); |
| 84 | right = new BinaryTreeTest(l-1); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public void print(int levels) { |
| 89 | for (int i = 0; i < level; i++) { |
| 90 | System.out.print(" "); |
| 91 | } |
| 92 | System.err.println("node " + id); |
| 93 | |
| 94 | if (level <= levels && left != null) { |
| 95 | left.print(levels); |
| 96 | } |
| 97 | |
| 98 | if (level <= levels && right != null) { |
| 99 | right.print(levels); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public boolean equals(BinaryTreeTest other) { |
| 104 | if (other == null) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | if (id != other.id) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | if (left != null && !left.equals(other.left)) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if (right != null && !right.equals(other.right)) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | } |