Review comments updated

Review comments updated
This commit is contained in:
Amitabh Tiwari
2021-11-06 05:53:22 +05:30
parent b56941596d
commit 56300022b1
4 changed files with 125 additions and 128 deletions

View File

@@ -1,7 +1,8 @@
package com.baeldung.serialization;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -16,94 +17,93 @@ import org.junit.Test;
public class SerializationUnitTest {
@Test(expected = NotSerializableException.class)
public void whenSerializing_ThenThrowsError() throws IOException {
Address address = new Address();
address.setHouseNumber(10);
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(address);
}
}
@Test
@Test(expected = NotSerializableException.class)
public void whenSerializing_ThenThrowsError() throws IOException {
Address address = new Address();
address.setHouseNumber(10);
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(address);
}
}
@Test
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
FileOutputStream fileOutputStream = new FileOutputStream("yofile.txt");
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(p);
}
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(p);
}
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
try ( ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
Person p2 = (Person) objectInputStream.readObject();
assertTrue(p2.getAge() == p.getAge());
assertTrue(p2.getName().equals(p.getName()));
}
}
FileInputStream fileInputStream = new FileInputStream("yofile.txt");
try (ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
Person p2 = (Person) objectInputStream.readObject();
assertEquals(p2.getAge(), p.getAge());
assertEquals(p2.getName(), p.getName());
}
}
@Test(expected = ClassCastException.class)
public void whenSerializingUsingApacheCommons_ThenThrowsError() {
Address address = new Address();
address.setHouseNumber(10);
SerializationUtils.serialize((Serializable) address);
}
@Test(expected = ClassCastException.class)
public void whenSerializingUsingApacheCommons_ThenThrowsError() {
Address address = new Address();
address.setHouseNumber(10);
SerializationUtils.serialize((Serializable) address);
}
@Test
public void whenSerializingAndDeserializingUsingApacheCommons_ThenObjectIsTheSame() {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
byte[] serialize = SerializationUtils.serialize(p);
Person p2 = (Person) SerializationUtils.deserialize(serialize);
assertTrue(p2.getAge() == p.getAge());
assertTrue(p2.getName().equals(p.getName()));
}
@Test
public void whenSerializingAndDeserializingUsingApacheCommons_ThenObjectIsTheSame() {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
byte[] serialize = SerializationUtils.serialize(p);
Person p2 = (Person) SerializationUtils.deserialize(serialize);
assertEquals(p2.getAge(), p.getAge());
assertEquals(p2.getName(), p.getName());
}
@Test(expected = ClassCastException.class)
public void whenSerializingUsingSpringSerializationUtils_ThenThrowsError() {
Address address = new Address();
address.setHouseNumber(10);
org.springframework.util.SerializationUtils.serialize((Serializable) address);
}
@Test(expected = ClassCastException.class)
public void whenSerializingUsingSpringSerializationUtils_ThenThrowsError() {
Address address = new Address();
address.setHouseNumber(10);
org.springframework.util.SerializationUtils.serialize((Serializable) address);
}
@Test
public void whenSerializingAndDeserializingUsingSpringSerializationUtils_ThenObjectIsTheSame() {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
byte[] serialize = org.springframework.util.SerializationUtils.serialize(p);
Person p2 = (Person) org.springframework.util.SerializationUtils.deserialize(serialize);
assertTrue(p2.getAge() == p.getAge());
assertTrue(p2.getName().equals(p.getName()));
}
@Test
public void whenSerializingAndDeserializingUsingSpringSerializationUtils_ThenObjectIsTheSame() {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
byte[] serialize = org.springframework.util.SerializationUtils.serialize(p);
Person p2 = (Person) org.springframework.util.SerializationUtils.deserialize(serialize);
assertEquals(p2.getAge(), p.getAge());
assertEquals(p2.getName(), p.getName());
}
@Test(expected = ClassCastException.class)
public void whenSerializingUsingCustomSerializationUtils_ThenThrowsError() throws IOException {
Address address = new Address();
address.setHouseNumber(10);
com.baeldung.util.SerializationUtils.serialize((Serializable) address);
}
@Test(expected = ClassCastException.class)
public void whenSerializingUsingCustomSerializationUtils_ThenThrowsError() throws IOException {
Address address = new Address();
address.setHouseNumber(10);
com.baeldung.util.MySerializationUtils.serialize((Serializable) address);
}
@Test
public void whenSerializingAndDeserializingUsingCustomSerializationUtils_ThenObjectIsTheSame()
throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
byte[] serialize = com.baeldung.util.SerializationUtils.serialize(p);
Person p2 = com.baeldung.util.SerializationUtils.deserialize(serialize, Person.class);
assertTrue(p2.getAge() == p.getAge());
assertTrue(p2.getName().equals(p.getName()));
}
@Test
public void whenSerializingAndDeserializingUsingCustomSerializationUtils_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(20);
p.setName("Joe");
byte[] serialize = com.baeldung.util.MySerializationUtils.serialize(p);
Person p2 = com.baeldung.util.MySerializationUtils.deserialize(serialize, Person.class);
assertEquals(p2.getAge(), p.getAge());
assertEquals(p2.getName(), p.getName());
}
@Test
public void whenSerializingUsingCustomSerializationUtils_ThanOk() {
assertFalse(com.baeldung.util.SerializationUtils.isSerializable(Address.class));
assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Person.class));
assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Integer.class));
}
@Test
public void whenSerializingUsingCustomSerializationUtils_ThanOk() {
assertFalse(com.baeldung.util.MySerializationUtils.isSerializable(Address.class));
assertTrue(com.baeldung.util.MySerializationUtils.isSerializable(Person.class));
assertTrue(com.baeldung.util.MySerializationUtils.isSerializable(Integer.class));
}
}