View Javadoc
1   package org.apache.commons.jcs.engine.memory.lru;
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 junit.extensions.ActiveTestSuite;
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import org.apache.commons.jcs.engine.CacheElement;
26  import org.apache.commons.jcs.engine.behavior.ICacheElement;
27  import org.apache.commons.jcs.engine.control.CompositeCache;
28  import org.apache.commons.jcs.engine.control.CompositeCacheManager;
29  
30  import java.util.HashSet;
31  import java.util.Map;
32  import java.util.Set;
33  
34  /**
35   * Test which exercises the LRUMemory cache. This one uses three different
36   * regions for three threads.
37   */
38  public class LRUMemoryCacheConcurrentUnitTest
39      extends TestCase
40  {
41      /**
42       * Number of items to cache, twice the configured maxObjects for the memory
43       * cache regions.
44       */
45      private static int items = 200;
46  
47      /**
48       * Constructor for the TestDiskCache object.
49       * <p>
50       * @param testName
51       */
52      public LRUMemoryCacheConcurrentUnitTest( String testName )
53      {
54          super( testName );
55      }
56  
57      /**
58       * Main method passes this test to the text test runner.
59       * <p>
60       * @param args
61       */
62      public static void main( String args[] )
63      {
64          String[] testCaseName = { LRUMemoryCacheConcurrentUnitTest.class.getName() };
65          junit.textui.TestRunner.main( testCaseName );
66      }
67  
68      /**
69       * A unit test suite for JUnit
70       * <p>
71       * @return The test suite
72       */
73      public static Test suite()
74      {
75          ActiveTestSuite suite = new ActiveTestSuite();
76  
77          suite.addTest( new LRUMemoryCacheConcurrentUnitTest( "testLRUMemoryCache" )
78          {
79              @Override
80              public void runTest()
81                  throws Exception
82              {
83                  this.runTestForRegion( "testRegion1" );
84              }
85          } );
86  
87          return suite;
88      }
89  
90      /**
91       * Test setup
92       */
93      @Override
94      public void setUp()
95      {
96          //JCS.setConfigFilename( "/TestDiskCache.ccf" );
97      }
98  
99      /**
100      * Adds items to cache, gets them, and removes them. The item count is more
101      * than the size of the memory cache, so items should be dumped.
102      * <p>
103      * @param region
104      *            Name of the region to access
105      * @throws Exception
106      *                If an error occurs
107      */
108     public void runTestForRegion( String region )
109         throws Exception
110     {
111         CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
112         cacheMgr.configure( "/TestDiskCache.ccf" );
113         CompositeCache<String, String> cache = cacheMgr.getCache( region );
114 
115         LRUMemoryCache<String, String> lru = new LRUMemoryCache<String, String>();
116         lru.initialize( cache );
117 
118         // Add items to cache
119 
120         for ( int i = 0; i < items; i++ )
121         {
122             ICacheElement<String, String> ice = new CacheElement<String, String>( cache.getCacheName(), i + ":key", region + " data " + i );
123             ice.setElementAttributes( cache.getElementAttributes() );
124             lru.update( ice );
125         }
126 
127         // Test that initial items have been purged
128         for ( int i = 0; i < 100; i++ )
129         {
130             assertNull( "Should not have " + i + ":key", lru.get( i + ":key" ) );
131         }
132 
133         // Test that last items are in cache
134         for ( int i = 100; i < items; i++ )
135         {
136             String value = lru.get( i + ":key" ).getVal();
137             assertEquals( region + " data " + i, value );
138         }
139 
140         // Test that getMultiple returns all the items remaining in cache and none of the missing ones
141         Set<String> keys = new HashSet<String>();
142         for ( int i = 0; i < items; i++ )
143         {
144             keys.add( i + ":key" );
145         }
146 
147         Map<String, ICacheElement<String, String>> elements = lru.getMultiple( keys );
148         for ( int i = 0; i < 100; i++ )
149         {
150             assertNull( "Should not have " + i + ":key", elements.get( i + ":key" ) );
151         }
152         for ( int i = 100; i < items; i++ )
153         {
154             ICacheElement<String, String> element = elements.get( i + ":key" );
155             assertNotNull( "element " + i + ":key is missing", element );
156             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
157         }
158 
159         // Remove all the items
160         for ( int i = 0; i < items; i++ )
161         {
162             lru.remove( i + ":key" );
163         }
164 
165         // Verify removal
166         for ( int i = 0; i < items; i++ )
167         {
168             assertNull( "Removed key should be null: " + i + ":key", lru.get( i + ":key" ) );
169         }
170     }
171 }