Browse Source

Refined AspectJ caching tests

Issue: SPR-14413
pull/1095/head
Juergen Hoeller 10 years ago
parent
commit
37e42e68e8
  1. 5
      spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java
  2. 43
      spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java
  3. 3
      spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java

5
spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java vendored

@ -24,10 +24,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role; import org.springframework.context.annotation.Role;
/** /**
* {@code @Configuration} class that registers the Spring infrastructure beans necessary * {@code @Configuration} class that registers the Spring infrastructure beans
* to enable AspectJ-based annotation-driven cache management. * necessary to enable AspectJ-based annotation-driven cache management.
* *
* @author Chris Beams * @author Chris Beams
* @author Stephane Nicoll
* @since 3.1 * @since 3.1
* @see org.springframework.cache.annotation.EnableCaching * @see org.springframework.cache.annotation.EnableCaching
* @see org.springframework.cache.annotation.CachingConfigurationSelector * @see org.springframework.cache.annotation.CachingConfigurationSelector

43
spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java vendored

@ -31,7 +31,6 @@ import org.springframework.cache.config.DefaultCacheableService;
import org.springframework.cache.config.SomeCustomKeyGenerator; import org.springframework.cache.config.SomeCustomKeyGenerator;
import org.springframework.cache.config.SomeKeyGenerator; import org.springframework.cache.config.SomeKeyGenerator;
import org.springframework.cache.interceptor.CacheErrorHandler; import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheInterceptor;
import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.NamedCacheResolver; import org.springframework.cache.interceptor.NamedCacheResolver;
@ -47,13 +46,17 @@ import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class AspectJEnableCachingIsolatedTests { public class AspectJEnableCachingIsolatedTests {
private ConfigurableApplicationContext ctx; private ConfigurableApplicationContext ctx;
private void load(Class<?>... config) {
this.ctx = new AnnotationConfigApplicationContext(config);
}
@After @After
public void closeContext() { public void closeContext() {
if (this.ctx != null) { if (this.ctx != null) {
@ -61,6 +64,7 @@ public class AspectJEnableCachingIsolatedTests {
} }
} }
@Test @Test
public void testKeyStrategy() { public void testKeyStrategy() {
load(EnableCachingConfig.class); load(EnableCachingConfig.class);
@ -75,22 +79,21 @@ public class AspectJEnableCachingIsolatedTests {
assertSame(this.ctx.getBean("errorHandler", CacheErrorHandler.class), aspect.getErrorHandler()); assertSame(this.ctx.getBean("errorHandler", CacheErrorHandler.class), aspect.getErrorHandler());
} }
// --- local tests ------- // --- local tests -------
@Test @Test
public void singleCacheManagerBean() throws Throwable { public void singleCacheManagerBean() {
load(SingleCacheManagerConfig.class); load(SingleCacheManagerConfig.class);
} }
@Test(expected = IllegalStateException.class) @Test
public void multipleCacheManagerBeans() throws Throwable { public void multipleCacheManagerBeans() {
try { try {
load(MultiCacheManagerConfig.class); load(MultiCacheManagerConfig.class);
} }
catch (BeanCreationException ex) { catch (IllegalStateException ex) {
Throwable root = ex.getRootCause(); assertTrue(ex.getMessage().contains("bean of type CacheManager"));
assertTrue(root.getMessage().contains("beans of type CacheManager"));
throw root;
} }
} }
@ -99,27 +102,25 @@ public class AspectJEnableCachingIsolatedTests {
load(MultiCacheManagerConfigurer.class); // does not throw load(MultiCacheManagerConfigurer.class); // does not throw
} }
@Test(expected = IllegalStateException.class) @Test
public void multipleCachingConfigurers() throws Throwable { public void multipleCachingConfigurers() {
try { try {
load(MultiCacheManagerConfigurer.class, EnableCachingConfig.class); load(MultiCacheManagerConfigurer.class, EnableCachingConfig.class);
} }
catch (BeanCreationException ex) { catch (BeanCreationException ex) {
Throwable root = ex.getRootCause(); Throwable root = ex.getRootCause();
assertTrue(root.getMessage().contains("implementations of CachingConfigurer")); assertTrue(root instanceof IllegalStateException);
throw root; assertTrue(ex.getMessage().contains("implementations of CachingConfigurer"));
} }
} }
@Test(expected = IllegalStateException.class) @Test
public void noCacheManagerBeans() throws Throwable { public void noCacheManagerBeans() {
try { try {
load(EmptyConfig.class); load(EmptyConfig.class);
} }
catch (BeanCreationException ex) { catch (IllegalStateException ex) {
Throwable root = ex.getRootCause(); assertTrue(ex.getMessage().contains("No bean of type CacheManager"));
assertTrue(root.getMessage().contains("No bean of type CacheManager"));
throw root;
} }
} }
@ -143,10 +144,6 @@ public class AspectJEnableCachingIsolatedTests {
assertSame(this.ctx.getBean("keyGenerator"), aspect.getKeyGenerator()); assertSame(this.ctx.getBean("keyGenerator"), aspect.getKeyGenerator());
} }
private void load(Class<?>... config) {
this.ctx = new AnnotationConfigApplicationContext(config);
}
@Configuration @Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ) @EnableCaching(mode = AdviceMode.ASPECTJ)

3
spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java vendored

@ -36,17 +36,16 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/** /**
*
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class AspectJEnableCachingTests extends AbstractCacheAnnotationTests { public class AspectJEnableCachingTests extends AbstractCacheAnnotationTests {
/** hook into superclass suite of tests */
@Override @Override
protected ConfigurableApplicationContext getApplicationContext() { protected ConfigurableApplicationContext getApplicationContext() {
return new AnnotationConfigApplicationContext(EnableCachingConfig.class); return new AnnotationConfigApplicationContext(EnableCachingConfig.class);
} }
@Configuration @Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ) @EnableCaching(mode = AdviceMode.ASPECTJ)
static class EnableCachingConfig extends CachingConfigurerSupport { static class EnableCachingConfig extends CachingConfigurerSupport {

Loading…
Cancel
Save