|
| 1 | +import java.io.ByteArrayOutputStream; |
| 2 | +import java.io.FileOutputStream; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.ObjectInputStream; |
| 5 | +import java.io.ObjectOutputStream; |
| 6 | +import java.io.Serializable; |
| 7 | +import java.util.Hashtable; |
| 8 | +import java.util.Vector; |
| 9 | + |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public class OneTest { |
| 14 | + |
| 15 | + ObjectOutputStream oos; |
| 16 | + ByteArrayOutputStream bao; |
| 17 | + FileOutputStream fos; |
| 18 | + static int counter; |
| 19 | + |
| 20 | + public class SerializableTestHelper implements Serializable { |
| 21 | + |
| 22 | + public String aField1; |
| 23 | + |
| 24 | + public String aField2; |
| 25 | + |
| 26 | + SerializableTestHelper() { |
| 27 | + aField1 = null; |
| 28 | + aField2 = null; |
| 29 | + } |
| 30 | + |
| 31 | + SerializableTestHelper(String s, String t) { |
| 32 | + aField1 = s; |
| 33 | + aField2 = t; |
| 34 | + } |
| 35 | + |
| 36 | + private void readObject(ObjectInputStream ois) throws Exception { |
| 37 | + // note aField2 is not read |
| 38 | + ObjectInputStream.GetField fields = ois.readFields(); |
| 39 | + aField1 = (String) fields.get("aField1", "Zap"); |
| 40 | + } |
| 41 | + |
| 42 | + private void writeObject(ObjectOutputStream oos) throws IOException { |
| 43 | + // note aField2 is not written |
| 44 | + ObjectOutputStream.PutField fields = oos.putFields(); |
| 45 | + fields.put("aField1", aField1); |
| 46 | + oos.writeFields(); |
| 47 | + } |
| 48 | + |
| 49 | + public String getText1() { |
| 50 | + return aField1; |
| 51 | + } |
| 52 | + |
| 53 | + public void setText1(String s) { |
| 54 | + aField1 = s; |
| 55 | + } |
| 56 | + |
| 57 | + public String getText2() { |
| 58 | + return aField2; |
| 59 | + } |
| 60 | + |
| 61 | + public void setText2(String s) { |
| 62 | + aField2 = s; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static class A1 implements Serializable { |
| 67 | + private static final long serialVersionUID = 5942584913446079661L; |
| 68 | + B1 b1 = new B1(); |
| 69 | + B1 b2 = b1; |
| 70 | + Vector v = new Vector(); |
| 71 | + } |
| 72 | + |
| 73 | + public static class B1 implements Serializable { |
| 74 | + int i = 5; |
| 75 | + Hashtable h = new Hashtable(); |
| 76 | + } |
| 77 | + |
| 78 | + @Before |
| 79 | + public void setUp() throws Exception { |
| 80 | + oos = new ObjectOutputStream(fos = new FileOutputStream("obj" + counter++ |
| 81 | + + ".ser")); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testDouble() throws IOException { |
| 86 | + oos.writeDouble(Double.MAX_VALUE); |
| 87 | + oos.close(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testBytes() throws IOException { |
| 92 | + oos.writeBytes("HelloWorld"); |
| 93 | + oos.close(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testBoolean() throws IOException { |
| 98 | + oos.writeBoolean(false); |
| 99 | + oos.close(); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testByte() throws IOException { |
| 104 | + oos.writeByte(127); |
| 105 | + oos.close(); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testChar() throws IOException { |
| 110 | + oos.writeChar('C'); |
| 111 | + oos.close(); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void test_readFields() throws Exception { |
| 116 | + oos.writeObject(new SerializableTestHelper("Gabba", "Jabba")); |
| 117 | + oos.flush(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testClass() throws Exception { |
| 122 | + oos.writeObject(String.class); |
| 123 | + oos.flush(); |
| 124 | + } |
| 125 | + |
| 126 | +// public void test_readObject() throws Exception { |
| 127 | +// String s = "HelloWorld"; |
| 128 | +// oos.writeObject(s); |
| 129 | +// oos.close(); |
| 130 | +// ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); |
| 131 | +// assertEquals("Read incorrect Object value", s, ois.readObject()); |
| 132 | +// ois.close(); |
| 133 | +// |
| 134 | +// // Regression for HARMONY-91 |
| 135 | +// // dynamically create serialization byte array for the next hierarchy: |
| 136 | +// // - class A implements Serializable |
| 137 | +// // - class C extends A |
| 138 | +// |
| 139 | +// byte[] cName = C.class.getName().getBytes("UTF-8"); |
| 140 | +// byte[] aName = A.class.getName().getBytes("UTF-8"); |
| 141 | +// |
| 142 | +// ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 143 | +// |
| 144 | +// byte[] begStream = new byte[] { (byte) 0xac, (byte) 0xed, // STREAM_MAGIC |
| 145 | +// (byte) 0x00, (byte) 0x05, // STREAM_VERSION |
| 146 | +// (byte) 0x73, // TC_OBJECT |
| 147 | +// (byte) 0x72, // TC_CLASSDESC |
| 148 | +// (byte) 0x00, // only first byte for C class name length |
| 149 | +// }; |
| 150 | +// |
| 151 | +// out.write(begStream, 0, begStream.length); |
| 152 | +// out.write(cName.length); // second byte for C class name length |
| 153 | +// out.write(cName, 0, cName.length); // C class name |
| 154 | +// |
| 155 | +// byte[] midStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 156 | +// (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 157 | +// (byte) 0x21, // serialVersionUID = 33L |
| 158 | +// (byte) 0x02, // flags |
| 159 | +// (byte) 0x00, (byte) 0x00, // fields : none |
| 160 | +// (byte) 0x78, // TC_ENDBLOCKDATA |
| 161 | +// (byte) 0x72, // Super class for C: TC_CLASSDESC for A class |
| 162 | +// (byte) 0x00, // only first byte for A class name length |
| 163 | +// }; |
| 164 | +// |
| 165 | +// out.write(midStream, 0, midStream.length); |
| 166 | +// out.write(aName.length); // second byte for A class name length |
| 167 | +// out.write(aName, 0, aName.length); // A class name |
| 168 | +// |
| 169 | +// byte[] endStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 170 | +// (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 171 | +// (byte) 0x0b, // serialVersionUID = 11L |
| 172 | +// (byte) 0x02, // flags |
| 173 | +// (byte) 0x00, (byte) 0x01, // fields |
| 174 | +// |
| 175 | +// (byte) 0x4c, // field description: type L (object) |
| 176 | +// (byte) 0x00, (byte) 0x04, // length |
| 177 | +// // field = 'name' |
| 178 | +// (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, |
| 179 | +// |
| 180 | +// (byte) 0x74, // className1: TC_STRING |
| 181 | +// (byte) 0x00, (byte) 0x12, // length |
| 182 | +// // |
| 183 | +// (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76, |
| 184 | +// (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61, |
| 185 | +// (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53, |
| 186 | +// (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e, |
| 187 | +// (byte) 0x67, (byte) 0x3b, |
| 188 | +// |
| 189 | +// (byte) 0x78, // TC_ENDBLOCKDATA |
| 190 | +// (byte) 0x70, // NULL super class for A class |
| 191 | +// |
| 192 | +// // classdata |
| 193 | +// (byte) 0x74, // TC_STRING |
| 194 | +// (byte) 0x00, (byte) 0x04, // length |
| 195 | +// (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, // value |
| 196 | +// }; |
| 197 | +// |
| 198 | +// out.write(endStream, 0, endStream.length); |
| 199 | +// out.flush(); |
| 200 | +// |
| 201 | +// // read created serial. form |
| 202 | +// ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( |
| 203 | +// out.toByteArray())); |
| 204 | +// Object o = ois.readObject(); |
| 205 | +// assertEquals(C.class, o.getClass()); |
| 206 | +// |
| 207 | +// // Regression for HARMONY-846 |
| 208 | +// assertNull(new ObjectInputStream() {}.readObject()); |
| 209 | +// } |
| 210 | + |
| 211 | + |
| 212 | +} |
0 commit comments