|
1 | 1 | package com.github.kuangcp.serialize; |
2 | 2 |
|
3 | | -import static org.hamcrest.CoreMatchers.equalTo; |
4 | | -import static org.junit.Assert.assertThat; |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Test; |
5 | 6 |
|
6 | 7 | import java.io.ByteArrayInputStream; |
7 | 8 | import java.io.ByteArrayOutputStream; |
|
11 | 12 | import java.io.NotSerializableException; |
12 | 13 | import java.io.ObjectInputStream; |
13 | 14 | import java.io.ObjectOutputStream; |
14 | | -import lombok.extern.slf4j.Slf4j; |
15 | | -import org.junit.Assert; |
16 | | -import org.junit.Test; |
| 15 | + |
| 16 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 17 | +import static org.junit.Assert.assertThat; |
17 | 18 |
|
18 | 19 | /** |
19 | 20 | * Created by https://github.com/kuangcp on 17-10-24 下午2:27 使用jdk自带的Serializable接口序列化对象 然后反序列化对象 |
|
23 | 24 | @Slf4j |
24 | 25 | public class SerializeTest { |
25 | 26 |
|
26 | | - // 字节数组流 |
27 | | - @Test |
28 | | - public void testSerializeWithByte() throws IOException, ClassNotFoundException { |
29 | | - Person person = new Person("name"); |
30 | | - |
31 | | - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); |
32 | | - ObjectOutputStream output = new ObjectOutputStream(byteOutput); |
33 | | - output.writeObject(person); |
34 | | - |
35 | | - log.info("content={}", byteOutput.toString()); |
36 | | - |
37 | | - ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray()); |
38 | | - |
39 | | - ObjectInputStream input = new ObjectInputStream(byteInput); |
40 | | - Person result = (Person) input.readObject(); |
41 | | - assertThat(result.getName(), equalTo("name")); |
42 | | - } |
43 | | - |
44 | | - /** |
45 | | - * 序列化对象上某个属性没有实现序列化接口,但该属性没有值 |
46 | | - */ |
47 | | - @Test |
48 | | - public void testSerializeWithNonSerializableField() throws IOException, ClassNotFoundException { |
49 | | - Address address = new Address(); |
50 | | - address.setCountry("country"); |
51 | | - |
52 | | - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); |
53 | | - ObjectOutputStream output = new ObjectOutputStream(byteOutput); |
54 | | - output.writeObject(address); |
55 | | - |
56 | | - log.info("content={}", byteOutput.toString()); |
57 | | - |
58 | | - ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray()); |
59 | | - |
60 | | - ObjectInputStream input = new ObjectInputStream(byteInput); |
61 | | - Address result = (Address) input.readObject(); |
62 | | - assertThat(result.getCountry(), equalTo("country")); |
63 | | - } |
64 | | - |
65 | | - /** |
66 | | - * 序列化对象上某个属性没有实现序列化接口,且该属性有值 |
67 | | - */ |
68 | | - @Test(expected = NotSerializableException.class) |
69 | | - public void testSerializeWithNonSerializableFieldAndValue() throws IOException { |
70 | | - Address address = new Address(); |
71 | | - address.setCountry("country"); |
72 | | - Street street = new Street(); |
73 | | - street.setStreet("street"); |
74 | | - address.setStreet(street); |
75 | | - |
76 | | - ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); |
77 | | - ObjectOutputStream output = new ObjectOutputStream(byteOutput); |
78 | | - output.writeObject(address); |
79 | | - } |
80 | | - |
81 | | - @Test |
82 | | - public void testSerializeWithFile() { |
83 | | - try { |
84 | | - writeFile(); |
85 | | - readFile(); |
86 | | - } catch (IOException | ClassNotFoundException e) { |
87 | | - log.error(e.getMessage(), e); |
88 | | - Assert.fail(); |
| 27 | + // 字节数组流 |
| 28 | + @Test |
| 29 | + public void testSerializeWithByte() throws IOException, ClassNotFoundException { |
| 30 | + Person person = new Person("name"); |
| 31 | + |
| 32 | + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); |
| 33 | + ObjectOutputStream output = new ObjectOutputStream(byteOutput); |
| 34 | + output.writeObject(person); |
| 35 | + |
| 36 | + log.info("content={}", byteOutput.toString()); |
| 37 | + |
| 38 | + ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray()); |
| 39 | + |
| 40 | + ObjectInputStream input = new ObjectInputStream(byteInput); |
| 41 | + Person result = (Person) input.readObject(); |
| 42 | + assertThat(result.getName(), equalTo("name")); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * 序列化对象上某个属性没有实现序列化接口,但该属性没有值 |
| 47 | + */ |
| 48 | + @Test |
| 49 | + public void testSerializeWithNonSerializableField() throws IOException, ClassNotFoundException { |
| 50 | + Address address = new Address(); |
| 51 | + address.setCountry("country"); |
| 52 | + |
| 53 | + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); |
| 54 | + ObjectOutputStream output = new ObjectOutputStream(byteOutput); |
| 55 | + output.writeObject(address); |
| 56 | + |
| 57 | + log.info("content={}", byteOutput.toString()); |
| 58 | + |
| 59 | + ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray()); |
| 60 | + |
| 61 | + ObjectInputStream input = new ObjectInputStream(byteInput); |
| 62 | + Address result = (Address) input.readObject(); |
| 63 | + assertThat(result.getCountry(), equalTo("country")); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * 序列化对象上某个属性没有实现序列化接口,且该属性有值 |
| 68 | + */ |
| 69 | + @Test(expected = NotSerializableException.class) |
| 70 | + public void testSerializeWithNonSerializableFieldAndValue() throws IOException { |
| 71 | + Address address = new Address(); |
| 72 | + address.setCountry("country"); |
| 73 | + Street street = new Street(); |
| 74 | + street.setStreet("street"); |
| 75 | + address.setStreet(street); |
| 76 | + |
| 77 | + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); |
| 78 | + ObjectOutputStream output = new ObjectOutputStream(byteOutput); |
| 79 | + output.writeObject(address); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testSerializeWithFile() { |
| 84 | + try { |
| 85 | + writeFile(); |
| 86 | + readFile(); |
| 87 | + } catch (IOException | ClassNotFoundException e) { |
| 88 | + log.error(e.getMessage(), e); |
| 89 | + Assert.fail(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void writeFile() throws IOException { |
| 94 | + Person person = new Person("myth"); |
| 95 | + |
| 96 | + FileOutputStream fileOutputStream = new FileOutputStream("/tmp/person.log"); |
| 97 | + ObjectOutputStream out = new ObjectOutputStream(fileOutputStream); |
| 98 | + out.writeObject(person); |
| 99 | + out.close(); |
| 100 | + fileOutputStream.close(); |
| 101 | + log.debug("序列化完成, person={}", person); |
| 102 | + } |
| 103 | + |
| 104 | + private void readFile() throws IOException, ClassNotFoundException { |
| 105 | + FileInputStream fileInputStream = new FileInputStream("/tmp/person.log"); |
| 106 | + ObjectInputStream in = new ObjectInputStream(fileInputStream); |
| 107 | + Person object = (Person) in.readObject(); |
| 108 | + in.close(); |
| 109 | + fileInputStream.close(); |
| 110 | + System.out.println(object.toString()); |
| 111 | + assertThat(object.getName(), equalTo("myth")); |
89 | 112 | } |
90 | | - } |
91 | | - |
92 | | - private void writeFile() throws IOException { |
93 | | - Person person = new Person("myth"); |
94 | | - |
95 | | - FileOutputStream fileOutputStream = new FileOutputStream("/tmp/person.log"); |
96 | | - ObjectOutputStream out = new ObjectOutputStream(fileOutputStream); |
97 | | - out.writeObject(person); |
98 | | - out.close(); |
99 | | - fileOutputStream.close(); |
100 | | - log.debug("序列化完成, person={}", person); |
101 | | - } |
102 | | - |
103 | | - private void readFile() throws IOException, ClassNotFoundException { |
104 | | - FileInputStream fileInputStream = new FileInputStream("/tmp/person.log"); |
105 | | - ObjectInputStream in = new ObjectInputStream(fileInputStream); |
106 | | - Person object = (Person) in.readObject(); |
107 | | - in.close(); |
108 | | - fileInputStream.close(); |
109 | | - System.out.println(object.toString()); |
110 | | - assertThat(object.getName(), equalTo("myth")); |
111 | | - } |
112 | 113 | } |
0 commit comments