serialization code (#1844)
* adds serialization code * fixes serialization unit test * adds code for custom serialization * changed test case names * fixes test names
This commit is contained in:
parent
1a10fda1a8
commit
47a97d3177
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeuldung.serialization;
|
||||||
|
|
||||||
|
public class Address {
|
||||||
|
|
||||||
|
private int houseNumber;
|
||||||
|
|
||||||
|
public int getHouseNumber() {
|
||||||
|
return houseNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHouseNumber(int houseNumber) {
|
||||||
|
this.houseNumber = houseNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeuldung.serialization;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Employee implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private transient Address address; // not an serializable object
|
||||||
|
private Person person;
|
||||||
|
|
||||||
|
public Address getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(Address address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Person getPerson() {
|
||||||
|
return person;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPerson(Person person) {
|
||||||
|
this.person = person;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeObject(ObjectOutputStream oos) throws IOException {
|
||||||
|
oos.defaultWriteObject();
|
||||||
|
oos.writeObject(address.getHouseNumber());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
|
||||||
|
ois.defaultReadObject();
|
||||||
|
Integer houseNumber = (Integer) ois.readObject();
|
||||||
|
Address a = new Address();
|
||||||
|
a.setHouseNumber(houseNumber);
|
||||||
|
this.setAddress(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package com.baeuldung.serialization;
|
package com.baeuldung.serialization;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@ -13,7 +13,7 @@ import org.junit.Test;
|
|||||||
public class PersonTest {
|
public class PersonTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPeron() throws IOException, ClassNotFoundException {
|
public void whenSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
||||||
Person p = new Person();
|
Person p = new Person();
|
||||||
p.setAge(20);
|
p.setAge(20);
|
||||||
p.setName("Joe");
|
p.setName("Joe");
|
||||||
@ -28,8 +28,37 @@ public class PersonTest {
|
|||||||
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||||
Person p2 = (Person) objectInputStream.readObject();
|
Person p2 = (Person) objectInputStream.readObject();
|
||||||
objectInputStream.close();
|
objectInputStream.close();
|
||||||
|
|
||||||
assertTrue(p2.getAge() == p.getAge());
|
assertTrue(p2.getAge() == p.getAge());
|
||||||
assertTrue(p2.getName().equals(p.getName()));
|
assertTrue(p2.getName().equals(p.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCustomSerializingAndDeserializing_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
||||||
|
Person p = new Person();
|
||||||
|
p.setAge(20);
|
||||||
|
p.setName("Joe");
|
||||||
|
|
||||||
|
Address a = new Address();
|
||||||
|
a.setHouseNumber(1);
|
||||||
|
|
||||||
|
Employee e = new Employee();
|
||||||
|
e.setPerson(p);
|
||||||
|
e.setAddress(a);
|
||||||
|
|
||||||
|
FileOutputStream fileOutputStream = new FileOutputStream("yofile2.txt");
|
||||||
|
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||||
|
objectOutputStream.writeObject(e);
|
||||||
|
objectOutputStream.flush();
|
||||||
|
objectOutputStream.close();
|
||||||
|
|
||||||
|
FileInputStream fileInputStream = new FileInputStream("yofile2.txt");
|
||||||
|
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
|
||||||
|
Employee e2 = (Employee) objectInputStream.readObject();
|
||||||
|
objectInputStream.close();
|
||||||
|
|
||||||
|
assertTrue(e2.getPerson().getAge() == e.getPerson().getAge());
|
||||||
|
assertTrue(e2.getAddress().getHouseNumber() == (e.getAddress().getHouseNumber()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user