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