View Javadoc
1   package org.apache.commons.jcs3.engine.memory.lru;
2   
3   import java.util.HashSet;
4   import java.util.Map;
5   import java.util.Set;
6   
7   import org.apache.commons.jcs3.engine.CacheElement;
8   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
9   import org.apache.commons.jcs3.engine.control.CompositeCache;
10  import org.apache.commons.jcs3.engine.control.CompositeCacheManager;
11  
12  /*
13   * Licensed to the Apache Software Foundation (ASF) under one
14   * or more contributor license agreements.  See the NOTICE file
15   * distributed with this work for additional information
16   * regarding copyright ownership.  The ASF licenses this file
17   * to you under the Apache License, Version 2.0 (the
18   * "License"); you may not use this file except in compliance
19   * with the License.  You may obtain a copy of the License at
20   *
21   *   http://www.apache.org/licenses/LICENSE-2.0
22   *
23   * Unless required by applicable law or agreed to in writing,
24   * software distributed under the License is distributed on an
25   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26   * KIND, either express or implied.  See the License for the
27   * specific language governing permissions and limitations
28   * under the License.
29   */
30  
31  import junit.extensions.ActiveTestSuite;
32  import junit.framework.Test;
33  import junit.framework.TestCase;
34  
35  /**
36   * Test which exercises the LRUMemory cache. This one uses three different
37   * regions for three threads.
38   */
39  public class LRUMemoryCacheConcurrentUnitTest
40      extends TestCase
41  {
42      /**
43       * Number of items to cache, twice the configured maxObjects for the memory
44       * cache regions.
45       */
46      private static final int items = 200;
47  
48      /**
49       * Constructor for the TestDiskCache object.
50       * <p>
51       * @param testName
52       */
53      public LRUMemoryCacheConcurrentUnitTest( final String testName )
54      {
55          super( testName );
56      }
57  
58      /**
59       * A unit test suite for JUnit
60       * <p>
61       * @return The test suite
62       */
63      public static Test suite()
64      {
65          final ActiveTestSuite suite = new ActiveTestSuite();
66  
67          suite.addTest( new LRUMemoryCacheConcurrentUnitTest( "testLRUMemoryCache" )
68          {
69              @Override
70              public void runTest()
71                  throws Exception
72              {
73                  this.runTestForRegion( "testRegion1" );
74              }
75          } );
76  
77          return suite;
78      }
79  
80      /**
81       * Test setup
82       */
83      @Override
84      public void setUp()
85      {
86          //JCS.setConfigFilename( "/TestDiskCache.ccf" );
87      }
88  
89      /**
90       * Adds items to cache, gets them, and removes them. The item count is more
91       * than the size of the memory cache, so items should be dumped.
92       * <p>
93       * @param region
94       *            Name of the region to access
95       * @throws Exception
96       *                If an error occurs
97       */
98      public void runTestForRegion( final String region )
99          throws Exception
100     {
101         final CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
102         cacheMgr.configure( "/TestDiskCache.ccf" );
103         final CompositeCache<String, String> cache = cacheMgr.getCache( region );
104 
105         final LRUMemoryCache<String, String> lru = new LRUMemoryCache<>();
106         lru.initialize( cache );
107 
108         // Add items to cache
109 
110         for ( int i = 0; i < items; i++ )
111         {
112             final ICacheElement<String, String> ice = new CacheElement<>( cache.getCacheName(), i + ":key", region + " data " + i );
113             ice.setElementAttributes( cache.getElementAttributes() );
114             lru.update( ice );
115         }
116 
117         // Test that initial items have been purged
118         for ( int i = 0; i < 100; i++ )
119         {
120             assertNull( "Should not have " + i + ":key", lru.get( i + ":key" ) );
121         }
122 
123         // Test that last items are in cache
124         for ( int i = 100; i < items; i++ )
125         {
126             final String value = lru.get( i + ":key" ).getVal();
127             assertEquals( region + " data " + i, value );
128         }
129 
130         // Test that getMultiple returns all the items remaining in cache and none of the missing ones
131         final Set<String> keys = new HashSet<>();
132         for ( int i = 0; i < items; i++ )
133         {
134             keys.add( i + ":key" );
135         }
136 
137         final Map<String, ICacheElement<String, String>> elements = lru.getMultiple( keys );
138         for ( int i = 0; i < 100; i++ )
139         {
140             assertNull( "Should not have " + i + ":key", elements.get( i + ":key" ) );
141         }
142         for ( int i = 100; i < items; i++ )
143         {
144             final ICacheElement<String, String> element = elements.get( i + ":key" );
145             assertNotNull( "element " + i + ":key is missing", element );
146             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
147         }
148 
149         // Remove all the items
150         for ( int i = 0; i < items; i++ )
151         {
152             lru.remove( i + ":key" );
153         }
154 
155         // Verify removal
156         for ( int i = 0; i < items; i++ )
157         {
158             assertNull( "Removed key should be null: " + i + ":key", lru.get( i + ":key" ) );
159         }
160     }
161 }