1 package org.apache.jcs.utils.struct;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.Map.Entry;
25
26 import org.apache.jcs.utils.struct.LRUMap;
27
28 import junit.framework.TestCase;
29
30 /***
31 * Basic unit tests for the LRUMap
32 *
33 * @author Aaron Smuts
34 *
35 */
36 public class LRUMapUnitTest
37 extends TestCase
38 {
39
40 /***
41 * Put up to the size limit and then make sure they are all there.
42 *
43 */
44 public void testPutWithSizeLimit()
45 {
46 int size = 10;
47 Map cache = new LRUMap( size );
48
49 for ( int i = 0; i < size; i++ )
50 {
51 cache.put( "key:" + i, "data:" + i );
52 }
53
54 for ( int i = 0; i < size; i++ )
55 {
56 String data = (String)cache.get( "key:" + i );
57 assertEquals( "Data is wrong.", "data:" + i, data );
58 }
59 }
60
61 /***
62 * Put into the lru with no limit and then make sure they are all there.
63 *
64 */
65 public void testPutWithNoSizeLimit()
66 {
67 int size = 10;
68 Map cache = new LRUMap( );
69
70 for ( int i = 0; i < size; i++ )
71 {
72 cache.put( "key:" + i, "data:" + i );
73 }
74
75 for ( int i = 0; i < size; i++ )
76 {
77 String data = (String)cache.get( "key:" + i );
78 assertEquals( "Data is wrong.", "data:" + i, data );
79 }
80 }
81
82 /***
83 * Put and then remove. Make sure the element is returned.
84 *
85 */
86 public void testPutAndRemove()
87 {
88 int size = 10;
89 Map cache = new LRUMap( size );
90
91 cache.put( "key:" + 1, "data:" + 1 );
92 String data = (String)cache.remove( "key:" + 1 );
93 assertEquals( "Data is wrong.", "data:" + 1, data );
94 }
95
96 /***
97 * Call remove on an empty map
98 *
99 */
100 public void testRemoveEmpty()
101 {
102 int size = 10;
103 Map cache = new LRUMap( size );
104
105 Object returned = cache.remove( "key:" + 1 );
106 assertNull( "Shouldn't hvae anything.", returned );
107 }
108
109
110 /***
111 * Add items to the map and then test to see that they come back in the entry set.
112 *
113 */
114 public void testGetEntrySet()
115 {
116 int size = 10;
117 Map cache = new LRUMap( size );
118
119 for ( int i = 0; i < size; i++ )
120 {
121 cache.put( "key:" + i, "data:" + i );
122 }
123
124 Set entries = cache.entrySet();
125 assertEquals( "Set contains the wrong number of items.", size, entries.size() );
126
127
128 Object[] entryArray = entries.toArray();
129 for ( int i = 0; i < size; i++ )
130 {
131 Entry data = (Entry)entryArray[i];
132 assertTrue( "Data is wrong.", data.getValue().toString().indexOf( "data:") != -1 );
133 }
134 }
135
136
137 }