1 package org.apache.commons.jcs.auxiliary.disk.indexed;
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 import org.apache.commons.jcs.JCS;
26 import org.apache.commons.jcs.access.CacheAccess;
27 import org.apache.commons.jcs.engine.behavior.ICacheElement;
28
29 import java.util.HashSet;
30 import java.util.Map;
31 import java.util.Set;
32
33
34
35
36
37
38 public class IndexedDiskCacheNoMemoryUnitTest
39 extends TestCase
40 {
41
42
43
44
45 private static int items = 2000;
46
47
48
49
50 public IndexedDiskCacheNoMemoryUnitTest( String testName )
51 {
52 super( testName );
53 }
54
55
56
57
58
59
60 public static void main( String args[] )
61 {
62 String[] testCaseName = { IndexedDiskCacheNoMemoryUnitTest.class.getName() };
63 junit.textui.TestRunner.main( testCaseName );
64 }
65
66
67
68
69
70
71 public static Test suite()
72 {
73 ActiveTestSuite suite = new ActiveTestSuite();
74
75 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache1" )
76 {
77 @Override
78 public void runTest()
79 throws Exception
80 {
81 this.runTestForRegion( "indexedRegion1" );
82 }
83 } );
84
85 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache2" )
86 {
87 @Override
88 public void runTest()
89 throws Exception
90 {
91 this.runTestForRegion( "indexedRegion2" );
92 }
93 } );
94
95 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache3" )
96 {
97 @Override
98 public void runTest()
99 throws Exception
100 {
101 this.runTestForRegion( "indexedRegion3" );
102 }
103 } );
104
105 return suite;
106 }
107
108
109
110
111 @Override
112 public void setUp()
113 {
114 JCS.setConfigFilename( "/TestDiskCacheNoMemory.ccf" );
115 }
116
117
118
119
120
121
122
123
124
125
126
127 public void runTestForRegion( String region )
128 throws Exception
129 {
130 CacheAccess<String, String> jcs = JCS.getInstance( region );
131
132
133
134 for ( int i = 0; i <= items; i++ )
135 {
136 jcs.put( i + ":key", region + " data " + i );
137 }
138
139
140
141 for ( int i = 0; i <= items; i++ )
142 {
143 String value = jcs.get( i + ":key" );
144
145 assertEquals( region + " data " + i, value );
146 }
147
148
149 Set<String> keys = new HashSet<String>();
150 for ( int i = 0; i <= items; i++ )
151 {
152 keys.add( i + ":key" );
153 }
154
155 Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
156 for ( int i = 0; i <= items; i++ )
157 {
158 ICacheElement<String, String> element = elements.get( i + ":key" );
159 assertNotNull( "element " + i + ":key is missing", element );
160 assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
161 }
162
163
164 for ( int i = 0; i <= items; i++ )
165 {
166 jcs.remove( i + ":key" );
167 }
168
169
170 for ( int i = 0; i <= items; i++ )
171 {
172 assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs.get( i + ":key" ) );
173 }
174
175
176
177 }
178 }