1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.io; 19 20import dalvik.annotation.AndroidOnly; 21import dalvik.annotation.BrokenTest; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetClass; 24import dalvik.annotation.TestTargetNew; 25 26import java.io.ByteArrayOutputStream; 27import java.io.DataInputStream; 28import java.io.IOException; 29import java.io.InputStream; 30import java.io.InvalidObjectException; 31import java.io.NotActiveException; 32import java.io.ObjectInputStream; 33import java.io.ObjectOutputStream; 34import java.io.ObjectStreamClass; 35import java.io.ObjectStreamException; 36import java.io.Serializable; 37import java.io.StreamCorruptedException; 38import java.io.WriteAbortedException; 39import java.util.Arrays; 40import java.util.Hashtable; 41import java.util.Vector; 42 43/** 44 * Automated Test Suite for class java.io.ObjectOutputStream 45 * 46 */ 47@TestTargetClass(Serializable.class) 48public class SerializationStressTest0 extends SerializationStressTest { 49 50 private static class ObjectInputStreamSubclass extends ObjectInputStream { 51 private Vector resolvedClasses = new Vector(); 52 53 public ObjectInputStreamSubclass(InputStream in) throws IOException, 54 StreamCorruptedException { 55 super(in); 56 } 57 58 public Class resolveClass(ObjectStreamClass osClass) 59 throws IOException, ClassNotFoundException { 60 Class result = super.resolveClass(osClass); 61 resolvedClasses.addElement(result); 62 return result; 63 } 64 65 public Class[] resolvedClasses() { 66 return (Class[]) resolvedClasses.toArray(new Class[resolvedClasses 67 .size()]); 68 } 69 } 70 71 @TestTargetNew( 72 level = TestLevel.COMPLETE, 73 notes = "Verifies serialization.", 74 method = "!Serialization", 75 args = {} 76 ) 77 public void test_1_Constructor() { 78 // Test for method java.io.ObjectOutputStream(java.io.OutputStream) 79 80 try { 81 oos.close(); 82 oos = new ObjectOutputStream(new ByteArrayOutputStream()); 83 oos.close(); 84 } catch (Exception e) { 85 fail("Failed to create ObjectOutputStream : " + e.getMessage()); 86 } 87 } 88 89 @TestTargetNew( 90 level = TestLevel.COMPLETE, 91 notes = "Verifies serialization.", 92 method = "!Serialization", 93 args = {} 94 ) 95 public void test_2_close() { 96 // Test for method void java.io.ObjectOutputStream.close() 97 try { 98 oos.close(); 99 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 100 oos.close(); 101 oos.writeChar('T'); 102 oos.writeObject(FOO); 103 // Writing to a closed stream does not cause problems. This is 104 // the expected behavior 105 } catch (IOException e) { 106 fail("Operation on closed stream threw IOException : " 107 + e.getMessage()); 108 } 109 } 110 111 @TestTargetNew( 112 level = TestLevel.COMPLETE, 113 notes = "Verifies serialization.", 114 method = "!Serialization", 115 args = {} 116 ) 117 public void test_3_defaultWriteObject() { 118 // Test for method void java.io.ObjectOutputStream.defaultWriteObject() 119 120 try { 121 oos.defaultWriteObject(); 122 } catch (NotActiveException e) { 123 // Correct 124 return; 125 } catch (IOException e) { 126 } 127 fail( 128 "Failed to throw NotActiveException when invoked outside readObject"); 129 } 130 131 @TestTargetNew( 132 level = TestLevel.COMPLETE, 133 notes = "Verifies serialization.", 134 method = "!Serialization", 135 args = {} 136 ) 137 public void test_4_flush() { 138 // Test for method void java.io.ObjectOutputStream.flush() 139 try { 140 oos.close(); 141 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 142 int size = bao.size(); 143 oos.writeByte(127); 144 assertTrue("Data flushed already", bao.size() == size); 145 oos.flush(); 146 assertTrue("Failed to flush data", bao.size() > size); 147 // we don't know how many bytes are actually written for 1 byte, 148 // so we test > <before> 149 oos.close(); 150 oos = null; 151 } catch (IOException e) { 152 fail("IOException serializing data : " + e.getMessage()); 153 } 154 } 155 156 @TestTargetNew( 157 level = TestLevel.COMPLETE, 158 notes = "Verifies serialization.", 159 method = "!Serialization", 160 args = {} 161 ) 162 public void test_5_reset() { 163 // Test for method void java.io.ObjectOutputStream.reset() 164 try { 165 String o = "HelloWorld"; 166 oos.writeObject(o); 167 oos.writeObject(o); 168 oos.reset(); 169 oos.writeObject(o); 170 ois = new ObjectInputStream(loadStream()); 171 ois.close(); 172 } catch (IOException e) { 173 fail("IOException serializing data : " + e.getMessage()); 174 } 175 } 176 177 @TestTargetNew( 178 level = TestLevel.COMPLETE, 179 notes = "Verifies serialization.", 180 method = "!Serialization", 181 args = {} 182 ) 183 public void test_6_write() { 184 // Test for method void java.io.ObjectOutputStream.write(byte [], int, 185 // int) 186 try { 187 byte[] buf = new byte[255]; 188 byte[] output = new byte[255]; 189 for (int i = 0; i < output.length; i++) 190 output[i] = (byte) i; 191 oos.write(output, 0, output.length); 192 oos.close(); 193 ois = new ObjectInputStream(loadStream()); 194 ois.readFully(buf); 195 ois.close(); 196 for (int i = 0; i < output.length; i++) 197 if (buf[i] != output[i]) 198 fail("Read incorrect byte: " + i); 199 } catch (IOException e) { 200 fail("IOException serializing data : " + e.getMessage()); 201 } 202 } 203 204 @TestTargetNew( 205 level = TestLevel.COMPLETE, 206 notes = "Verifies serialization.", 207 method = "!Serialization", 208 args = {} 209 ) 210 public void test_6a_write() { 211 // Test for method void java.io.ObjectOutputStream.write(byte [], int, 212 // int) 213 try { 214 byte[] buf = new byte[256]; 215 byte[] output = new byte[256]; 216 for (int i = 0; i < output.length; i++) 217 output[i] = (byte) (i & 0xff); 218 oos.write(output, 0, output.length); 219 oos.close(); 220 ois = new ObjectInputStream(loadStream()); 221 ois.readFully(buf); 222 ois.close(); 223 for (int i = 0; i < output.length; i++) 224 if (buf[i] != output[i]) 225 fail("Read incorrect byte: " + i); 226 } catch (IOException e) { 227 fail("IOException serializing data : " + e.getMessage()); 228 } 229 } 230 231 @TestTargetNew( 232 level = TestLevel.COMPLETE, 233 notes = "Verifies serialization.", 234 method = "!Serialization", 235 args = {} 236 ) 237 public void test_7_write() { 238 // Test for method void java.io.ObjectOutputStream.write(int) 239 try { 240 byte[] buf = new byte[10]; 241 oos.write('T'); 242 oos.close(); 243 ois = new ObjectInputStream(loadStream()); 244 assertEquals("Read incorrect byte", 'T', ois.read()); 245 ois.close(); 246 } catch (IOException e) { 247 fail("IOException serializing data : " + e.getMessage()); 248 } 249 } 250 251 @TestTargetNew( 252 level = TestLevel.COMPLETE, 253 notes = "Verifies serialization.", 254 method = "!Serialization", 255 args = {} 256 ) 257 public void test_8_write() { 258 // Test for method void java.io.ObjectOutputStream.write(byte []) 259 try { 260 byte[] buf = new byte[10]; 261 oos.write("HelloWorld".getBytes()); 262 oos.close(); 263 ois = new ObjectInputStream(loadStream()); 264 ois.read(buf, 0, 10); 265 ois.close(); 266 assertEquals("Read incorrect bytes", "HelloWorld", new String(buf, 0, 10) 267 ); 268 } catch (IOException e) { 269 fail("IOException serializing data : " + e.getMessage()); 270 } 271 } 272 273 @TestTargetNew( 274 level = TestLevel.COMPLETE, 275 notes = "Verifies serialization.", 276 method = "!Serialization", 277 args = {} 278 ) 279 public void test_9_writeBoolean() { 280 // Test for method void java.io.ObjectOutputStream.writeBoolean(boolean) 281 try { 282 oos.writeBoolean(true); 283 oos.close(); 284 ois = new ObjectInputStream(loadStream()); 285 assertTrue("Wrote incorrect byte value", ois.readBoolean()); 286 } catch (IOException e) { 287 fail("IOException serializing data : " + e.getMessage()); 288 } 289 } 290 291 @TestTargetNew( 292 level = TestLevel.COMPLETE, 293 notes = "Verifies serialization.", 294 method = "!Serialization", 295 args = {} 296 ) 297 public void test_10_writeByte() { 298 // Test for method void java.io.ObjectOutputStream.writeByte(int) 299 try { 300 oos.writeByte(127); 301 oos.close(); 302 ois = new ObjectInputStream(loadStream()); 303 assertEquals("Wrote incorrect byte value", 127, ois.readByte()); 304 } catch (IOException e) { 305 fail("IOException serializing data : " + e.getMessage()); 306 } 307 } 308 309 @TestTargetNew( 310 level = TestLevel.COMPLETE, 311 notes = "Verifies serialization.", 312 method = "!Serialization", 313 args = {} 314 ) 315 public void test_11_writeBytes() { 316 // Test for method void 317 // java.io.ObjectOutputStream.writeBytes(java.lang.String) 318 try { 319 byte[] buf = new byte[10]; 320 oos.writeBytes("HelloWorld"); 321 oos.close(); 322 ois = new ObjectInputStream(loadStream()); 323 ois.readFully(buf); 324 ois.close(); 325 assertEquals("Wrote incorrect bytes value", "HelloWorld", new String(buf, 0, 10) 326 ); 327 } catch (IOException e) { 328 fail("IOException serializing data : " + e.getMessage()); 329 } 330 } 331 332 @TestTargetNew( 333 level = TestLevel.COMPLETE, 334 notes = "Verifies serialization.", 335 method = "!Serialization", 336 args = {} 337 ) 338 public void test_12_writeChar() { 339 // Test for method void java.io.ObjectOutputStream.writeChar(int) 340 try { 341 oos.writeChar('T'); 342 oos.close(); 343 ois = new ObjectInputStream(loadStream()); 344 assertEquals("Wrote incorrect char value", 'T', ois.readChar()); 345 } catch (IOException e) { 346 fail("IOException serializing data : " + e.getMessage()); 347 } 348 } 349 350 @TestTargetNew( 351 level = TestLevel.COMPLETE, 352 notes = "Verifies serialization.", 353 method = "!Serialization", 354 args = {} 355 ) 356 public void test_13_writeChars() { 357 // Test for method void 358 // java.io.ObjectOutputStream.writeChars(java.lang.String) 359 try { 360 int avail = 0; 361 char[] buf = new char[10]; 362 oos.writeChars("HelloWorld"); 363 oos.close(); 364 ois = new ObjectInputStream(loadStream()); 365 // Number of prim data bytes in stream / 2 to give char index 366 avail = ois.available() / 2; 367 for (int i = 0; i < avail; ++i) 368 buf[i] = ois.readChar(); 369 ois.close(); 370 assertEquals("Wrote incorrect chars", "HelloWorld", new String(buf, 0, 10) 371 ); 372 } catch (IOException e) { 373 fail("IOException serializing data : " + e.getMessage()); 374 } 375 } 376 377 @TestTargetNew( 378 level = TestLevel.COMPLETE, 379 notes = "Verifies serialization.", 380 method = "!Serialization", 381 args = {} 382 ) 383 public void test_14_writeDouble() { 384 // Test for method void java.io.ObjectOutputStream.writeDouble(double) 385 try { 386 oos.writeDouble(Double.MAX_VALUE); 387 oos.close(); 388 ois = new ObjectInputStream(loadStream()); 389 assertTrue("Wrote incorrect double value", 390 ois.readDouble() == Double.MAX_VALUE); 391 } catch (IOException e) { 392 fail("IOException serializing data : " + e.getMessage()); 393 } 394 } 395 396 @TestTargetNew( 397 level = TestLevel.COMPLETE, 398 notes = "Verifies serialization.", 399 method = "!Serialization", 400 args = {} 401 ) 402 public void test_15_writeFloat() { 403 // Test for method void java.io.ObjectOutputStream.writeFloat(float) 404 try { 405 oos.writeFloat(Float.MAX_VALUE); 406 oos.close(); 407 ois = new ObjectInputStream(loadStream()); 408 assertTrue("Wrote incorrect double value", 409 ois.readFloat() == Float.MAX_VALUE); 410 ois.close(); 411 ois = null; 412 } catch (IOException e) { 413 fail("IOException serializing data : " + e.getMessage()); 414 } 415 } 416 417 @TestTargetNew( 418 level = TestLevel.COMPLETE, 419 notes = "Verifies serialization.", 420 method = "!Serialization", 421 args = {} 422 ) 423 public void test_16_writeInt() { 424 // Test for method void java.io.ObjectOutputStream.writeInt(int) 425 try { 426 oos.writeInt(Integer.MAX_VALUE); 427 oos.close(); 428 ois = new ObjectInputStream(loadStream()); 429 assertTrue("Wrote incorrect double value", 430 ois.readInt() == Integer.MAX_VALUE); 431 ois.close(); 432 } catch (IOException e) { 433 fail("IOException serializing data : " + e.getMessage()); 434 } 435 } 436 437 @TestTargetNew( 438 level = TestLevel.COMPLETE, 439 notes = "Verifies serialization.", 440 method = "!Serialization", 441 args = {} 442 ) 443 public void test_17_writeLong() { 444 // Test for method void java.io.ObjectOutputStream.writeLong(long) 445 try { 446 oos.writeLong(Long.MAX_VALUE); 447 oos.close(); 448 ois = new ObjectInputStream(loadStream()); 449 assertTrue("Wrote incorrect double value", 450 ois.readLong() == Long.MAX_VALUE); 451 } catch (IOException e) { 452 fail("IOException serializing data : " + e.getMessage()); 453 } 454 } 455 456 @TestTargetNew( 457 level = TestLevel.COMPLETE, 458 notes = "Verifies serialization.", 459 method = "!Serialization", 460 args = {} 461 ) 462 public void test_19_writeShort() { 463 // Test for method void java.io.ObjectOutputStream.writeShort(int) 464 try { 465 oos.writeShort(127); 466 oos.close(); 467 ois = new ObjectInputStream(loadStream()); 468 assertEquals("Wrote incorrect short value", 127, ois.readShort()); 469 } catch (IOException e) { 470 fail("IOException serializing data : " + e.getMessage()); 471 } 472 } 473 474 @TestTargetNew( 475 level = TestLevel.COMPLETE, 476 notes = "Verifies serialization.", 477 method = "!Serialization", 478 args = {} 479 ) 480 public void test_20_writeUTF() { 481 // Test for method void 482 // java.io.ObjectOutputStream.writeUTF(java.lang.String) 483 try { 484 oos.writeUTF("HelloWorld"); 485 oos.close(); 486 ois = new ObjectInputStream(loadStream()); 487 assertEquals("Wrote incorrect UTF value", 488 "HelloWorld", ois.readUTF()); 489 } catch (IOException e) { 490 fail("IOException serializing data : " + e.getMessage()); 491 } 492 } 493 494 @TestTargetNew( 495 level = TestLevel.COMPLETE, 496 notes = "Verifies serialization.", 497 method = "!Serialization", 498 args = {} 499 ) 500 public void test_25_available() { 501 try { 502 oos.writeObject(FOO); 503 oos.writeObject(FOO); 504 oos.flush(); 505 int available1 = 0; 506 int available2 = 0; 507 Object obj1 = null; 508 Object obj2 = null; 509 ObjectInputStream ois = new ObjectInputStream(loadStream()); 510 available1 = ois.available(); 511 obj1 = ois.readObject(); 512 available2 = ois.available(); 513 obj2 = ois.readObject(); 514 515 assertEquals("available returned incorrect value", 0, available1); 516 assertEquals("available returned incorrect value", 0, available2); 517 518 assertTrue("available caused incorrect reading", FOO.equals(obj1)); 519 assertTrue("available returned incorrect value", FOO.equals(obj2)); 520 521 } catch (IOException e) { 522 fail("IOException serializing object : " + e.getMessage()); 523 } catch (ClassNotFoundException e) { 524 fail("Unable to read Object type : " + e.toString()); 525 } catch (Error err) { 526 System.out.println("Error " + err); 527 throw err; 528 } 529 530 } 531 532 @TestTargetNew( 533 level = TestLevel.COMPLETE, 534 notes = "Verifies serialization.", 535 method = "!Serialization", 536 args = {} 537 ) 538 public void test_resolveClass() { 539 try { 540 oos.writeObject(new Object[] { Integer.class, new Integer(1) }); 541 oos.close(); 542 543 ois = new ObjectInputStreamSubclass(loadStream()); 544 ois.readObject(); 545 ois.close(); 546 } catch (IOException e1) { 547 fail("IOException : " + e1.getMessage()); 548 } catch (ClassNotFoundException e2) { 549 fail("ClassNotFoundException : " + e2.getMessage()); 550 } 551 552 Class[] resolvedClasses = ((ObjectInputStreamSubclass) ois) 553 .resolvedClasses(); 554 assertEquals("missing resolved", 3, resolvedClasses.length); 555 assertTrue("resolved class 1", resolvedClasses[0] == Object[].class); 556 assertTrue("resolved class 2", resolvedClasses[1] == Integer.class); 557 assertTrue("resolved class 3", resolvedClasses[2] == Number.class); 558 } 559 560 @TestTargetNew( 561 level = TestLevel.COMPLETE, 562 notes = "Verifies serialization.", 563 method = "!Serialization", 564 args = {} 565 ) 566 public void test_reset() { 567 try { 568 oos.reset(); 569 oos.writeObject("R"); 570 oos.reset(); 571 oos.writeByte(24); 572 oos.close(); 573 574 DataInputStream dis = new DataInputStream(loadStream()); 575 byte[] input = new byte[dis.available()]; 576 dis.readFully(input); 577 byte[] result = new byte[] { (byte) 0xac, (byte) 0xed, (byte) 0, 578 (byte) 5, (byte) 0x79, (byte) 0x74, (byte) 0, (byte) 1, 579 (byte) 'R', (byte) 0x79, (byte) 0x77, (byte) 1, (byte) 24 }; 580 assertTrue("incorrect output", Arrays.equals(input, result)); 581 582 ois = new ObjectInputStreamSubclass(loadStream()); 583 assertEquals("Wrong result from readObject()", "R", ois.readObject() 584 ); 585 assertEquals("Wrong result from readByte()", 24, ois.readByte()); 586 ois.close(); 587 } catch (IOException e1) { 588 fail("IOException : " + e1.getMessage()); 589 } catch (ClassNotFoundException e2) { 590 fail("ClassNotFoundException : " + e2.getMessage()); 591 } 592 } 593 594 @TestTargetNew( 595 level = TestLevel.COMPLETE, 596 notes = "Verifies serialization.", 597 method = "!Serialization", 598 args = {} 599 ) 600 public void test_serialVersionUID(Class clazz, long svUID) { 601 final String idWrong = "serialVersionUID is wrong for: "; 602 long reflectedSvUID = 0L; 603 try { 604 reflectedSvUID = clazz.getField("serialVersionUID").getLong(null); 605 } catch (Exception e) { 606 fail("Unable to determine serialVersionUID of " + clazz); 607 } 608 assertTrue(idWrong + clazz + ": " + reflectedSvUID + " does not equal " 609 + svUID, reflectedSvUID == svUID); 610 } 611 612 private static class ResolveObjectTest implements Serializable { 613 Object field1, field2; 614 } 615 616 private static class ResolveObjectInputStream extends ObjectInputStream { 617 ResolveObjectInputStream(InputStream in) 618 throws StreamCorruptedException, IOException { 619 super(in); 620 } 621 622 public void enableResolve() { 623 enableResolveObject(true); 624 } 625 626 public Object resolveObject(Object obj) { 627 if (obj instanceof Vector) // test_1_resolveObject() 628 return new Hashtable(); 629 else if ("abc".equals(obj)) // test_2_resolveObject() 630 return "ABC"; 631 else if (obj instanceof String) // test_3_resolveObject() 632 return String.valueOf(((String) obj).length()); 633 else if (obj instanceof int[]) // test_4_resolveObject() 634 return new Object[1]; 635 else if (obj instanceof Object[] && ((Object[]) obj).length == 2) // test_5_resolveObject() 636 return new char[1]; 637 return obj; 638 } 639 } 640 641 @TestTargetNew( 642 level = TestLevel.COMPLETE, 643 notes = "Verifies serialization.", 644 method = "!Serialization", 645 args = {} 646 ) 647 public void test_1_resolveObject() { 648 try { 649 ResolveObjectTest obj = new ResolveObjectTest(); 650 obj.field1 = new Vector(); 651 obj.field2 = obj.field1; 652 oos.writeObject(obj); 653 oos.close(); 654 ois = new ResolveObjectInputStream(loadStream()); 655 ((ResolveObjectInputStream) ois).enableResolve(); 656 ResolveObjectTest result = null; 657 try { 658 result = (ResolveObjectTest) ois.readObject(); 659 } catch (ClassNotFoundException e) { 660 fail(e.toString()); 661 } 662 assertTrue("Object not resolved", 663 result.field1 instanceof Hashtable); 664 assertTrue("Second reference not resolved", 665 result.field1 == result.field2); 666 } catch (IOException e) { 667 fail("IOException serializing data : " + e.getMessage()); 668 } 669 } 670 671 @TestTargetNew( 672 level = TestLevel.COMPLETE, 673 notes = "Verifies serialization.", 674 method = "!Serialization", 675 args = {} 676 ) 677 public void test_2_resolveObject() { 678 try { 679 ResolveObjectTest obj = new ResolveObjectTest(); 680 obj.field1 = "abc"; 681 obj.field2 = obj.field1; 682 oos.writeObject(obj); 683 oos.close(); 684 ois = new ResolveObjectInputStream(loadStream()); 685 ((ResolveObjectInputStream) ois).enableResolve(); 686 ResolveObjectTest result = null; 687 try { 688 result = (ResolveObjectTest) ois.readObject(); 689 } catch (ClassNotFoundException e) { 690 fail(e.toString()); 691 } 692 assertEquals("String not resolved", "ABC", result.field1); 693 assertTrue("Second reference not resolved", 694 result.field1 == result.field2); 695 } catch (IOException e) { 696 fail("IOException serializing data : " + e.getMessage()); 697 } 698 } 699 700 @TestTargetNew( 701 level = TestLevel.COMPLETE, 702 notes = "Verifies serialization.", 703 method = "!Serialization", 704 args = {} 705 ) 706 public void test_3_resolveObject() { 707 try { 708 ResolveObjectTest obj = new ResolveObjectTest(); 709 char[] lchars = new char[70000]; 710 obj.field1 = new String(lchars); 711 obj.field2 = obj.field1; 712 oos.writeObject(obj); 713 oos.close(); 714 ois = new ResolveObjectInputStream(loadStream()); 715 ((ResolveObjectInputStream) ois).enableResolve(); 716 ResolveObjectTest result = null; 717 try { 718 result = (ResolveObjectTest) ois.readObject(); 719 } catch (ClassNotFoundException e) { 720 fail(e.toString()); 721 } 722 assertTrue("Long String not resolved", "70000" 723 .equals(result.field1)); 724 assertTrue("Second reference not resolved", 725 result.field1 == result.field2); 726 } catch (IOException e) { 727 fail("IOException serializing data : " + e.getMessage()); 728 } 729 } 730 731 @TestTargetNew( 732 level = TestLevel.COMPLETE, 733 notes = "Verifies serialization.", 734 method = "!Serialization", 735 args = {} 736 ) 737 public void test_4_resolveObject() { 738 try { 739 ResolveObjectTest obj = new ResolveObjectTest(); 740 obj.field1 = new int[5]; 741 obj.field2 = obj.field1; 742 oos.writeObject(obj); 743 oos.close(); 744 ois = new ResolveObjectInputStream(loadStream()); 745 ((ResolveObjectInputStream) ois).enableResolve(); 746 ResolveObjectTest result = null; 747 try { 748 result = (ResolveObjectTest) ois.readObject(); 749 } catch (ClassNotFoundException e) { 750 fail(e.toString()); 751 } 752 Class cl = new Object[0].getClass(); 753 assertTrue("int[] not resolved", result.field1.getClass() == cl); 754 assertTrue("Second reference not resolved", 755 result.field1 == result.field2); 756 } catch (IOException e) { 757 fail("IOException serializing data : " + e.getMessage()); 758 } 759 } 760 761 @TestTargetNew( 762 level = TestLevel.COMPLETE, 763 notes = "Verifies serialization.", 764 method = "!Serialization", 765 args = {} 766 ) 767 public void test_5_resolveObject() { 768 try { 769 ResolveObjectTest obj = new ResolveObjectTest(); 770 obj.field1 = new Object[2]; 771 obj.field2 = obj.field1; 772 oos.writeObject(obj); 773 oos.close(); 774 ois = new ResolveObjectInputStream(loadStream()); 775 ((ResolveObjectInputStream) ois).enableResolve(); 776 ResolveObjectTest result = null; 777 try { 778 result = (ResolveObjectTest) ois.readObject(); 779 } catch (ClassNotFoundException e) { 780 fail(e.toString()); 781 } 782 Class cl = new char[0].getClass(); 783 assertTrue("int[] not resolved", result.field1.getClass() == cl); 784 assertTrue("Second reference not resolved", 785 result.field1 == result.field2); 786 } catch (IOException e) { 787 fail("IOException serializing data : " + e.getMessage()); 788 } 789 } 790 791 static class WriteReplaceTestA implements Serializable { 792 public Object writeReplace() throws ObjectStreamException { 793 return new ReadResolveTestB(); 794 } 795 } 796 797 static class WriteReplaceTestB extends WriteReplaceTestA { 798 } 799 800 static class WriteReplaceTestC extends WriteReplaceTestA { 801 public Object writeReplace() throws ObjectStreamException { 802 return new ReadResolveTestC(); 803 } 804 } 805 806 static class WriteReplaceTestD implements Serializable { 807 private Object writeReplace() throws ObjectStreamException { 808 return new ReadResolveTestD(); 809 } 810 } 811 812 static class WriteReplaceTestE extends WriteReplaceTestD { 813 } 814 815 static class WriteReplaceTestF implements Serializable { 816 int type, readType; 817 818 public WriteReplaceTestF(int type, int readType) { 819 this.type = type; 820 this.readType = readType; 821 } 822 823 public Object writeReplace() throws ObjectStreamException { 824 switch (type) { 825 case 0: 826 throw new InvalidObjectException("invalid"); 827 case 1: 828 throw new RuntimeException("runtime"); 829 case 2: 830 throw new Error("error"); 831 default: 832 return new ReadResolveTestE(readType); 833 } 834 } 835 } 836 837 static class ReadResolveTestA implements Serializable { 838 public Object readResolve() throws ObjectStreamException { 839 return new ReadResolveTestA(); 840 } 841 } 842 843 static class ReadResolveTestB extends ReadResolveTestA { 844 } 845 846 static class ReadResolveTestC implements Serializable { 847 private Object readResolve() throws ObjectStreamException { 848 return new ReadResolveTestB(); 849 } 850 } 851 852 static class ReadResolveTestD extends ReadResolveTestC { 853 } 854 855 static class ReadResolveTestE implements Serializable { 856 int type; 857 858 public ReadResolveTestE(int type) { 859 this.type = type; 860 } 861 862 public Object readResolve() throws ObjectStreamException { 863 switch (type) { 864 case 0: 865 throw new InvalidObjectException("invalid"); 866 case 1: 867 throw new RuntimeException("runtime"); 868 case 2: 869 throw new Error("error"); 870 case 3: 871 return this; 872 default: 873 return new ReadResolveTestF(); 874 } 875 } 876 } 877 878 static class ReadResolveTestF implements Serializable { 879 } 880 881 @TestTargetNew( 882 level = TestLevel.COMPLETE, 883 notes = "Verifies serialization.", 884 method = "!Serialization", 885 args = {} 886 ) 887 public void test_1_writeReplace() { 888 try { 889 Vector v = new Vector(); 890 v.addElement(new WriteReplaceTestA()); 891 v.addElement(new WriteReplaceTestB()); 892 v.addElement(new WriteReplaceTestB()); 893 v.addElement(new WriteReplaceTestC()); 894 v.addElement(new WriteReplaceTestD()); 895 v.addElement(new WriteReplaceTestE()); 896 oos.writeObject(v); 897 oos.close(); 898 ois = new ObjectInputStream(loadStream()); 899 Vector result = (Vector) ois.readObject(); 900 assertTrue("invalid 0 : " + result.elementAt(0), result 901 .elementAt(0).getClass() == ReadResolveTestA.class); 902 assertTrue("invalid 1 : " + result.elementAt(1), result 903 .elementAt(1).getClass() == ReadResolveTestA.class); 904 assertTrue("invalid 2 : " + result.elementAt(2), result 905 .elementAt(2).getClass() == ReadResolveTestA.class); 906 assertTrue("invalid 3 : " + result.elementAt(3), result 907 .elementAt(3).getClass() == ReadResolveTestB.class); 908 assertTrue("invalid 4 : " + result.elementAt(4), result 909 .elementAt(4).getClass() == ReadResolveTestD.class); 910 assertTrue("invalid 5 : " + result.elementAt(5), result 911 .elementAt(5).getClass() == WriteReplaceTestE.class); 912 } catch (IOException e) { 913 fail("IOException serializing data : " + e.getMessage()); 914 } catch (ClassNotFoundException e) { 915 fail("ClassNotFoundException serializing data : " + e.getMessage()); 916 } 917 } 918 919 @TestTargetNew( 920 level = TestLevel.COMPLETE, 921 notes = "Verifies serialization.", 922 method = "!Serialization", 923 args = {} 924 ) 925 public void test_2_writeReplace_01() throws IOException { 926 try { 927 oos.writeObject(new WriteReplaceTestF(0, -1)); 928 fail("Should throw ObjectStreamException"); 929 } catch (ObjectStreamException e) { 930 // expected 931 } 932 } 933 934 @TestTargetNew( 935 level = TestLevel.COMPLETE, 936 notes = "Verifies serialization.", 937 method = "!Serialization", 938 args = {} 939 ) 940 public void test_2_writeReplace_02() throws IOException { 941 try { 942 oos.writeObject(new WriteReplaceTestF(1, -1)); 943 fail("Should throw RuntimeException"); 944 } catch (RuntimeException e) { 945 // expected 946 } 947 } 948 949 @TestTargetNew( 950 level = TestLevel.COMPLETE, 951 notes = "Verifies serialization.", 952 method = "!Serialization", 953 args = {} 954 ) 955 public void test_2_writeReplace_03() throws IOException { 956 try { 957 oos.writeObject(new WriteReplaceTestF(2, -1)); 958 fail("Should throw Error"); 959 } catch (Error e) { 960 // expected 961 } 962 } 963 964 @TestTargetNew( 965 level = TestLevel.COMPLETE, 966 notes = "Verifies serialization.", 967 method = "!Serialization", 968 args = {} 969 ) 970 public void test_2_writeReplace_04() throws IOException, 971 ClassNotFoundException { 972 oos.writeObject(new WriteReplaceTestF(3, 0)); 973 oos.writeObject(new WriteReplaceTestF(3, 1)); 974 oos.writeObject(new WriteReplaceTestF(3, 2)); 975 WriteReplaceTestF test = new WriteReplaceTestF(3, 3); 976 oos.writeObject(test); 977 oos.writeObject(test); 978 WriteReplaceTestF test2 = new WriteReplaceTestF(3, 4); 979 oos.writeObject(test2); 980 oos.writeObject(test2); 981 oos.close(); 982 ois = new ObjectInputStream(loadStream()); 983 try { 984 ois.readObject(); 985 fail("Expected InvalidObjectException"); 986 } catch (InvalidObjectException e) { 987 // expected 988 } 989 990 try { 991 ois.readObject(); 992 fail("Expected RuntimeException"); 993 } catch (RuntimeException e) { 994 // expected 995 } 996 997 try { 998 ois.readObject(); 999 fail("Expected Error"); 1000 } catch (Error e) { 1001 // expected 1002 } 1003 1004 Object readE1 = ois.readObject(); 1005 Object readE2 = ois.readObject(); 1006 assertTrue("Replaced objects should be identical", readE1 == readE2); 1007 Object readF1 = ois.readObject(); 1008 Object readF2 = ois.readObject(); 1009 assertTrue("Replaced resolved objects should be identical: " 1010 + readF1 + " " + readF2, readF1 == readF2); 1011 } 1012} 1013