Search in sources :

Example 1 with MethodBeforeAdvice

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

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) -> {
        StaticPart aspectJVersionJp = Factory.makeEncSJP(method.getMethod());
        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 : 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) 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 2 with MethodBeforeAdvice

use of cn.taketoday.aop.MethodBeforeAdvice in project today-infrastructure 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 3 with MethodBeforeAdvice

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

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) -> {
        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 : 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) SourceLocation(org.aspectj.lang.reflect.SourceLocation) 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) Test(org.junit.jupiter.api.Test)

Example 4 with MethodBeforeAdvice

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

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) -> {
        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 : 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) SourceLocation(org.aspectj.lang.reflect.SourceLocation) 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) Test(org.junit.jupiter.api.Test)

Example 5 with MethodBeforeAdvice

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

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) -> {
        StaticPart aspectJVersionJp = Factory.makeEncSJP(method.getMethod());
        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 : 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) 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)

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