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 LHMLRUMemoryCacheConcurrentUnitTest
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       *
51       * @param testName
52       */
53      public LHMLRUMemoryCacheConcurrentUnitTest( 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 LHMLRUMemoryCacheConcurrentUnitTest( "testLHMLRUMemoryCache" )
68          {
69              @Override
70              public void runTest()
71                  throws Exception
72              {
73                  this.runTestForRegion( "indexedRegion1" );
74              }
75          } );
76  
77          return suite;
78      }
79  
80      /**
81       * Test setup
82       */
83      @Override
84      public void setUp()
85      {
86          //JCS.setConfigFilename( "/TestLHMLRUCache.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       *
96       * @throws Exception
97       *                If an error occurs
98       */
99      public void runTestForRegion( final String region )
100         throws Exception
101     {
102         final CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
103         cacheMgr.configure( "/TestLHMLRUCache.ccf" );
104         final CompositeCache<String, String> cache = cacheMgr.getCache( region );
105 
106         final LRUMemoryCache<String, String> lru = new LRUMemoryCache<>();
107         lru.initialize( cache );
108 
109         // Add items to cache
110 
111         for ( int i = 0; i < items; i++ )
112         {
113             final ICacheElement<String, String> ice = new CacheElement<>( cache.getCacheName(), i + ":key", region + " data " + i );
114             ice.setElementAttributes( cache.getElementAttributes() );
115             lru.update( ice );
116         }
117 
118         // Test that initial items have been purged
119         for ( int i = 0; i < 100; i++ )
120         {
121             assertNull( "Should not have " + i + ":key", lru.get( i + ":key" ) );
122         }
123 
124         // Test that last items are in cache
125         for ( int i = 100; i < items; i++ )
126         {
127             final String value = lru.get( i + ":key" ).getVal();
128             assertEquals( region + " data " + i, value );
129         }
130 
131         // Test that getMultiple returns all the items remaining in cache and none of the missing ones
132         final Set<String> keys = new HashSet<>();
133         for ( int i = 0; i < items; i++ )
134         {
135             keys.add( i + ":key" );
136         }
137 
138         final Map<String, ICacheElement<String, String>> elements = lru.getMultiple( keys );
139         for ( int i = 0; i < 100; i++ )
140         {
141             assertNull( "Should not have " + i + ":key", elements.get( i + ":key" ) );
142         }
143         for ( int i = 100; i < items; i++ )
144         {
145             final ICacheElement<String, String> element = elements.get( i + ":key" );
146             assertNotNull( "element " + i + ":key is missing", element );
147             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
148         }
149 
150         // Remove all the items
151 
152         for ( int i = 0; i < items; i++ )
153         {
154             lru.remove( i + ":key" );
155         }
156 
157         // Verify removal
158 
159         for ( int i = 0; i < items; i++ )
160         {
161             assertNull( "Removed key should be null: " + i + ":key", lru.get( i + ":key" ) );
162         }
163     }
164 
165 }