1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.cache;
17
18 import static org.junit.jupiter.api.Assertions.assertNotNull;
19 import static org.junit.jupiter.api.Assertions.assertNull;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21
22 import org.apache.ibatis.cache.decorators.SerializedCache;
23 import org.apache.ibatis.cache.decorators.SoftCache;
24 import org.apache.ibatis.cache.impl.PerpetualCache;
25 import org.junit.jupiter.api.Test;
26
27 class SoftCacheTest {
28
29 @Test
30 void shouldDemonstrateObjectsBeingCollectedAsNeeded() {
31 final int N = 3000000;
32 SoftCache cache = new SoftCache(new PerpetualCache("default"));
33 for (int i = 0; i < N; i++) {
34 byte[] array = new byte[5001];
35 array[5000] = 1;
36 cache.putObject(i, array);
37 cache.getObject(i);
38 if (cache.getSize() < i + 1) {
39
40 break;
41 }
42 }
43 assertTrue(cache.getSize() < N);
44 }
45
46 @Test
47 void shouldDemonstrateCopiesAreEqual() {
48 Cache cache = new SoftCache(new PerpetualCache("default"));
49 cache = new SerializedCache(cache);
50 for (int i = 0; i < 1000; i++) {
51 cache.putObject(i, i);
52 Object value = cache.getObject(i);
53 assertTrue(value == null || value.equals(i));
54 }
55 }
56
57 @Test
58 void shouldRemoveItemOnDemand() {
59 Cache cache = new SoftCache(new PerpetualCache("default"));
60 cache.putObject(0, 0);
61 assertNotNull(cache.getObject(0));
62 cache.removeObject(0);
63 assertNull(cache.getObject(0));
64 }
65
66 @Test
67 void shouldFlushAllItemsOnDemand() {
68 Cache cache = new SoftCache(new PerpetualCache("default"));
69 for (int i = 0; i < 5; i++) {
70 cache.putObject(i, i);
71 }
72 assertNotNull(cache.getObject(0));
73 assertNotNull(cache.getObject(4));
74 cache.clear();
75 assertNull(cache.getObject(0));
76 assertNull(cache.getObject(4));
77 }
78
79 }