Skip to content

Commit 9ffabe2

Browse files
authored
Merge pull request mybatis#1084 from hazendaz/master
[bugfix] Remove invalid marked transient on cacheKey as reported by sonarlint
2 parents b6c1e80 + 4e8be85 commit 9ffabe2

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

+2-1
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

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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)