1 package org.apache.commons.jcs.engine.memory.fifo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23
24 import junit.framework.TestCase;
25
26 import org.apache.commons.jcs.engine.CacheElement;
27 import org.apache.commons.jcs.engine.CompositeCacheAttributes;
28 import org.apache.commons.jcs.engine.ElementAttributes;
29 import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
30 import org.apache.commons.jcs.engine.control.CompositeCache;
31
32
33 public class FIFOMemoryCacheUnitTest
34 extends TestCase
35 {
36
37
38
39
40
41 public void testExpirationPolicy_oneExtra()
42 throws IOException
43 {
44
45 int maxObjects = 10;
46 String cacheName = "testExpirationPolicy_oneExtra";
47
48 ICompositeCacheAttributes attributes = new CompositeCacheAttributes();
49 attributes.setCacheName(cacheName);
50 attributes.setMaxObjects( maxObjects );
51 attributes.setSpoolChunkSize( 1 );
52
53 FIFOMemoryCache<String, String> cache = new FIFOMemoryCache<String, String>();
54 cache.initialize( new CompositeCache<String, String>( attributes, new ElementAttributes() ) );
55
56 for ( int i = 0; i <= maxObjects; i++ )
57 {
58 CacheElement<String, String> element = new CacheElement<String, String>( cacheName, "key" + i, "value" + i );
59 cache.update( element );
60 }
61
62 CacheElement<String, String> oneMoreElement = new CacheElement<String, String>( cacheName, "onemore", "onemore" );
63
64
65 cache.update( oneMoreElement );
66
67
68 assertEquals( "Should have max elements", maxObjects, cache.getSize() );
69 System.out.println(cache.getKeySet());
70 for ( int i = maxObjects; i > 1; i-- )
71 {
72 assertNotNull( "Should have element " + i, cache.get( "key" + i ) );
73 }
74 assertNotNull( "Should have oneMoreElement", cache.get( "onemore" ) );
75 }
76
77
78
79
80
81
82 public void testExpirationPolicy_doubleOver()
83 throws IOException
84 {
85
86 int maxObjects = 10;
87 String cacheName = "testExpirationPolicy_oneExtra";
88
89 ICompositeCacheAttributes attributes = new CompositeCacheAttributes();
90 attributes.setCacheName(cacheName);
91 attributes.setMaxObjects( maxObjects );
92 attributes.setSpoolChunkSize( 1 );
93
94 FIFOMemoryCache<String, String> cache = new FIFOMemoryCache<String, String>();
95 cache.initialize( new CompositeCache<String, String>( attributes, new ElementAttributes() ) );
96
97
98 for ( int i = 0; i <= (maxObjects * 2); i++ )
99 {
100 CacheElement<String, String> element = new CacheElement<String, String>( cacheName, "key" + i, "value" + i );
101 cache.update( element );
102 }
103
104
105 assertEquals( "Should have max elements", maxObjects, cache.getSize() );
106 for ( int i = (maxObjects * 2); i > maxObjects; i-- )
107 {
108 assertNotNull( "Shjould have elemnt " + i, cache.get( "key" + i ) );
109 }
110 }
111 }