1 package org.apache.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
26 import org.apache.jcs.JCS;
27
28 /***
29 * Test which exercises the indexed disk cache. This one uses three different
30 * regions for thre threads. It uses a config file that specifies 0 items in
31 * memory.
32 *
33 * @version $Id: TestDiskCacheNoMemory.java 224346 2005-06-04 02:01:59Z asmuts $
34 */
35 public class IndexedDiskCacheNoMemoryUnitTest
36 extends TestCase
37 {
38 /***
39 * Number of items to cache; the configured maxObjects for the memory cache
40 * regions is 0.
41 */
42 private static int items = 2000;
43
44 /***
45 * Constructor for the TestDiskCache object.
46 *
47 * @param testName
48 */
49 public IndexedDiskCacheNoMemoryUnitTest( String testName )
50 {
51 super( testName );
52 }
53
54 /***
55 * Main method passes this test to the text test runner.
56 *
57 * @param args
58 */
59 public static void main( String args[] )
60 {
61 String[] testCaseName = { IndexedDiskCacheNoMemoryUnitTest.class.getName() };
62 junit.textui.TestRunner.main( testCaseName );
63 }
64
65 /***
66 * A unit test suite for JUnit
67 *
68 * @return The test suite
69 */
70 public static Test suite()
71 {
72 ActiveTestSuite suite = new ActiveTestSuite();
73
74 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache1" )
75 {
76 public void runTest()
77 throws Exception
78 {
79 this.runTestForRegion( "indexedRegion1" );
80 }
81 } );
82
83 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache2" )
84 {
85 public void runTest()
86 throws Exception
87 {
88 this.runTestForRegion( "indexedRegion2" );
89 }
90 } );
91
92 suite.addTest( new IndexedDiskCacheNoMemoryUnitTest( "testIndexedDiskCache3" )
93 {
94 public void runTest()
95 throws Exception
96 {
97 this.runTestForRegion( "indexedRegion3" );
98 }
99 } );
100
101 return suite;
102 }
103
104 /***
105 * Test setup
106 */
107 public void setUp()
108 {
109 JCS.setConfigFilename( "/TestDiskCacheNoMemory.ccf" );
110 }
111
112 /***
113 * Adds items to cache, gets them, and removes them. The item count is more
114 * than the size of the memory cache, so items should spool to disk.
115 *
116 * @param region
117 * Name of the region to access
118 *
119 * @exception Exception
120 * If an error occurs
121 */
122 public void runTestForRegion( String region )
123 throws Exception
124 {
125 JCS jcs = JCS.getInstance( region );
126
127
128
129 for ( int i = 0; i <= items; i++ )
130 {
131 jcs.put( i + ":key", region + " data " + i );
132 }
133
134
135
136 for ( int i = 0; i <= items; i++ )
137 {
138 String value = (String) jcs.get( i + ":key" );
139
140 assertEquals( region + " data " + i, value );
141 }
142
143
144 for ( int i = 0; i <= items; i++ )
145 {
146 jcs.remove( i + ":key" );
147 }
148
149
150 for ( int i = 0; i <= items; i++ )
151 {
152 assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs.get( i + ":key" ) );
153 }
154
155
156 System.out.println( jcs.getStats() );
157 }
158 }