1   package org.apache.jcs.utils.struct;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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         // check minimal correctness
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 }