1 package org.apache.commons.jcs3.engine.control;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.Map;
6
7 import org.apache.commons.jcs3.auxiliary.MockAuxiliaryCache;
8 import org.apache.commons.jcs3.engine.CacheElement;
9 import org.apache.commons.jcs3.engine.CompositeCacheAttributes;
10 import org.apache.commons.jcs3.engine.ElementAttributes;
11 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
12 import org.apache.commons.jcs3.engine.behavior.ICacheType.CacheType;
13 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheAttributes;
14 import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
15 import org.apache.commons.jcs3.engine.memory.MockMemoryCache;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 import junit.framework.TestCase;
37
38
39
40
41 public class CompositeCacheUnitTest
42 extends TestCase
43 {
44
45
46
47
48
49
50 public void testShutdownMemoryFlush()
51 throws IOException
52 {
53
54 final String cacheName = "testCacheName";
55 final String mockMemoryCacheClassName = "org.apache.commons.jcs3.engine.memory.MockMemoryCache";
56 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
57 cattr.setMemoryCacheName( mockMemoryCacheClassName );
58
59 final IElementAttributes attr = new ElementAttributes();
60
61 final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
62
63 final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
64 diskMock.cacheType = CacheType.DISK_CACHE;
65 cache.setAuxCaches(Arrays.asList(diskMock));
66
67
68 final int numToInsert = 10;
69 for ( int i = 0; i < numToInsert; i++ )
70 {
71 final ICacheElement<String, Integer> element = new CacheElement<>( cacheName, String.valueOf( i ), Integer.valueOf( i ) );
72 cache.update( element, false );
73 }
74
75 cache.dispose();
76
77
78 final MockMemoryCache<String, Integer> memoryCache = (MockMemoryCache<String, Integer>) cache.getMemoryCache();
79 assertEquals( "Wrong number freed.", numToInsert, memoryCache.lastNumberOfFreedElements );
80 }
81
82
83
84
85
86
87
88 public void testShutdownMemoryFlush_noDisk()
89 throws IOException
90 {
91
92 final String cacheName = "testCacheName";
93 final String mockMemoryCacheClassName = "org.apache.commons.jcs3.engine.memory.MockMemoryCache";
94 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
95 cattr.setMemoryCacheName( mockMemoryCacheClassName );
96
97 final IElementAttributes attr = new ElementAttributes();
98
99 final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
100
101 final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
102 diskMock.cacheType = CacheType.REMOTE_CACHE;
103 cache.setAuxCaches(Arrays.asList(diskMock));
104
105
106 final int numToInsert = 10;
107 for ( int i = 0; i < numToInsert; i++ )
108 {
109 final ICacheElement<String, Integer> element = new CacheElement<>( cacheName, String.valueOf( i ), Integer.valueOf( i ) );
110 cache.update( element, false );
111 }
112
113 cache.dispose();
114
115
116 final MockMemoryCache<String, Integer> memoryCache = (MockMemoryCache<String, Integer>) cache.getMemoryCache();
117 assertEquals( "Wrong number freed.", 0, memoryCache.lastNumberOfFreedElements );
118 }
119
120
121
122
123
124
125 public void testGetMatching_Normal()
126 throws IOException
127 {
128
129 final int maxMemorySize = 1000;
130 final String keyprefix1 = "MyPrefix1";
131 final String keyprefix2 = "MyPrefix2";
132 final String cacheName = "testGetMatching_Normal";
133 final String memoryCacheClassName = "org.apache.commons.jcs3.engine.memory.lru.LRUMemoryCache";
134 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
135 cattr.setMemoryCacheName( memoryCacheClassName );
136 cattr.setMaxObjects( maxMemorySize );
137
138 final IElementAttributes attr = new ElementAttributes();
139
140 final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
141
142 final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
143 diskMock.cacheType = CacheType.DISK_CACHE;
144 cache.setAuxCaches(Arrays.asList(diskMock));
145
146
147 final int numToInsertPrefix1 = 10;
148
149 for ( int i = 0; i < numToInsertPrefix1; i++ )
150 {
151 final ICacheElement<String, Integer> element = new CacheElement<>( cacheName, keyprefix1 + String.valueOf( i ), Integer.valueOf( i ) );
152 cache.update( element, false );
153 }
154
155 final int numToInsertPrefix2 = 50;
156
157 for ( int i = 0; i < numToInsertPrefix2; i++ )
158 {
159 final ICacheElement<String, Integer> element = new CacheElement<>( cacheName, keyprefix2 + String.valueOf( i ), Integer.valueOf( i ) );
160 cache.update( element, false );
161 }
162
163 final Map<?, ?> result1 = cache.getMatching( keyprefix1 + "\\S+" );
164 final Map<?, ?> result2 = cache.getMatching( keyprefix2 + "\\S+" );
165
166
167 assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result1.size() );
168 assertEquals( "Wrong number returned 2:", numToInsertPrefix2, result2.size() );
169 }
170
171
172
173
174
175
176 public void testGetMatching_NotOnDisk()
177 throws IOException
178 {
179
180 final int maxMemorySize = 0;
181 final String cacheName = "testGetMatching_NotOnDisk";
182 final String memoryCacheClassName = "org.apache.commons.jcs3.engine.memory.lru.LRUMemoryCache";
183 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
184 cattr.setCacheName(cacheName);
185 cattr.setMemoryCacheName( memoryCacheClassName );
186 cattr.setMaxObjects( maxMemorySize );
187
188 final IElementAttributes attr = new ElementAttributes();
189
190 final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
191
192 final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
193 diskMock.cacheType = CacheType.DISK_CACHE;
194 cache.setAuxCaches(Arrays.asList(diskMock));
195
196
197 cache.getMatching( "junk" );
198
199
200 assertEquals( "Wrong number of calls", 1, diskMock.getMatchingCallCount );
201 }
202
203
204
205
206
207
208 public void testGetMatching_NotOnRemote()
209 throws IOException
210 {
211
212 final int maxMemorySize = 0;
213 final String cacheName = "testGetMatching_NotOnDisk";
214 final String memoryCacheClassName = "org.apache.commons.jcs3.engine.memory.lru.LRUMemoryCache";
215 final ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
216 cattr.setCacheName(cacheName);
217 cattr.setMemoryCacheName( memoryCacheClassName );
218 cattr.setMaxObjects( maxMemorySize );
219
220 final IElementAttributes attr = new ElementAttributes();
221
222 final CompositeCache<String, Integer> cache = new CompositeCache<>( cattr, attr );
223
224 final MockAuxiliaryCache<String, Integer> diskMock = new MockAuxiliaryCache<>();
225 diskMock.cacheType = CacheType.REMOTE_CACHE;
226 cache.setAuxCaches(Arrays.asList(diskMock));
227
228
229 cache.getMatching( "junk" );
230
231
232 assertEquals( "Wrong number of calls", 1, diskMock.getMatchingCallCount );
233 }
234 }