diff --git a/spring-core/aop/src/test/java/com/example/aop/pointcut/ArgsTest.java b/spring-core/aop/src/test/java/com/example/aop/pointcut/ArgsTest.java new file mode 100644 index 00000000..be262d6a --- /dev/null +++ b/spring-core/aop/src/test/java/com/example/aop/pointcut/ArgsTest.java @@ -0,0 +1,68 @@ +package com.example.aop.pointcut; + +import com.example.aop.member.MemberServiceImpl; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.aop.aspectj.AspectJExpressionPointcut; + +import java.lang.reflect.Method; + +import static org.assertj.core.api.Assertions.*; + +public class ArgsTest { + + Method helloMethod; + + @BeforeEach + public void init() throws NoSuchMethodException { + helloMethod = MemberServiceImpl.class.getMethod("hello", String.class); + } + + private AspectJExpressionPointcut pointcut(String expression) { + AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); + pointcut.setExpression(expression); + return pointcut; + } + + @Test + void args() { + // hello(String)과 매칭 + assertThat(pointcut("args(String)") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + assertThat(pointcut("args(Object)") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + assertThat(pointcut("args()") + .matches(helloMethod, MemberServiceImpl.class)).isFalse(); + assertThat(pointcut("args(..)") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + assertThat(pointcut("args(*)") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + assertThat(pointcut("args(String, ..)") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + + } + + /** + * execution(* *(java.io.Serializable)): 메서드의 시그니처로 판단 (정적) + * args(java.io.Serializable): 런타임에 전달된 인수로 판단 (동적) + */ + @Test + void argsVsExecution() { + // Args + assertThat(pointcut("args(String)") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + assertThat(pointcut("args(java.io.Serializable)") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + assertThat(pointcut("args(Object)") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + + // Execution + assertThat(pointcut("execution(* *(String))") + .matches(helloMethod, MemberServiceImpl.class)).isTrue(); + assertThat(pointcut("execution(* *(java.io.Serializable))") + .matches(helloMethod, MemberServiceImpl.class)).isFalse(); + assertThat(pointcut("execution(* *(Object))") + .matches(helloMethod, MemberServiceImpl.class)).isFalse(); + } +} diff --git a/spring-core/aop/src/test/java/com/example/aop/pointcut/WithinTest.java b/spring-core/aop/src/test/java/com/example/aop/pointcut/WithinTest.java new file mode 100644 index 00000000..1878f7a9 --- /dev/null +++ b/spring-core/aop/src/test/java/com/example/aop/pointcut/WithinTest.java @@ -0,0 +1,59 @@ +package com.example.aop.pointcut; + +import com.example.aop.member.MemberServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.aop.aspectj.AspectJExpressionPointcut; + +import java.lang.reflect.Method; + +import static org.assertj.core.api.Assertions.assertThat; + +public class WithinTest { + + AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); + Method helloMethod; + + @BeforeEach + public void init() throws NoSuchMethodException { + helloMethod = MemberServiceImpl.class.getMethod("hello", String.class); + } + + @Test + void withinMatch() { + pointcut.setExpression("within(com.example.aop.member.MemberServiceImpl)"); + + assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isTrue(); + } + + @Test + void withinStar() { + pointcut.setExpression("within(com.example.aop.member.*Service*)"); + + assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isTrue(); + } + + @Test + void withinSubPackage() { + pointcut.setExpression("within(com.example.aop..*)"); + + assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isTrue(); + } + + @Test + @DisplayName("타겟의 타입에만 직접 적용, 인터페이스 선정 불가능") + void withinSuperTypeFalse() { + pointcut.setExpression("within(com.example.aop.member.MemberService)"); + + assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isFalse(); + } + + @Test + @DisplayName("execution 은 타입 기반, 인터페이스를 선정 가능") + void withinSuperTypeTrue() { + pointcut.setExpression("execution(* com.example.aop.member.MemberService.*(..))"); + + assertThat(pointcut.matches(helloMethod, MemberServiceImpl.class)).isTrue(); + } +}