1 package org.apache.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 java.io.IOException;
23
24 import junit.framework.TestCase;
25
26 import org.apache.jcs.auxiliary.AuxiliaryCache;
27 import org.apache.jcs.auxiliary.AuxiliaryCacheMockImpl;
28 import org.apache.jcs.engine.CacheElement;
29 import org.apache.jcs.engine.CompositeCacheAttributes;
30 import org.apache.jcs.engine.ElementAttributes;
31 import org.apache.jcs.engine.behavior.ICache;
32 import org.apache.jcs.engine.behavior.ICacheElement;
33 import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
34 import org.apache.jcs.engine.behavior.IElementAttributes;
35 import org.apache.jcs.engine.memory.MemoryCacheMockImpl;
36
37 /***
38 * Tests that directly engage the composite cache.
39 * <p>
40 * @author Aaron Smuts
41 */
42 public class CompositeCacheUnitTest
43 extends TestCase
44 {
45 /***
46 * Verify that the freeMemoryElements method on the memory cache is called on shutdown if there
47 * is a disk cache.
48 * <p>
49 * @throws IOException
50 */
51 public void testShutdownMemoryFlush()
52 throws IOException
53 {
54
55 String cacheName = "testCacheName";
56 String mockMemoryCacheClassName = "org.apache.jcs.engine.memory.MemoryCacheMockImpl";
57 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
58 cattr.setMemoryCacheName( mockMemoryCacheClassName );
59
60 IElementAttributes attr = new ElementAttributes();
61
62 CompositeCache cache = new CompositeCache( cacheName, cattr, attr );
63
64 AuxiliaryCacheMockImpl diskMock = new AuxiliaryCacheMockImpl();
65 diskMock.cacheType = ICache.DISK_CACHE;
66 AuxiliaryCache[] aux = new AuxiliaryCache[] { diskMock };
67 cache.setAuxCaches( aux );
68
69
70 int numToInsert = 10;
71 for ( int i = 0; i < numToInsert; i++ )
72 {
73 ICacheElement element = new CacheElement( cacheName, String.valueOf( i ), new Integer( i ) );
74 cache.update( element, false );
75 }
76
77 cache.dispose();
78
79
80 MemoryCacheMockImpl memoryCache = (MemoryCacheMockImpl) cache.getMemoryCache();
81 assertEquals( "Wrong number freed.", numToInsert, memoryCache.lastNumberOfFreedElements );
82 }
83
84 /***
85 * Verify that the freeMemoryElements method on the memory cache is NOT called on shutdown if
86 * there is NOT a disk cache.
87 * <p>
88 * @throws IOException
89 */
90 public void testShutdownMemoryFlush_noDisk()
91 throws IOException
92 {
93
94 String cacheName = "testCacheName";
95 String mockMemoryCacheClassName = "org.apache.jcs.engine.memory.MemoryCacheMockImpl";
96 ICompositeCacheAttributes cattr = new CompositeCacheAttributes();
97 cattr.setMemoryCacheName( mockMemoryCacheClassName );
98
99 IElementAttributes attr = new ElementAttributes();
100
101 CompositeCache cache = new CompositeCache( cacheName, cattr, attr );
102
103 AuxiliaryCacheMockImpl diskMock = new AuxiliaryCacheMockImpl();
104 diskMock.cacheType = ICache.REMOTE_CACHE;
105 AuxiliaryCache[] aux = new AuxiliaryCache[] { diskMock };
106 cache.setAuxCaches( aux );
107
108
109 int numToInsert = 10;
110 for ( int i = 0; i < numToInsert; i++ )
111 {
112 ICacheElement element = new CacheElement( cacheName, String.valueOf( i ), new Integer( i ) );
113 cache.update( element, false );
114 }
115
116 cache.dispose();
117
118
119 MemoryCacheMockImpl memoryCache = (MemoryCacheMockImpl) cache.getMemoryCache();
120 assertEquals( "Wrong number freed.", 0, memoryCache.lastNumberOfFreedElements );
121 }
122 }