Skip to content

Commit faf7b25

Browse files
committed
[bugfix] Remove invalid marked transient on cacheKey as reported by sonarlint
Test cases written around this to avoid this serious flaw in the future, configured origin of scanning asking for transient to be added, added comment about when this would actually fail serialization, and wrote test for that as well.
1 parent b6c1e80 commit faf7b25

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/main/java/org/apache/ibatis/cache/CacheKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class CacheKey implements Cloneable, Serializable {
3737
private int hashcode;
3838
private long checksum;
3939
private int count;
40-
private transient List<Object> updateList;
40+
// 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.
41+
private List<Object> updateList;
4142

4243
public CacheKey() {
4344
this.hashcode = DEFAULT_HASHCODE;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@
1616
package org.apache.ibatis.cache;
1717

1818
import static org.junit.Assert.*;
19+
20+
import org.junit.Assert;
1921
import org.junit.Test;
2022

23+
import java.io.FileInputStream;
24+
import java.io.FileOutputStream;
25+
import java.io.IOException;
26+
import java.io.NotSerializableException;
27+
import java.io.ObjectInputStream;
28+
import java.io.ObjectOutputStream;
2129
import java.util.Date;
2230

2331
public class CacheKeyTest {
@@ -80,4 +88,31 @@ public void shouldTestCacheKeysWithBinaryArrays() throws Exception {
8088
assertTrue(key1.equals(key2));
8189
}
8290

91+
@Test (expected = NotSerializableException.class)
92+
public void serializationExceptionTest() throws ClassNotFoundException, IOException {
93+
CacheKey cacheKey = new CacheKey();
94+
cacheKey.update(new Object());
95+
canSerialize(cacheKey);
96+
}
97+
98+
@Test
99+
public void serializationTest() throws ClassNotFoundException, IOException {
100+
CacheKey cacheKey = new CacheKey();
101+
cacheKey.update("serializable");
102+
canSerialize(cacheKey);
103+
}
104+
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();
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());
117+
}
83118
}

0 commit comments

Comments
 (0)