1 package org.apache.commons.jcs.auxiliary.disk.indexed;
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 import org.apache.commons.jcs.engine.CacheElement;
24 import org.apache.commons.jcs.engine.ElementAttributes;
25 import org.apache.commons.jcs.engine.behavior.ICacheElement;
26 import org.apache.commons.jcs.engine.behavior.IElementAttributes;
27
28
29
30
31
32
33
34 public class IndexedDiskCacheKeyStoreUnitTest
35 extends TestCase
36 {
37
38
39
40
41
42
43
44
45 public void testStoreKeys()
46 throws Exception
47 {
48 IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
49 cattr.setCacheName( "testStoreKeys" );
50 cattr.setMaxKeySize( 100 );
51 cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
52 IndexedDiskCache<String, String> disk = new IndexedDiskCache<String, String>( cattr );
53
54 disk.processRemoveAll();
55
56 int cnt = 25;
57 for ( int i = 0; i < cnt; i++ )
58 {
59 IElementAttributes eAttr = new ElementAttributes();
60 eAttr.setIsSpool( true );
61 ICacheElement<String, String> element = new CacheElement<String, String>( cattr.getCacheName(), "key:" + i, "data:" + i );
62 element.setElementAttributes( eAttr );
63 disk.processUpdate( element );
64 }
65
66 for ( int i = 0; i < cnt; i++ )
67 {
68 ICacheElement<String, String> element = disk.processGet( "key:" + i );
69 assertNotNull( "presave, Should have received an element.", element );
70 assertEquals( "presave, element is wrong.", "data:" + i, element.getVal() );
71 }
72
73 disk.saveKeys();
74
75 disk.loadKeys();
76
77 assertEquals( "The disk is the wrong size.", cnt, disk.getSize() );
78
79 for ( int i = 0; i < cnt; i++ )
80 {
81 ICacheElement<String, String> element = disk.processGet( "key:" + i );
82 assertNotNull( "postsave, Should have received an element.", element );
83 assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
84 }
85
86 disk.dump();
87
88 }
89
90
91
92
93
94
95
96
97
98
99 public void testOptiimize()
100 throws Exception
101 {
102 IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
103 cattr.setCacheName( "testOptimize" );
104 cattr.setMaxKeySize( 100 );
105 cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
106 IndexedDiskCache<String, String> disk = new IndexedDiskCache<String, String>( cattr );
107
108 disk.processRemoveAll();
109
110 int cnt = 25;
111 for ( int i = 0; i < cnt; i++ )
112 {
113 IElementAttributes eAttr = new ElementAttributes();
114 eAttr.setIsSpool( true );
115 ICacheElement<String, String> element = new CacheElement<String, String>( cattr.getCacheName(), "key:" + i, "data:" + i );
116 element.setElementAttributes( eAttr );
117 disk.processUpdate( element );
118 }
119
120 long preAddRemoveSize = disk.getDataFileSize();
121
122 IElementAttributes eAttr = new ElementAttributes();
123 eAttr.setIsSpool( true );
124 ICacheElement<String, String> elementSetup = new CacheElement<String, String>( cattr.getCacheName(), "key:" + "A", "data:" + "A" );
125 elementSetup.setElementAttributes( eAttr );
126 disk.processUpdate( elementSetup );
127
128 ICacheElement<String, String> elementRet = disk.processGet( "key:" + "A" );
129 assertNotNull( "postsave, Should have received an element.", elementRet );
130 assertEquals( "postsave, element is wrong.", "data:" + "A", elementRet.getVal() );
131
132 disk.remove( "key:" + "A" );
133
134 long preSize = disk.getDataFileSize();
135
136 disk.optimizeFile();
137 long postSize = disk.getDataFileSize();
138
139 assertTrue( "Should be smaller. postsize="+postSize+" preSize="+preSize, postSize < preSize );
140 assertEquals( "Should be the same size after optimization as before add and remove.", preAddRemoveSize, postSize );
141
142 for ( int i = 0; i < cnt; i++ )
143 {
144 ICacheElement<String, String> element = disk.processGet( "key:" + i );
145 assertNotNull( "postsave, Should have received an element.", element );
146 assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
147 }
148 }
149 }