Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/main/java/org/apache/ibatis/cache/CacheKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class CacheKey implements Cloneable, Serializable {
private int hashcode;
private long checksum;
private int count;
private transient List<Object> updateList;
// 8/21/2017 - Sonarlint flags this as needing to be marked transient. While true if content is not serializable, this is not always true and thus should not be marked transient.
private List<Object> updateList;

public CacheKey() {
this.hashcode = DEFAULT_HASHCODE;
Expand Down
37 changes: 36 additions & 1 deletion src/test/java/org/apache/ibatis/cache/CacheKeyTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,16 @@
package org.apache.ibatis.cache;

import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class CacheKeyTest {
Expand Down Expand Up @@ -80,4 +88,31 @@ public void shouldTestCacheKeysWithBinaryArrays() throws Exception {
assertTrue(key1.equals(key2));
}

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

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

private void canSerialize(final CacheKey object) throws ClassNotFoundException, IOException {
FileOutputStream fout = new FileOutputStream("target/address.ser");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use files - ByteArrayOutputStream and ByteArrayInputStream will do just fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show me how to do that and read the data back? I was using ByteArrayOutputStream but then wanted to read it back so changed to files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static <T> T serialize(T object) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new ObjectOutputStream(baos).writeObject(object);
 
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    return (T) new ObjectInputStream(bais).readObject();
}

ObjectOutputStream output = new ObjectOutputStream(fout);
output.writeObject(object);
output.close();

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

Assert.assertEquals(1, cacheKey.getUpdateCount());
}
}