1 package org.apache.commons.jcs.auxiliary.disk.block;
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 public class BlockDiskCacheSameRegionConcurrentUnitTest
37 extends TestCase
38 {
39
40
41
42
43
44 public BlockDiskCacheSameRegionConcurrentUnitTest( String testName )
45 {
46 super( testName );
47 }
48
49
50
51
52
53
54
55 public static void main( String args[] ) throws InterruptedException
56 {
57 String[] testCaseName = { BlockDiskCacheSameRegionConcurrentUnitTest.class.getName() };
58 junit.textui.TestRunner.main( testCaseName );
59
60
61 Thread.sleep(2000);
62 }
63
64
65
66
67
68 public static Test suite()
69 {
70 ActiveTestSuite suite = new ActiveTestSuite();
71
72 suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache1" )
73 {
74 @Override
75 public void runTest()
76 throws Exception
77 {
78 this.runTestForRegion( "blockRegion4", 0, 200 );
79 }
80 } );
81
82 suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache2" )
83 {
84 @Override
85 public void runTest()
86 throws Exception
87 {
88 this.runTestForRegion( "blockRegion4", 1000, 1200 );
89 }
90 } );
91
92 suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache3" )
93 {
94 @Override
95 public void runTest()
96 throws Exception
97 {
98 this.runTestForRegion( "blockRegion4", 2000, 2200 );
99 }
100 } );
101
102 suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache4" )
103 {
104 @Override
105 public void runTest()
106 throws Exception
107 {
108 this.runTestForRegion( "blockRegion4", 2200, 5200 );
109 }
110 } );
111
112 return suite;
113 }
114
115
116
117
118
119
120 @Override
121 public void setUp()
122 throws Exception
123 {
124 JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
125 }
126
127
128
129
130
131
132
133
134
135 public void runTestForRegion( String region, int start, int end )
136 throws Exception
137 {
138 CacheAccess<String, String> jcs = JCS.getInstance( region );
139
140
141
142 for ( int i = start; i <= end; i++ )
143 {
144 jcs.put( i + ":key", region + " data " + i + "-" + region );
145 }
146
147
148
149 for ( int i = start; i <= end; i++ )
150 {
151 String key = i + ":key";
152 String value = jcs.get( key );
153
154 assertEquals( "Wrong value for key [" + key + "]", region + " data " + i + "-" + region, value );
155 }
156
157
158 Set<String> keys = new HashSet<String>();
159 for ( int i = start; i <= end; i++ )
160 {
161 keys.add( i + ":key" );
162 }
163
164 Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
165 for ( int i = start; i <= end; i++ )
166 {
167 ICacheElement<String, String> element = elements.get( i + ":key" );
168 assertNotNull( "element " + i + ":key is missing", element );
169 assertEquals( "value " + i + ":key", region + " data " + i + "-" + region, element.getVal() );
170 }
171 }
172 }