From 37e42e68e858675849c90089ff2123cd5e47daec Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 29 Jun 2016 10:43:54 +0200 Subject: [PATCH] Refined AspectJ caching tests Issue: SPR-14413 --- .../aspectj/AspectJCachingConfiguration.java | 5 ++- .../AspectJEnableCachingIsolatedTests.java | 43 +++++++++---------- .../aspectj/AspectJEnableCachingTests.java | 3 +- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java index 4ef89f30e0e..9932c79d122 100644 --- a/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java +++ b/spring-aspects/src/main/java/org/springframework/cache/aspectj/AspectJCachingConfiguration.java @@ -24,10 +24,11 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Role; /** - * {@code @Configuration} class that registers the Spring infrastructure beans necessary - * to enable AspectJ-based annotation-driven cache management. + * {@code @Configuration} class that registers the Spring infrastructure beans + * necessary to enable AspectJ-based annotation-driven cache management. * * @author Chris Beams + * @author Stephane Nicoll * @since 3.1 * @see org.springframework.cache.annotation.EnableCaching * @see org.springframework.cache.annotation.CachingConfigurationSelector diff --git a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java index 17b2d5608c4..15680c9ac68 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java +++ b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java @@ -31,7 +31,6 @@ import org.springframework.cache.config.DefaultCacheableService; import org.springframework.cache.config.SomeCustomKeyGenerator; import org.springframework.cache.config.SomeKeyGenerator; import org.springframework.cache.interceptor.CacheErrorHandler; -import org.springframework.cache.interceptor.CacheInterceptor; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.NamedCacheResolver; @@ -47,13 +46,17 @@ import org.springframework.context.annotation.Configuration; import static org.junit.Assert.*; /** - * * @author Stephane Nicoll */ public class AspectJEnableCachingIsolatedTests { private ConfigurableApplicationContext ctx; + + private void load(Class... config) { + this.ctx = new AnnotationConfigApplicationContext(config); + } + @After public void closeContext() { if (this.ctx != null) { @@ -61,6 +64,7 @@ public class AspectJEnableCachingIsolatedTests { } } + @Test public void testKeyStrategy() { load(EnableCachingConfig.class); @@ -75,22 +79,21 @@ public class AspectJEnableCachingIsolatedTests { assertSame(this.ctx.getBean("errorHandler", CacheErrorHandler.class), aspect.getErrorHandler()); } + // --- local tests ------- @Test - public void singleCacheManagerBean() throws Throwable { + public void singleCacheManagerBean() { load(SingleCacheManagerConfig.class); } - @Test(expected = IllegalStateException.class) - public void multipleCacheManagerBeans() throws Throwable { + @Test + public void multipleCacheManagerBeans() { try { load(MultiCacheManagerConfig.class); } - catch (BeanCreationException ex) { - Throwable root = ex.getRootCause(); - assertTrue(root.getMessage().contains("beans of type CacheManager")); - throw root; + catch (IllegalStateException ex) { + assertTrue(ex.getMessage().contains("bean of type CacheManager")); } } @@ -99,27 +102,25 @@ public class AspectJEnableCachingIsolatedTests { load(MultiCacheManagerConfigurer.class); // does not throw } - @Test(expected = IllegalStateException.class) - public void multipleCachingConfigurers() throws Throwable { + @Test + public void multipleCachingConfigurers() { try { load(MultiCacheManagerConfigurer.class, EnableCachingConfig.class); } catch (BeanCreationException ex) { Throwable root = ex.getRootCause(); - assertTrue(root.getMessage().contains("implementations of CachingConfigurer")); - throw root; + assertTrue(root instanceof IllegalStateException); + assertTrue(ex.getMessage().contains("implementations of CachingConfigurer")); } } - @Test(expected = IllegalStateException.class) - public void noCacheManagerBeans() throws Throwable { + @Test + public void noCacheManagerBeans() { try { load(EmptyConfig.class); } - catch (BeanCreationException ex) { - Throwable root = ex.getRootCause(); - assertTrue(root.getMessage().contains("No bean of type CacheManager")); - throw root; + catch (IllegalStateException ex) { + assertTrue(ex.getMessage().contains("No bean of type CacheManager")); } } @@ -143,10 +144,6 @@ public class AspectJEnableCachingIsolatedTests { assertSame(this.ctx.getBean("keyGenerator"), aspect.getKeyGenerator()); } - private void load(Class... config) { - this.ctx = new AnnotationConfigApplicationContext(config); - } - @Configuration @EnableCaching(mode = AdviceMode.ASPECTJ) diff --git a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java index fbd9e78355e..a8ae57d20a6 100644 --- a/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java +++ b/spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingTests.java @@ -36,17 +36,16 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** - * * @author Stephane Nicoll */ public class AspectJEnableCachingTests extends AbstractCacheAnnotationTests { - /** hook into superclass suite of tests */ @Override protected ConfigurableApplicationContext getApplicationContext() { return new AnnotationConfigApplicationContext(EnableCachingConfig.class); } + @Configuration @EnableCaching(mode = AdviceMode.ASPECTJ) static class EnableCachingConfig extends CachingConfigurerSupport {