1 package org.apache.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 /***
25 * Tests for the keyStore.
26 * <p>
27 * @author Aaron Smuts
28 */
29 public class BlockDiskCacheKeyStoreUnitTest
30 extends TestCase
31 {
32 /*** Directory name */
33 private String rootDirName = "target/test-sandbox/block";
34
35 /***
36 * Put a bunch of keys inthe key store and verify that they are present.
37 * <p>
38 * @throws Exception
39 */
40 public void testPutKeys()
41 throws Exception
42 {
43
44 String regionName = "testPutKeys";
45 int maxKeys = 1000;
46 int bytesPerBlock = 2000;
47
48 BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
49 attributes.setCacheName( regionName );
50 attributes.setDiskPath( rootDirName );
51 attributes.setMaxKeySize( maxKeys );
52 attributes.setBlockSizeBytes( bytesPerBlock );
53
54 BlockDiskCache blockDiskCache = new BlockDiskCache( attributes );
55
56 BlockDiskKeyStore keyStore = new BlockDiskKeyStore( attributes, blockDiskCache );
57
58
59 int numElements = 100;
60 for ( int i = 0; i < numElements; i++ )
61 {
62 keyStore.put( String.valueOf( i ), new int[i] );
63 }
64 System.out.println( "testPutKeys " + keyStore );
65
66
67 assertEquals( "Wrong number of keys", numElements, keyStore.size() );
68 for ( int i = 0; i < numElements; i++ )
69 {
70 int[] result = keyStore.get( String.valueOf( i ) );
71 assertEquals( "Wrong array returned.", i, result.length );
72 }
73 }
74
75 /***
76 * Verify that we can load keys that we saved. Add a bunch. Save them. Clear the memory keyhash.
77 * Load the keys. Verify.
78 * <p>
79 * @throws Exception
80 */
81 public void testSaveLoadKeys()
82 throws Exception
83 {
84
85 String regionName = "testSaveLoadKeys";
86 int maxKeys = 10000;
87 int bytesPerBlock = 2000;
88
89 BlockDiskCacheAttributes attributes = new BlockDiskCacheAttributes();
90 attributes.setCacheName( regionName );
91 attributes.setDiskPath( rootDirName );
92 attributes.setMaxKeySize( maxKeys );
93 attributes.setBlockSizeBytes( bytesPerBlock );
94
95 BlockDiskCache blockDiskCache = new BlockDiskCache( attributes );
96
97 BlockDiskKeyStore keyStore = new BlockDiskKeyStore( attributes, blockDiskCache );
98
99
100 int numElements = 1000;
101
102 for ( int i = 0; i < numElements; i++ )
103 {
104 int blocks = i;
105 keyStore.put( String.valueOf( i ), new int[blocks] );
106 keyStore.put( String.valueOf( i ), new int[i] );
107 }
108 System.out.println( "testSaveLoadKeys " + keyStore );
109
110
111 assertEquals( "Wrong number of keys", numElements, keyStore.size() );
112
113
114 keyStore.saveKeys();
115 keyStore.clearMemoryMap();
116
117
118 assertEquals( "Wrong number of keys after clearing memory", 0, keyStore.size() );
119
120
121 keyStore.loadKeys();
122
123
124 assertEquals( "Wrong number of keys after loading", numElements, keyStore.size() );
125 for ( int i = 0; i < numElements; i++ )
126 {
127 int[] result = keyStore.get( String.valueOf( i ) );
128 assertEquals( "Wrong array returned.", i, result.length );
129 }
130 }
131 }