1 package org.apache.commons.jcs3.auxiliary.disk.block;
2
3 import java.util.HashSet;
4 import java.util.Map;
5 import java.util.Set;
6
7 import org.apache.commons.jcs3.JCS;
8 import org.apache.commons.jcs3.access.CacheAccess;
9 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 import junit.extensions.ActiveTestSuite;
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33
34
35
36
37 public class BlockDiskCacheSameRegionConcurrentUnitTest
38 extends TestCase
39 {
40
41
42
43
44
45 public BlockDiskCacheSameRegionConcurrentUnitTest( final String testName )
46 {
47 super( testName );
48 }
49
50
51
52
53
54 public static Test suite()
55 {
56 final ActiveTestSuite suite = new ActiveTestSuite();
57
58 suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache1" )
59 {
60 @Override
61 public void runTest()
62 throws Exception
63 {
64 this.runTestForRegion( "blockRegion4", 0, 200 );
65 }
66 } );
67
68 suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache2" )
69 {
70 @Override
71 public void runTest()
72 throws Exception
73 {
74 this.runTestForRegion( "blockRegion4", 1000, 1200 );
75 }
76 } );
77
78 suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache3" )
79 {
80 @Override
81 public void runTest()
82 throws Exception
83 {
84 this.runTestForRegion( "blockRegion4", 2000, 2200 );
85 }
86 } );
87
88 suite.addTest( new BlockDiskCacheSameRegionConcurrentUnitTest( "testBlockDiskCache4" )
89 {
90 @Override
91 public void runTest()
92 throws Exception
93 {
94 this.runTestForRegion( "blockRegion4", 2200, 5200 );
95 }
96 } );
97
98 return suite;
99 }
100
101
102
103
104
105
106 @Override
107 public void setUp()
108 throws Exception
109 {
110 JCS.setConfigFilename( "/TestBlockDiskCacheCon.ccf" );
111 }
112
113
114
115
116
117
118
119
120
121 public void runTestForRegion( final String region, final int start, final int end )
122 throws Exception
123 {
124 final CacheAccess<String, String> jcs = JCS.getInstance( region );
125
126
127
128 for ( int i = start; i <= end; i++ )
129 {
130 jcs.put( i + ":key", region + " data " + i + "-" + region );
131 }
132
133
134
135 for ( int i = start; i <= end; i++ )
136 {
137 final String key = i + ":key";
138 final String value = jcs.get( key );
139
140 assertEquals( "Wrong value for key [" + key + "]", region + " data " + i + "-" + region, value );
141 }
142
143
144 final Set<String> keys = new HashSet<>();
145 for ( int i = start; i <= end; i++ )
146 {
147 keys.add( i + ":key" );
148 }
149
150 final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
151 for ( int i = start; i <= end; i++ )
152 {
153 final ICacheElement<String, String> element = elements.get( i + ":key" );
154 assertNotNull( "element " + i + ":key is missing", element );
155 assertEquals( "value " + i + ":key", region + " data " + i + "-" + region, element.getVal() );
156 }
157 }
158 }