LRUMap - The removeLRU() method was passed the wrong LinkEntry [28433]

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131669 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-04-16 23:53:59 +00:00
parent 2c7a9427eb
commit 845a2177db
3 changed files with 61 additions and 3 deletions

View File

@ -65,6 +65,7 @@ No interface changes, or deprecations have occurred.
<li>AbstractHashedMap subclasses failed to clone() correctly [27159]</li> <li>AbstractHashedMap subclasses failed to clone() correctly [27159]</li>
<li>ExtendedProperties - Close input stream in constructor [27737]</li> <li>ExtendedProperties - Close input stream in constructor [27737]</li>
<li>Flat3Map - Handle infinite loops in toString</li> <li>Flat3Map - Handle infinite loops in toString</li>
<li>LRUMap - The removeLRU() method was passed the wrong LinkEntry [28433]</li>
</ul> </ul>
<center><h3>JAVADOC</h3></center> <center><h3>JAVADOC</h3></center>

View File

@ -41,7 +41,7 @@ import org.apache.commons.collections.BoundedMap;
* <code>ResettableIterator</code> and calling <code>reset()</code>. * <code>ResettableIterator</code> and calling <code>reset()</code>.
* *
* @since Commons Collections 3.0 (previously in main package v1.0) * @since Commons Collections 3.0 (previously in main package v1.0)
* @version $Revision: 1.9 $ $Date: 2004/02/18 01:13:19 $ * @version $Revision: 1.10 $ $Date: 2004/04/16 23:53:59 $
* *
* @author James Strachan * @author James Strachan
* @author Morgan Delagrange * @author Morgan Delagrange
@ -169,7 +169,7 @@ public class LRUMap
* @param value the value to add * @param value the value to add
*/ */
protected void addMapping(int hashIndex, int hashCode, Object key, Object value) { protected void addMapping(int hashIndex, int hashCode, Object key, Object value) {
if (size >= maxSize && removeLRU(header.before)) { if (size >= maxSize && removeLRU(header.after)) {
reuseMapping(header.after, hashIndex, hashCode, key, value); reuseMapping(header.after, hashIndex, hashCode, key, value);
} else { } else {
super.addMapping(hashIndex, hashCode, key, value); super.addMapping(hashIndex, hashCode, key, value);

View File

@ -30,7 +30,7 @@ import org.apache.commons.collections.ResettableIterator;
/** /**
* JUnit tests. * JUnit tests.
* *
* @version $Revision: 1.6 $ $Date: 2004/02/27 00:25:14 $ * @version $Revision: 1.7 $ $Date: 2004/04/16 23:53:59 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -218,6 +218,63 @@ public class TestLRUMap extends AbstractTestOrderedMap {
assertSame(map.get("1"), cloned.get("1")); assertSame(map.get("1"), cloned.get("1"));
} }
public void testRemoveLRU() {
MockLRUMapSubclass map = new MockLRUMapSubclass(2);
assertNull(map.entry);
map.put("A", "a");
assertNull(map.entry);
map.put("B", "b");
assertNull(map.entry);
map.put("C", "c"); // removes oldest, which is A=a
assertNotNull(map.entry);
assertEquals("A", map.key);
assertEquals("a", map.value);
assertEquals("C", map.entry.getKey()); // entry is reused
assertEquals("c", map.entry.getValue()); // entry is reused
assertEquals(false, map.containsKey("A"));
assertEquals(true, map.containsKey("B"));
assertEquals(true, map.containsKey("C"));
}
static class MockLRUMapSubclass extends LRUMap {
LinkEntry entry;
Object key;
Object value;
MockLRUMapSubclass(int size) {
super(size);
}
protected boolean removeLRU(LinkEntry entry) {
this.entry = entry;
this.key = entry.getKey();
this.value = entry.getValue();
return true;
}
}
public void testRemoveLRUBlocksRemove() {
MockLRUMapSubclassBlocksRemove map = new MockLRUMapSubclassBlocksRemove(2);
assertEquals(0, map.size());
map.put("A", "a");
assertEquals(1, map.size());
map.put("B", "b");
assertEquals(2, map.size());
map.put("C", "c"); // should remove oldest, which is A=a, but this is blocked
assertEquals(3, map.size());
assertEquals(2, map.maxSize());
assertEquals(true, map.containsKey("A"));
assertEquals(true, map.containsKey("B"));
assertEquals(true, map.containsKey("C"));
}
static class MockLRUMapSubclassBlocksRemove extends LRUMap {
MockLRUMapSubclassBlocksRemove(int size) {
super(size);
}
protected boolean removeLRU(LinkEntry entry) {
return false;
}
}
// public void testCreate() throws Exception { // public void testCreate() throws Exception {
// resetEmpty(); // resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LRUMap.emptyCollection.version3.obj"); // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LRUMap.emptyCollection.version3.obj");