SPR-8023
+ fix bug in Ehcache cache that considered expired entries for key checks
This commit is contained in:
Costin Leau
2011-03-06 11:36:36 +00:00
parent d7a8cf4b7e
commit 523a83ca28
3 changed files with 32 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010 the original author or authors.
* Copyright 2010-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,8 +16,11 @@
package org.springframework.cache.ehcache;
import static org.junit.Assert.*;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.vendor.AbstractNativeCacheTest;
@@ -41,4 +44,21 @@ public class EhCacheCacheTest extends AbstractNativeCacheTest<Ehcache> {
protected Cache createCache(Ehcache nativeCache) {
return new EhCacheCache(nativeCache);
}
}
@Test
public void testExpiredElements() throws Exception {
String key = "brancusi";
String value = "constantin";
Element brancusi = new Element(key, value);
// ttl = 10s
brancusi.setTimeToLive(3);
nativeCache.put(brancusi);
assertTrue(cache.containsKey(key));
assertEquals(value, cache.get(key));
// wait for the entry to expire
Thread.sleep(5 * 1000);
assertFalse("expired entry returned", cache.containsKey(key));
assertNull(cache.get(key));
}
}

View File

@@ -16,9 +16,7 @@
package org.springframework.cache.vendor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
@@ -31,8 +29,8 @@ import org.springframework.cache.Cache;
*/
public abstract class AbstractNativeCacheTest<T> {
private T nativeCache;
private Cache cache;
protected T nativeCache;
protected Cache cache;
protected final static String CACHE_NAME = "testCache";
@Before