diff --git a/core-java/src/main/java/com/baeldung/deserialization/AppleProduct.java b/core-java/src/main/java/com/baeldung/deserialization/AppleProduct.java new file mode 100644 index 0000000000..d672b9a4f5 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/deserialization/AppleProduct.java @@ -0,0 +1,26 @@ +package com.baeldung.deserialization; + +import java.io.Serializable; + +public class AppleProduct implements Serializable { + + private static final long serialVersionUID = 1234567L; // user-defined (i.e. not default or generated) +// private static final long serialVersionUID = 7654321L; // user-defined (i.e. not default or generated) + + public String headphonePort; + public String thunderboltPort; + public String lighteningPort; + + public String getHeadphonePort() { + return headphonePort; + } + + public String getThunderboltPort() { + return thunderboltPort; + } + + public static long getSerialVersionUID() { + return serialVersionUID; + } + +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/deserialization/DeserializationUtility.java b/core-java/src/main/java/com/baeldung/deserialization/DeserializationUtility.java new file mode 100644 index 0000000000..3ed2b8be1d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/deserialization/DeserializationUtility.java @@ -0,0 +1,27 @@ +package com.baeldung.deserialization; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.Base64; + +public class DeserializationUtility { + + public static void main(String[] args) throws ClassNotFoundException, IOException { + + String serializedObj = "rO0ABXNyACljb20uYmFlbGR1bmcuZGVzZXJpYWxpemF0aW9uLkFwcGxlUHJvZHVjdAAAAAAAEtaHAgADTAANaGVhZHBob25lUG9ydHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wADmxpZ2h0ZW5pbmdQb3J0cQB+AAFMAA90aHVuZGVyYm9sdFBvcnRxAH4AAXhwdAARaGVhZHBob25lUG9ydDIwMjBwdAATdGh1bmRlcmJvbHRQb3J0MjAyMA=="; + System.out.println("Deserializing AppleProduct..."); + AppleProduct deserializedObj = (AppleProduct) deSerializeObjectFromString(serializedObj); + System.out.println("Headphone port of AppleProduct:" + deserializedObj.getHeadphonePort()); + System.out.println("Thunderbolt port of AppleProduct:" + deserializedObj.getThunderboltPort()); + } + + public static Object deSerializeObjectFromString(String s) throws IOException, ClassNotFoundException { + byte[] data = Base64.getDecoder().decode(s); + ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data)); + Object o = ois.readObject(); + ois.close(); + return o; + } + +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/deserialization/SerializationUtility.java b/core-java/src/main/java/com/baeldung/deserialization/SerializationUtility.java new file mode 100644 index 0000000000..1dbcc40e6b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/deserialization/SerializationUtility.java @@ -0,0 +1,30 @@ +package com.baeldung.deserialization; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.Base64; + +public class SerializationUtility { + + public static void main(String[] args) throws ClassNotFoundException, IOException { + + AppleProduct macBook = new AppleProduct(); + macBook.headphonePort = "headphonePort2020"; + macBook.thunderboltPort = "thunderboltPort2020"; + + String serializedObj = serializeObjectToString(macBook); + System.out.println("Serialized AppleProduct object to string:"); + System.out.println(serializedObj); + } + + public static String serializeObjectToString(Serializable o) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(o); + oos.close(); + return Base64.getEncoder().encodeToString(baos.toByteArray()); + } + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/deserialization/DeserializationUnitTest.java b/core-java/src/test/java/com/baeldung/deserialization/DeserializationUnitTest.java new file mode 100644 index 0000000000..887e7e41da --- /dev/null +++ b/core-java/src/test/java/com/baeldung/deserialization/DeserializationUnitTest.java @@ -0,0 +1,67 @@ +package com.baeldung.deserialization; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.InvalidClassException; + +import org.junit.Ignore; +import org.junit.Test; + +public class DeserializationUnitTest { + + private static final String serializedObj = "rO0ABXNyACljb20uYmFlbGR1bmcuZGVzZXJpYWxpemF0aW9uLkFwcGxlUHJvZHVjdAAAAAAAEtaHAgADTAANaGVhZHBob25lUG9ydHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wADmxpZ2h0ZW5pbmdQb3J0cQB+AAFMAA90aHVuZGVyYm9sdFBvcnRxAH4AAXhwdAARaGVhZHBob25lUG9ydDIwMjBwdAATdGh1bmRlcmJvbHRQb3J0MjAyMA=="; + + private static long userDefinedSerialVersionUID = 1234567L; + + /** + * Tests the deserialization of the original "AppleProduct" (no exceptions are thrown) + * @throws ClassNotFoundException + * @throws IOException + */ + @Test + public void testDeserializeObj_compatible() throws IOException, ClassNotFoundException { + + assertEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID()); + + AppleProduct macBook = new AppleProduct(); + macBook.headphonePort = "headphonePort2020"; + macBook.thunderboltPort = "thunderboltPort2020"; + + // serializes the "AppleProduct" object + String serializedProduct = SerializationUtility.serializeObjectToString(macBook); + + // deserializes the "AppleProduct" object + AppleProduct deserializedProduct = (AppleProduct) DeserializationUtility.deSerializeObjectFromString(serializedProduct); + + assertTrue(deserializedProduct.headphonePort.equalsIgnoreCase(macBook.headphonePort)); + assertTrue(deserializedProduct.thunderboltPort.equalsIgnoreCase(macBook.thunderboltPort)); + + } + + /** + * Tests the deserialization of the modified (non-compatible) "AppleProduct". + * The test should result in an InvalidClassException being thrown. + * + * Note: to run this test: + * 1. Modify the value of the serialVersionUID identifier in AppleProduct.java + * 2. Remove the @Ignore annotation + * 3. Run the test individually (do not run the entire set of tests) + * 4. Revert the changes made in 1 & 2 (so that you're able to re-run the tests successfully) + * + * @throws ClassNotFoundException + * @throws IOException + */ + @Ignore + @Test(expected = InvalidClassException.class) + public void testDeserializeObj_incompatible() throws ClassNotFoundException, IOException { + + assertNotEquals(userDefinedSerialVersionUID, AppleProduct.getSerialVersionUID()); + + // attempts to deserialize the "AppleProduct" object + DeserializationUtility.deSerializeObjectFromString(serializedObj); + } + +} diff --git a/spring-all/src/main/resources/beanInjection-setter.xml b/spring-all/src/main/resources/beanInjection-setter.xml index b07826c31e..b2d3a05594 100644 --- a/spring-all/src/main/resources/beanInjection-setter.xml +++ b/spring-all/src/main/resources/beanInjection-setter.xml @@ -11,5 +11,4 @@ - \ No newline at end of file