Skip to content

Commit cf0ea86

Browse files
authored
BAEL-7429: Java Serialization with Non Serializable Parts (#18849)
* BAEL-7429: Java Serialization with Non Serializable Parts * Some updates
1 parent fb8a1be commit cf0ea86

File tree

4 files changed

+301
-0
lines changed

4 files changed

+301
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.serialization.nonserializable;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.io.ObjectInputStream;
11+
import java.io.ObjectOutputStream;
12+
import java.io.Serializable;
13+
import java.nio.file.FileSystems;
14+
import java.nio.file.Path;
15+
16+
import org.junit.jupiter.api.Test;
17+
18+
public class CustomSerializationUnitTest {
19+
20+
@Test
21+
void givenAClassWithCustomSerialization_whenSerializingTheClass_thenItSerializesCorrectly() throws Exception {
22+
CustomSerializationUser user = new CustomSerializationUser("Graham", "/graham.png");
23+
assertNotNull(user.profile);
24+
25+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
26+
ObjectOutputStream oos = new ObjectOutputStream(baos);
27+
oos.writeObject(user);
28+
29+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
30+
ObjectInputStream ois = new ObjectInputStream(bais);
31+
Object read = ois.readObject();
32+
33+
assertTrue(read instanceof CustomSerializationUser);
34+
CustomSerializationUser readUser = (CustomSerializationUser) read;
35+
assertEquals(user.name, readUser.name);
36+
assertEquals(user.profile, readUser.profile);
37+
}
38+
39+
@Test
40+
void givenAClassWithReadResolve_whenDeserializingTheClass_thenItDeserializesCorrectly() throws Exception {
41+
ReadResolveUser user = new ReadResolveUser("Graham", "/graham.png");
42+
assertNotNull(user.profile);
43+
44+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
45+
ObjectOutputStream oos = new ObjectOutputStream(baos);
46+
oos.writeObject(user);
47+
48+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
49+
ObjectInputStream ois = new ObjectInputStream(bais);
50+
Object read = ois.readObject();
51+
52+
assertTrue(read instanceof ReadResolveUser);
53+
ReadResolveUser readUser = (ReadResolveUser) read;
54+
assertEquals(user.name, readUser.name);
55+
assertEquals(user.profilePath, readUser.profilePath);
56+
assertEquals(user.profile, readUser.profile);
57+
}
58+
59+
static class CustomSerializationUser implements Serializable {
60+
private String name;
61+
private Path profile;
62+
63+
public CustomSerializationUser(String name, String profilePath) {
64+
this.name = name;
65+
this.profile = FileSystems.getDefault().getPath(profilePath);
66+
}
67+
68+
private void writeObject(ObjectOutputStream out) throws IOException {
69+
out.writeObject(name);
70+
out.writeObject(profile.toString());
71+
}
72+
73+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
74+
String nameTemp = (String) in.readObject();
75+
String profilePathTemp = (String) in.readObject();
76+
77+
this.name = nameTemp;
78+
this.profile = FileSystems.getDefault().getPath(profilePathTemp);
79+
}
80+
}
81+
82+
static class ReadResolveUser implements Serializable {
83+
private String name;
84+
private String profilePath;
85+
private transient Path profile;
86+
87+
public ReadResolveUser(String name, String profilePath) {
88+
this.name = name;
89+
this.profilePath = profilePath;
90+
this.profile = FileSystems.getDefault().getPath(profilePath);
91+
}
92+
93+
public Object readResolve() {
94+
this.profile = FileSystems.getDefault().getPath(this.profilePath);
95+
return this;
96+
}
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.serialization.nonserializable;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.NotSerializableException;
10+
import java.io.ObjectInputStream;
11+
import java.io.ObjectOutputStream;
12+
import java.io.Serializable;
13+
import java.nio.file.FileSystems;
14+
import java.nio.file.Path;
15+
16+
import org.junit.jupiter.api.Test;
17+
18+
class SimpleSerializationUnitTest {
19+
@Test
20+
void whenSerializingASerializableClass_thenItCanDeserializeCorrectly() throws Exception {
21+
SerializableUser user = new SerializableUser("Graham", "/graham.png");
22+
23+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
24+
ObjectOutputStream oos = new ObjectOutputStream(baos);
25+
oos.writeObject(user);
26+
27+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
28+
ObjectInputStream ois = new ObjectInputStream(bais);
29+
Object read = ois.readObject();
30+
31+
assertTrue(read instanceof SerializableUser);
32+
SerializableUser readUser = (SerializableUser) read;
33+
assertEquals(user.name, readUser.name);
34+
assertEquals(user.profilePath, readUser.profilePath);
35+
}
36+
37+
@Test
38+
void whenSerializingANonSerializableClass_thenItCanDeserializeCorrectly() throws Exception {
39+
NonSerializableUser user = new NonSerializableUser("Graham", "/graham.png");
40+
user.getProfile();
41+
42+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
43+
ObjectOutputStream oos = new ObjectOutputStream(baos);
44+
45+
assertThrows(NotSerializableException.class, () -> oos.writeObject(user));
46+
}
47+
48+
static class SerializableUser implements Serializable {
49+
private String name;
50+
private String profilePath;
51+
52+
public SerializableUser(String name, String profilePath) {
53+
this.name = name;
54+
this.profilePath = profilePath;
55+
}
56+
}
57+
58+
static class NonSerializableUser implements Serializable {
59+
private String name;
60+
private String profilePath;
61+
private Path profile;
62+
63+
public NonSerializableUser(String name, String profilePath) {
64+
this.name = name;
65+
this.profilePath = profilePath;
66+
}
67+
68+
public Path getProfile() {
69+
if (this.profile == null) {
70+
this.profile = FileSystems.getDefault().getPath(profilePath);
71+
}
72+
73+
return this.profile;
74+
}
75+
}
76+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.serialization.nonserializable;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.io.ByteArrayInputStream;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.ObjectInputStream;
11+
import java.io.ObjectOutputStream;
12+
import java.io.Serializable;
13+
import java.nio.file.FileSystems;
14+
import java.nio.file.Path;
15+
16+
import org.junit.jupiter.api.Test;
17+
18+
class TransientUnitTest {
19+
@Test
20+
void whenSerializingATransient_thenThatFieldIsNotDeserialized() throws Exception {
21+
User user = new User("Graham", "/graham.png");
22+
user.getProfile();
23+
assertNotNull(user.profile);
24+
25+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
26+
ObjectOutputStream oos = new ObjectOutputStream(baos);
27+
oos.writeObject(user);
28+
29+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
30+
ObjectInputStream ois = new ObjectInputStream(bais);
31+
Object read = ois.readObject();
32+
33+
assertTrue(read instanceof User);
34+
User readUser = (User) read;
35+
assertEquals(user.name, readUser.name);
36+
assertEquals(user.profilePath, readUser.profilePath);
37+
assertNull(readUser.profile);
38+
}
39+
40+
static class User implements Serializable {
41+
private String name;
42+
private String profilePath;
43+
private transient Path profile;
44+
45+
public User(String name, String profilePath) {
46+
this.name = name;
47+
this.profilePath = profilePath;
48+
}
49+
50+
public Path getProfile() {
51+
if (this.profile == null) {
52+
this.profile = FileSystems.getDefault().getPath(profilePath);
53+
}
54+
55+
return this.profile;
56+
}
57+
}
58+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.serialization.nonserializable;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.ByteArrayInputStream;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.io.ObjectInputStream;
11+
import java.io.ObjectOutputStream;
12+
import java.io.Serializable;
13+
import java.nio.file.FileSystems;
14+
import java.nio.file.Path;
15+
16+
import org.junit.jupiter.api.Test;
17+
18+
public class WrapperUnitTest {
19+
20+
@Test
21+
void whenSerializingAWrapper_thenTheClassIsSerialized() throws Exception {
22+
User user = new User("Graham", "/graham.png");
23+
assertNotNull(user.profile);
24+
25+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
26+
ObjectOutputStream oos = new ObjectOutputStream(baos);
27+
oos.writeObject(new UserWrapper(user));
28+
29+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
30+
ObjectInputStream ois = new ObjectInputStream(bais);
31+
Object read = ois.readObject();
32+
33+
assertTrue(read instanceof UserWrapper);
34+
UserWrapper wrapper = (UserWrapper) read;
35+
User readUser = wrapper.user;
36+
assertEquals(user.name, readUser.name);
37+
assertEquals(user.profile, readUser.profile);
38+
}
39+
40+
static class User {
41+
private String name;
42+
private transient Path profile;
43+
44+
public User(String name, String profilePath) {
45+
this.name = name;
46+
this.profile = FileSystems.getDefault().getPath(profilePath);
47+
}
48+
}
49+
50+
static class UserWrapper implements Serializable {
51+
private User user;
52+
53+
public UserWrapper(User user) {
54+
this.user = user;
55+
}
56+
57+
private void writeObject(ObjectOutputStream out) throws IOException {
58+
out.writeObject(user.name);
59+
out.writeObject(user.profile.toString());
60+
}
61+
62+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
63+
String nameTemp = (String) in.readObject();
64+
String profilePathTemp = (String) in.readObject();
65+
66+
this.user = new User(nameTemp, profilePathTemp);
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)