1 package org.apache.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
24 import org.apache.jcs.engine.CacheElement;
25 import org.apache.jcs.engine.ElementAttributes;
26 import org.apache.jcs.engine.behavior.ICacheElement;
27 import org.apache.jcs.engine.behavior.IElementAttributes;
28
29 /***
30 * Test store and load keys.
31 *
32 * @author Aaron Smuts
33 *
34 */
35 public class IndexedDiskCacheKeyStoreUnitTest
36 extends TestCase
37 {
38
39 /***
40 * Add some keys, store them, load them from disk, then check to see that we
41 * can get the items.
42 *
43 * @throws Exception
44 *
45 */
46 public void testStoreKeys()
47 throws Exception
48 {
49 IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
50 cattr.setCacheName( "testStoreKeys" );
51 cattr.setMaxKeySize( 100 );
52 cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
53 IndexedDiskCache disk = new IndexedDiskCache( cattr );
54
55 disk.doRemoveAll();
56
57 int cnt = 25;
58 for ( int i = 0; i < cnt; i++ )
59 {
60 IElementAttributes eAttr = new ElementAttributes();
61 eAttr.setIsSpool( true );
62 ICacheElement element = new CacheElement( cattr.getCacheName(), "key:" + i, "data:" + i );
63 element.setElementAttributes( eAttr );
64 disk.doUpdate( element );
65 }
66
67 for ( int i = 0; i < cnt; i++ )
68 {
69 ICacheElement element = disk.doGet( "key:" + i );
70 assertNotNull( "presave, Should have recevied an element.", element );
71 assertEquals( "presave, element is wrong.", "data:" + i, element.getVal() );
72 }
73
74 disk.saveKeys();
75
76 disk.loadKeys();
77
78 assertEquals( "The disk is the wrong size.", cnt, disk.getSize() );
79
80 for ( int i = 0; i < cnt; i++ )
81 {
82 ICacheElement element = disk.doGet( "key:" + i );
83 assertNotNull( "postsave, Should have recevied an element.", element );
84 assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
85 }
86
87 disk.dump();
88
89 }
90
91
92 /***
93 * Add some elements, remove 1, call optiiize, verify that the removed isn't present.
94 *
95 * We should also compare the data file sizes. . . .
96 *
97 * @throws Exception
98 *
99 */
100 public void testOptiimize()
101 throws Exception
102 {
103 IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
104 cattr.setCacheName( "testOptimize" );
105 cattr.setMaxKeySize( 100 );
106 cattr.setDiskPath( "target/test-sandbox/KeyStoreUnitTest" );
107 IndexedDiskCache disk = new IndexedDiskCache( cattr );
108
109 disk.doRemoveAll();
110
111 int cnt = 25;
112 for ( int i = 0; i < cnt; i++ )
113 {
114 IElementAttributes eAttr = new ElementAttributes();
115 eAttr.setIsSpool( true );
116 ICacheElement element = new CacheElement( cattr.getCacheName(), "key:" + i, "data:" + i );
117 element.setElementAttributes( eAttr );
118 disk.doUpdate( element );
119 }
120
121 long preAddRemoveSize = disk.getDataFileSize();
122
123 IElementAttributes eAttr = new ElementAttributes();
124 eAttr.setIsSpool( true );
125 ICacheElement elementSetup = new CacheElement( cattr.getCacheName(), "key:" + "A", "data:" + "A" );
126 elementSetup.setElementAttributes( eAttr );
127 disk.doUpdate( elementSetup );
128
129 ICacheElement elementRet = disk.doGet( "key:" + "A" );
130 assertNotNull( "postsave, Should have recevied an element.", elementRet );
131 assertEquals( "postsave, element is wrong.", "data:" + "A", elementRet.getVal() );
132
133 disk.remove( "key:" + "A" );
134
135 long preSize = disk.getDataFileSize();
136
137 disk.optimizeFile();
138 long postSize = disk.getDataFileSize();
139
140 System.out.println( "preAddRemoveSize " + preAddRemoveSize );
141 System.out.println( "preSize " + preSize );
142 System.out.println( "postSize " + postSize );
143
144 assertTrue( "Should be smaller.", postSize < preSize );
145 assertEquals( "Should be the same size after optimization as before add and remove.", preAddRemoveSize, postSize );
146
147 for ( int i = 0; i < cnt; i++ )
148 {
149 ICacheElement element = disk.doGet( "key:" + i );
150 assertNotNull( "postsave, Should have recevied an element.", element );
151 assertEquals( "postsave, element is wrong.", "data:" + i, element.getVal() );
152 }
153 }
154 }