Skip to content

Commit 5a184d4

Browse files
amsokkarpivovarit
authored andcommitted
BAEL-871 What is serialVersionUID? (eugenp#2203)
* Bean Injection in Spring – ashrafmsokkar@gmail.com * Bean Injection in Spring – ashrafmsokkar@gmail.com * BAEL-871: What is serialVersionUID? * BAEL-871: What is serialVersionUID? * BAEL-871 What is serialVersionUID? * BAEL-871 Implemented feedback
1 parent f3c99a7 commit 5a184d4

5 files changed

Lines changed: 150 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.deserialization;
2+
3+
import java.io.Serializable;
4+
5+
public class AppleProduct implements Serializable {
6+
7+
private static final long serialVersionUID = 1234567L; // user-defined (i.e. not default or generated)
8+
// private static final long serialVersionUID = 7654321L; // user-defined (i.e. not default or generated)
9+
10+
public String headphonePort;
11+
public String thunderboltPort;
12+
public String lighteningPort;
13+
14+
public String getHeadphonePort() {
15+
return headphonePort;
16+
}
17+
18+
public String getThunderboltPort() {
19+
return thunderboltPort;
20+
}
21+
22+
public static long getSerialVersionUID() {
23+
return serialVersionUID;
24+
}
25+
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.deserialization;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.IOException;
5+
import java.io.ObjectInputStream;
6+
import java.util.Base64;
7+
8+
public class DeserializationUtility {
9+
10+
public static void main(String[] args) throws ClassNotFoundException, IOException {
11+
12+
String serializedObj = "rO0ABXNyACljb20uYmFlbGR1bmcuZGVzZXJpYWxpemF0aW9uLkFwcGxlUHJvZHVjdAAAAAAAEtaHAgADTAANaGVhZHBob25lUG9ydHQAEkxqYXZhL2xhbmcvU3RyaW5nO0wADmxpZ2h0ZW5pbmdQb3J0cQB+AAFMAA90aHVuZGVyYm9sdFBvcnRxAH4AAXhwdAARaGVhZHBob25lUG9ydDIwMjBwdAATdGh1bmRlcmJvbHRQb3J0MjAyMA==";
13+
System.out.println("Deserializing AppleProduct...");
14+
AppleProduct deserializedObj = (AppleProduct) deSerializeObjectFromString(serializedObj);
15+
System.out.println("Headphone port of AppleProduct:" + deserializedObj.getHeadphonePort());
16+
System.out.println("Thunderbolt port of AppleProduct:" + deserializedObj.getThunderboltPort());
17+
}
18+
19+
public static Object deSerializeObjectFromString(String s) throws IOException, ClassNotFoundException {
20+
byte[] data = Base64.getDecoder().decode(s);
21+
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
22+
Object o = ois.readObject();
23+
ois.close();
24+
return o;
25+
}
26+
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.deserialization;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.ObjectOutputStream;
6+
import java.io.Serializable;
7+
import java.util.Base64;
8+
9+
public class SerializationUtility {
10+
11+
public static void main(String[] args) throws ClassNotFoundException, IOException {
12+
13+
AppleProduct macBook = new AppleProduct();
14+
macBook.headphonePort = "headphonePort2020";
15+
macBook.thunderboltPort = "thunderboltPort2020";
16+
17+
String serializedObj = serializeObjectToString(macBook);
18+
System.out.println("Serialized AppleProduct object to string:");
19+
System.out.println(serializedObj);
20+
}
21+
22+
public static String serializeObjectToString(Serializable o) throws IOException {
23+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
24+
ObjectOutputStream oos = new ObjectOutputStream(baos);
25+
oos.writeObject(o);
26+
oos.close();
27+
return Base64.getEncoder().encodeToString(baos.toByteArray());
28+
}
29+
30+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

‎spring-all/src/main/resources/beanInjection-setter.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111
</bean>
1212

1313
<bean id="helm" class="org.baeldung.bean.injection.Helm" />
14-
1514
</beans>

0 commit comments

Comments
 (0)