| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.assertion; |
| 12 | |
| 13 | import java.io.ByteArrayInputStream; |
| 14 | import java.io.ByteArrayOutputStream; |
| 15 | import java.io.IOException; |
| 16 | import java.io.ObjectInputStream; |
| 17 | import java.io.ObjectOutputStream; |
| 18 | import java.io.Serializable; |
| 19 | |
| 20 | final class SerializationTesterFactory |
| 21 | { |
| 22 | private SerializationTesterFactory() |
| 23 | { |
| 24 | |
| 25 | } |
| 26 | |
| 27 | static SerializationTester newSerializationTester(final Serializable s) throws NullPointerException |
| 28 | { |
| 29 | if(s == null) |
| 30 | { |
| 31 | throw new NullPointerException(); |
| 32 | } |
| 33 | |
| 34 | return new SerializationTesterImpl(s); |
| 35 | } |
| 36 | |
| 37 | private static final class SerializationTesterImpl implements SerializationTester |
| 38 | { |
| 39 | private final Serializable s; |
| 40 | |
| 41 | SerializationTesterImpl(final Serializable s) |
| 42 | { |
| 43 | this.s = s; |
| 44 | } |
| 45 | |
| 46 | public boolean serializes() |
| 47 | { |
| 48 | try |
| 49 | { |
| 50 | serializeDeserialize(s); |
| 51 | |
| 52 | return true; |
| 53 | } |
| 54 | catch(IOException ioe) |
| 55 | { |
| 56 | return false; |
| 57 | } |
| 58 | catch(ClassNotFoundException cnfe) |
| 59 | { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public boolean serializesSame() |
| 65 | { |
| 66 | try |
| 67 | { |
| 68 | final Object o = serializeDeserialize(s); |
| 69 | |
| 70 | return o == s; |
| 71 | } |
| 72 | catch(IOException ioe) |
| 73 | { |
| 74 | return false; |
| 75 | } |
| 76 | catch(ClassNotFoundException cnfe) |
| 77 | { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public boolean serializesEqual() |
| 83 | { |
| 84 | try |
| 85 | { |
| 86 | final Object o = serializeDeserialize(s); |
| 87 | |
| 88 | return o.equals(s); |
| 89 | } |
| 90 | catch(IOException ioe) |
| 91 | { |
| 92 | return false; |
| 93 | } |
| 94 | catch(ClassNotFoundException cnfe) |
| 95 | { |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private Object serializeDeserialize(final Serializable serializable) throws IOException, ClassNotFoundException |
| 101 | { |
| 102 | final ByteArrayOutputStream baout = new ByteArrayOutputStream(); |
| 103 | |
| 104 | final ObjectOutputStream out = new ObjectOutputStream(baout); |
| 105 | |
| 106 | out.writeObject(serializable); |
| 107 | |
| 108 | final ByteArrayInputStream bain = new ByteArrayInputStream(baout.toByteArray()); |
| 109 | |
| 110 | final ObjectInputStream in = new ObjectInputStream(bain); |
| 111 | |
| 112 | return in.readObject(); |
| 113 | } |
| 114 | } |
| 115 | } |