Search in sources :

Example 6 with ProxyFactory

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

the class EventPublicationInterceptorTests method testExpectedBehavior.

@Test
public void testExpectedBehavior() {
    TestBean target = new TestBean();
    final TestApplicationListener listener = new TestApplicationListener();
    class TestContext extends StaticApplicationContext {

        @Override
        protected void onRefresh() throws BeansException {
            addApplicationListener(listener);
        }
    }
    StaticApplicationContext ctx = new TestContext();
    PropertyValues pvs = new PropertyValues();
    pvs.add("applicationEventClass", TestEvent.class.getName());
    // should automatically receive applicationEventPublisher reference
    ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs);
    ctx.registerSingleton("otherListener", FactoryBeanTestListener.class);
    ctx.refresh();
    EventPublicationInterceptor interceptor = (EventPublicationInterceptor) ctx.getBean("publisher");
    ProxyFactory factory = new ProxyFactory(target);
    factory.addAdvice(0, interceptor);
    ITestBean testBean = (ITestBean) factory.getProxy();
    // invoke any method on the advised proxy to see if the interceptor has been invoked
    testBean.getAge();
    // two events: ContextRefreshedEvent and TestEvent
    assertThat(listener.getEventCount() == 2).as("Interceptor must have published 2 events").isTrue();
    TestApplicationListener otherListener = (TestApplicationListener) ctx.getBean("&otherListener");
    assertThat(otherListener.getEventCount() == 2).as("Interceptor must have published 2 events").isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) PropertyValues(cn.taketoday.beans.PropertyValues) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) TestEvent(cn.taketoday.context.event.test.TestEvent) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 7 with ProxyFactory

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

the class FormattingConversionServiceTests method proxiedConverter.

@Test
public void proxiedConverter() {
    Converter<?, ?> converter = new IntegerConverter();
    formattingService.addConverter((Converter<?, ?>) new ProxyFactory(converter).getProxy());
    assertThat(formattingService.convert("1", Integer.class)).isEqualTo(Integer.valueOf(1));
}
Also used : ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 8 with ProxyFactory

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

the class FormattingConversionServiceTests method proxiedFormatter.

@Test
public void proxiedFormatter() {
    Formatter<?> formatter = new NumberStyleFormatter();
    formattingService.addFormatter((Formatter<?>) new ProxyFactory(formatter).getProxy());
    assertThat(formattingService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))).isNull();
}
Also used : ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) NumberStyleFormatter(cn.taketoday.format.number.NumberStyleFormatter) Test(org.junit.jupiter.api.Test)

Example 9 with ProxyFactory

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

the class AnnotationTransactionAttributeSourceTests method serializable.

@Test
public void serializable() throws Exception {
    TestBean1 tb = new TestBean1();
    CallCountingTransactionManager ptm = new CallCountingTransactionManager();
    AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
    TransactionInterceptor ti = new TransactionInterceptor((PlatformTransactionManager) ptm, tas);
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean1.class);
    proxyFactory.addAdvice(ti);
    proxyFactory.setTarget(tb);
    ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
    proxy.getAge();
    assertThat(ptm.commits).isEqualTo(1);
    ITestBean1 serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    serializedProxy.getAge();
    Advised advised = (Advised) serializedProxy;
    TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
    CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager();
    assertThat(serializedPtm.commits).isEqualTo(2);
}
Also used : TransactionInterceptor(cn.taketoday.transaction.interceptor.TransactionInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) Test(org.junit.jupiter.api.Test)

Example 10 with ProxyFactory

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

the class ReactiveTransactionInterceptorTests method advised.

/**
 * Template method to create an advised object given the
 * target object and transaction setup.
 * Creates a TransactionInterceptor and applies it.
 */
@Override
protected Object advised(Object target, ReactiveTransactionManager ptm, TransactionAttributeSource tas) {
    TransactionInterceptor ti = new TransactionInterceptor();
    ti.setTransactionManager(ptm);
    assertThat(ti.getTransactionManager()).isEqualTo(ptm);
    ti.setTransactionAttributeSource(tas);
    assertThat(ti.getTransactionAttributeSource()).isEqualTo(tas);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(0, ti);
    return pf.getProxy();
}
Also used : ProxyFactory(cn.taketoday.aop.framework.ProxyFactory)

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