Skip to content

Commit 144bae7

Browse files
authored
Merge pull request #1085 from hazendaz/master
[test] Cleanup cacheKeyTest per review
2 parents 9ffabe2 + 6d378bb commit 144bae7

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/test/java/org/apache/ibatis/cache/CacheKeyTest.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.junit.Assert;
2121
import org.junit.Test;
2222

23+
import java.io.ByteArrayInputStream;
24+
import java.io.ByteArrayOutputStream;
2325
import java.io.FileInputStream;
2426
import java.io.FileOutputStream;
2527
import java.io.IOException;
@@ -89,30 +91,25 @@ public void shouldTestCacheKeysWithBinaryArrays() throws Exception {
8991
}
9092

9193
@Test (expected = NotSerializableException.class)
92-
public void serializationExceptionTest() throws ClassNotFoundException, IOException {
94+
public void serializationExceptionTest() throws Exception {
9395
CacheKey cacheKey = new CacheKey();
9496
cacheKey.update(new Object());
95-
canSerialize(cacheKey);
97+
serialize(cacheKey);
9698
}
9799

98100
@Test
99-
public void serializationTest() throws ClassNotFoundException, IOException {
101+
public void serializationTest() throws Exception {
100102
CacheKey cacheKey = new CacheKey();
101103
cacheKey.update("serializable");
102-
canSerialize(cacheKey);
104+
Assert.assertEquals(cacheKey, serialize(cacheKey));
103105
}
104106

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);
110110

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();
117113
}
114+
118115
}

0 commit comments

Comments
 (0)