SerializationStressTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import java.io.ByteArrayInputStream; 26import java.io.ByteArrayOutputStream; 27import java.io.DataInputStream; 28import java.io.FileInputStream; 29import java.io.FileOutputStream; 30import java.io.IOException; 31import java.io.InputStream; 32import java.io.InvalidObjectException; 33import java.io.NotActiveException; 34import java.io.ObjectInputStream; 35import java.io.ObjectOutputStream; 36import java.io.ObjectStreamClass; 37import java.io.ObjectStreamException; 38import java.io.Serializable; 39import java.io.StreamCorruptedException; 40import java.io.WriteAbortedException; 41import java.security.Permission; 42import java.security.PermissionCollection; 43import java.util.ArrayList; 44import java.util.Arrays; 45import java.util.Calendar; 46import java.util.GregorianCalendar; 47import java.util.HashMap; 48import java.util.HashSet; 49import java.util.Hashtable; 50import java.util.IdentityHashMap; 51import java.util.LinkedHashMap; 52import java.util.LinkedHashSet; 53import java.util.LinkedList; 54import java.util.List; 55import java.util.Map; 56import java.util.PropertyPermission; 57import java.util.Set; 58import java.util.SimpleTimeZone; 59import java.util.SortedMap; 60import java.util.SortedSet; 61import java.util.TimeZone; 62import java.util.TreeMap; 63import java.util.TreeSet; 64import java.util.Vector; 65 66/** 67 * Automated Test Suite for class java.io.ObjectOutputStream 68 * 69 */ 70@TestTargetClass(Serializable.class) 71public class SerializationStressTest extends junit.framework.TestCase implements 72 Serializable { 73 74 // protected static final String MODE_XLOAD = "xload"; 75 76 // protected static final String MODE_XDUMP = "xdump"; 77 78 static final String FOO = "foo"; 79 80 static final String MSG_TEST_FAILED = "Failed to write/read/assertion checking: "; 81 82 protected static final boolean DEBUG = false; 83 84 protected static boolean xload = false; 85 86 protected static boolean xdump = false; 87 88 protected static String xFileName = null; 89 90 protected transient int dumpCount = 0; 91 92 protected transient ObjectInputStream ois; 93 94 protected transient ObjectOutputStream oos; 95 96 protected transient ByteArrayOutputStream bao; 97 98 // ----------------------------------------------------------------------------------- 99 100 private static class ObjectInputStreamSubclass extends ObjectInputStream { 101 private Vector resolvedClasses = new Vector(); 102 103 public ObjectInputStreamSubclass(InputStream in) throws IOException, 104 StreamCorruptedException { 105 super(in); 106 } 107 108 public Class resolveClass(ObjectStreamClass osClass) 109 throws IOException, ClassNotFoundException { 110 Class result = super.resolveClass(osClass); 111 resolvedClasses.addElement(result); 112 return result; 113 } 114 115 public Class[] resolvedClasses() { 116 return (Class[]) resolvedClasses.toArray(new Class[resolvedClasses 117 .size()]); 118 } 119 } 120 121 static final Map TABLE = new Hashtable(); 122 123 static final Map MAP = new HashMap(); 124 125 static final SortedMap TREE = new TreeMap(); 126 127 static final LinkedHashMap LINKEDMAP = new LinkedHashMap(); 128 129 static final LinkedHashSet LINKEDSET = new LinkedHashSet(); 130 131 static final IdentityHashMap IDENTITYMAP = new IdentityHashMap(); 132 133 static final List ALIST = Arrays.asList(new String[] { "a", "list", "of", 134 "strings" }); 135 136 static final List LIST = new ArrayList(ALIST); 137 138 static final Set SET = new HashSet(Arrays.asList(new String[] { "one", 139 "two", "three" })); 140 141 static final Permission PERM = new PropertyPermission("file.encoding", 142 "write"); 143 144 static final PermissionCollection PERMCOL = PERM.newPermissionCollection(); 145 146 static final SortedSet SORTSET = new TreeSet(Arrays.asList(new String[] { 147 "one", "two", "three" })); 148 149 static final java.text.DateFormat DATEFORM = java.text.DateFormat 150 .getInstance(); 151 152 static final java.text.ChoiceFormat CHOICE = new java.text.ChoiceFormat( 153 "1#one|2#two|3#three"); 154 155 static final java.text.NumberFormat NUMBERFORM = java.text.NumberFormat 156 .getInstance(); 157 158 static final java.text.MessageFormat MESSAGE = new java.text.MessageFormat( 159 "the time: {0,time} and date {0,date}"); 160 161 static final LinkedList LINKEDLIST = new LinkedList(Arrays 162 .asList(new String[] { "a", "linked", "list", "of", "strings" })); 163 164 static final SimpleTimeZone TIME_ZONE = new SimpleTimeZone(3600000, 165 "S-TEST"); 166 167 static final Calendar CALENDAR = new GregorianCalendar(TIME_ZONE); 168 169 static Exception INITIALIZE_EXCEPTION = null; 170 171 static { 172 try { 173 TABLE.put("one", "1"); 174 TABLE.put("two", "2"); 175 TABLE.put("three", "3"); 176 MAP.put("one", "1"); 177 MAP.put("two", "2"); 178 MAP.put("three", "3"); 179 LINKEDMAP.put("one", "1"); 180 LINKEDMAP.put("two", "2"); 181 LINKEDMAP.put("three", "3"); 182 IDENTITYMAP.put("one", "1"); 183 IDENTITYMAP.put("two", "2"); 184 IDENTITYMAP.put("three", "3"); 185 LINKEDSET.add("one"); 186 LINKEDSET.add("two"); 187 LINKEDSET.add("three"); 188 TREE.put("one", "1"); 189 TREE.put("two", "2"); 190 TREE.put("three", "3"); 191 PERMCOL.add(PERM); 192 // To make sure they all use the same Calendar 193 CALENDAR.setTimeZone(new SimpleTimeZone(0, "GMT")); 194 CALENDAR.set(1999, Calendar.JUNE, 23, 15, 47, 13); 195 CALENDAR.set(Calendar.MILLISECOND, 553); 196 DATEFORM.setCalendar(CALENDAR); 197 java.text.DateFormatSymbols symbols = new java.text.DateFormatSymbols(); 198 symbols.setZoneStrings(new String[][] { { "a", "b", "c", "d" }, 199 { "e", "f", "g", "h" } }); 200 ((java.text.SimpleDateFormat) DATEFORM).setDateFormatSymbols(symbols); 201 DATEFORM.setNumberFormat(new java.text.DecimalFormat("#.#;'-'#.#")); 202 DATEFORM.setTimeZone(TimeZone.getTimeZone("EST")); 203 ((java.text.DecimalFormat) NUMBERFORM).applyPattern("#.#;'-'#.#"); 204 MESSAGE.setFormat(0, DATEFORM); 205 MESSAGE.setFormat(1, DATEFORM); 206 } catch (Exception e) { 207 INITIALIZE_EXCEPTION = e; 208 } 209 } 210 211 public SerializationStressTest() { 212 } 213 214 public SerializationStressTest(String name) { 215 super(name); 216 } 217 218 public String getDumpName() { 219 return getName() + dumpCount; 220 } 221 222 protected void dump(Object o) throws IOException, ClassNotFoundException { 223 if (dumpCount > 0) 224 setUp(); 225 // Dump the object 226 try { 227 oos.writeObject(o); 228 } finally { 229 oos.close(); 230 } 231 } 232 233 protected Object dumpAndReload(Object o) throws IOException, 234 ClassNotFoundException { 235 dump(o); 236 return reload(); 237 } 238 239 protected InputStream loadStream() throws IOException { 240 // Choose the load stream 241 if (xload || xdump) { 242 // Load from pre-existing file 243 return new FileInputStream(xFileName + "-" + getDumpName() + ".ser"); 244 } else { 245 // Just load from memory, we dumped to memory 246 return new ByteArrayInputStream(bao.toByteArray()); 247 } 248 } 249 250 protected Object reload() throws IOException, ClassNotFoundException { 251 ois = new ObjectInputStream(loadStream()); 252 dumpCount++; 253 try { 254 return ois.readObject(); 255 } finally { 256 ois.close(); 257 } 258 } 259 260 /** 261 * Sets up the fixture, for example, open a network connection. This method 262 * is called before a test is executed. 263 */ 264 protected void setUp() { 265 if (INITIALIZE_EXCEPTION != null) { 266 throw new ExceptionInInitializerError(INITIALIZE_EXCEPTION); 267 } 268 try { 269 if (xdump) { 270 oos = new ObjectOutputStream(new FileOutputStream(xFileName 271 + "-" + getDumpName() + ".ser")); 272 } else { 273 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 274 } 275 } catch (Exception e) { 276 fail("Exception thrown during setup : " + e.getMessage()); 277 } 278 } 279 280 /** 281 * Tears down the fixture, for example, close a network connection. This 282 * method is called after a test is executed. 283 */ 284 protected void tearDown() { 285 if (oos != null) { 286 try { 287 oos.close(); 288 } catch (Exception e) { 289 } 290 } 291 } 292 293 @TestTargetNew( 294 level = TestLevel.COMPLETE, 295 notes = "Verifies serialization.", 296 method = "!Serialization", 297 args = {} 298 ) 299 public void test_1_Constructor() { 300 // Test for method java.io.ObjectOutputStream(java.io.OutputStream) 301 302 try { 303 oos.close(); 304 oos = new ObjectOutputStream(new ByteArrayOutputStream()); 305 oos.close(); 306 } catch (Exception e) { 307 fail("Failed to create ObjectOutputStream : " + e.getMessage()); 308 } 309 } 310 311 @TestTargetNew( 312 level = TestLevel.COMPLETE, 313 notes = "Verifies serialization.", 314 method = "!Serialization", 315 args = {} 316 ) 317 public void test_2_close() { 318 // Test for method void java.io.ObjectOutputStream.close() 319 try { 320 oos.close(); 321 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 322 oos.close(); 323 oos.writeChar('T'); 324 oos.writeObject(FOO); 325 // Writing to a closed stream does not cause problems. This is 326 // the expected behavior 327 } catch (IOException e) { 328 fail("Operation on closed stream threw IOException : " 329 + e.getMessage()); 330 } 331 } 332 333 @TestTargetNew( 334 level = TestLevel.COMPLETE, 335 notes = "Verifies serialization.", 336 method = "!Serialization", 337 args = {} 338 ) 339 public void test_3_defaultWriteObject() { 340 // Test for method void java.io.ObjectOutputStream.defaultWriteObject() 341 342 try { 343 oos.defaultWriteObject(); 344 } catch (NotActiveException e) { 345 // Correct 346 return; 347 } catch (IOException e) { 348 } 349 fail( 350 "Failed to throw NotActiveException when invoked outside readObject"); 351 } 352 353 @TestTargetNew( 354 level = TestLevel.COMPLETE, 355 notes = "Verifies serialization.", 356 method = "!Serialization", 357 args = {} 358 ) 359 public void test_4_flush() { 360 // Test for method void java.io.ObjectOutputStream.flush() 361 try { 362 oos.close(); 363 oos = new ObjectOutputStream(bao = new ByteArrayOutputStream()); 364 int size = bao.size(); 365 oos.writeByte(127); 366 assertTrue("Data flushed already", bao.size() == size); 367 oos.flush(); 368 assertTrue("Failed to flush data", bao.size() > size); 369 // we don't know how many bytes are actually written for 1 byte, 370 // so we test > <before> 371 oos.close(); 372 oos = null; 373 } catch (IOException e) { 374 fail("IOException serializing data : " + e.getMessage()); 375 } 376 } 377 378 @TestTargetNew( 379 level = TestLevel.COMPLETE, 380 notes = "Verifies serialization.", 381 method = "!Serialization", 382 args = {} 383 ) 384 public void test_5_reset() { 385 // Test for method void java.io.ObjectOutputStream.reset() 386 try { 387 String o = "HelloWorld"; 388 oos.writeObject(o); 389 oos.writeObject(o); 390 oos.reset(); 391 oos.writeObject(o); 392 ois = new ObjectInputStream(loadStream()); 393 ois.close(); 394 } catch (IOException e) { 395 fail("IOException serializing data : " + e.getMessage()); 396 } 397 } 398 399 @TestTargetNew( 400 level = TestLevel.COMPLETE, 401 notes = "Verifies serialization.", 402 method = "!Serialization", 403 args = {} 404 ) 405 public void test_6_write() { 406 // Test for method void java.io.ObjectOutputStream.write(byte [], int, 407 // int) 408 try { 409 byte[] buf = new byte[255]; 410 byte[] output = new byte[255]; 411 for (int i = 0; i < output.length; i++) 412 output[i] = (byte) i; 413 oos.write(output, 0, output.length); 414 oos.close(); 415 ois = new ObjectInputStream(loadStream()); 416 ois.readFully(buf); 417 ois.close(); 418 for (int i = 0; i < output.length; i++) 419 if (buf[i] != output[i]) 420 fail("Read incorrect byte: " + i); 421 } catch (IOException e) { 422 fail("IOException serializing data : " + e.getMessage()); 423 } 424 } 425 426 @TestTargetNew( 427 level = TestLevel.COMPLETE, 428 notes = "Verifies serialization.", 429 method = "!Serialization", 430 args = {} 431 ) 432 public void test_6a_write() { 433 // Test for method void java.io.ObjectOutputStream.write(byte [], int, 434 // int) 435 try { 436 byte[] buf = new byte[256]; 437 byte[] output = new byte[256]; 438 for (int i = 0; i < output.length; i++) 439 output[i] = (byte) (i & 0xff); 440 oos.write(output, 0, output.length); 441 oos.close(); 442 ois = new ObjectInputStream(loadStream()); 443 ois.readFully(buf); 444 ois.close(); 445 for (int i = 0; i < output.length; i++) 446 if (buf[i] != output[i]) 447 fail("Read incorrect byte: " + i); 448 } catch (IOException e) { 449 fail("IOException serializing data : " + e.getMessage()); 450 } 451 } 452 453 @TestTargetNew( 454 level = TestLevel.COMPLETE, 455 notes = "Verifies serialization.", 456 method = "!Serialization", 457 args = {} 458 ) 459 public void test_7_write() { 460 // Test for method void java.io.ObjectOutputStream.write(int) 461 try { 462 byte[] buf = new byte[10]; 463 oos.write('T'); 464 oos.close(); 465 ois = new ObjectInputStream(loadStream()); 466 assertEquals("Read incorrect byte", 'T', ois.read()); 467 ois.close(); 468 } catch (IOException e) { 469 fail("IOException serializing data : " + e.getMessage()); 470 } 471 } 472 473 @TestTargetNew( 474 level = TestLevel.COMPLETE, 475 notes = "Verifies serialization.", 476 method = "!Serialization", 477 args = {} 478 ) 479 public void test_8_write() { 480 // Test for method void java.io.ObjectOutputStream.write(byte []) 481 try { 482 byte[] buf = new byte[10]; 483 oos.write("HelloWorld".getBytes()); 484 oos.close(); 485 ois = new ObjectInputStream(loadStream()); 486 ois.read(buf, 0, 10); 487 ois.close(); 488 assertEquals("Read incorrect bytes", "HelloWorld", new String(buf, 0, 10) 489 ); 490 } catch (IOException e) { 491 fail("IOException serializing data : " + e.getMessage()); 492 } 493 } 494 495 @TestTargetNew( 496 level = TestLevel.COMPLETE, 497 notes = "Verifies serialization.", 498 method = "!Serialization", 499 args = {} 500 ) 501 public void test_9_writeBoolean() { 502 // Test for method void java.io.ObjectOutputStream.writeBoolean(boolean) 503 try { 504 oos.writeBoolean(true); 505 oos.close(); 506 ois = new ObjectInputStream(loadStream()); 507 assertTrue("Wrote incorrect byte value", ois.readBoolean()); 508 } catch (IOException e) { 509 fail("IOException serializing data : " + e.getMessage()); 510 } 511 } 512 513 @TestTargetNew( 514 level = TestLevel.COMPLETE, 515 notes = "Verifies serialization.", 516 method = "!Serialization", 517 args = {} 518 ) 519 public void test_10_writeByte() { 520 // Test for method void java.io.ObjectOutputStream.writeByte(int) 521 try { 522 oos.writeByte(127); 523 oos.close(); 524 ois = new ObjectInputStream(loadStream()); 525 assertEquals("Wrote incorrect byte value", 127, ois.readByte()); 526 } catch (IOException e) { 527 fail("IOException serializing data : " + e.getMessage()); 528 } 529 } 530 531 @TestTargetNew( 532 level = TestLevel.COMPLETE, 533 notes = "Verifies serialization.", 534 method = "!Serialization", 535 args = {} 536 ) 537 public void test_11_writeBytes() { 538 // Test for method void 539 // java.io.ObjectOutputStream.writeBytes(java.lang.String) 540 try { 541 byte[] buf = new byte[10]; 542 oos.writeBytes("HelloWorld"); 543 oos.close(); 544 ois = new ObjectInputStream(loadStream()); 545 ois.readFully(buf); 546 ois.close(); 547 assertEquals("Wrote incorrect bytes value", "HelloWorld", new String(buf, 0, 10) 548 ); 549 } catch (IOException e) { 550 fail("IOException serializing data : " + e.getMessage()); 551 } 552 } 553 554 @TestTargetNew( 555 level = TestLevel.COMPLETE, 556 notes = "Verifies serialization.", 557 method = "!Serialization", 558 args = {} 559 ) 560 public void test_12_writeChar() { 561 // Test for method void java.io.ObjectOutputStream.writeChar(int) 562 try { 563 oos.writeChar('T'); 564 oos.close(); 565 ois = new ObjectInputStream(loadStream()); 566 assertEquals("Wrote incorrect char value", 'T', ois.readChar()); 567 } catch (IOException e) { 568 fail("IOException serializing data : " + e.getMessage()); 569 } 570 } 571 572 @TestTargetNew( 573 level = TestLevel.COMPLETE, 574 notes = "Verifies serialization.", 575 method = "!Serialization", 576 args = {} 577 ) 578 public void test_13_writeChars() { 579 // Test for method void 580 // java.io.ObjectOutputStream.writeChars(java.lang.String) 581 try { 582 int avail = 0; 583 char[] buf = new char[10]; 584 oos.writeChars("HelloWorld"); 585 oos.close(); 586 ois = new ObjectInputStream(loadStream()); 587 // Number of prim data bytes in stream / 2 to give char index 588 avail = ois.available() / 2; 589 for (int i = 0; i < avail; ++i) 590 buf[i] = ois.readChar(); 591 ois.close(); 592 assertEquals("Wrote incorrect chars", "HelloWorld", new String(buf, 0, 10) 593 ); 594 } catch (IOException e) { 595 fail("IOException serializing data : " + e.getMessage()); 596 } 597 } 598 599 @TestTargetNew( 600 level = TestLevel.COMPLETE, 601 notes = "Verifies serialization.", 602 method = "!Serialization", 603 args = {} 604 ) 605 public void test_14_writeDouble() { 606 // Test for method void java.io.ObjectOutputStream.writeDouble(double) 607 try { 608 oos.writeDouble(Double.MAX_VALUE); 609 oos.close(); 610 ois = new ObjectInputStream(loadStream()); 611 assertTrue("Wrote incorrect double value", 612 ois.readDouble() == Double.MAX_VALUE); 613 } catch (IOException e) { 614 fail("IOException serializing data : " + e.getMessage()); 615 } 616 } 617 618 @TestTargetNew( 619 level = TestLevel.COMPLETE, 620 notes = "Verifies serialization.", 621 method = "!Serialization", 622 args = {} 623 ) 624 public void test_15_writeFloat() { 625 // Test for method void java.io.ObjectOutputStream.writeFloat(float) 626 try { 627 oos.writeFloat(Float.MAX_VALUE); 628 oos.close(); 629 ois = new ObjectInputStream(loadStream()); 630 assertTrue("Wrote incorrect double value", 631 ois.readFloat() == Float.MAX_VALUE); 632 ois.close(); 633 ois = null; 634 } catch (IOException e) { 635 fail("IOException serializing data : " + e.getMessage()); 636 } 637 } 638 639 @TestTargetNew( 640 level = TestLevel.COMPLETE, 641 notes = "Verifies serialization.", 642 method = "!Serialization", 643 args = {} 644 ) 645 public void test_16_writeInt() { 646 // Test for method void java.io.ObjectOutputStream.writeInt(int) 647 try { 648 oos.writeInt(Integer.MAX_VALUE); 649 oos.close(); 650 ois = new ObjectInputStream(loadStream()); 651 assertTrue("Wrote incorrect double value", 652 ois.readInt() == Integer.MAX_VALUE); 653 ois.close(); 654 } catch (IOException e) { 655 fail("IOException serializing data : " + e.getMessage()); 656 } 657 } 658 659 @TestTargetNew( 660 level = TestLevel.COMPLETE, 661 notes = "Verifies serialization.", 662 method = "!Serialization", 663 args = {} 664 ) 665 public void test_17_writeLong() { 666 // Test for method void java.io.ObjectOutputStream.writeLong(long) 667 try { 668 oos.writeLong(Long.MAX_VALUE); 669 oos.close(); 670 ois = new ObjectInputStream(loadStream()); 671 assertTrue("Wrote incorrect double value", 672 ois.readLong() == Long.MAX_VALUE); 673 } catch (IOException e) { 674 fail("IOException serializing data : " + e.getMessage()); 675 } 676 } 677 678 @TestTargetNew( 679 level = TestLevel.COMPLETE, 680 notes = "Verifies serialization.", 681 method = "!Serialization", 682 args = {} 683 ) 684 public void test_19_writeShort() { 685 // Test for method void java.io.ObjectOutputStream.writeShort(int) 686 try { 687 oos.writeShort(127); 688 oos.close(); 689 ois = new ObjectInputStream(loadStream()); 690 assertEquals("Wrote incorrect short value", 127, ois.readShort()); 691 } catch (IOException e) { 692 fail("IOException serializing data : " + e.getMessage()); 693 } 694 } 695 696 @TestTargetNew( 697 level = TestLevel.COMPLETE, 698 notes = "Verifies serialization.", 699 method = "!Serialization", 700 args = {} 701 ) 702 public void test_20_writeUTF() { 703 // Test for method void 704 // java.io.ObjectOutputStream.writeUTF(java.lang.String) 705 try { 706 oos.writeUTF("HelloWorld"); 707 oos.close(); 708 ois = new ObjectInputStream(loadStream()); 709 assertEquals("Wrote incorrect UTF value", 710 "HelloWorld", ois.readUTF()); 711 } catch (IOException e) { 712 fail("IOException serializing data : " + e.getMessage()); 713 } 714 } 715 716 @TestTargetNew( 717 level = TestLevel.COMPLETE, 718 notes = "Verifies serialization.", 719 method = "!Serialization", 720 args = {} 721 ) 722 public void test_25_available() { 723 try { 724 oos.writeObject(FOO); 725 oos.writeObject(FOO); 726 oos.flush(); 727 int available1 = 0; 728 int available2 = 0; 729 Object obj1 = null; 730 Object obj2 = null; 731 ObjectInputStream ois = new ObjectInputStream(loadStream()); 732 available1 = ois.available(); 733 obj1 = ois.readObject(); 734 available2 = ois.available(); 735 obj2 = ois.readObject(); 736 737 assertEquals("available returned incorrect value", 0, available1); 738 assertEquals("available returned incorrect value", 0, available2); 739 740 assertTrue("available caused incorrect reading", FOO.equals(obj1)); 741 assertTrue("available returned incorrect value", FOO.equals(obj2)); 742 743 } catch (IOException e) { 744 fail("IOException serializing object : " + e.getMessage()); 745 } catch (ClassNotFoundException e) { 746 fail("Unable to read Object type : " + e.toString()); 747 } catch (Error err) { 748 System.out.println("Error " + err); 749 throw err; 750 } 751 752 } 753 754 protected void t_MixPrimitivesAndObjects() throws IOException, 755 ClassNotFoundException { 756 int i = 7; 757 String s1 = "string 1"; 758 String s2 = "string 2"; 759 byte[] bytes = { 1, 2, 3 }; 760 761 oos.writeInt(i); 762 oos.writeObject(s1); 763 oos.writeUTF(s2); 764 oos.writeObject(bytes); 765 oos.close(); 766 try { 767 ois = new ObjectInputStream(loadStream()); 768 769 int j = ois.readInt(); 770 assertTrue("Wrong int :" + j, i == j); 771 772 String l1 = (String) ois.readObject(); 773 assertTrue("Wrong obj String :" + l1, s1.equals(l1)); 774 775 String l2 = (String) ois.readUTF(); 776 assertTrue("Wrong UTF String :" + l2, s2.equals(l2)); 777 778 byte[] bytes2 = (byte[]) ois.readObject(); 779 assertTrue("Wrong byte[]", Arrays.equals(bytes, bytes2)); 780 781 } finally { 782 ois.close(); 783 } 784 } 785 786 @TestTargetNew( 787 level = TestLevel.COMPLETE, 788 notes = "Verifies serialization.", 789 method = "!Serialization", 790 args = {} 791 ) 792 public void test_resolveClass() { 793 try { 794 oos.writeObject(new Object[] { Integer.class, new Integer(1) }); 795 oos.close(); 796 797 ois = new ObjectInputStreamSubclass(loadStream()); 798 ois.readObject(); 799 ois.close(); 800 } catch (IOException e1) { 801 fail("IOException : " + e1.getMessage()); 802 } catch (ClassNotFoundException e2) { 803 fail("ClassNotFoundException : " + e2.getMessage()); 804 } 805 806 Class[] resolvedClasses = ((ObjectInputStreamSubclass) ois) 807 .resolvedClasses(); 808 assertEquals("missing resolved", 3, resolvedClasses.length); 809 assertTrue("resolved class 1", resolvedClasses[0] == Object[].class); 810 assertTrue("resolved class 2", resolvedClasses[1] == Integer.class); 811 assertTrue("resolved class 3", resolvedClasses[2] == Number.class); 812 } 813 814 @TestTargetNew( 815 level = TestLevel.COMPLETE, 816 notes = "Verifies serialization.", 817 method = "!Serialization", 818 args = {} 819 ) 820 public void test_reset() { 821 try { 822 oos.reset(); 823 oos.writeObject("R"); 824 oos.reset(); 825 oos.writeByte(24); 826 oos.close(); 827 828 DataInputStream dis = new DataInputStream(loadStream()); 829 byte[] input = new byte[dis.available()]; 830 dis.readFully(input); 831 byte[] result = new byte[] { (byte) 0xac, (byte) 0xed, (byte) 0, 832 (byte) 5, (byte) 0x79, (byte) 0x74, (byte) 0, (byte) 1, 833 (byte) 'R', (byte) 0x79, (byte) 0x77, (byte) 1, (byte) 24 }; 834 assertTrue("incorrect output", Arrays.equals(input, result)); 835 836 ois = new ObjectInputStreamSubclass(loadStream()); 837 assertEquals("Wrong result from readObject()", "R", ois.readObject() 838 ); 839 assertEquals("Wrong result from readByte()", 24, ois.readByte()); 840 ois.close(); 841 } catch (IOException e1) { 842 fail("IOException : " + e1.getMessage()); 843 } catch (ClassNotFoundException e2) { 844 fail("ClassNotFoundException : " + e2.getMessage()); 845 } 846 } 847 848 @TestTargetNew( 849 level = TestLevel.COMPLETE, 850 notes = "Verifies serialization.", 851 method = "!Serialization", 852 args = {} 853 ) 854 public void test_serialVersionUID(Class clazz, long svUID) { 855 final String idWrong = "serialVersionUID is wrong for: "; 856 long reflectedSvUID = 0L; 857 try { 858 reflectedSvUID = clazz.getField("serialVersionUID").getLong(null); 859 } catch (Exception e) { 860 fail("Unable to determine serialVersionUID of " + clazz); 861 } 862 assertTrue(idWrong + clazz + ": " + reflectedSvUID + " does not equal " 863 + svUID, reflectedSvUID == svUID); 864 } 865 866 private static class ResolveObjectTest implements Serializable { 867 Object field1, field2; 868 } 869 870 private static class ResolveObjectInputStream extends ObjectInputStream { 871 ResolveObjectInputStream(InputStream in) 872 throws StreamCorruptedException, IOException { 873 super(in); 874 } 875 876 public void enableResolve() { 877 enableResolveObject(true); 878 } 879 880 public Object resolveObject(Object obj) { 881 if (obj instanceof Vector) // test_1_resolveObject() 882 return new Hashtable(); 883 else if ("abc".equals(obj)) // test_2_resolveObject() 884 return "ABC"; 885 else if (obj instanceof String) // test_3_resolveObject() 886 return String.valueOf(((String) obj).length()); 887 else if (obj instanceof int[]) // test_4_resolveObject() 888 return new Object[1]; 889 else if (obj instanceof Object[] && ((Object[]) obj).length == 2) // test_5_resolveObject() 890 return new char[1]; 891 return obj; 892 } 893 } 894 895 @TestTargetNew( 896 level = TestLevel.COMPLETE, 897 notes = "Verifies serialization.", 898 method = "!Serialization", 899 args = {} 900 ) 901 public void test_1_resolveObject() { 902 try { 903 ResolveObjectTest obj = new ResolveObjectTest(); 904 obj.field1 = new Vector(); 905 obj.field2 = obj.field1; 906 oos.writeObject(obj); 907 oos.close(); 908 ois = new ResolveObjectInputStream(loadStream()); 909 ((ResolveObjectInputStream) ois).enableResolve(); 910 ResolveObjectTest result = null; 911 try { 912 result = (ResolveObjectTest) ois.readObject(); 913 } catch (ClassNotFoundException e) { 914 fail(e.toString()); 915 } 916 assertTrue("Object not resolved", 917 result.field1 instanceof Hashtable); 918 assertTrue("Second reference not resolved", 919 result.field1 == result.field2); 920 } catch (IOException e) { 921 fail("IOException serializing data : " + e.getMessage()); 922 } 923 } 924 925 @TestTargetNew( 926 level = TestLevel.COMPLETE, 927 notes = "Verifies serialization.", 928 method = "!Serialization", 929 args = {} 930 ) 931 public void test_2_resolveObject() { 932 try { 933 ResolveObjectTest obj = new ResolveObjectTest(); 934 obj.field1 = "abc"; 935 obj.field2 = obj.field1; 936 oos.writeObject(obj); 937 oos.close(); 938 ois = new ResolveObjectInputStream(loadStream()); 939 ((ResolveObjectInputStream) ois).enableResolve(); 940 ResolveObjectTest result = null; 941 try { 942 result = (ResolveObjectTest) ois.readObject(); 943 } catch (ClassNotFoundException e) { 944 fail(e.toString()); 945 } 946 assertEquals("String not resolved", "ABC", result.field1); 947 assertTrue("Second reference not resolved", 948 result.field1 == result.field2); 949 } catch (IOException e) { 950 fail("IOException serializing data : " + e.getMessage()); 951 } 952 } 953 954 @TestTargetNew( 955 level = TestLevel.COMPLETE, 956 notes = "Verifies serialization.", 957 method = "!Serialization", 958 args = {} 959 ) 960 public void test_3_resolveObject() { 961 try { 962 ResolveObjectTest obj = new ResolveObjectTest(); 963 char[] lchars = new char[70000]; 964 obj.field1 = new String(lchars); 965 obj.field2 = obj.field1; 966 oos.writeObject(obj); 967 oos.close(); 968 ois = new ResolveObjectInputStream(loadStream()); 969 ((ResolveObjectInputStream) ois).enableResolve(); 970 ResolveObjectTest result = null; 971 try { 972 result = (ResolveObjectTest) ois.readObject(); 973 } catch (ClassNotFoundException e) { 974 fail(e.toString()); 975 } 976 assertTrue("Long String not resolved", "70000" 977 .equals(result.field1)); 978 assertTrue("Second reference not resolved", 979 result.field1 == result.field2); 980 } catch (IOException e) { 981 fail("IOException serializing data : " + e.getMessage()); 982 } 983 } 984 985 @TestTargetNew( 986 level = TestLevel.COMPLETE, 987 notes = "Verifies serialization.", 988 method = "!Serialization", 989 args = {} 990 ) 991 public void test_4_resolveObject() { 992 try { 993 ResolveObjectTest obj = new ResolveObjectTest(); 994 obj.field1 = new int[5]; 995 obj.field2 = obj.field1; 996 oos.writeObject(obj); 997 oos.close(); 998 ois = new ResolveObjectInputStream(loadStream()); 999 ((ResolveObjectInputStream) ois).enableResolve(); 1000 ResolveObjectTest result = null; 1001 try { 1002 result = (ResolveObjectTest) ois.readObject(); 1003 } catch (ClassNotFoundException e) { 1004 fail(e.toString()); 1005 } 1006 Class cl = new Object[0].getClass(); 1007 assertTrue("int[] not resolved", result.field1.getClass() == cl); 1008 assertTrue("Second reference not resolved", 1009 result.field1 == result.field2); 1010 } catch (IOException e) { 1011 fail("IOException serializing data : " + e.getMessage()); 1012 } 1013 } 1014 1015 @TestTargetNew( 1016 level = TestLevel.COMPLETE, 1017 notes = "Verifies serialization.", 1018 method = "!Serialization", 1019 args = {} 1020 ) 1021 public void test_5_resolveObject() { 1022 try { 1023 ResolveObjectTest obj = new ResolveObjectTest(); 1024 obj.field1 = new Object[2]; 1025 obj.field2 = obj.field1; 1026 oos.writeObject(obj); 1027 oos.close(); 1028 ois = new ResolveObjectInputStream(loadStream()); 1029 ((ResolveObjectInputStream) ois).enableResolve(); 1030 ResolveObjectTest result = null; 1031 try { 1032 result = (ResolveObjectTest) ois.readObject(); 1033 } catch (ClassNotFoundException e) { 1034 fail(e.toString()); 1035 } 1036 Class cl = new char[0].getClass(); 1037 assertTrue("int[] not resolved", result.field1.getClass() == cl); 1038 assertTrue("Second reference not resolved", 1039 result.field1 == result.field2); 1040 } catch (IOException e) { 1041 fail("IOException serializing data : " + e.getMessage()); 1042 } 1043 } 1044 1045 static class WriteReplaceTestA implements Serializable { 1046 public Object writeReplace() throws ObjectStreamException { 1047 return new ReadResolveTestB(); 1048 } 1049 } 1050 1051 static class WriteReplaceTestB extends WriteReplaceTestA { 1052 } 1053 1054 static class WriteReplaceTestC extends WriteReplaceTestA { 1055 public Object writeReplace() throws ObjectStreamException { 1056 return new ReadResolveTestC(); 1057 } 1058 } 1059 1060 static class WriteReplaceTestD implements Serializable { 1061 private Object writeReplace() throws ObjectStreamException { 1062 return new ReadResolveTestD(); 1063 } 1064 } 1065 1066 static class WriteReplaceTestE extends WriteReplaceTestD { 1067 } 1068 1069 static class WriteReplaceTestF implements Serializable { 1070 int type, readType; 1071 1072 public WriteReplaceTestF(int type, int readType) { 1073 this.type = type; 1074 this.readType = readType; 1075 } 1076 1077 public Object writeReplace() throws ObjectStreamException { 1078 switch (type) { 1079 case 0: 1080 throw new InvalidObjectException("invalid"); 1081 case 1: 1082 throw new RuntimeException("runtime"); 1083 case 2: 1084 throw new Error("error"); 1085 default: 1086 return new ReadResolveTestE(readType); 1087 } 1088 } 1089 } 1090 1091 static class ReadResolveTestA implements Serializable { 1092 public Object readResolve() throws ObjectStreamException { 1093 return new ReadResolveTestA(); 1094 } 1095 } 1096 1097 static class ReadResolveTestB extends ReadResolveTestA { 1098 } 1099 1100 static class ReadResolveTestC implements Serializable { 1101 private Object readResolve() throws ObjectStreamException { 1102 return new ReadResolveTestB(); 1103 } 1104 } 1105 1106 static class ReadResolveTestD extends ReadResolveTestC { 1107 } 1108 1109 static class ReadResolveTestE implements Serializable { 1110 int type; 1111 1112 public ReadResolveTestE(int type) { 1113 this.type = type; 1114 } 1115 1116 public Object readResolve() throws ObjectStreamException { 1117 switch (type) { 1118 case 0: 1119 throw new InvalidObjectException("invalid"); 1120 case 1: 1121 throw new RuntimeException("runtime"); 1122 case 2: 1123 throw new Error("error"); 1124 case 3: 1125 return this; 1126 default: 1127 return new ReadResolveTestF(); 1128 } 1129 } 1130 } 1131 1132 static class ReadResolveTestF implements Serializable { 1133 } 1134 1135 @TestTargetNew( 1136 level = TestLevel.COMPLETE, 1137 notes = "Verifies serialization.", 1138 method = "!Serialization", 1139 args = {} 1140 ) 1141 public void test_1_writeReplace() { 1142 try { 1143 Vector v = new Vector(); 1144 v.addElement(new WriteReplaceTestA()); 1145 v.addElement(new WriteReplaceTestB()); 1146 v.addElement(new WriteReplaceTestB()); 1147 v.addElement(new WriteReplaceTestC()); 1148 v.addElement(new WriteReplaceTestD()); 1149 v.addElement(new WriteReplaceTestE()); 1150 oos.writeObject(v); 1151 oos.close(); 1152 ois = new ObjectInputStream(loadStream()); 1153 Vector result = (Vector) ois.readObject(); 1154 assertTrue("invalid 0 : " + result.elementAt(0), result 1155 .elementAt(0).getClass() == ReadResolveTestA.class); 1156 assertTrue("invalid 1 : " + result.elementAt(1), result 1157 .elementAt(1).getClass() == ReadResolveTestA.class); 1158 assertTrue("invalid 2 : " + result.elementAt(2), result 1159 .elementAt(2).getClass() == ReadResolveTestA.class); 1160 assertTrue("invalid 3 : " + result.elementAt(3), result 1161 .elementAt(3).getClass() == ReadResolveTestB.class); 1162 assertTrue("invalid 4 : " + result.elementAt(4), result 1163 .elementAt(4).getClass() == ReadResolveTestD.class); 1164 assertTrue("invalid 5 : " + result.elementAt(5), result 1165 .elementAt(5).getClass() == WriteReplaceTestE.class); 1166 } catch (IOException e) { 1167 fail("IOException serializing data : " + e.getMessage()); 1168 } catch (ClassNotFoundException e) { 1169 fail("ClassNotFoundException serializing data : " + e.getMessage()); 1170 } 1171 } 1172 1173 @TestTargetNew( 1174 level = TestLevel.COMPLETE, 1175 notes = "Verifies serialization.", 1176 method = "!Serialization", 1177 args = {} 1178 ) 1179 public void test_2_writeReplace() { 1180 try { 1181 boolean exception = false; 1182 try { 1183 oos.writeObject(new WriteReplaceTestF(0, -1)); 1184 } catch (ObjectStreamException e) { 1185 exception = true; 1186 } 1187 assertTrue("Should throw ObjectStreamException", exception); 1188 exception = false; 1189 try { 1190 oos.writeObject(new WriteReplaceTestF(1, -1)); 1191 } catch (RuntimeException e) { 1192 exception = true; 1193 } 1194 assertTrue("Should throw RuntimeException", exception); 1195 exception = false; 1196 try { 1197 oos.writeObject(new WriteReplaceTestF(2, -1)); 1198 } catch (Error e) { 1199 exception = true; 1200 } 1201 assertTrue("Should throw Error", exception); 1202 1203 oos.writeObject(new WriteReplaceTestF(3, 0)); 1204 oos.writeObject(new WriteReplaceTestF(3, 1)); 1205 oos.writeObject(new WriteReplaceTestF(3, 2)); 1206 WriteReplaceTestF test = new WriteReplaceTestF(3, 3); 1207 oos.writeObject(test); 1208 oos.writeObject(test); 1209 WriteReplaceTestF test2 = new WriteReplaceTestF(3, 4); 1210 oos.writeObject(test2); 1211 oos.writeObject(test2); 1212 oos.close(); 1213 ois = new ObjectInputStream(loadStream()); 1214 try { 1215 ois.readObject(); 1216 } catch (WriteAbortedException e) { 1217 } 1218 1219 exception = false; 1220 try { 1221 ois.readObject(); 1222 } catch (ObjectStreamException e) { 1223 exception = true; 1224 } 1225 assertTrue("Expected ObjectStreamException", exception); 1226 exception = false; 1227 try { 1228 ois.readObject(); 1229 } catch (RuntimeException e) { 1230 exception = true; 1231 } 1232 assertTrue("Expected RuntimeException", exception); 1233 exception = false; 1234 try { 1235 ois.readObject(); 1236 } catch (Error e) { 1237 exception = true; 1238 } 1239 assertTrue("Expected Error", exception); 1240 1241 Object readE1 = ois.readObject(); 1242 Object readE2 = ois.readObject(); 1243 assertTrue("Replaced objects should be identical", readE1 == readE2); 1244 Object readF1 = ois.readObject(); 1245 Object readF2 = ois.readObject(); 1246 assertTrue("Replaced resolved objects should be identical: " 1247 + readF1 + " " + readF2, readF1 == readF2); 1248 } catch (IOException e) { 1249 fail("IOException serializing data : " + e.getMessage()); 1250 } catch (ClassNotFoundException e) { 1251 fail("ClassNotFoundException serializing data : " + e.getMessage()); 1252 } 1253 } 1254} 1255