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