Skip to content

Commit 5fd510e

Browse files
committed
added tests for cache package
1 parent 0428fa3 commit 5fd510e

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.ObjectOutputStream;
2525
import java.util.Date;
2626

27-
import org.junit.jupiter.api.Assertions;
2827
import org.junit.jupiter.api.Test;
2928

3029
class CacheKeyTest {
@@ -87,11 +86,31 @@ void shouldTestCacheKeysWithBinaryArrays() {
8786
assertEquals(key1, key2);
8887
}
8988

89+
@Test
90+
void throwExceptionWhenTryingToUpdateNullCacheKey() {
91+
CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
92+
assertThrows(CacheException.class, () -> cacheKey.update("null"));
93+
}
94+
95+
@Test
96+
void throwExceptionWhenTryingToUpdateAllNullCacheKey() {
97+
CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
98+
assertThrows(CacheException.class, () -> cacheKey.updateAll(new Object[]{"null", "null"}));
99+
}
100+
101+
@Test
102+
void shouldDemonstrateClonedNullCacheKeysAreEqual() throws Exception {
103+
CacheKey cacheKey = CacheKey.NULL_CACHE_KEY;
104+
CacheKey clonedCacheKey = cacheKey.clone();
105+
assertEquals(cacheKey, clonedCacheKey);
106+
assertEquals(cacheKey.hashCode(), clonedCacheKey.hashCode());
107+
}
108+
90109
@Test
91110
void serializationExceptionTest() {
92111
CacheKey cacheKey = new CacheKey();
93112
cacheKey.update(new Object());
94-
Assertions.assertThrows(NotSerializableException.class, () -> {
113+
assertThrows(NotSerializableException.class, () -> {
95114
serialize(cacheKey);
96115
});
97116
}
@@ -100,7 +119,7 @@ void serializationExceptionTest() {
100119
void serializationTest() throws Exception {
101120
CacheKey cacheKey = new CacheKey();
102121
cacheKey.update("serializable");
103-
Assertions.assertEquals(cacheKey, serialize(cacheKey));
122+
assertEquals(cacheKey, serialize(cacheKey));
104123
}
105124

106125
private static <T> T serialize(T object) throws Exception {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,10 @@ void shouldFlushAllItemsOnDemand() {
6969
assertNull(cache.getObject(4));
7070
}
7171

72+
@Test
73+
void shouldDemonstrateIdIsNull() {
74+
Cache cache = new PerpetualCache(null);
75+
assertThrows(CacheException.class, () -> cache.hashCode());
76+
assertThrows(CacheException.class, () -> cache.equals(new Object()));
77+
}
7278
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.apache.ibatis.cache;
2+
3+
import org.apache.ibatis.cache.decorators.SerializedCache;
4+
import org.apache.ibatis.cache.impl.PerpetualCache;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.Serializable;
8+
import java.util.Objects;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
class SerializedCacheTest {
14+
15+
@Test
16+
void shouldDemonstrateSerializedObjectAreEqual() {
17+
SerializedCache cache = new SerializedCache(new PerpetualCache("default"));
18+
for (int i = 0; i < 5; i++) {
19+
cache.putObject(i, new CachingObject(i));
20+
}
21+
for (int i = 0; i < 5; i++) {
22+
assertEquals(new CachingObject(i), cache.getObject(i));
23+
}
24+
}
25+
26+
@Test
27+
void shouldDemonstrateNullsAreSerializable() {
28+
SerializedCache cache = new SerializedCache(new PerpetualCache("default"));
29+
for (int i = 0; i < 5; i++) {
30+
cache.putObject(i, null);
31+
}
32+
for (int i = 0; i < 5; i++) {
33+
assertEquals(null, cache.getObject(i));
34+
}
35+
}
36+
37+
@Test
38+
void throwExceptionWhenTryingToCacheNonSerializableObject() {
39+
SerializedCache cache = new SerializedCache(new PerpetualCache("default"));
40+
assertThrows(CacheException.class,
41+
() -> cache.putObject(0, new CachingObjectWithoutSerializable(0)));
42+
}
43+
44+
static class CachingObject implements Serializable {
45+
int x;
46+
47+
public CachingObject(int x) {
48+
this.x = x;
49+
}
50+
51+
@Override
52+
public boolean equals(Object o) {
53+
if (this == o) return true;
54+
if (o == null || getClass() != o.getClass()) return false;
55+
CachingObject obj = (CachingObject) o;
56+
return x == obj.x;
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
return Objects.hash(x);
62+
}
63+
}
64+
65+
static class CachingObjectWithoutSerializable {
66+
int x;
67+
68+
public CachingObjectWithoutSerializable(int x) {
69+
this.x = x;
70+
}
71+
72+
@Override
73+
public boolean equals(Object o) {
74+
if (this == o) return true;
75+
if (o == null || getClass() != o.getClass()) return false;
76+
CachingObjectWithoutSerializable obj = (CachingObjectWithoutSerializable) o;
77+
return x == obj.x;
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hash(x);
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)