ObjectOutputStreamTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 java.io.ByteArrayInputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.Externalizable; 23import java.io.FileInputStream; 24import java.io.FileOutputStream; 25import java.io.IOException; 26import java.io.NotActiveException; 27import java.io.NotSerializableException; 28import java.io.ObjectInput; 29import java.io.ObjectInputStream; 30import java.io.ObjectOutput; 31import java.io.ObjectOutputStream; 32import java.io.ObjectStreamClass; 33import java.io.ObjectStreamException; 34import java.io.ObjectStreamField; 35import java.io.OutputStream; 36import java.io.Serializable; 37import java.io.SerializablePermission; 38import java.io.WriteAbortedException; 39import java.security.Permission; 40import java.util.Arrays; 41 42import tests.support.Support_ASimpleOutputStream; 43import tests.support.Support_IOTestSecurityManager; 44import tests.support.Support_OutputStream; 45import dalvik.annotation.TestLevel; 46import dalvik.annotation.TestTargetClass; 47import dalvik.annotation.TestTargetNew; 48import dalvik.annotation.TestTargets; 49 50@TestTargetClass( 51 value = ObjectOutputStream.class, 52 untestedMethods = { 53 @TestTargetNew( 54 method = "annotateClass", 55 args = {Class.class}, 56 level = TestLevel.NOT_NECESSARY, 57 notes = "According to specification, the implementation " + 58 "does nothing." 59 ), 60 @TestTargetNew( 61 method = "annotateProxyClass", 62 args = {Class.class}, 63 level = TestLevel.NOT_NECESSARY, 64 notes = "According to specification, the implementation " + 65 "does nothing." 66 ) 67 } 68) 69public class ObjectOutputStreamTest extends junit.framework.TestCase implements 70 Serializable { 71 72 static final long serialVersionUID = 1L; 73 74 java.io.File f; 75 76 public class SerializableTestHelper implements Serializable { 77 public String aField1; 78 79 public String aField2; 80 81 SerializableTestHelper() { 82 aField1 = null; 83 aField2 = null; 84 } 85 86 SerializableTestHelper(String s, String t) { 87 aField1 = s; 88 aField2 = t; 89 } 90 91 private void readObject(ObjectInputStream ois) throws IOException { 92 // note aField2 is not read 93 try { 94 ObjectInputStream.GetField fields = ois.readFields(); 95 aField1 = (String) fields.get("aField1", "Zap"); 96 } catch (Exception e) { 97 } 98 } 99 100 private void writeObject(ObjectOutputStream oos) throws IOException { 101 // note aField2 is not written 102 ObjectOutputStream.PutField fields = oos.putFields(); 103 fields.put("aField1", aField1); 104 oos.writeFields(); 105 } 106 107 public String getText1() { 108 return aField1; 109 } 110 111 public void setText1(String s) { 112 aField1 = s; 113 } 114 115 public String getText2() { 116 return aField2; 117 } 118 119 public void setText2(String s) { 120 aField2 = s; 121 } 122 } 123 124 private static class SpecTestSuperClass implements Runnable, Serializable { 125 static final long serialVersionUID = 1L; 126 protected java.lang.String instVar; 127 128 public void run() { 129 } 130 } 131 132 private static class SpecTest extends SpecTestSuperClass implements 133 Cloneable, Serializable { 134 static final long serialVersionUID = 1L; 135 136 public java.lang.String instVar1; 137 138 public static java.lang.String staticVar1; 139 140 public static java.lang.String staticVar2; 141 { 142 instVar1 = "NonStaticInitialValue"; 143 } 144 static { 145 staticVar1 = "StaticInitialValue"; 146 staticVar1 = new String(staticVar1); 147 } 148 149 public Object method(Object objParam, Object objParam2) { 150 return new Object(); 151 } 152 153 public boolean method(boolean bParam, Object objParam) { 154 return true; 155 } 156 157 public boolean method(boolean bParam, Object objParam, Object objParam2) { 158 return true; 159 } 160 161 } 162 163 private static class SpecTestSubclass extends SpecTest implements 164 Serializable { 165 static final long serialVersionUID = 1L; 166 public transient java.lang.String transientInstVar = "transientValue"; 167 } 168 169 private static class ReadWriteObject implements java.io.Serializable { 170 static final long serialVersionUID = 1L; 171 172 public boolean calledWriteObject = false; 173 174 public boolean calledReadObject = false; 175 176 public ReadWriteObject() { 177 super(); 178 } 179 180 private void readObject(java.io.ObjectInputStream in) 181 throws java.io.IOException, ClassNotFoundException { 182 calledReadObject = true; 183 in.readObject(); 184 } 185 186 private void writeObject(java.io.ObjectOutputStream out) 187 throws java.io.IOException { 188 calledWriteObject = true; 189 out.writeObject(FOO); 190 } 191 } 192 193 private static class PublicReadWriteObject implements java.io.Serializable { 194 public boolean calledWriteObject = false; 195 196 public boolean calledReadObject = false; 197 198 public PublicReadWriteObject() { 199 super(); 200 } 201 202 public void readObject(java.io.ObjectInputStream in) 203 throws java.io.IOException, ClassNotFoundException { 204 calledReadObject = true; 205 in.readObject(); 206 } 207 208 public void writeObject(java.io.ObjectOutputStream out) 209 throws java.io.IOException { 210 calledWriteObject = true; 211 out.writeObject(FOO); 212 } 213 } 214 215 private static class FieldOrder implements Serializable { 216 String aaa1NonPrimitive = "aaa1"; 217 218 int bbb1PrimitiveInt = 5; 219 220 boolean aaa2PrimitiveBoolean = true; 221 222 String bbb2NonPrimitive = "bbb2"; 223 } 224 225 private static class JustReadObject implements java.io.Serializable { 226 public boolean calledReadObject = false; 227 228 public JustReadObject() { 229 super(); 230 } 231 232 private void readObject(java.io.ObjectInputStream in) 233 throws java.io.IOException, ClassNotFoundException { 234 calledReadObject = true; 235 in.defaultReadObject(); 236 } 237 } 238 239 private static class JustWriteObject implements java.io.Serializable { 240 static final long serialVersionUID = 1L; 241 public boolean calledWriteObject = false; 242 243 public JustWriteObject() { 244 super(); 245 } 246 247 private void writeObject(java.io.ObjectOutputStream out) 248 throws java.io.IOException, ClassNotFoundException { 249 calledWriteObject = true; 250 out.defaultWriteObject(); 251 } 252 } 253 254 private static class ClassBasedReplacementWhenDumping implements 255 java.io.Serializable { 256 public boolean calledReplacement = false; 257 258 public ClassBasedReplacementWhenDumping() { 259 super(); 260 } 261 262 private Object writeReplace() { 263 calledReplacement = true; 264 return FOO; // Replacement is a String 265 } 266 } 267 268 private static class MultipleClassBasedReplacementWhenDumping implements 269 java.io.Serializable { 270 private static class C1 implements java.io.Serializable { 271 private Object writeReplace() { 272 return new C2(); 273 } 274 } 275 276 private static class C2 implements java.io.Serializable { 277 private Object writeReplace() { 278 return new C3(); 279 } 280 } 281 282 private static class C3 implements java.io.Serializable { 283 private Object writeReplace() { 284 return FOO; 285 } 286 } 287 288 public MultipleClassBasedReplacementWhenDumping() { 289 super(); 290 } 291 292 private Object writeReplace() { 293 return new C1(); 294 } 295 } 296 297 private static class ClassBasedReplacementWhenLoading implements 298 java.io.Serializable { 299 public ClassBasedReplacementWhenLoading() { 300 super(); 301 } 302 303 private Object readResolve() { 304 return FOO; // Replacement is a String 305 } 306 } 307 308 private static class ClassBasedReplacementWhenLoadingViolatesFieldType 309 implements java.io.Serializable { 310 public ClassBasedReplacementWhenLoading classBasedReplacementWhenLoading = new ClassBasedReplacementWhenLoading(); 311 312 public ClassBasedReplacementWhenLoadingViolatesFieldType() { 313 super(); 314 } 315 } 316 317 private static class MyExceptionWhenDumping implements java.io.Serializable { 318 private static class MyException extends java.io.IOException { 319 }; 320 321 public boolean anInstanceVar = false; 322 323 public MyExceptionWhenDumping() { 324 super(); 325 } 326 327 private void readObject(java.io.ObjectInputStream in) 328 throws java.io.IOException, ClassNotFoundException { 329 in.defaultReadObject(); 330 } 331 332 private void writeObject(java.io.ObjectOutputStream out) 333 throws java.io.IOException, ClassNotFoundException { 334 throw new MyException(); 335 } 336 } 337 338 private static class NonSerializableExceptionWhenDumping implements 339 java.io.Serializable { 340 public Object anInstanceVar = new Object(); 341 342 public NonSerializableExceptionWhenDumping() { 343 super(); 344 } 345 } 346 347 private static class MyUnserializableExceptionWhenDumping implements 348 java.io.Serializable { 349 private static class MyException extends java.io.IOException { 350 private Object notSerializable = new Object(); 351 }; 352 353 public boolean anInstanceVar = false; 354 355 public MyUnserializableExceptionWhenDumping() { 356 super(); 357 } 358 359 private void readObject(java.io.ObjectInputStream in) 360 throws java.io.IOException, ClassNotFoundException { 361 in.defaultReadObject(); 362 } 363 364 private void writeObject(java.io.ObjectOutputStream out) 365 throws java.io.IOException, ClassNotFoundException { 366 throw new MyException(); 367 } 368 } 369 370 private static class WithUnmatchingSerialPersistentFields implements 371 java.io.Serializable { 372 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 373 "value", String.class) }; 374 375 public int anInstanceVar = 5; 376 377 public WithUnmatchingSerialPersistentFields() { 378 super(); 379 } 380 } 381 382 private static class WithMatchingSerialPersistentFields implements 383 java.io.Serializable { 384 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 385 "anInstanceVar", String.class) }; 386 387 public String anInstanceVar = FOO + FOO; 388 389 public WithMatchingSerialPersistentFields() { 390 super(); 391 } 392 } 393 394 private static class SerialPersistentFields implements java.io.Serializable { 395 private static final String SIMULATED_FIELD_NAME = "text"; 396 397 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 398 SIMULATED_FIELD_NAME, String.class) }; 399 400 public int anInstanceVar = 5; 401 402 public SerialPersistentFields() { 403 super(); 404 } 405 406 private void readObject(java.io.ObjectInputStream in) 407 throws java.io.IOException, ClassNotFoundException { 408 ObjectInputStream.GetField fields = in.readFields(); 409 anInstanceVar = Integer.parseInt((String) fields.get( 410 SIMULATED_FIELD_NAME, "-5")); 411 } 412 413 private void writeObject(java.io.ObjectOutputStream out) 414 throws java.io.IOException, ClassNotFoundException { 415 ObjectOutputStream.PutField fields = out.putFields(); 416 fields.put(SIMULATED_FIELD_NAME, Integer.toString(anInstanceVar)); 417 out.writeFields(); 418 } 419 } 420 421 private static class WriteFieldsWithoutFetchingPutFields implements 422 java.io.Serializable { 423 private static final String SIMULATED_FIELD_NAME = "text"; 424 425 private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField( 426 SIMULATED_FIELD_NAME, String.class) }; 427 428 public int anInstanceVar = 5; 429 430 public WriteFieldsWithoutFetchingPutFields() { 431 super(); 432 } 433 434 private void readObject(java.io.ObjectInputStream in) 435 throws java.io.IOException, ClassNotFoundException { 436 in.readFields(); 437 } 438 439 private void writeObject(java.io.ObjectOutputStream out) 440 throws java.io.IOException, ClassNotFoundException { 441 out.writeFields(); 442 } 443 } 444 445 private static class SerialPersistentFieldsWithoutField implements 446 java.io.Serializable { 447 public int anInstanceVar = 5; 448 449 public SerialPersistentFieldsWithoutField() { 450 super(); 451 } 452 453 private void readObject(java.io.ObjectInputStream in) 454 throws java.io.IOException, ClassNotFoundException { 455 in.readFields(); 456 } 457 458 private void writeObject(java.io.ObjectOutputStream out) 459 throws java.io.IOException, ClassNotFoundException { 460 out.putFields(); 461 out.writeFields(); 462 } 463 } 464 465 private static class NotSerializable { 466 private int foo; 467 468 public NotSerializable() { 469 } 470 471 protected Object writeReplace() throws ObjectStreamException { 472 return new Integer(42); 473 } 474 } 475 476 private static class WriteReplaceObject implements Serializable { 477 private Object replaceObject; 478 479 private static enum Color { 480 red, blue, green 481 }; 482 483 public WriteReplaceObject(Object o) { 484 replaceObject = o; 485 } 486 487 protected Object writeReplace() throws ObjectStreamException { 488 return replaceObject; 489 } 490 } 491 492 private static class ExternalizableWithReplace implements Externalizable { 493 private int foo; 494 495 public ExternalizableWithReplace() { 496 } 497 498 protected Object writeReplace() throws ObjectStreamException { 499 return new Integer(42); 500 } 501 502 public void writeExternal(ObjectOutput out) { 503 } 504 505 public void readExternal(ObjectInput in) { 506 } 507 } 508 509 private static class ObjectOutputStreamWithReplace extends ObjectOutputStream { 510 public ObjectOutputStreamWithReplace(OutputStream out) throws IOException { 511 super(out); 512 enableReplaceObject(true); 513 } 514 515 protected Object replaceObject(Object obj) throws IOException { 516 if (obj instanceof NotSerializable) { 517 return new Long(10); 518 } else if (obj instanceof Integer) { 519 return new Long(((Integer) obj).longValue()); 520 } else { 521 return obj; 522 } 523 } 524 } 525 526 private static class ObjectOutputStreamWithReplace2 extends 527 ObjectOutputStream { 528 public ObjectOutputStreamWithReplace2(OutputStream out) 529 throws IOException { 530 super(out); 531 enableReplaceObject(true); 532 } 533 534 protected Object replaceObject(Object obj) throws IOException { 535 return new Long(10); 536 } 537 } 538 539 private static class BasicObjectOutputStream extends ObjectOutputStream { 540 public boolean writeStreamHeaderCalled; 541 542 public BasicObjectOutputStream() throws IOException, SecurityException { 543 super(); 544 writeStreamHeaderCalled = false; 545 } 546 547 public BasicObjectOutputStream(OutputStream output) throws IOException { 548 super(output); 549 } 550 551 public void drain() throws IOException { 552 super.drain(); 553 } 554 555 public boolean enableReplaceObject(boolean enable) 556 throws SecurityException { 557 return super.enableReplaceObject(enable); 558 } 559 560 public void writeObjectOverride(Object object) throws IOException { 561 super.writeObjectOverride(object); 562 } 563 564 public void writeStreamHeader() throws IOException { 565 super.writeStreamHeader(); 566 writeStreamHeaderCalled = true; 567 } 568} 569 570 private static class NoFlushTestOutputStream extends ByteArrayOutputStream { 571 public boolean flushCalled; 572 573 public NoFlushTestOutputStream() { 574 super(); 575 flushCalled = false; 576 } 577 578 public void flush() throws IOException { 579 super.flush(); 580 flushCalled = true; 581 } 582 } 583 584 protected static final String MODE_XLOAD = "xload"; 585 586 protected static final String MODE_XDUMP = "xdump"; 587 588 static final String FOO = "foo"; 589 590 static final String MSG_WITE_FAILED = "Failed to write: "; 591 592 private static final boolean DEBUG = false; 593 594 protected static boolean xload = false; 595 596 protected static boolean xdump = false; 597 598 protected static String xFileName = null; 599 600 protected ObjectInputStream ois; 601 602 protected ObjectOutputStream oos; 603 604 protected ObjectOutputStream oos_ioe; 605 606 protected Support_OutputStream sos; 607 608 protected ByteArrayOutputStream bao; 609 610 static final int INIT_INT_VALUE = 7; 611 612 static final String INIT_STR_VALUE = "a string that is blortz"; 613 614 /** 615 * @tests java.io.ObjectInputStream#ObjectOutputStream() 616 */ 617 @TestTargetNew( 618 level = TestLevel.COMPLETE, 619 notes = "Verifies the protected ObjectOutputStream() constructor.", 620 method = "ObjectOutputStream", 621 args = {} 622 ) 623 public void test_Constructor() throws IOException { 624 SecurityManager sm = System.getSecurityManager(); 625 System.setSecurityManager(new Support_IOTestSecurityManager()); 626 627 try { 628 oos = new BasicObjectOutputStream(); 629 fail("SecurityException expected."); 630 } catch (SecurityException e) { 631 // expected 632 } finally { 633 System.setSecurityManager(sm); 634 } 635 } 636 637 /** 638 * @tests java.io.ObjectOutputStream#ObjectOutputStream(java.io.OutputStream) 639 */ 640 @TestTargetNew( 641 level = TestLevel.PARTIAL_COMPLETE, 642 notes = "Checks valid construction, NullPointerException and IOException.", 643 method = "ObjectOutputStream", 644 args = {java.io.OutputStream.class} 645 ) 646 public void test_ConstructorLjava_io_OutputStream() throws IOException { 647 oos.close(); 648 oos = new ObjectOutputStream(new ByteArrayOutputStream()); 649 oos.close(); 650 651 try { 652 oos = new ObjectOutputStream(null); 653 fail("Test 1: NullPointerException expected."); 654 } catch (NullPointerException e) { 655 // Expected. 656 } 657 658 Support_ASimpleOutputStream sos = new Support_ASimpleOutputStream(true); 659 try { 660 oos = new ObjectOutputStream(sos); 661 fail("Test 2: IOException expected."); 662 } catch (IOException e) { 663 // Expected. 664 } 665 } 666 667 /** 668 * @tests java.io.ObjectOutputStream#ObjectOutputStream(java.io.OutputStream) 669 */ 670 @TestTargetNew( 671 level = TestLevel.PARTIAL_COMPLETE, 672 notes = "Checks SecurityException.", 673 method = "ObjectOutputStream", 674 args = {java.io.OutputStream.class} 675 ) 676 public void test_ConstructorLjava_io_OutputStream_subtest0() throws IOException { 677 678 // custom security manager 679 SecurityManager sm = new SecurityManager() { 680 681 final SerializablePermission forbidenPermission = 682 new SerializablePermission("enableSubclassImplementation"); 683 684 public void checkPermission(Permission perm) { 685 if (forbidenPermission.equals(perm)) { 686 throw new SecurityException(); 687 } 688 } 689 }; 690 691 SecurityManager oldSm = System.getSecurityManager(); 692 System.setSecurityManager(sm); 693 try { 694 ByteArrayOutputStream out = new ByteArrayOutputStream(); 695 // should not cause SecurityException 696 new ObjectOutputStream(out); 697 // should not cause SecurityException 698 class SubTest1 extends ObjectOutputStream { 699 SubTest1(OutputStream out) throws IOException { 700 super(out); 701 } 702 } 703 704 // should not cause SecurityException 705 new SubTest1(out); 706 class SubTest2 extends ObjectOutputStream { 707 SubTest2(OutputStream out) throws IOException { 708 super(out); 709 } 710 711 public void writeUnshared(Object obj) throws IOException { 712 } 713 } 714 715 try { 716 new SubTest2(out); 717 fail("should throw SecurityException 1"); 718 } catch (SecurityException e) { 719 } 720 class SubTest3 extends ObjectOutputStream { 721 SubTest3(OutputStream out) throws IOException { 722 super(out); 723 } 724 725 public PutField putFields() throws IOException { 726 return null; 727 } 728 } 729 730 try { 731 new SubTest3(out); 732 fail("should throw SecurityException 2"); 733 } catch (SecurityException e) { 734 } 735 } finally { 736 System.setSecurityManager(oldSm); 737 } 738 } 739 740 /** 741 * @tests java.io.ObjectOutputStream#close() 742 */ 743 @TestTargetNew( 744 level = TestLevel.COMPLETE, 745 method = "close", 746 args = {} 747 ) 748 public void test_close() throws IOException { 749 int outputSize = bao.size(); 750 // Writing of a primitive type should be buffered. 751 oos.writeInt(42); 752 assertTrue("Test 1: Primitive data unexpectedly written to the target stream.", 753 bao.size() == outputSize); 754 // Closing should write the buffered data to the target stream. 755 oos.close(); 756 assertTrue("Test 2: Primitive data has not been written to the the target stream.", 757 bao.size() > outputSize); 758 759 try { 760 oos_ioe.close(); 761 fail("Test 3: IOException expected."); 762 } catch (IOException e) { 763 // Expected. 764 } 765 } 766 767 /** 768 * @tests java.io.ObjectOutputStream#drain() 769 */ 770 @TestTargetNew( 771 level = TestLevel.COMPLETE, 772 method = "drain", 773 args = {} 774 ) 775 public void test_drain() throws IOException { 776 NoFlushTestOutputStream target = new NoFlushTestOutputStream(); 777 BasicObjectOutputStream boos = new BasicObjectOutputStream(target); 778 int initialSize = target.size(); 779 boolean written = false; 780 781 boos.writeBytes("Lorem ipsum"); 782 // If there is no buffer then the bytes have already been written. 783 written = (target.size() > initialSize); 784 785 boos.drain(); 786 assertTrue("Content has not been written to the target.", 787 written || (target.size() > initialSize)); 788 assertFalse("flush() has been called on the target.", 789 target.flushCalled); 790 } 791 792 /** 793 * @tests java.io.ObjectOutputStream#defaultWriteObject() 794 */ 795 @TestTargetNew( 796 level = TestLevel.SUFFICIENT, 797 notes = "IOException can not be tested because this method" + 798 "always throws a NotActiveException if called directly.", 799 method = "defaultWriteObject", 800 args = {} 801 ) 802 public void test_defaultWriteObject() throws IOException { 803 try { 804 oos.defaultWriteObject(); 805 fail("Test 1: NotActiveException expected."); 806 } catch (NotActiveException e) { 807 // Expected. 808 } 809 } 810 811 /** 812 * @tests java.io.ObjectOutputStream#enableReplaceObject(boolean) 813 */ 814 @TestTargetNew( 815 level = TestLevel.COMPLETE, 816 method = "enableReplaceObject", 817 args = {boolean.class} 818 ) 819 public void test_enableReplaceObjectB() throws IOException { 820 // Start testing without a SecurityManager. 821 BasicObjectOutputStream boos = new BasicObjectOutputStream(); 822 assertFalse("Test 1: Object resolving must be disabled by default.", 823 boos.enableReplaceObject(true)); 824 825 assertTrue("Test 2: enableReplaceObject did not return the previous value.", 826 boos.enableReplaceObject(false)); 827 828 // Test 3: Check that a security exception is thrown. 829 SecurityManager sm = System.getSecurityManager(); 830 System.setSecurityManager(new Support_IOTestSecurityManager()); 831 try { 832 boos.enableReplaceObject(true); 833 fail("Test 3: SecurityException expected."); 834 } catch (SecurityException e) { 835 // expected 836 } finally { 837 System.setSecurityManager(sm); 838 } 839 } 840 841 /** 842 * @tests java.io.ObjectOutputStream#flush() 843 */ 844 @TestTargetNew( 845 level = TestLevel.COMPLETE, 846 method = "flush", 847 args = {} 848 ) 849 public void test_flush() throws Exception { 850 // Test for method void java.io.ObjectOutputStream.flush() 851 int size = bao.size(); 852 oos.writeByte(127); 853 assertTrue("Test 1: Data already flushed.", bao.size() == size); 854 oos.flush(); 855 assertTrue("Test 2: Failed to flush data.", bao.size() > size); 856 857 try { 858 oos_ioe.flush(); 859 fail("Test 3: IOException expected."); 860 } catch (IOException e) { 861 // Expected. 862 } 863 } 864 865 /** 866 * @tests java.io.ObjectOutputStream#putFields() 867 */ 868 @TestTargetNew( 869 level = TestLevel.SUFFICIENT, 870 notes = "IOException can not be tested because this method" + 871 "always throws a NotActiveException if called directly.", 872 method = "putFields", 873 args = {} 874 ) 875 public void test_putFields() throws Exception { 876 /* 877 * "SerializableTestHelper" is an object created for these tests with 878 * two fields (Strings) and simple implementations of readObject and 879 * writeObject which simply read and write the first field but not the 880 * second one. 881 */ 882 SerializableTestHelper sth; 883 884 try { 885 oos.putFields(); 886 fail("Test 1: NotActiveException expected."); 887 } catch (NotActiveException e) { 888 // Expected. 889 } 890 891 oos.writeObject(new SerializableTestHelper("Gabba", "Jabba")); 892 oos.flush(); 893 ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); 894 sth = (SerializableTestHelper) (ois.readObject()); 895 assertEquals("Test 2: readFields or writeFields failed; first field not set.", 896 "Gabba", sth.getText1()); 897 assertNull("Test 3: readFields or writeFields failed; second field should not have been set.", 898 sth.getText2()); 899 } 900 901 /** 902 * @tests java.io.ObjectOutputStream#reset() 903 */ 904 @TestTargetNew( 905 level = TestLevel.COMPLETE, 906 method = "reset", 907 args = {} 908 ) 909 public void test_reset() throws Exception { 910 String o = "HelloWorld"; 911 sos = new Support_OutputStream(200); 912 oos.close(); 913 oos = new ObjectOutputStream(sos); 914 oos.writeObject(o); 915 oos.writeObject(o); 916 oos.reset(); 917 oos.writeObject(o); 918 919 sos.setThrowsException(true); 920 try { 921 oos.reset(); 922 fail("Test 1: IOException expected."); 923 } catch (IOException e) { 924 // Expected. 925 } 926 sos.setThrowsException(false); 927 928 ois = new ObjectInputStream(new ByteArrayInputStream(sos.toByteArray())); 929 assertEquals("Test 2: Incorrect object read.", o, ois.readObject()); 930 assertEquals("Test 3: Incorrect object read.", o, ois.readObject()); 931 assertEquals("Test 4: Incorrect object read.", o, ois.readObject()); 932 ois.close(); 933 } 934 935 private static class ExternalTest implements Externalizable { 936 public String value; 937 938 public ExternalTest() { 939 } 940 941 public void setValue(String val) { 942 value = val; 943 } 944 945 public String getValue() { 946 return value; 947 } 948 949 public void writeExternal(ObjectOutput output) { 950 try { 951 output.writeUTF(value); 952 } catch (IOException e) { 953 e.printStackTrace(); 954 } 955 } 956 957 public void readExternal(ObjectInput input) { 958 try { 959 value = input.readUTF(); 960 } catch (IOException e) { 961 e.printStackTrace(); 962 } 963 } 964 } 965 966 /** 967 * @tests java.io.ObjectOutputStream#useProtocolVersion(int) 968 */ 969 @TestTargetNew( 970 level = TestLevel.SUFFICIENT, 971 notes = "IOException seems to be never thrown, therefore there is no such test.", 972 method = "useProtocolVersion", 973 args = {int.class} 974 ) 975 public void test_useProtocolVersionI() throws Exception { 976 977 oos.useProtocolVersion(ObjectOutputStream.PROTOCOL_VERSION_1); 978 ExternalTest t1 = new ExternalTest(); 979 t1.setValue("hello1"); 980 oos.writeObject(t1); 981 oos.close(); 982 ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); 983 ExternalTest t2 = (ExternalTest) ois.readObject(); 984 ois.close(); 985 assertTrue( 986 "Cannot read/write PROTOCAL_VERSION_1 Externalizable objects: " 987 + t2.getValue(), t1.getValue().equals(t2.getValue())); 988 } 989 990 /** 991 * @tests java.io.ObjectOutputStream#write(byte[]) 992 */ 993 @TestTargetNew( 994 level = TestLevel.SUFFICIENT, 995 notes = "Tests against golden file missing. IOException can " + 996 "not be checked since is never thrown (primitive data " + 997 "is written into a self-expanding buffer).", 998 method = "write", 999 args = {byte[].class} 1000 ) 1001 public void test_write$B() throws Exception { 1002 // Test for method void java.io.ObjectOutputStream.write(byte []) 1003 byte[] buf = new byte[10]; 1004 oos.write("HelloWorld".getBytes()); 1005 oos.close(); 1006 ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); 1007 ois.read(buf, 0, 10); 1008 ois.close(); 1009 assertEquals("Read incorrect bytes", "HelloWorld", new String(buf, 0, 1010 10)); 1011 } 1012 1013 /** 1014 * @tests java.io.ObjectOutputStream#write(byte[], int, int) 1015 */ 1016 @TestTargetNew( 1017 level = TestLevel.SUFFICIENT, 1018 notes = "Tests against golden file missing. IOException can " + 1019 "not be checked since is never thrown (primitive data " + 1020 "is written into a self-expanding buffer).", 1021 method = "write", 1022 args = {byte[].class, int.class, int.class} 1023 ) 1024 public void test_write$BII() throws Exception { 1025 // Test for method void java.io.ObjectOutputStream.write(byte [], int, 1026 // int) 1027 byte[] buf = new byte[10]; 1028 oos.write("HelloWorld".getBytes(), 0, 10); 1029 oos.close(); 1030 ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); 1031 ois.read(buf, 0, 10); 1032 ois.close(); 1033 assertEquals("Read incorrect bytes", "HelloWorld", new String(buf, 0, 1034 10)); 1035 } 1036 1037 /** 1038 * @tests java.io.ObjectOutputStream#write(int) 1039 */ 1040 @TestTargetNew( 1041 level = TestLevel.SUFFICIENT, 1042 notes = "Tests against golden file missing. IOException can " + 1043 "not be checked since is never thrown (primitive data " + 1044 "is written into a self-expanding buffer).", 1045 method = "write", 1046 args = {int.class} 1047 ) 1048 public void test_writeI() throws Exception { 1049 // Test for method void java.io.ObjectOutputStream.write(int) 1050 oos.write('T'); 1051 oos.close(); 1052 ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); 1053 assertEquals("Read incorrect byte", 'T', ois.read()); 1054 ois.close(); 1055 } 1056 1057 /** 1058 * @tests java.io.ObjectOutputStream#writeBytes(java.lang.String) 1059 */ 1060 @TestTargetNew( 1061 level = TestLevel.SUFFICIENT, 1062 notes = "Tests against golden file missing. IOException can " + 1063 "not be checked since is never thrown (primitive data " + 1064 "is written into a self-expanding buffer).", 1065 method = "writeBytes", 1066 args = {java.lang.String.class} 1067 ) 1068 public void test_writeBytesLjava_lang_String() throws Exception { 1069 // Test for method void 1070 // java.io.ObjectOutputStream.writeBytes(java.lang.String) 1071 byte[] buf = new byte[10]; 1072 oos.writeBytes("HelloWorld"); 1073 oos.close(); 1074 ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); 1075 ois.readFully(buf); 1076 ois.close(); 1077 assertEquals("Wrote incorrect bytes value", "HelloWorld", new String( 1078 buf, 0, 10)); 1079 } 1080 1081 /** 1082 * @tests java.io.ObjectOutputStream#writeChars(java.lang.String) 1083 */ 1084 @TestTargetNew( 1085 level = TestLevel.SUFFICIENT, 1086 notes = "Tests against golden file missing. IOException can " + 1087 "not be checked since is never thrown (primitive data " + 1088 "is written into a self-expanding buffer).", 1089 method = "writeChars", 1090 args = {java.lang.String.class} 1091 ) 1092 public void test_writeCharsLjava_lang_String() throws Exception { 1093 // Test for method void 1094 // java.io.ObjectOutputStream.writeChars(java.lang.String) 1095 int avail = 0; 1096 char[] buf = new char[10]; 1097 oos.writeChars("HelloWorld"); 1098 oos.close(); 1099 ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); 1100 // Number of prim data bytes in stream / 2 to give char index 1101 avail = ois.available() / 2; 1102 for (int i = 0; i < avail; ++i) 1103 buf[i] = ois.readChar(); 1104 ois.close(); 1105 assertEquals("Wrote incorrect chars", "HelloWorld", new String(buf, 0, 1106 10)); 1107 } 1108 1109 /** 1110 * @tests java.io.ObjectOutputStream#writeObject(java.lang.Object) 1111 */ 1112 @TestTargetNew( 1113 level = TestLevel.COMPLETE, 1114 notes = "", 1115 method = "writeObject", 1116 args = {java.lang.Object.class} 1117 ) 1118 public void test_writeObjectLjava_lang_Object() throws Exception { 1119 // Test for method void 1120 // java.io.ObjectOutputStream.writeObject(java.lang.Object) 1121 1122 Object objToSave = null; 1123 Object objLoaded; 1124 1125 SerialPersistentFieldsWithoutField spf = new SerialPersistentFieldsWithoutField(); 1126 final int CONST = -500; 1127 spf.anInstanceVar = CONST; 1128 objToSave = spf; 1129 if (DEBUG) 1130 System.out.println("Obj = " + objToSave); 1131 objLoaded = dumpAndReload(objToSave); 1132 assertTrue( 1133 "serialPersistentFields do not work properly in this implementation", 1134 ((SerialPersistentFieldsWithoutField) objLoaded).anInstanceVar != CONST); 1135 1136 } 1137 1138 /** 1139 * @tests java.io.ObjectOutputStream#writeObject(java.lang.Object) 1140 */ 1141 @TestTargetNew( 1142 level = TestLevel.COMPLETE, 1143 notes = "", 1144 method = "writeObject", 1145 args = {java.lang.Object.class} 1146 ) 1147 public void test_writeObject_NotSerializable() throws Exception { 1148 ObjectOutput out = null; 1149 try { 1150 out = new ObjectOutputStream(new ByteArrayOutputStream()); 1151 out.writeObject(new NotSerializable()); 1152 fail("Expected NotSerializableException"); 1153 } catch (NotSerializableException e) {} 1154 out.writeObject(new ExternalizableWithReplace()); 1155 } 1156 1157 /** 1158 * @tests java.io.ObjectOutputStream#writeObjectOverride(Object) 1159 */ 1160 @TestTargetNew( 1161 level = TestLevel.COMPLETE, 1162 notes = "Verifies that writeObjectOverride() throws an IOException.", 1163 method = "writeObjectOverride", 1164 args = {java.lang.Object.class} 1165 ) 1166 public void test_writeObjectOverrideLjava_lang_Object() throws IOException { 1167 BasicObjectOutputStream boos = 1168 new BasicObjectOutputStream(new ByteArrayOutputStream()); 1169 1170 try { 1171 boos.writeObjectOverride(new Object()); 1172 fail("IOException expected."); 1173 } 1174 catch (IOException e) { 1175 } 1176 finally { 1177 boos.close(); 1178 } 1179 } 1180 1181 /** 1182 * @tests java.io.ObjectOutputStream#writeStreamHeader() 1183 */ 1184 @TestTargetNew( 1185 level = TestLevel.COMPLETE, 1186 notes = "Verifies writeStreamHeader().", 1187 method = "writeStreamHeader", 1188 args = {} 1189 ) 1190 public void test_writeStreamHeader() throws IOException { 1191 BasicObjectOutputStream boos; 1192 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1193 short s; 1194 byte[] buffer; 1195 1196 // Test 1: Make sure that writeStreamHeader() has been called. 1197 boos = new BasicObjectOutputStream(baos); 1198 try { 1199 assertTrue("Test 1: writeStreamHeader() has not been called.", 1200 boos.writeStreamHeaderCalled); 1201 1202 // Test 2: Check that at least four bytes have been written. 1203 buffer = baos.toByteArray(); 1204 assertTrue("Test 2: At least four bytes should have been written", 1205 buffer.length >= 4); 1206 1207 // Test 3: Check the magic number. 1208 s = buffer[0]; 1209 s <<= 8; 1210 s += ((short) buffer[1] & 0x00ff); 1211 assertEquals("Test 3: Invalid magic number written.", 1212 java.io.ObjectStreamConstants.STREAM_MAGIC, s); 1213 1214 // Test 4: Check the stream version number. 1215 s = buffer[2]; 1216 s <<= 8; 1217 s += ((short) buffer[3] & 0x00ff); 1218 assertEquals("Invalid stream version number written.", 1219 java.io.ObjectStreamConstants.STREAM_VERSION, s); 1220 } 1221 finally { 1222 boos.close(); 1223 } 1224 } 1225 1226 /** 1227 * @tests java.io.ObjectOutputStream#writeUTF(java.lang.String) 1228 */ 1229 @TestTargetNew( 1230 level = TestLevel.PARTIAL, 1231 notes = "IOException checking missed.", 1232 method = "writeUTF", 1233 args = {java.lang.String.class} 1234 ) 1235 public void test_writeUTFLjava_lang_String() throws Exception { 1236 // Test for method void 1237 // java.io.ObjectOutputStream.writeUTF(java.lang.String) 1238 oos.writeUTF("HelloWorld"); 1239 oos.close(); 1240 ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); 1241 assertEquals("Wrote incorrect UTF value", "HelloWorld", ois.readUTF()); 1242 } 1243 1244 /** 1245 * @tests java.io.ObjectOutputStream#writeObject(java.lang.Object) 1246 */ 1247 @TestTargetNew( 1248 level = TestLevel.COMPLETE, 1249 notes = "", 1250 method = "writeObject", 1251 args = {java.lang.Object.class} 1252 ) 1253 public void test_writeObject_Exception() throws ClassNotFoundException, IOException { 1254 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); 1255 ObjectOutputStream oos = new ObjectOutputStream(baos); 1256 1257 try { 1258 oos.writeObject(new Object()); 1259 fail("should throw ObjectStreamException"); 1260 } catch (ObjectStreamException e) { 1261 // expected 1262 } finally { 1263 oos.close(); 1264 baos.close(); 1265 } 1266 1267 byte[] bytes = baos.toByteArray(); 1268 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( 1269 bytes)); 1270 try { 1271 ois.readObject(); 1272 fail("should throw WriteAbortedException"); 1273 } catch (WriteAbortedException e) { 1274 // expected 1275 } finally { 1276 ois.close(); 1277 } 1278 } 1279 1280 /** 1281 * Sets up the fixture, for example, open a network connection. This method 1282 * is called before a test is executed. 1283 */ 1284 protected void setUp() throws Exception { 1285 super.setUp(); 1286 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 1287 oos_ioe = new ObjectOutputStream(sos = new Support_OutputStream()); 1288 sos.setThrowsException(true); 1289 } 1290 1291 /** 1292 * Tears down the fixture, for example, close a network connection. This 1293 * method is called after a test is executed. 1294 */ 1295 protected void tearDown() throws Exception { 1296 super.tearDown(); 1297 if (oos != null) { 1298 try { 1299 oos.close(); 1300 } catch (Exception e) {} 1301 } 1302 if (oos_ioe != null) { 1303 try { 1304 oos_ioe.close(); 1305 } catch (Exception e) {} 1306 } 1307 if (f != null && f.exists()) { 1308 if (!f.delete()) { 1309 fail("Error cleaning up files during teardown"); 1310 } 1311 } 1312 } 1313 1314 protected Object reload() throws IOException, ClassNotFoundException { 1315 1316 // Choose the load stream 1317 if (xload || xdump) { 1318 // Load from pre-existing file 1319 ois = new ObjectInputStream(new FileInputStream(xFileName + "-" 1320 + getName() + ".ser")); 1321 } else { 1322 // Just load from memory, we dumped to memory 1323 ois = new ObjectInputStream(new ByteArrayInputStream(bao 1324 .toByteArray())); 1325 } 1326 1327 try { 1328 return ois.readObject(); 1329 } finally { 1330 ois.close(); 1331 } 1332 } 1333 1334 protected void dump(Object o) throws IOException, ClassNotFoundException { 1335 1336 // Choose the dump stream 1337 if (xdump) { 1338 oos = new ObjectOutputStream(new FileOutputStream( 1339 f = new java.io.File(xFileName + "-" + getName() + ".ser"))); 1340 } else { 1341 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 1342 } 1343 1344 // Dump the object 1345 try { 1346 oos.writeObject(o); 1347 } finally { 1348 oos.close(); 1349 } 1350 } 1351 1352 /** 1353 * @tests java.io.ObjectOutputStream#writeInt(int) 1354 * @tests java.io.ObjectOutputStream#writeObject(java.lang.Object) 1355 * @tests java.io.ObjectOutputStream#writeUTF(java.lang.String) 1356 */ 1357 @TestTargets({ 1358 @TestTargetNew( 1359 level = TestLevel.PARTIAL, 1360 notes = "", 1361 method = "writeInt", 1362 args = {int.class} 1363 ), 1364 @TestTargetNew( 1365 level = TestLevel.PARTIAL, 1366 notes = "", 1367 method = "writeObject", 1368 args = {java.lang.Object.class} 1369 ), 1370 @TestTargetNew( 1371 level = TestLevel.PARTIAL, 1372 notes = "", 1373 method = "writeUTF", 1374 args = {java.lang.String.class} 1375 ) 1376 }) 1377 public void testMixPrimitivesAndObjects() throws Exception { 1378 int i = 7; 1379 String s1 = "string 1"; 1380 String s2 = "string 2"; 1381 byte[] bytes = { 1, 2, 3 }; 1382 try { 1383 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 1384 oos.writeInt(i); 1385 oos.writeObject(s1); 1386 oos.writeUTF(s2); 1387 oos.writeObject(bytes); 1388 oos.close(); 1389 1390 ois = new ObjectInputStream(new ByteArrayInputStream(bao 1391 .toByteArray())); 1392 1393 int j = ois.readInt(); 1394 assertTrue("Wrong int :" + j, i == j); 1395 1396 String l1 = (String) ois.readObject(); 1397 assertTrue("Wrong obj String :" + l1, s1.equals(l1)); 1398 1399 String l2 = ois.readUTF(); 1400 assertTrue("Wrong UTF String :" + l2, s2.equals(l2)); 1401 1402 byte[] bytes2 = (byte[]) ois.readObject(); 1403 assertTrue("Wrong byte[]", Arrays.equals(bytes, bytes2)); 1404 } finally { 1405 try { 1406 if (oos != null) 1407 oos.close(); 1408 if (ois != null) 1409 ois.close(); 1410 } catch (IOException e) {} 1411 } 1412 } 1413 1414 /** 1415 * @tests java.io.ObjectOutputStream#writeUnshared(java.lang.Object) 1416 */ 1417 @TestTargetNew( 1418 level = TestLevel.COMPLETE, 1419 notes = "", 1420 method = "writeUnshared", 1421 args = {java.lang.Object.class} 1422 ) 1423 public void test_writeUnshared() throws Exception { 1424 //Regression for HARMONY-187 1425 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1426 ObjectOutputStream oos = new ObjectOutputStream(baos); 1427 1428 Object o = "foobar"; 1429 oos.writeObject(o); 1430 oos.writeUnshared(o); 1431 oos.writeObject(o); 1432 oos.flush(); 1433 1434 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream (baos.toByteArray())); 1435 1436 Object[] oa = new Object[3]; 1437 for (int i = 0; i < oa.length; i++) { 1438 oa[i] = ois.readObject(); 1439 } 1440 1441 oos.close(); 1442 ois.close(); 1443 1444 // All three conditions must be met 1445 assertNotSame("oa[0] != oa[1]", oa[0], oa[1]); 1446 assertNotSame("oa[1] != oa[2]", oa[1], oa[2]); 1447 assertSame("oa[0] == oa[2]", oa[0], oa[2]); 1448 } 1449 1450 /** 1451 * @tests java.io.ObjectOutputStream#writeUnshared(java.lang.Object) 1452 */ 1453 @TestTargetNew( 1454 level = TestLevel.COMPLETE, 1455 notes = "", 1456 method = "writeUnshared", 1457 args = {java.lang.Object.class} 1458 ) 1459 public void test_writeUnshared2() throws Exception { 1460 //Regression for HARMONY-187 1461 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1462 ObjectOutputStream oos = new ObjectOutputStream(baos); 1463 1464 Object o = new Object[1]; 1465 oos.writeObject(o); 1466 oos.writeUnshared(o); 1467 oos.writeObject(o); 1468 oos.flush(); 1469 1470 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream (baos.toByteArray())); 1471 1472 Object[] oa = new Object[3]; 1473 for (int i = 0; i < oa.length; i++) { 1474 oa[i] = ois.readObject(); 1475 } 1476 1477 oos.close(); 1478 ois.close(); 1479 1480 // All three conditions must be met 1481 assertNotSame("oa[0] != oa[1]", oa[0], oa[1]); 1482 assertNotSame("oa[1] != oa[2]", oa[1], oa[2]); 1483 assertSame("oa[0] == oa[2]", oa[0], oa[2]); 1484 } 1485 1486 protected Object dumpAndReload(Object o) throws IOException, 1487 ClassNotFoundException { 1488 dump(o); 1489 return reload(); 1490 } 1491 1492 /** 1493 * @tests java.io.ObjectOutputStream#useProtocolVersion(int) 1494 */ 1495 @TestTargetNew( 1496 level = TestLevel.PARTIAL, 1497 notes = "IOException & IllegalStateException checking missed.", 1498 method = "useProtocolVersion", 1499 args = {int.class} 1500 ) 1501 public void test_useProtocolVersionI_2() throws Exception { 1502 ObjectOutputStream oos = new ObjectOutputStream( 1503 new ByteArrayOutputStream()); 1504 1505 oos.useProtocolVersion(ObjectOutputStream.PROTOCOL_VERSION_1); 1506 oos.useProtocolVersion(ObjectOutputStream.PROTOCOL_VERSION_2); 1507 try { 1508 oos.useProtocolVersion(3); 1509 fail("Protocol 3 should not be accepted"); 1510 } catch (IllegalArgumentException e) { 1511 // expected 1512 } finally { 1513 oos.close(); 1514 } 1515 } 1516 1517 /** 1518 * @tests java.io.ObjectOutputStream#replaceObject(java.lang.Object) 1519 */ 1520 @TestTargetNew( 1521 level = TestLevel.COMPLETE, 1522 notes = "", 1523 method = "replaceObject", 1524 args = {java.lang.Object.class} 1525 ) 1526 public void test_replaceObject() throws Exception { 1527 //Regression for HARMONY-1429 1528 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1529 ObjectOutputStreamWithReplace oos = new ObjectOutputStreamWithReplace(baos); 1530 1531 oos.writeObject(new NotSerializable()); 1532 oos.flush(); 1533 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream (baos.toByteArray())); 1534 Object obj = ois.readObject(); 1535 oos.close(); 1536 ois.close(); 1537 assertTrue("replaceObject has not been called", (obj instanceof Long)); 1538 1539 //Regression for HARMONY-2239 1540 Object replaceObject = int.class; 1541 baos = new ByteArrayOutputStream(); 1542 ObjectOutputStreamWithReplace2 oos2 = new ObjectOutputStreamWithReplace2( 1543 baos); 1544 oos2.writeObject(new WriteReplaceObject(replaceObject)); 1545 oos2.flush(); 1546 ois = new ObjectInputStream( 1547 new ByteArrayInputStream(baos.toByteArray())); 1548 obj = ois.readObject(); 1549 oos.close(); 1550 ois.close(); 1551 assertTrue("replaceObject has not been called", (obj instanceof Long)); 1552 1553 replaceObject = ObjectStreamClass.lookup(Integer.class); 1554 baos = new ByteArrayOutputStream(); 1555 oos2 = new ObjectOutputStreamWithReplace2(baos); 1556 oos2.writeObject(new WriteReplaceObject(replaceObject)); 1557 oos2.flush(); 1558 ois = new ObjectInputStream( 1559 new ByteArrayInputStream(baos.toByteArray())); 1560 obj = ois.readObject(); 1561 oos.close(); 1562 ois.close(); 1563 assertTrue("replaceObject has not been called", (obj instanceof Long)); 1564 1565 replaceObject = WriteReplaceObject.Color.red; 1566 baos = new ByteArrayOutputStream(); 1567 oos2 = new ObjectOutputStreamWithReplace2(baos); 1568 oos2.writeObject(new WriteReplaceObject(replaceObject)); 1569 oos2.flush(); 1570 ois = new ObjectInputStream( 1571 new ByteArrayInputStream(baos.toByteArray())); 1572 obj = ois.readObject(); 1573 oos.close(); 1574 ois.close(); 1575 assertTrue("replaceObject has not been called", (obj instanceof Long)); 1576 1577 // Regression for HARMONY-3158 1578 Object obj1; 1579 Object obj2; 1580 Object obj3; 1581 1582 baos = new ByteArrayOutputStream(); 1583 oos = new ObjectOutputStreamWithReplace(baos); 1584 1585 oos.writeObject(new Integer(99)); 1586 oos.writeObject(Integer.class); 1587 oos.writeObject(ObjectStreamClass.lookup(Integer.class)); 1588 oos.flush(); 1589 1590 ois = new ObjectInputStream(new ByteArrayInputStream (baos.toByteArray())); 1591 obj1 = ois.readObject(); 1592 obj2 = ois.readObject(); 1593 obj3 = ois.readObject(); 1594 oos.close(); 1595 ois.close(); 1596 1597 assertTrue("1st replaceObject worked incorrectly", obj1 instanceof Long); 1598 assertEquals("1st replaceObject worked incorrectly", 1599 99, ((Long) obj1).longValue()); 1600 assertEquals("2nd replaceObject worked incorrectly", Integer.class, obj2); 1601 assertEquals("3rd replaceObject worked incorrectly", 1602 ObjectStreamClass.class, obj3.getClass()); 1603 } 1604} 1605