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 LHMLRUMemoryCacheConcurrentUnitTest
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       *
50       * @param testName
51       */
52      public LHMLRUMemoryCacheConcurrentUnitTest( String testName )
53      {
54          super( testName );
55      }
56  
57      /**
58       * Main method passes this test to the text test runner.
59       *
60       * @param args
61       */
62      public static void main( String args[] )
63      {
64          String[] testCaseName = { LHMLRUMemoryCacheConcurrentUnitTest.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 LHMLRUMemoryCacheConcurrentUnitTest( "testLHMLRUMemoryCache" )
78          {
79              @Override
80              public void runTest()
81                  throws Exception
82              {
83                  this.runTestForRegion( "indexedRegion1" );
84              }
85          } );
86  
87          return suite;
88      }
89  
90      /**
91       * Test setup
92       */
93      @Override
94      public void setUp()
95      {
96          //JCS.setConfigFilename( "/TestLHMLRUCache.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      *
106      * @throws Exception
107      *                If an error occurs
108      */
109     public void runTestForRegion( String region )
110         throws Exception
111     {
112         CompositeCacheManager cacheMgr = CompositeCacheManager.getUnconfiguredInstance();
113         cacheMgr.configure( "/TestLHMLRUCache.ccf" );
114         CompositeCache<String, String> cache = cacheMgr.getCache( region );
115 
116         LRUMemoryCache<String, String> lru = new LRUMemoryCache<String, String>();
117         lru.initialize( cache );
118 
119         // Add items to cache
120 
121         for ( int i = 0; i < items; i++ )
122         {
123             ICacheElement<String, String> ice = new CacheElement<String, String>( cache.getCacheName(), i + ":key", region + " data " + i );
124             ice.setElementAttributes( cache.getElementAttributes() );
125             lru.update( ice );
126         }
127 
128         // Test that initial items have been purged
129         for ( int i = 0; i < 100; i++ )
130         {
131             assertNull( "Should not have " + i + ":key", lru.get( i + ":key" ) );
132         }
133 
134         // Test that last items are in cache
135         for ( int i = 100; i < items; i++ )
136         {
137             String value = lru.get( i + ":key" ).getVal();
138             assertEquals( region + " data " + i, value );
139         }
140 
141         // Test that getMultiple returns all the items remaining in cache and none of the missing ones
142         Set<String> keys = new HashSet<String>();
143         for ( int i = 0; i < items; i++ )
144         {
145             keys.add( i + ":key" );
146         }
147 
148         Map<String, ICacheElement<String, String>> elements = lru.getMultiple( keys );
149         for ( int i = 0; i < 100; i++ )
150         {
151             assertNull( "Should not have " + i + ":key", elements.get( i + ":key" ) );
152         }
153         for ( int i = 100; i < items; i++ )
154         {
155             ICacheElement<String, String> element = elements.get( i + ":key" );
156             assertNotNull( "element " + i + ":key is missing", element );
157             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
158         }
159 
160         // Remove all the items
161 
162         for ( int i = 0; i < items; i++ )
163         {
164             lru.remove( i + ":key" );
165         }
166 
167         // Verify removal
168 
169         for ( int i = 0; i < items; i++ )
170         {
171             assertNull( "Removed key should be null: " + i + ":key", lru.get( i + ":key" ) );
172         }
173     }
174 
175 }