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