Skip to content

Commit 705d4f0

Browse files
authored
Merge pull request #2054 from tacoo/cache
Added tests for cache package
2 parents b94c110 + 8ac6163 commit 705d4f0

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright 2009-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.cache;
17+
18+
import org.apache.ibatis.cache.decorators.SerializedCache;
19+
import org.apache.ibatis.cache.impl.PerpetualCache;
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.io.Serializable;
23+
import java.util.Objects;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
28+
class SerializedCacheTest {
29+
30+
@Test
31+
void shouldDemonstrateSerializedObjectAreEqual() {
32+
SerializedCache cache = new SerializedCache(new PerpetualCache("default"));
33+
for (int i = 0; i < 5; i++) {
34+
cache.putObject(i, new CachingObject(i));
35+
}
36+
for (int i = 0; i < 5; i++) {
37+
assertEquals(new CachingObject(i), cache.getObject(i));
38+
}
39+
}
40+
41+
@Test
42+
void shouldDemonstrateNullsAreSerializable() {
43+
SerializedCache cache = new SerializedCache(new PerpetualCache("default"));
44+
for (int i = 0; i < 5; i++) {
45+
cache.putObject(i, null);
46+
}
47+
for (int i = 0; i < 5; i++) {
48+
assertEquals(null, cache.getObject(i));
49+
}
50+
}
51+
52+
@Test
53+
void throwExceptionWhenTryingToCacheNonSerializableObject() {
54+
SerializedCache cache = new SerializedCache(new PerpetualCache("default"));
55+
assertThrows(CacheException.class,
56+
() -> cache.putObject(0, new CachingObjectWithoutSerializable(0)));
57+
}
58+
59+
static class CachingObject implements Serializable {
60+
int x;
61+
62+
public CachingObject(int x) {
63+
this.x = x;
64+
}
65+
66+
@Override
67+
public boolean equals(Object o) {
68+
if (this == o) return true;
69+
if (o == null || getClass() != o.getClass()) return false;
70+
CachingObject obj = (CachingObject) o;
71+
return x == obj.x;
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(x);
77+
}
78+
}
79+
80+
static class CachingObjectWithoutSerializable {
81+
int x;
82+
83+
public CachingObjectWithoutSerializable(int x) {
84+
this.x = x;
85+
}
86+
87+
@Override
88+
public boolean equals(Object o) {
89+
if (this == o) return true;
90+
if (o == null || getClass() != o.getClass()) return false;
91+
CachingObjectWithoutSerializable obj = (CachingObjectWithoutSerializable) o;
92+
return x == obj.x;
93+
}
94+
95+
@Override
96+
public int hashCode() {
97+
return Objects.hash(x);
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)