From 62bff1017e97eff4fbacc5c36ce07ec09bc27bf0 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 22 Dec 2016 14:48:54 +0100 Subject: [PATCH] Validate that AopAutoConfiguration back off properly Closes gh-7694 --- .../aop/AopAutoConfigurationTests.java | 78 +++++++++++++------ 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/aop/AopAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/aop/AopAutoConfigurationTests.java index ac756aabdbc..e11eacbd784 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/aop/AopAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/aop/AopAutoConfigurationTests.java @@ -18,13 +18,15 @@ package org.springframework.boot.autoconfigure.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; +import org.junit.After; import org.junit.Test; -import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.context.annotation.Import; import static org.assertj.core.api.Assertions.assertThat; @@ -32,18 +34,22 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link AopAutoConfiguration}. * * @author Eberhard Wolff + * @author Stephane Nicoll */ public class AopAutoConfigurationTests { private AnnotationConfigApplicationContext context; + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + @Test - public void testNoAopAutoConfiguration() { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(TestConfiguration.class, AopAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - EnvironmentTestUtils.addEnvironment(this.context, "spring.aop.auto:false"); - this.context.refresh(); + public void aopDisabled() { + load(TestConfiguration.class, "spring.aop.auto:false"); TestAspect aspect = this.context.getBean(TestAspect.class); assertThat(aspect.isCalled()).isFalse(); TestBean bean = this.context.getBean(TestBean.class); @@ -52,13 +58,30 @@ public class AopAutoConfigurationTests { } @Test - public void testAopAutoConfigurationProxyTargetClass() { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(TestConfiguration.class, AopAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.aop.proxyTargetClass:true"); - this.context.refresh(); + public void aopWithDefaultSettings() { + load(TestConfiguration.class); + testProxyTargetClassDisabled(); + } + + @Test + public void aopWithEnabledProxyTargetClass() { + load(TestConfiguration.class, "spring.aop.proxy-target-class:true"); + testProxyTargetClassEnabled(); + } + + @Test + public void aopWithDisabledProxyTargetClass() { + load(TestConfiguration.class, "spring.aop.proxy-target-class:false"); + testProxyTargetClassDisabled(); + } + + @Test + public void aopWithCustomConfiguration() { + load(CustomTestConfiguration.class); + testProxyTargetClassEnabled(); + } + + private void testProxyTargetClassEnabled() { TestAspect aspect = this.context.getBean(TestAspect.class); assertThat(aspect.isCalled()).isFalse(); TestBean bean = this.context.getBean(TestBean.class); @@ -66,19 +89,30 @@ public class AopAutoConfigurationTests { assertThat(aspect.isCalled()).isTrue(); } - @Test - public void testAopAutoConfigurationNoProxyTargetClass() { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(TestConfiguration.class, AopAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.aop.proxyTargetClass:false"); - this.context.refresh(); + private void testProxyTargetClassDisabled() { TestAspect aspect = this.context.getBean(TestAspect.class); assertThat(aspect.isCalled()).isFalse(); TestInterface bean = this.context.getBean(TestInterface.class); bean.foo(); assertThat(aspect.isCalled()).isTrue(); + assertThat(this.context.getBeansOfType(TestBean.class)).isEmpty(); + } + + private void load(Class config, String... environment) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(config); + EnvironmentTestUtils.addEnvironment(ctx, environment); + ctx.register(AopAutoConfiguration.class); + ctx.refresh(); + this.context = ctx; + } + + + @EnableAspectJAutoProxy(proxyTargetClass = true) + @Configuration + @Import(TestConfiguration.class) + protected static class CustomTestConfiguration { + } @Configuration