SPR-7736
+ clarify storage of null values
This commit is contained in:
Costin Leau
2010-12-17 10:15:50 +00:00
parent 18e141cbaa
commit 8285e9c2a7
8 changed files with 157 additions and 27 deletions

View File

@@ -16,8 +16,7 @@
package org.springframework.cache.config;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
@@ -26,6 +25,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Abstract annotation test (containing several reusable methods).
*
* @author Costin Leau
*/
public abstract class AbstractAnnotationTest {
@@ -72,7 +72,8 @@ public abstract class AbstractAnnotationTest {
assertSame(r3, r4);
}
public void testConditionalExpression(CacheableService service) throws Exception {
public void testConditionalExpression(CacheableService service)
throws Exception {
Object r1 = service.conditional(4);
Object r2 = service.conditional(4);
@@ -96,6 +97,16 @@ public abstract class AbstractAnnotationTest {
assertNotSame(r3, r4);
}
public void testNullValue(CacheableService service) throws Exception {
Object key = new Object();
assertNull(service.nullValue(key));
int nr = service.nullInvocations().intValue();
assertNull(service.nullValue(key));
assertEquals(nr, service.nullInvocations().intValue());
assertNull(service.nullValue(new Object()));
assertEquals(nr + 1, service.nullInvocations().intValue());
}
@Test
public void testCacheable() throws Exception {
testCacheable(cs);
@@ -126,4 +137,22 @@ public abstract class AbstractAnnotationTest {
testInvalidate(ccs);
}
@Test
public void testNullValue() throws Exception {
testNullValue(cs);
}
@Test
public void testClassNullValue() throws Exception {
Object key = new Object();
assertNull(ccs.nullValue(key));
int nr = ccs.nullInvocations().intValue();
assertNull(ccs.nullValue(key));
assertEquals(nr, ccs.nullInvocations().intValue());
assertNull(ccs.nullValue(new Object()));
// the check method is also cached
assertEquals(nr, ccs.nullInvocations().intValue());
assertEquals(nr + 1, AnnotatedClassCacheableService.nullInvocations
.intValue());
}
}

View File

@@ -27,7 +27,8 @@ import org.springframework.cache.annotation.Cacheable;
@Cacheable("default")
public class AnnotatedClassCacheableService implements CacheableService {
private AtomicLong counter = new AtomicLong();
private final AtomicLong counter = new AtomicLong();
public static final AtomicLong nullInvocations = new AtomicLong();
public Object cache(Object arg1) {
return counter.getAndIncrement();
@@ -45,4 +46,13 @@ public class AnnotatedClassCacheableService implements CacheableService {
public Object key(Object arg1, Object arg2) {
return counter.getAndIncrement();
}
public Object nullValue(Object arg1) {
nullInvocations.incrementAndGet();
return null;
}
public Number nullInvocations() {
return nullInvocations.get();
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cache.config;
/**
* Basic service interface.
*
@@ -31,4 +32,8 @@ public interface CacheableService<T> {
T key(Object arg1, Object arg2);
T nullValue(Object arg1);
Number nullInvocations();
}

View File

@@ -21,7 +21,6 @@ import java.util.concurrent.atomic.AtomicLong;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
/**
* Simple cacheable service
*
@@ -29,7 +28,8 @@ import org.springframework.cache.annotation.Cacheable;
*/
public class DefaultCacheableService implements CacheableService<Long> {
private AtomicLong counter = new AtomicLong();
private final AtomicLong counter = new AtomicLong();
private final AtomicLong nullInvocations = new AtomicLong();
@Cacheable("default")
public Long cache(Object arg1) {
@@ -49,4 +49,14 @@ public class DefaultCacheableService implements CacheableService<Long> {
public Long key(Object arg1, Object arg2) {
return counter.getAndIncrement();
}
@Cacheable("default")
public Long nullValue(Object arg1) {
nullInvocations.incrementAndGet();
return null;
}
public Number nullInvocations() {
return nullInvocations.get();
}
}