Search in sources :

Example 1 with MethodBeforeAdvice

use of org.springframework.aop.MethodBeforeAdvice in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method testCanGetSourceLocationFromJoinPoint.

@Test
public void testCanGetSourceLocationFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
        SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()).as("Same source location must be returned on subsequent requests").isEqualTo(sloc);
        assertThat(sloc.getWithinType()).isEqualTo(TestBean.class);
        assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getLine);
        assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(sloc::getFileName);
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) SourceLocation(org.aspectj.lang.reflect.SourceLocation) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 2 with MethodBeforeAdvice

use of org.springframework.aop.MethodBeforeAdvice in project spring-framework by spring-projects.

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, args, target) -> {
        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 : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) Test(org.junit.jupiter.api.Test)

Example 3 with MethodBeforeAdvice

use of org.springframework.aop.MethodBeforeAdvice in project spring-framework by spring-projects.

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, args, target) -> {
        JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
        assertThat(jp.toString().contains(method.getName())).as("Method named in toString").isTrue();
        // Ensure that these don't cause problems
        jp.toShortString();
        jp.toLongString();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getTarget()).isSameAs(target);
        assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget())).isFalse();
        ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
        assertThat(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis())).isTrue();
        assertThat(thisProxy).isNotSameAs(target);
        // Check getting again doesn't cause a problem
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getThis()).isSameAs(thisProxy);
        // Be sure to increment depth to avoid infinite recursion
        if (depth.getAndIncrement() == 0) {
            // Check that toString doesn't cause a problem
            thisProxy.toString();
            // Change age, so this will be returned by invocation
            thisProxy.setAge(newAge);
            assertThat(thisProxy.getAge()).isEqualTo(newAge);
        }
        assertThat(thisProxy).isSameAs(AopContext.currentProxy());
        assertThat(raw).isSameAs(target);
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()).isSameAs(method.getName());
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()).isEqualTo(method.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.getDeclaringClass());
        assertThat(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes())).isTrue();
        assertThat(msig.getReturnType()).isEqualTo(method.getReturnType());
        assertThat(Arrays.equals(method.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 : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.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 4 with MethodBeforeAdvice

use of org.springframework.aop.MethodBeforeAdvice in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method toShortAndLongStringFormedCorrectly.

@Test
public void toShortAndLongStringFormedCorrectly() throws Exception {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
        // makeEncSJP, although meant for computing the enclosing join point,
        // it serves our purpose here
        StaticPart aspectJVersionJp = Factory.makeEncSJP(method);
        JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
        assertThat(jp.getSignature().toLongString()).isEqualTo(aspectJVersionJp.getSignature().toLongString());
        assertThat(jp.getSignature().toShortString()).isEqualTo(aspectJVersionJp.getSignature().toShortString());
        assertThat(jp.getSignature().toString()).isEqualTo(aspectJVersionJp.getSignature().toString());
        assertThat(jp.toLongString()).isEqualTo(aspectJVersionJp.toLongString());
        assertThat(jp.toShortString()).isEqualTo(aspectJVersionJp.toShortString());
        assertThat(jp.toString()).isEqualTo(aspectJVersionJp.toString());
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    itb.getAge();
    itb.setName("foo");
    itb.getDoctor();
    itb.getStringArray();
    itb.getSpouse();
    itb.setSpouse(new TestBean());
    try {
        itb.unreliableFileOperation();
    } catch (IOException ex) {
    // we don't really care...
    }
}
Also used : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IOException(java.io.IOException) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 5 with MethodBeforeAdvice

use of org.springframework.aop.MethodBeforeAdvice in project spring-framework by spring-projects.

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(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixinAdvisor(test.mixin.LockMixinAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Advisor(org.springframework.aop.Advisor) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) Test(org.junit.jupiter.api.Test)

Aggregations

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