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