|
| 1 | +package com.baeldung.deserialization; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotEquals; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InvalidClassException; |
| 9 | + |
| 10 | +import org.junit.Ignore; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public class DeserializationUnitTest { |
| 14 | + |
| 15 | + private static final String serializedObj = "rO0ABXNyACljb20uYmFlbGR1bmcuZGVzZXJpYWxpemF0aW9uLkFwcGxlUHJvZHVjdAAAAAAAEtaHAgADTAANaGVhZHBob25lUG9ydHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wADmxpZ2h0ZW5pbmdQb3J0cQB+AAFMAA90aHVuZGVyYm9sdFBvcnRxAH4AAXhwdAARaGVhZHBob25lUG9ydDIwMjBwdAATdGh1bmRlcmJvbHRQb3J0MjAyMA=="; |
| 16 | + |
| 17 | + private static long userDefinedSerialVersionUID = 1234567L; |
| 18 | + |
| 19 | + /** |
| 20 | + * Tests the deserialization of the original "AppleProduct" (no exceptions are thrown) |
| 21 | + * @throws ClassNotFoundException |
| 22 | + * @throws IOException |
| 23 | + */ |
| 24 | + @Test |
| 25 | + public void testDeserializeObj_compatible() throws IOException, ClassNotFoundException { |
| 26 | + |
| 27 | + assertEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID()); |
| 28 | + |
| 29 | + AppleProduct macBook = new AppleProduct(); |
| 30 | + macBook.headphonePort = "headphonePort2020"; |
| 31 | + macBook.thunderboltPort = "thunderboltPort2020"; |
| 32 | + |
| 33 | + // serializes the "AppleProduct" object |
| 34 | + String serializedProduct = SerializationUtility.serializeObjectToString(macBook); |
| 35 | + |
| 36 | + // deserializes the "AppleProduct" object |
| 37 | + AppleProduct deserializedProduct = (AppleProduct) DeserializationUtility.deSerializeObjectFromString(serializedProduct); |
| 38 | + |
| 39 | + assertTrue(deserializedProduct.headphonePort.equalsIgnoreCase(macBook.headphonePort)); |
| 40 | + assertTrue(deserializedProduct.thunderboltPort.equalsIgnoreCase(macBook.thunderboltPort)); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Tests the deserialization of the modified (non-compatible) "AppleProduct". |
| 46 | + * The test should result in an InvalidClassException being thrown. |
| 47 | + * |
| 48 | + * Note: to run this test: |
| 49 | + * 1. Modify the value of the serialVersionUID identifier in AppleProduct.java |
| 50 | + * 2. Remove the @Ignore annotation |
| 51 | + * 3. Run the test individually (do not run the entire set of tests) |
| 52 | + * 4. Revert the changes made in 1 & 2 (so that you're able to re-run the tests successfully) |
| 53 | + * |
| 54 | + * @throws ClassNotFoundException |
| 55 | + * @throws IOException |
| 56 | + */ |
| 57 | + @Ignore |
| 58 | + @Test(expected = InvalidClassException.class) |
| 59 | + public void testDeserializeObj_incompatible() throws ClassNotFoundException, IOException { |
| 60 | + |
| 61 | + assertNotEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID()); |
| 62 | + |
| 63 | + // attempts to deserialize the "AppleProduct" object |
| 64 | + DeserializationUtility.deSerializeObjectFromString(serializedObj); |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments