Skip to content

Commit 665b142

Browse files
author
John McNally
committed
added a test for just the lru memory cache.
PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/jakarta/jcs/trunk@224003 13f79535-47bb-0310-9956-ffa450edef68
1 parent e1b8606 commit 665b142

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed

project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<unitTestPattern>include = **/TestDiskCache.java</unitTestPattern>
128128
<unitTestPattern>include = **/TestDiskCacheConcurrent.java</unitTestPattern>
129129
<unitTestPattern>include = **/TestTCPLateralCache.java</unitTestPattern>
130+
<unitTestPattern>include = **/TestLRUMemoryCache.java</unitTestPattern>
130131
</unitTestPatterns>
131132
<jarResources></jarResources>
132133
<jars></jars>
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package org.apache.jcs.engine.memory.lru;
2+
3+
/*
4+
* ====================================================================
5+
* The Apache Software License, Version 1.1
6+
*
7+
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
8+
* reserved.
9+
*
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions
12+
* are met:
13+
*
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
*
17+
* 2. Redistributions in binary form must reproduce the above copyright
18+
* notice, this list of conditions and the following disclaimer in
19+
* the documentation and/or other materials provided with the
20+
* distribution.
21+
*
22+
* 3. The end-user documentation included with the redistribution,
23+
* if any, must include the following acknowledgment:
24+
* "This product includes software developed by the
25+
* Apache Software Foundation (http://www.apache.org/)."
26+
* Alternately, this acknowledgment may appear in the software itself,
27+
* if and wherever such third-party acknowledgments normally appear.
28+
*
29+
* 4. The names "Apache" and "Apache Software Foundation" and
30+
* "Apache Turbine" must not be used to endorse or promote products
31+
* derived from this software without prior written permission. For
32+
* written permission, please contact apache@apache.org.
33+
*
34+
* 5. Products derived from this software may not be called "Apache",
35+
* "Apache Turbine", nor may "Apache" appear in their name, without
36+
* prior written permission of the Apache Software Foundation.
37+
*
38+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49+
* SUCH DAMAGE.
50+
* ====================================================================
51+
*
52+
* This software consists of voluntary contributions made by many
53+
* individuals on behalf of the Apache Software Foundation. For more
54+
* information on the Apache Software Foundation, please see
55+
* <http://www.apache.org/>.
56+
*/
57+
58+
import junit.extensions.ActiveTestSuite;
59+
import junit.framework.Test;
60+
import junit.framework.TestCase;
61+
62+
import org.apache.jcs.JCS;
63+
import org.apache.jcs.engine.CacheElement;
64+
import org.apache.jcs.engine.behavior.ICacheElement;
65+
import org.apache.jcs.engine.control.CompositeCacheManager;
66+
import org.apache.jcs.engine.control.CompositeCache;
67+
68+
/**
69+
* Test which exercises the indexed disk cache. This one uses three different
70+
* regions for thre threads.
71+
*
72+
* @author <a href="mailto:james@jamestaylor.org">James Taylor</a>
73+
* @version $Id$
74+
*/
75+
public class TestLRUMemoryCache extends TestCase
76+
{
77+
/**
78+
* Number of items to cache, twice the configured maxObjects for the
79+
* memory cache regions.
80+
*/
81+
private static int items = 200;
82+
83+
/**
84+
* Constructor for the TestDiskCache object.
85+
*/
86+
public TestLRUMemoryCache( String testName )
87+
{
88+
super( testName );
89+
}
90+
91+
/**
92+
* Main method passes this test to the text test runner.
93+
*/
94+
public static void main( String args[] )
95+
{
96+
String[] testCaseName = {TestLRUMemoryCache.class.getName()};
97+
junit.textui.TestRunner.main( testCaseName );
98+
}
99+
100+
/**
101+
* A unit test suite for JUnit
102+
*
103+
* @return The test suite
104+
*/
105+
public static Test suite()
106+
{
107+
ActiveTestSuite suite = new ActiveTestSuite();
108+
109+
suite.addTest( new TestLRUMemoryCache( "testLRUMemoryCache" )
110+
{
111+
public void runTest() throws Exception
112+
{
113+
this.runTestForRegion( "indexedRegion1" );
114+
}
115+
} );
116+
117+
/*
118+
suite.addTest( new TestDiskCache( "testIndexedDiskCache2" )
119+
{
120+
public void runTest() throws Exception
121+
{
122+
this.runTestForRegion( "indexedRegion2" );
123+
}
124+
} );
125+
126+
suite.addTest( new TestDiskCache( "testIndexedDiskCache3" )
127+
{
128+
public void runTest() throws Exception
129+
{
130+
this.runTestForRegion( "indexedRegion3" );
131+
}
132+
} );
133+
*/
134+
return suite;
135+
}
136+
137+
/**
138+
* Test setup
139+
*/
140+
public void setUp()
141+
{
142+
//JCS.setConfigFilename( "/TestDiskCache.ccf" );
143+
}
144+
145+
/**
146+
* Adds items to cache, gets them, and removes them. The item count is more
147+
* than the size of the memory cache, so items should be dumped.
148+
*
149+
* @param region Name of the region to access
150+
*
151+
* @exception Exception If an error occurs
152+
*/
153+
public void runTestForRegion( String region )
154+
throws Exception
155+
{
156+
CompositeCacheManager cacheMgr = CompositeCacheManager
157+
.getUnconfiguredInstance();
158+
cacheMgr.configure( "/TestDiskCache.ccf" );
159+
CompositeCache cache = cacheMgr.getCache( region );
160+
161+
LRUMemoryCache lru = new LRUMemoryCache();
162+
lru.initialize(cache);
163+
164+
// Add items to cache
165+
166+
for ( int i = 0; i < items; i++ )
167+
{
168+
ICacheElement ice = new CacheElement(
169+
cache.getCacheName(), i + ":key", region + " data " + i);
170+
ice.setElementAttributes(cache.getElementAttributes().copy());
171+
lru.update(ice);
172+
}
173+
174+
// Test that initial items have been purged
175+
176+
for ( int i = 0; i < 102; i++ )
177+
{
178+
this.assertNull(lru.get( i + ":key" ));
179+
}
180+
181+
// Test that last items are in cache
182+
183+
for ( int i = 102; i < items; i++ )
184+
{
185+
String value = (String)
186+
((ICacheElement)lru.get( i + ":key" )).getVal();
187+
this.assertEquals( region + " data " + i, value );
188+
}
189+
190+
// Remove all the items
191+
192+
for ( int i = 0; i < items; i++ )
193+
{
194+
lru.remove( i + ":key" );
195+
}
196+
197+
// Verify removal
198+
199+
for ( int i = 0; i < items; i++ )
200+
{
201+
assertNull( "Removed key should be null: " + i + ":key",
202+
lru.get( i + ":key" ) );
203+
}
204+
}
205+
}

0 commit comments

Comments
 (0)