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