Search in sources :

Example 6 with MethodBeforeAdvice

use of cn.taketoday.aop.MethodBeforeAdvice in project today-framework by TAKETODAY.

the class AbstractAopProxyTests method testProxyConfigString.

/**
 * Check that the string is informative.
 */
@Test
public void testProxyConfigString() {
    TestBean target = new TestBean();
    ProxyFactory pc = new ProxyFactory(target);
    pc.setInterfaces(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    MethodBeforeAdvice mba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
    pc.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) createProxy(pc);
    String proxyConfigString = ((Advised) proxied).toProxyConfigString();
    assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
    assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) Advisor(cn.taketoday.aop.Advisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) NameMatchMethodPointcut(cn.taketoday.aop.support.NameMatchMethodPointcut) Test(org.junit.jupiter.api.Test)

Example 7 with MethodBeforeAdvice

use of cn.taketoday.aop.MethodBeforeAdvice in project today-framework by TAKETODAY.

the class MethodInvocationProceedingJoinPointTests method testCanGetStaticPartFromJoinPoint.

@Test
public void testCanGetStaticPartFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice((MethodBeforeAdvice) (method) -> {
        StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getStaticPart()).as("Same static part must be returned on subsequent requests").isEqualTo(staticPart);
        assertThat(staticPart.getKind()).isEqualTo(ProceedingJoinPoint.METHOD_EXECUTION);
        assertThat(staticPart.getSignature()).isSameAs(AbstractAspectJAdvice.currentJoinPoint().getSignature());
        assertThat(staticPart.getSourceLocation()).isEqualTo(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) SourceLocation(org.aspectj.lang.reflect.SourceLocation) AopContext(cn.taketoday.aop.framework.AopContext) ExposeInvocationInterceptor(cn.taketoday.aop.interceptor.ExposeInvocationInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) AopUtils(cn.taketoday.aop.support.AopUtils) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) Test(org.junit.jupiter.api.Test)

Example 8 with MethodBeforeAdvice

use of cn.taketoday.aop.MethodBeforeAdvice in project today-framework by TAKETODAY.

the class MethodInvocationProceedingJoinPointTests method testCanGetMethodSignatureFromJoinPoint.

@Test
public void testCanGetMethodSignatureFromJoinPoint() {
    final Object raw = new TestBean();
    // Will be set by advice during a method call
    final int newAge = 23;
    ProxyFactory pf = new ProxyFactory(raw);
    pf.setExposeProxy(true);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    AtomicInteger depth = new AtomicInteger();
    pf.addAdvice((MethodBeforeAdvice) (method) -> {
        JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
        assertThat(jp.toString().contains(method.getMethod().getName())).as("Method named in toString").isTrue();
        jp.toShortString();
        jp.toLongString();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(method.getThis());
        assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
        ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
        assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
        assertThat(thisProxy).isNotSameAs(method.getThis());
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
        if (depth.getAndIncrement() == 0) {
            thisProxy.toString();
            thisProxy.setAge(newAge);
            assertThat(thisProxy.getAge()).isEqualTo(newAge);
        }
        assertThat(thisProxy).isSameAs(AopContext.currentProxy());
        assertThat(raw).isSameAs(method.getThis());
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getMethod().getName());
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getMethod().getModifiers());
        MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig);
        assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint());
        assertThat(msig.getDeclaringType()).isEqualTo(method.getMethod().getDeclaringClass());
        assertThat(Arrays.equals(method.getMethod().getParameterTypes(), msig.getParameterTypes())).isTrue();
        assertThat(msig.getReturnType()).isEqualTo(method.getMethod().getReturnType());
        assertThat(Arrays.equals(method.getMethod().getExceptionTypes(), msig.getExceptionTypes())).isTrue();
        msig.toLongString();
        msig.toShortString();
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    assertThat(itb.getAge()).as("Advice reentrantly set age").isEqualTo(newAge);
}
Also used : Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) SourceLocation(org.aspectj.lang.reflect.SourceLocation) AopContext(cn.taketoday.aop.framework.AopContext) ExposeInvocationInterceptor(cn.taketoday.aop.interceptor.ExposeInvocationInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) AopUtils(cn.taketoday.aop.support.AopUtils) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 9 with MethodBeforeAdvice

