|
21 | 21 | import java.util.Set; |
22 | 22 | import java.util.TreeSet; |
23 | 23 | import java.util.Vector; |
| 24 | +import java.util.Random; |
24 | 25 |
|
25 | 26 | import javax.swing.JScrollPane; |
26 | 27 | import javax.swing.SwingUtilities; |
@@ -326,7 +327,7 @@ public void testTime() throws Exception { |
326 | 327 | ZonedDateTime.now(), |
327 | 328 | }); |
328 | 329 | oos.flush(); |
329 | | - } |
| 330 | + } |
330 | 331 |
|
331 | 332 | /** |
332 | 333 | * Tests th pull request #27 by @qistoph: |
@@ -388,115 +389,70 @@ public void windowClosing(final WindowEvent e) { |
388 | 389 | }); |
389 | 390 | } |
390 | 391 |
|
391 | | - // public void test_readObject() throws Exception { |
392 | | - // String s = "HelloWorld"; |
393 | | - // oos.writeObject(s); |
394 | | - // oos.close(); |
395 | | - // ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray())); |
396 | | - // assertEquals("Read incorrect Object value", s, ois.readObject()); |
397 | | - // ois.close(); |
398 | | - // |
399 | | - // // Regression for HARMONY-91 |
400 | | - // // dynamically create serialization byte array for the next hierarchy: |
401 | | - // // - class A implements Serializable |
402 | | - // // - class C extends A |
403 | | - // |
404 | | - // byte[] cName = C.class.getName().getBytes("UTF-8"); |
405 | | - // byte[] aName = A.class.getName().getBytes("UTF-8"); |
406 | | - // |
407 | | - // ByteArrayOutputStream out = new ByteArrayOutputStream(); |
408 | | - // |
409 | | - // byte[] begStream = new byte[] { (byte) 0xac, (byte) 0xed, // STREAM_MAGIC |
410 | | - // (byte) 0x00, (byte) 0x05, // STREAM_VERSION |
411 | | - // (byte) 0x73, // TC_OBJECT |
412 | | - // (byte) 0x72, // TC_CLASSDESC |
413 | | - // (byte) 0x00, // only first byte for C class name length |
414 | | - // }; |
415 | | - // |
416 | | - // out.write(begStream, 0, begStream.length); |
417 | | - // out.write(cName.length); // second byte for C class name length |
418 | | - // out.write(cName, 0, cName.length); // C class name |
419 | | - // |
420 | | - // byte[] midStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, |
421 | | - // (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
422 | | - // (byte) 0x21, // serialVersionUID = 33L |
423 | | - // (byte) 0x02, // flags |
424 | | - // (byte) 0x00, (byte) 0x00, // fields : none |
425 | | - // (byte) 0x78, // TC_ENDBLOCKDATA |
426 | | - // (byte) 0x72, // Super class for C: TC_CLASSDESC for A class |
427 | | - // (byte) 0x00, // only first byte for A class name length |
428 | | - // }; |
429 | | - // |
430 | | - // out.write(midStream, 0, midStream.length); |
431 | | - // out.write(aName.length); // second byte for A class name length |
432 | | - // out.write(aName, 0, aName.length); // A class name |
433 | | - // |
434 | | - // byte[] endStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, |
435 | | - // (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
436 | | - // (byte) 0x0b, // serialVersionUID = 11L |
437 | | - // (byte) 0x02, // flags |
438 | | - // (byte) 0x00, (byte) 0x01, // fields |
439 | | - // |
440 | | - // (byte) 0x4c, // field description: type L (object) |
441 | | - // (byte) 0x00, (byte) 0x04, // length |
442 | | - // // field = 'name' |
443 | | - // (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, |
444 | | - // |
445 | | - // (byte) 0x74, // className1: TC_STRING |
446 | | - // (byte) 0x00, (byte) 0x12, // length |
447 | | - // // |
448 | | - // (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76, |
449 | | - // (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61, |
450 | | - // (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53, |
451 | | - // (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e, |
452 | | - // (byte) 0x67, (byte) 0x3b, |
453 | | - // |
454 | | - // (byte) 0x78, // TC_ENDBLOCKDATA |
455 | | - // (byte) 0x70, // NULL super class for A class |
456 | | - // |
457 | | - // // classdata |
458 | | - // (byte) 0x74, // TC_STRING |
459 | | - // (byte) 0x00, (byte) 0x04, // length |
460 | | - // (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, // value |
461 | | - // }; |
462 | | - // |
463 | | - // out.write(endStream, 0, endStream.length); |
464 | | - // out.flush(); |
465 | | - // |
466 | | - // // read created serial. form |
467 | | - // ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream( |
468 | | - // out.toByteArray())); |
469 | | - // Object o = ois.readObject(); |
470 | | - // assertEquals(C.class, o.getClass()); |
471 | | - // |
472 | | - // // Regression for HARMONY-846 |
473 | | - // assertNull(new ObjectInputStream() {}.readObject()); |
474 | | - // } |
475 | | - |
| 392 | + |
| 393 | + /** |
| 394 | + * Tests the pull request #38 by @UruDev: |
| 395 | + * Add support for custom writeObject |
| 396 | + */ |
| 397 | + @Test |
| 398 | + public void testCustomWriteObject() throws Exception { |
| 399 | + CustomClass writer = new CustomClass(); |
| 400 | + writer.start(oos); |
| 401 | + } |
476 | 402 | } |
477 | 403 |
|
478 | 404 | class SuperAaaa implements Serializable { |
479 | | - |
480 | | - /** |
481 | | - * |
482 | | - */ |
483 | 405 | private static final long serialVersionUID = 1L; |
484 | 406 | public boolean bool = true; |
485 | 407 | public int integer = -1; |
486 | 408 | public String superString = "Super!!"; |
487 | | - |
488 | 409 | } |
489 | 410 |
|
490 | 411 | class TestConcrete extends SuperAaaa implements Serializable { |
491 | | - |
492 | | - /** |
493 | | - * |
494 | | - */ |
495 | 412 | private static final long serialVersionUID = 1L; |
496 | 413 | public String childString = "Child!!"; |
497 | 414 |
|
498 | 415 | TestConcrete() { |
499 | 416 | super(); |
500 | 417 | } |
| 418 | +} |
| 419 | + |
| 420 | +//Custom writeObject section |
| 421 | +class CustomClass implements Serializable { |
| 422 | + private static final long serialVersionUID = 1; |
| 423 | + |
| 424 | + public void start(ObjectOutputStream out) throws Exception { |
| 425 | + this.writeObject(out); |
| 426 | + } |
| 427 | + |
| 428 | + private void writeObject(ObjectOutputStream out) throws IOException { |
| 429 | + CustomWriter custom = new CustomWriter(42); |
| 430 | + out.writeObject(custom); |
| 431 | + out.flush(); |
| 432 | + } |
| 433 | +} |
| 434 | + |
| 435 | +class RandomChild extends Random { |
| 436 | + private static final long serialVersionUID = 1; |
| 437 | + private int num = 1; |
| 438 | + private double doub = 4.5; |
501 | 439 |
|
| 440 | + RandomChild(int seed) { |
| 441 | + super(seed); |
| 442 | + } |
| 443 | +} |
| 444 | + |
| 445 | +class CustomWriter implements Serializable { |
| 446 | + protected RandomChild custom_obj = null; |
| 447 | + |
| 448 | + CustomWriter(int seed) { |
| 449 | + custom_obj = new RandomChild(seed); |
| 450 | + } |
| 451 | + |
| 452 | + private static final long serialVersionUID = 1; |
| 453 | + private static final int CURRENT_SERIAL_VERSION = 0; |
| 454 | + private void writeObject(ObjectOutputStream out) throws IOException { |
| 455 | + out.writeInt(CURRENT_SERIAL_VERSION); |
| 456 | + out.writeObject(custom_obj); |
| 457 | + } |
502 | 458 | } |
0 commit comments