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