@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -17,7 +17,7 @@
|
||||
package org.springframework.cache.config;
|
||||
|
||||
/**
|
||||
* Basic service interface.
|
||||
* Basic service interface for caching tests.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Phillip Webb
|
||||
@@ -83,7 +83,6 @@ public interface CacheableService<T> {
|
||||
|
||||
T throwUncheckedSync(Object arg1);
|
||||
|
||||
// multi annotations
|
||||
T multiCache(Object arg1);
|
||||
|
||||
T multiEvict(Object arg1);
|
||||
@@ -95,4 +94,5 @@ public interface CacheableService<T> {
|
||||
T multiUpdate(Object arg1);
|
||||
|
||||
TestEntity putRefersToResult(TestEntity arg1);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -25,7 +25,7 @@ import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.cache.annotation.Caching;
|
||||
|
||||
/**
|
||||
* Simple cacheable service
|
||||
* Simple cacheable service.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Phillip Webb
|
||||
@@ -34,12 +34,14 @@ import org.springframework.cache.annotation.Caching;
|
||||
public class DefaultCacheableService implements CacheableService<Long> {
|
||||
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
|
||||
private final AtomicLong nullInvocations = new AtomicLong();
|
||||
|
||||
|
||||
@Override
|
||||
@Cacheable("testCache")
|
||||
public Long cache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,7 +53,7 @@ public class DefaultCacheableService implements CacheableService<Long> {
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", sync = true)
|
||||
public Long cacheSync(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -96,13 +98,13 @@ public class DefaultCacheableService implements CacheableService<Long> {
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", condition = "#p0 == 3")
|
||||
public Long conditional(int classField) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", sync = true, condition = "#p0 == 3")
|
||||
public Long conditionalSync(int classField) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -114,55 +116,55 @@ public class DefaultCacheableService implements CacheableService<Long> {
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", key = "#p0")
|
||||
public Long key(Object arg1, Object arg2) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache")
|
||||
public Long varArgsKey(Object... args) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", key = "#root.methodName")
|
||||
public Long name(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", key = "#root.methodName + #root.method.name + #root.targetClass + #root.target")
|
||||
public Long rootVars(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", keyGenerator = "customKeyGenerator")
|
||||
public Long customKeyGenerator(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", keyGenerator = "unknownBeanName")
|
||||
public Long unknownCustomKeyGenerator(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", cacheManager = "customCacheManager")
|
||||
public Long customCacheManager(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(cacheNames = "testCache", cacheManager = "unknownBeanName")
|
||||
public Long unknownCustomCacheManager(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@CachePut("testCache")
|
||||
public Long update(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -174,13 +176,13 @@ public class DefaultCacheableService implements CacheableService<Long> {
|
||||
@Override
|
||||
@Cacheable("testCache")
|
||||
public Long nullValue(Object arg1) {
|
||||
nullInvocations.incrementAndGet();
|
||||
this.nullInvocations.incrementAndGet();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number nullInvocations() {
|
||||
return nullInvocations.get();
|
||||
return this.nullInvocations.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -212,25 +214,25 @@ public class DefaultCacheableService implements CacheableService<Long> {
|
||||
@Override
|
||||
@Caching(cacheable = { @Cacheable("primary"), @Cacheable("secondary") })
|
||||
public Long multiCache(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Caching(evict = { @CacheEvict("primary"), @CacheEvict(cacheNames = "secondary", key = "#p0"), @CacheEvict(cacheNames = "primary", key = "#p0 + 'A'") })
|
||||
public Long multiEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Caching(cacheable = { @Cacheable(cacheNames = "primary", key = "#root.methodName") }, evict = { @CacheEvict("secondary") })
|
||||
public Long multiCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Caching(cacheable = { @Cacheable(cacheNames = "primary", condition = "#p0 == 3") }, evict = { @CacheEvict("secondary") })
|
||||
public Long multiConditionalCacheAndEvict(Object arg1) {
|
||||
return counter.getAndIncrement();
|
||||
return this.counter.getAndIncrement();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2017 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.
|
||||
@@ -39,7 +39,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Integration tests for @EnableCaching and its related @Configuration classes.
|
||||
* Integration tests for {@code @EnableCaching} and its related
|
||||
* {@code @Configuration} classes.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Stephane Nicoll
|
||||
@@ -54,18 +55,16 @@ public class EnableCachingTests extends AbstractCacheAnnotationTests {
|
||||
|
||||
@Test
|
||||
public void testKeyStrategy() {
|
||||
CacheInterceptor ci = ctx.getBean(CacheInterceptor.class);
|
||||
assertSame(ctx.getBean("keyGenerator", KeyGenerator.class), ci.getKeyGenerator());
|
||||
CacheInterceptor ci = this.ctx.getBean(CacheInterceptor.class);
|
||||
assertSame(this.ctx.getBean("keyGenerator", KeyGenerator.class), ci.getKeyGenerator());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheErrorHandler() {
|
||||
CacheInterceptor ci = ctx.getBean(CacheInterceptor.class);
|
||||
assertSame(ctx.getBean("errorHandler", CacheErrorHandler.class), ci.getErrorHandler());
|
||||
CacheInterceptor ci = this.ctx.getBean(CacheInterceptor.class);
|
||||
assertSame(this.ctx.getBean("errorHandler", CacheErrorHandler.class), ci.getErrorHandler());
|
||||
}
|
||||
|
||||
// --- local tests -------
|
||||
|
||||
@Test
|
||||
public void singleCacheManagerBean() throws Throwable {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
@@ -91,7 +90,7 @@ public class EnableCachingTests extends AbstractCacheAnnotationTests {
|
||||
public void multipleCacheManagerBeans_implementsCachingConfigurer() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(MultiCacheManagerConfigurer.class);
|
||||
ctx.refresh(); // does not throw
|
||||
ctx.refresh(); // does not throw an exception
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@@ -124,22 +123,17 @@ public class EnableCachingTests extends AbstractCacheAnnotationTests {
|
||||
|
||||
@Test
|
||||
public void emptyConfigSupport() {
|
||||
ConfigurableApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class);
|
||||
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfigSupportConfig.class);
|
||||
CacheInterceptor ci = context.getBean(CacheInterceptor.class);
|
||||
assertNotNull(ci.getCacheResolver());
|
||||
assertEquals(SimpleCacheResolver.class, ci.getCacheResolver().getClass());
|
||||
assertSame(context.getBean(CacheManager.class),
|
||||
((SimpleCacheResolver)ci.getCacheResolver()).getCacheManager());
|
||||
assertSame(context.getBean(CacheManager.class), ((SimpleCacheResolver)ci.getCacheResolver()).getCacheManager());
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bothSetOnlyResolverIsUsed() {
|
||||
ConfigurableApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(FullCachingConfig.class);
|
||||
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(FullCachingConfig.class);
|
||||
CacheInterceptor ci = context.getBean(CacheInterceptor.class);
|
||||
assertSame(context.getBean("cacheResolver"), ci.getCacheResolver());
|
||||
assertSame(context.getBean("keyGenerator"), ci.getKeyGenerator());
|
||||
|
||||
Reference in New Issue
Block a user