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.framework.TestCase;
23
24 import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes.DiskLimitType;
25
26
27
28
29
30
31
32 public class BlockDiskCacheKeyStoreUnitTest
33 extends TestCase
34 {
35
36 private final String rootDirName = "target/test-sandbox/block";
37
38
39
40
41
42
43
44 public void testPutKeys()
45 throws Exception
46 {
47
48 BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
49 attributes.setCacheName("testPutKeys");
50 attributes.setDiskPath(rootDirName);
51 attributes.setMaxKeySize(1000);
52 attributes.setBlockSizeBytes(2000);
53
54 innerTestPutKeys(attributes);
55 }
56
57 public void testPutKeysSize()
58 throws Exception
59 {
60
61 BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
62 attributes.setCacheName("testPutKeys");
63 attributes.setDiskPath(rootDirName);
64 attributes.setMaxKeySize(100000);
65 attributes.setBlockSizeBytes(1024);
66 attributes.setDiskLimitType(DiskLimitType.SIZE);
67
68 innerTestPutKeys(attributes);
69 }
70
71 private void innerTestPutKeys(BlockDiskCacheAttributes attributes)
72 {
73 BlockDiskCache<String, String> blockDiskCache = new BlockDiskCache<String, String>(attributes);
74 BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<String>(attributes, blockDiskCache);
75
76
77 int numElements = 100;
78 for (int i = 0; i < numElements; i++)
79 {
80 keyStore.put(String.valueOf(i), new int[i]);
81 }
82
83
84
85 assertEquals("Wrong number of keys", numElements, keyStore.size());
86 for (int i = 0; i < numElements; i++)
87 {
88 int[] result = keyStore.get(String.valueOf(i));
89 assertEquals("Wrong array returned.", i, result.length);
90 }
91 }
92
93
94
95
96
97
98
99
100 public void testSaveLoadKeys()
101 throws Exception
102 {
103
104 BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
105 attributes.setCacheName("testSaveLoadKeys");
106 attributes.setDiskPath(rootDirName);
107 attributes.setMaxKeySize(10000);
108 attributes.setBlockSizeBytes(2000);
109
110 testSaveLoadKeysInner(attributes);
111 }
112
113 public void testSaveLoadKeysSize()
114 throws Exception
115 {
116
117 BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
118 attributes.setCacheName("testSaveLoadKeys");
119 attributes.setDiskPath(rootDirName);
120 attributes.setMaxKeySize(10000);
121 attributes.setBlockSizeBytes(2000);
122
123 testSaveLoadKeysInner(attributes);
124 }
125
126 private void testSaveLoadKeysInner(BlockDiskCacheAttributes attributes)
127 {
128 BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<String>(attributes, null);
129
130
131 int numElements = 1000;
132 int blockIndex = 0;
133
134 for (int i = 0; i < numElements; i++)
135 {
136 int blocks = i;
137
138
139 int[] block1 = new int[blocks];
140 int[] block2 = new int[blocks];
141 for (int j = 0; j < blocks; j++)
142 {
143 block1[j] = blockIndex++;
144 block2[j] = blockIndex++;
145 }
146 keyStore.put(String.valueOf(i), block1);
147 keyStore.put(String.valueOf(i), block2);
148 }
149
150
151
152 assertEquals("Wrong number of keys", numElements, keyStore.size());
153
154
155 keyStore.saveKeys();
156 keyStore.clearMemoryMap();
157
158
159 assertEquals("Wrong number of keys after clearing memory", 0, keyStore.size());
160
161
162 keyStore.loadKeys();
163
164
165 assertEquals("Wrong number of keys after loading", numElements, keyStore.size());
166 for (int i = 0; i < numElements; i++)
167 {
168 int[] result = keyStore.get(String.valueOf(i));
169 assertEquals("Wrong array returned.", i, result.length);
170 }
171 }
172
173 public void testObjectLargerThanMaxSize()
174 {
175 BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
176 attributes.setCacheName("testObjectLargerThanMaxSize");
177 attributes.setDiskPath(rootDirName);
178 attributes.setMaxKeySize(1000);
179 attributes.setBlockSizeBytes(2000);
180 attributes.setDiskLimitType(DiskLimitType.SIZE);
181
182 @SuppressWarnings({ "unchecked", "rawtypes" })
183 BlockDiskKeyStore<String> keyStore = new BlockDiskKeyStore<String>(attributes, new BlockDiskCache(attributes));
184
185 keyStore.put("1", new int[1000]);
186 keyStore.put("2", new int[1000]);
187 assertNull(keyStore.get("1"));
188 assertNotNull(keyStore.get("2"));
189 }
190 }