1 package org.apache.jcs.engine.memory.lru;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.extensions.ActiveTestSuite;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25
26 import org.apache.jcs.engine.CacheElement;
27 import org.apache.jcs.engine.behavior.ICacheElement;
28 import org.apache.jcs.engine.control.CompositeCache;
29 import org.apache.jcs.engine.control.CompositeCacheManager;
30
31 /***
32 * Test which exercises the LRUMemory cache. This one uses three different
33 * regions for three threads.
34 *
35 * @version $Id: LRUMemoryCacheConcurrentUnitTest.java 536904 2007-05-10 16:03:42Z tv $
36 */
37 public class LRUMemoryCacheConcurrentUnitTest
38 extends TestCase
39 {
40 /***
41 * Number of items to cache, twice the configured maxObjects for the memory
42 * cache regions.
43 */
44 private static int items = 200;
45
46 /***
47 * Constructor for the TestDiskCache object.
48 *
49 * @param testName
50 */
51 public LRUMemoryCacheConcurrentUnitTest( String testName )
52 {
53 super( testName );
54 }
55
56 /***
57 * Main method passes this test to the text test runner.
58 *
59 * @param args
60 */
61 public static void main( String args[] )
62 {
63 String[] testCaseName = { LRUMemoryCacheConcurrentUnitTest.class.getName() };
64 junit.textui.TestRunner.main( testCaseName );
65 }
66
67 /***
68 * A unit test suite for JUnit
69 *
70 * @return The test suite
71 */
72 public static Test suite()
73 {
74 ActiveTestSuite suite = new ActiveTestSuite();
75
76 suite.addTest( new LRUMemoryCacheConcurrentUnitTest( "testLRUMemoryCache" )
77 {
78 public void runTest()
79 throws Exception
80 {
81 this.runTestForRegion( "indexedRegion1" );
82 }
83 } );
84
85
86
87
88
89
90
91
92
93
94 return suite;
95 }
96
97 /***
98 * Test setup
99 */
100 public void setUp()
101 {
102
103 }
104
105 /***
106 * Adds items to cache, gets them, and removes them. The item count is more
107 * than the size of the memory cache, so items should be dumped.
108 *
109 * @param region
110 * Name of the region to access
111 *
112 * @exception Exception
113 * If an error occurs
114 */
115 public void runTestForRegion( String region )
116 throws Exception
117 {
118 CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
119 cacheMgr.configure( "/TestDiskCache.ccf" );
120 CompositeCache cache = cacheMgr.getCache( region );
121
122 LRUMemoryCache lru = new LRUMemoryCache();
123 lru.initialize( cache );
124
125
126
127 for ( int i = 0; i < items; i++ )
128 {
129 ICacheElement ice = new CacheElement( cache.getCacheName(), i + ":key", region + " data " + i );
130 ice.setElementAttributes( cache.getElementAttributes() );
131 lru.update( ice );
132 }
133
134
135
136 for ( int i = 0; i < 102; i++ )
137 {
138 assertNull( lru.get( i + ":key" ) );
139 }
140
141
142
143 for ( int i = 102; i < items; i++ )
144 {
145 String value = (String) lru.get( i + ":key" ).getVal();
146 assertEquals( region + " data " + i, value );
147 }
148
149
150
151 for ( int i = 0; i < items; i++ )
152 {
153 lru.remove( i + ":key" );
154 }
155
156
157
158 for ( int i = 0; i < items; i++ )
159 {
160 assertNull( "Removed key should be null: " + i + ":key", lru.get( i + ":key" ) );
161 }
162 }
163
164 }