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