Serialization Validation initial commit

Changes for Adding Tests Serialization Validation
1: Added utility Method.
2: Added Tests
This commit is contained in:
Amitabh Tiwari 2021-10-15 08:36:43 +05:30
parent 2dfdb51592
commit e2f8cdd817
3 changed files with 114 additions and 0 deletions

View File

@ -61,6 +61,11 @@
<artifactId>moneta</artifactId>
<version>${javamoney.moneta.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.core.version}</version>
</dependency>
</dependencies>
<build>
@ -187,6 +192,7 @@
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
<spring.core.version>4.3.20.RELEASE</spring.core.version>
</properties>
</project>

View File

@ -0,0 +1,29 @@
package com.baeldung.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationUtils {
public static <T extends Serializable> byte[] serialize(T obj)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.close();
return baos.toByteArray();
}
public static <T extends Serializable> T deserialize(byte[] b, Class<T> cl)
throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bais);
Object o = ois.readObject();
return cl.cast(o);
}
}

View File

@ -0,0 +1,79 @@
package com.baeldung.serialization;
import static org.junit.Assert.assertTrue;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.apache.commons.lang3.SerializationUtils;
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(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(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(expected = ClassCastException.class)
public void whenSerializingUsingCustomSerializationUtils_ThenThrowsError() throws IOException {
Address address = new Address();
address.setHouseNumber(10);
com.baeldung.util.SerializationUtils.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()));
}
}