| 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.Serializable; |
| 14 | import static org.jtiger.assertion.SerializationTesterFactory.newSerializationTester; |
| 15 | |
| 16 | /** |
| 17 | * Provides the ability to make assertions regarding the serializability of objects. |
| 18 | * |
| 19 | * @see Serializable |
| 20 | * @author %javadoc.author.tag% |
| 21 | * @version %version%<br/> |
| 22 | * <i>Build Number %build.number%</i><br/> |
| 23 | * <i>Build Time %build.time% CET (GMT + 1)</i> |
| 24 | */ |
| 25 | public final class Serialization |
| 26 | { |
| 27 | private Serialization() |
| 28 | { |
| 29 | |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Asserts that the given <code>Serializable</code> instance successfully (without exceptions) serializes and |
| 34 | * deserializes. |
| 35 | * |
| 36 | * @param s The <code>Serializable</code> instance to make the assertion on. |
| 37 | * @param message The assertion message. |
| 38 | * @throws AssertionException If the given <code>Serializable</code> instance does not successfully |
| 39 | * (without exceptions) serialize and deserialize. |
| 40 | */ |
| 41 | public static void assertSerializes(final Serializable s, final Object... message) throws AssertionException |
| 42 | { |
| 43 | final SerializationTester tester = newSerializationTester(s); |
| 44 | |
| 45 | if(!tester.serializes()) |
| 46 | { |
| 47 | throw new AssertionException(message); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Asserts that the given <code>Serializable</code> instance does not successfully (without exceptions) serialize |
| 53 | * and deserialize. |
| 54 | * |
| 55 | * @param s The <code>Serializable</code> instance to make the assertion on. |
| 56 | * @param message The assertion message. |
| 57 | * @throws AssertionException If the given <code>Serializable</code> instance successfully (without exceptions) |
| 58 | * serializes and deserializes. |
| 59 | */ |
| 60 | public static void assertNotSerializes(final Serializable s, final Object... message) throws AssertionException |
| 61 | { |
| 62 | final SerializationTester tester = newSerializationTester(s); |
| 63 | |
| 64 | if(tester.serializes()) |
| 65 | { |
| 66 | throw new AssertionException(message); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Asserts that the given <code>Serializable</code> instance serializes and deserializes to produce the same |
| 72 | * instance. This can only be achieved by overriding the <code>readResolve</code> method. |
| 73 | * |
| 74 | * @param s The <code>Serializable</code> instance to make the assertion on. |
| 75 | * @param message The assertion message. |
| 76 | * @throws AssertionException If the given <code>Serializable</code> instance does not serialize and deserialize to |
| 77 | * produce the same instance. |
| 78 | */ |
| 79 | public static void assertSerializesSame(final Serializable s, final Object... message) throws AssertionException |
| 80 | { |
| 81 | final SerializationTester tester = newSerializationTester(s); |
| 82 | |
| 83 | if(!tester.serializesSame()) |
| 84 | { |
| 85 | throw new AssertionException(message); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Asserts that the given <code>Serializable</code> instance serializes and deserializes to produce a new |
| 91 | * instance. |
| 92 | * |
| 93 | * @param s The <code>Serializable</code> instance to make the assertion on. |
| 94 | * @param message The assertion message. |
| 95 | * @throws AssertionException If the given <code>Serializable</code> instance does not serialize and deserialize to |
| 96 | * produce a new instance. |
| 97 | */ |
| 98 | public static void assertSerializesUnsame(final Serializable s, final Object... message) throws AssertionException |
| 99 | { |
| 100 | final SerializationTester tester = newSerializationTester(s); |
| 101 | |
| 102 | if(tester.serializesSame()) |
| 103 | { |
| 104 | throw new AssertionException(message); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Asserts that the given <code>Serializable</code> instance serializes and deserializes to produce an instance that |
| 110 | * is equal according to its equals method implementation. |
| 111 | * |
| 112 | * @see Object#equals(Object) |
| 113 | * @param s The <code>Serializable</code> instance to make the assertion on. |
| 114 | * @param message The assertion message. |
| 115 | * @throws AssertionException If the given <code>Serializable</code> instance does not serialize and deserialize to |
| 116 | * produce an instance that is equal according to its equals method implementation. |
| 117 | */ |
| 118 | public static void assertSerializesEqual(final Serializable s, final Object... message) throws AssertionException |
| 119 | { |
| 120 | final SerializationTester tester = newSerializationTester(s); |
| 121 | |
| 122 | if(!tester.serializesEqual()) |
| 123 | { |
| 124 | throw new AssertionException(message); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Asserts that the given <code>Serializable</code> instance serializes and deserializes to produce an instance that |
| 130 | * is unequal according to its equals method implementation. |
| 131 | * |
| 132 | * @see Object#equals(Object) |
| 133 | * @param s The <code>Serializable</code> instance to make the assertion on. |
| 134 | * @param message The assertion message. |
| 135 | * @throws AssertionException If the given <code>Serializable</code> instance does not serialize and deserialize to |
| 136 | * produce an instance that is unequal according to its equals method implementation. |
| 137 | */ |
| 138 | public static void assertSerializesUnequal(final Serializable s, final Object... message) throws AssertionException |
| 139 | { |
| 140 | final SerializationTester tester = newSerializationTester(s); |
| 141 | |
| 142 | if(tester.serializesEqual()) |
| 143 | { |
| 144 | throw new AssertionException(message); |
| 145 | } |
| 146 | } |
| 147 | } |