Last active
December 20, 2015 09:49
-
-
Save namkyu/6110936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package serializable; | |
import static org.hamcrest.CoreMatchers.*; | |
import static org.junit.Assert.*; | |
import java.io.Externalizable; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInput; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutput; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
import lombok.Data; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class Serializer { | |
private String filePath; | |
private String externalFilePath; | |
private User user; | |
private AdminUser adminUser; | |
@Before | |
public void init() { | |
filePath = "E:\\test\\serializable\\user.ser"; | |
externalFilePath = "E:\\test\\serializable\\adminUser.ser"; | |
user = new User(); | |
user.setName("kyu"); | |
user.setAge(32); | |
user.setSocialNumber("222222-1111111"); | |
adminUser = new AdminUser(); | |
adminUser.setName("adminKyu"); | |
adminUser.setAge(33); | |
user.setSocialNumber("121212121-121212121"); | |
} | |
@Test | |
public void 객체직렬화테스트() throws IOException { | |
FileOutputStream fos = new FileOutputStream(filePath); | |
ObjectOutputStream oos = new ObjectOutputStream(fos); | |
oos.writeObject(user); | |
oos.close(); | |
assertThat(true, is(new File(filePath).isFile())); | |
} | |
@Test | |
public void 객체역질렬화테스트() throws IOException, ClassNotFoundException { | |
FileInputStream fis = new FileInputStream(filePath); | |
ObjectInputStream ois = new ObjectInputStream(fis); | |
User user = (User) ois.readObject(); | |
ois.close(); | |
assertThat("kyu", is(user.getName())); | |
assertThat(32, is(user.getAge())); | |
assertThat(null, is(user.getSocialNumber())); | |
} | |
@Test | |
public void externalWrite() throws IOException { | |
FileOutputStream fos = new FileOutputStream(externalFilePath); | |
ObjectOutputStream oos = new ObjectOutputStream(fos); | |
oos.writeObject(adminUser); | |
oos.close(); | |
assertThat(true, is(new File(externalFilePath).isFile())); | |
} | |
@Test | |
public void externalRead() throws IOException, ClassNotFoundException { | |
FileInputStream fis = new FileInputStream(externalFilePath); | |
ObjectInputStream ois = new ObjectInputStream(fis); | |
AdminUser adminUser = (AdminUser) ois.readObject(); | |
assertThat("adminKyu", is(adminUser.getName())); | |
assertThat(33, is(adminUser.getAge())); | |
assertThat(null, is(adminUser.getSocialNumber())); | |
} | |
} | |
@Data | |
class User implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private String name; | |
private int age; | |
private transient String socialNumber; | |
} | |
@Data | |
class AdminUser implements Externalizable { | |
private static final long serialVersionUID = 1L; | |
private String name; | |
private int age; | |
private String socialNumber; | |
@Override | |
public void writeExternal(ObjectOutput out) throws IOException { | |
out.writeObject(name); | |
out.writeInt(age); | |
} | |
@Override | |
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { | |
this.name = (String) in.readObject(); | |
this.age = in.readInt(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment