Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/test/java/org/apache/ibatis/cache/CacheKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -89,30 +91,25 @@ public void shouldTestCacheKeysWithBinaryArrays() throws Exception {
}

@Test (expected = NotSerializableException.class)
public void serializationExceptionTest() throws ClassNotFoundException, IOException {
public void serializationExceptionTest() throws Exception {
CacheKey cacheKey = new CacheKey();
cacheKey.update(new Object());
canSerialize(cacheKey);
serialize(cacheKey);
}

@Test
public void serializationTest() throws ClassNotFoundException, IOException {
public void serializationTest() throws Exception {
CacheKey cacheKey = new CacheKey();
cacheKey.update("serializable");
canSerialize(cacheKey);
Assert.assertEquals(cacheKey, serialize(cacheKey));
}

private void canSerialize(final CacheKey object) throws ClassNotFoundException, IOException {
FileOutputStream fout = new FileOutputStream("target/address.ser");
ObjectOutputStream output = new ObjectOutputStream(fout);
output.writeObject(object);
output.close();
private static <T> T serialize(T object) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new ObjectOutputStream(baos).writeObject(object);

FileInputStream fin = new FileInputStream("target/address.ser");
ObjectInputStream input = new ObjectInputStream(fin);
CacheKey cacheKey = (CacheKey) input.readObject();
input.close();

Assert.assertEquals(1, cacheKey.getUpdateCount());
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
return (T) new ObjectInputStream(bais).readObject();
}

}