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