|
20 | 20 | import org.junit.Assert; |
21 | 21 | import org.junit.Test; |
22 | 22 |
|
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.ByteArrayOutputStream; |
23 | 25 | import java.io.FileInputStream; |
24 | 26 | import java.io.FileOutputStream; |
25 | 27 | import java.io.IOException; |
@@ -89,30 +91,25 @@ public void shouldTestCacheKeysWithBinaryArrays() throws Exception { |
89 | 91 | } |
90 | 92 |
|
91 | 93 | @Test (expected = NotSerializableException.class) |
92 | | - public void serializationExceptionTest() throws ClassNotFoundException, IOException { |
| 94 | + public void serializationExceptionTest() throws Exception { |
93 | 95 | CacheKey cacheKey = new CacheKey(); |
94 | 96 | cacheKey.update(new Object()); |
95 | | - canSerialize(cacheKey); |
| 97 | + serialize(cacheKey); |
96 | 98 | } |
97 | 99 |
|
98 | 100 | @Test |
99 | | - public void serializationTest() throws ClassNotFoundException, IOException { |
| 101 | + public void serializationTest() throws Exception { |
100 | 102 | CacheKey cacheKey = new CacheKey(); |
101 | 103 | cacheKey.update("serializable"); |
102 | | - canSerialize(cacheKey); |
| 104 | + Assert.assertEquals(cacheKey, serialize(cacheKey)); |
103 | 105 | } |
104 | 106 |
|
105 | | - private void canSerialize(final CacheKey object) throws ClassNotFoundException, IOException { |
106 | | - FileOutputStream fout = new FileOutputStream("target/address.ser"); |
107 | | - ObjectOutputStream output = new ObjectOutputStream(fout); |
108 | | - output.writeObject(object); |
109 | | - output.close(); |
| 107 | + private static <T> T serialize(T object) throws Exception { |
| 108 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 109 | + new ObjectOutputStream(baos).writeObject(object); |
110 | 110 |
|
111 | | - FileInputStream fin = new FileInputStream("target/address.ser"); |
112 | | - ObjectInputStream input = new ObjectInputStream(fin); |
113 | | - CacheKey cacheKey = (CacheKey) input.readObject(); |
114 | | - input.close(); |
115 | | - |
116 | | - Assert.assertEquals(1, cacheKey.getUpdateCount()); |
| 111 | + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); |
| 112 | + return (T) new ObjectInputStream(bais).readObject(); |
117 | 113 | } |
| 114 | + |
118 | 115 | } |
0 commit comments