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