Search in sources :

Example 16 with ProxyFactory

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

the class AnnotationTransactionInterceptorTests method withInterfaceOnTargetCglibProxy.

@Test
public void withInterfaceOnTargetCglibProxy() {
    ProxyFactory targetFactory = new ProxyFactory();
    targetFactory.setTarget(new TestWithInterfaceImpl());
    targetFactory.setProxyTargetClass(true);
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTarget(targetFactory.getProxy());
    proxyFactory.addInterface(TestWithInterface.class);
    proxyFactory.addAdvice(this.ti);
    TestWithInterface proxy = (TestWithInterface) proxyFactory.getProxy();
    proxy.doSomething();
    assertGetTransactionAndCommitCount(1);
    proxy.doSomethingElse();
    assertGetTransactionAndCommitCount(2);
    proxy.doSomethingElse();
    assertGetTransactionAndCommitCount(3);
    proxy.doSomething();
    assertGetTransactionAndCommitCount(4);
    proxy.doSomethingDefault();
    assertGetTransactionAndCommitCount(5);
}
Also used : ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 17 with ProxyFactory

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

the class CallCountingInterceptor method getAdvisedProxy.

private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) {
    TestBean target = new TestBean();
    Pointcut pointcut = getPointcut(pointcutExpression);
    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
    advisor.setAdvice(interceptor);
    advisor.setPointcut(pointcut);
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    return (TestBean) pf.getProxy();
}
Also used : Pointcut(cn.taketoday.aop.Pointcut) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor)

Example 18 with ProxyFactory

use of cn.taketoday.aop.framework.ProxyFactory 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 19 with ProxyFactory

use of cn.taketoday.aop.framework.ProxyFactory 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 20 with ProxyFactory

use of cn.taketoday.aop.framework.ProxyFactory 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)

Aggregations

ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)206 Test (org.junit.jupiter.api.Test)171 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)91 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)86 NopInterceptor (cn.taketoday.aop.NopInterceptor)39 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)29 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)21 Advised (cn.taketoday.aop.framework.Advised)19 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)17 INestedTestBean (cn.taketoday.beans.testfixture.beans.INestedTestBean)16 NestedTestBean (cn.taketoday.beans.testfixture.beans.NestedTestBean)16 TimeStamped (cn.taketoday.core.testfixture.TimeStamped)16 IOException (java.io.IOException)16 TransactionInterceptor (cn.taketoday.transaction.interceptor.TransactionInterceptor)14 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)13 Method (java.lang.reflect.Method)12 Advisor (cn.taketoday.aop.Advisor)10 MethodBeforeAdvice (cn.taketoday.aop.MethodBeforeAdvice)10 AopUtils (cn.taketoday.aop.support.AopUtils)10 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)10