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