use of cn.taketoday.aop.MethodBeforeAdvice in project today-infrastructure by TAKETODAY.

the class AbstractAopProxyTests method testProxyConfigString.

/**
 * Check that the string is informative.
 */
@Test
public void testProxyConfigString() {
    TestBean target = new TestBean();
    ProxyFactory pc = new ProxyFactory(target);
    pc.setInterfaces(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    MethodBeforeAdvice mba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
    pc.addAdvisor(advisor);
    ITestBean proxied = (ITestBean) createProxy(pc);
    String proxyConfigString = ((Advised) proxied).toProxyConfigString();
    assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
    assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) Advisor(cn.taketoday.aop.Advisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) NameMatchMethodPointcut(cn.taketoday.aop.support.NameMatchMethodPointcut) Test(org.junit.jupiter.api.Test)

Example 10 with MethodBeforeAdvice

use of cn.taketoday.aop.MethodBeforeAdvice in project today-infrastructure by TAKETODAY.

the class MethodInvocationProceedingJoinPointTests method testCanGetMethodSignatureFromJoinPoint.

@Test
public void testCanGetMethodSignatureFromJoinPoint() {
    final Object raw = new TestBean();
    // Will be set by advice during a method call
    final int newAge = 23;
    ProxyFactory pf = new ProxyFactory(raw);
    pf.setExposeProxy(true);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    AtomicInteger depth = new AtomicInteger();
    pf.addAdvice((MethodBeforeAdvice) (method) -> {
        JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
        assertThat(jp.toString().contains(method.getMethod().getName())).as("Method named in toString").isTrue();
        jp.toShortString();
        jp.toLongString();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(method.getThis());
        assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
        ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
        assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
        assertThat(thisProxy).isNotSameAs(method.getThis());
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
        if (depth.getAndIncrement() == 0) {
            thisProxy.toString();
            thisProxy.setAge(newAge);
            assertThat(thisProxy.getAge()).isEqualTo(newAge);
        }
        assertThat(thisProxy).isSameAs(AopContext.currentProxy());
        assertThat(raw).isSameAs(method.getThis());
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getMethod().getName());
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.getMethod().getModifiers());
        MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature()).as("Return same MethodSignature repeatedly").isSameAs(msig);
        assertThat(AbstractAspectJAdvice.currentJoinPoint()).as("Return same JoinPoint repeatedly").isSameAs(AbstractAspectJAdvice.currentJoinPoint());
        assertThat(msig.getDeclaringType()).isEqualTo(method.getMethod().getDeclaringClass());
        assertThat(Arrays.equals(method.getMethod().getParameterTypes(), msig.getParameterTypes())).isTrue();
        assertThat(msig.getReturnType()).isEqualTo(method.getMethod().getReturnType());
        assertThat(Arrays.equals(method.getMethod().getExceptionTypes(), msig.getExceptionTypes())).isTrue();
        msig.toLongString();
        msig.toShortString();
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    assertThat(itb.getAge()).as("Advice reentrantly set age").isEqualTo(newAge);
}
Also used : Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) SourceLocation(org.aspectj.lang.reflect.SourceLocation) AopContext(cn.taketoday.aop.framework.AopContext) ExposeInvocationInterceptor(cn.taketoday.aop.interceptor.ExposeInvocationInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) AopUtils(cn.taketoday.aop.support.AopUtils) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Aggregations

MethodBeforeAdvice (cn.taketoday.aop.MethodBeforeAdvice)11 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)11 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)11 Test (org.junit.jupiter.api.Test)11 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)9 AopContext (cn.taketoday.aop.framework.AopContext)8 ExposeInvocationInterceptor (cn.taketoday.aop.interceptor.ExposeInvocationInterceptor)8 AopUtils (cn.taketoday.aop.support.AopUtils)8 IOException (java.io.IOException)8 Arrays (java.util.Arrays)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 JoinPoint (org.aspectj.lang.JoinPoint)8 StaticPart (org.aspectj.lang.JoinPoint.StaticPart)8 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)8 MethodSignature (org.aspectj.lang.reflect.MethodSignature)8 SourceLocation (org.aspectj.lang.reflect.SourceLocation)8 Factory (org.aspectj.runtime.reflect.Factory)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)8 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)8