Search in sources :

Example 26 with ProxyFactory

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

the class ExposeBeanNameAdvisorsTests method testWithIntroduction.

@Test
public void testWithIntroduction() {
    String beanName = "foo";
    TestBean target = new RequiresBeanNameBoundTestBean(beanName);
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorIntroducingNamedBean(beanName));
    ITestBean proxy = (ITestBean) pf.getProxy();
    boolean condition = proxy instanceof NamedBean;
    assertThat(condition).as("Introduction was made").isTrue();
    // Requires binding
    proxy.getAge();
    NamedBean nb = (NamedBean) proxy;
    assertThat(nb.getBeanName()).as("Name returned correctly").isEqualTo(beanName);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) NamedBean(cn.taketoday.beans.factory.NamedBean) 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 27 with ProxyFactory

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

the class ConcurrencyThrottleInterceptorTests method testSerializable.

@Test
public void testSerializable() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    proxy.getAge();
    ITestBean serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    Advised advised = (Advised) serializedProxy;
    ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
    assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit());
    serializedProxy.getAge();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ConcurrencyThrottleInterceptor(cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor) Test(org.junit.jupiter.api.Test)

Example 28 with ProxyFactory

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

the class ConcurrencyThrottleInterceptorTests method testMultipleThreads.

private void testMultipleThreads(int concurrencyLimit) {
    TestBean tb = new TestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    cti.setConcurrencyLimit(concurrencyLimit);
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    Thread[] threads = new Thread[NR_OF_THREADS];
    for (int i = 0; i < NR_OF_THREADS; i++) {
        threads[i] = new ConcurrencyThread(proxy, null);
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS / 10; i++) {
        try {
            Thread.sleep(5);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        threads[i] = new ConcurrencyThread(proxy, i % 2 == 0 ? new OutOfMemoryError() : new IllegalStateException());
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) ConcurrencyThrottleInterceptor(cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor)

Example 29 with ProxyFactory

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

the class ClassUtilsTests method getShortNameForCglibClass.

@Test
void getShortNameForCglibClass() {
    TestBean tb = new TestBean();
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(tb);
    pf.setProxyTargetClass(true);
    TestBean proxy = (TestBean) pf.getProxy();
    String className = ClassUtils.getShortName(proxy.getClass());
    assertThat(className).as("Class name did not match").isEqualTo("TestBean");
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Test(org.junit.jupiter.api.Test)

Example 30 with ProxyFactory

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

the class NameMatchMethodPointcutTests method setup.

/**
 * Create an empty pointcut, populating instance variables.
 */
@BeforeEach
public void setup() {
    ProxyFactory pf = new ProxyFactory(new SerializablePerson());
    nop = new SerializableNopInterceptor();
    pc = new NameMatchMethodPointcut();
    pf.addAdvisor(new DefaultPointcutAdvisor(pc, nop));
    proxied = (Person) pf.getProxy();
}
Also used : SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) BeforeEach(org.junit.jupiter.api.BeforeEach)

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