Search in sources :

Example 1 with SingletonTargetSource

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

the class AbstractAopProxyTests method testExistingProxyChangesTarget.

@Test
public void testExistingProxyChangesTarget() throws Throwable {
    TestBean tb1 = new TestBean();
    tb1.setAge(33);
    TestBean tb2 = new TestBean();
    tb2.setAge(26);
    tb2.setName("Juergen");
    TestBean tb3 = new TestBean();
    tb3.setAge(37);
    ProxyFactory pc = new ProxyFactory(tb1);
    NopInterceptor nop = new NopInterceptor();
    pc.addAdvice(nop);
    ITestBean proxy = (ITestBean) createProxy(pc);
    assertThat(0).isEqualTo(nop.getCount());
    assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
    assertThat(1).isEqualTo(nop.getCount());
    // Change to a new static target
    pc.setTarget(tb2);
    assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
    assertThat(2).isEqualTo(nop.getCount());
    // Change to a new dynamic target
    HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
    pc.setTargetSource(hts);
    assertThat(proxy.getAge()).isEqualTo(tb3.getAge());
    assertThat(3).isEqualTo(nop.getCount());
    hts.swap(tb1);
    assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
    tb1.setName("Colin");
    assertThat(proxy.getName()).isEqualTo(tb1.getName());
    assertThat(5).isEqualTo(nop.getCount());
    // Change back, relying on casting to Advised
    Advised advised = (Advised) proxy;
    assertThat(advised.getTargetSource()).isSameAs(hts);
    SingletonTargetSource sts = new SingletonTargetSource(tb2);
    advised.setTargetSource(sts);
    assertThat(proxy.getName()).isEqualTo(tb2.getName());
    assertThat(advised.getTargetSource()).isSameAs(sts);
    assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) Test(org.junit.jupiter.api.Test)

Example 2 with SingletonTargetSource

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

the class StatefulRetryOperationsInterceptorTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    interceptor = new StatefulRetryOperationsInterceptor();
    retryTemplate.registerListener(new RetryListener() {

        @Override
        public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
            StatefulRetryOperationsInterceptorTests.this.context = context;
        }
    });
    interceptor.setRetryOperations(retryTemplate);
    service = ProxyFactory.getProxy(Service.class, new SingletonTargetSource(new ServiceImpl()));
    transformer = ProxyFactory.getProxy(Transformer.class, new SingletonTargetSource(new TransformerImpl()));
    count = 0;
}
Also used : RetryContext(cn.taketoday.retry.RetryContext) RetryListener(cn.taketoday.retry.RetryListener) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with SingletonTargetSource

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

the class AbstractAopProxyTests method testExistingProxyChangesTarget.

@Test
public void testExistingProxyChangesTarget() throws Throwable {
    TestBean tb1 = new TestBean();
    tb1.setAge(33);
    TestBean tb2 = new TestBean();
    tb2.setAge(26);
    tb2.setName("Juergen");
    TestBean tb3 = new TestBean();
    tb3.setAge(37);
    ProxyFactory pc = new ProxyFactory(tb1);
    NopInterceptor nop = new NopInterceptor();
    pc.addAdvice(nop);
    ITestBean proxy = (ITestBean) createProxy(pc);
    assertThat(0).isEqualTo(nop.getCount());
    assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
    assertThat(1).isEqualTo(nop.getCount());
    // Change to a new static target
    pc.setTarget(tb2);
    assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
    assertThat(2).isEqualTo(nop.getCount());
    // Change to a new dynamic target
    HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
    pc.setTargetSource(hts);
    assertThat(proxy.getAge()).isEqualTo(tb3.getAge());
    assertThat(3).isEqualTo(nop.getCount());
    hts.swap(tb1);
    assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
    tb1.setName("Colin");
    assertThat(proxy.getName()).isEqualTo(tb1.getName());
    assertThat(5).isEqualTo(nop.getCount());
    // Change back, relying on casting to Advised
    Advised advised = (Advised) proxy;
    assertThat(advised.getTargetSource()).isSameAs(hts);
    SingletonTargetSource sts = new SingletonTargetSource(tb2);
    advised.setTargetSource(sts);
    assertThat(proxy.getName()).isEqualTo(tb2.getName());
    assertThat(advised.getTargetSource()).isSameAs(sts);
    assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) HotSwappableTargetSource(cn.taketoday.aop.target.HotSwappableTargetSource) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 4 with SingletonTargetSource

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

the class HibernateEntityManagerFactoryIntegrationTests method testCanUnwrapAopProxy.

@Test
public void testCanUnwrapAopProxy() {
    EntityManager em = entityManagerFactory.createEntityManager();
    EntityManager proxy = ProxyFactory.getProxy(EntityManager.class, new SingletonTargetSource(em));
    boolean condition = em instanceof org.hibernate.jpa.HibernateEntityManager;
    assertThat(condition).isTrue();
    boolean condition1 = proxy instanceof org.hibernate.jpa.HibernateEntityManager;
    assertThat(condition1).isFalse();
    assertThat(proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class) != null).isTrue();
    assertThat(proxy.unwrap(org.hibernate.jpa.HibernateEntityManager.class)).isSameAs(em);
    assertThat(proxy.getDelegate()).isSameAs(em.getDelegate());
}
Also used : SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) EntityManager(jakarta.persistence.EntityManager) Test(org.junit.jupiter.api.Test)

Example 5 with SingletonTargetSource

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

the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithNonGenericConverter.

// SPR-11225
@Test
public void resolveArgumentTypeVariableWithNonGenericConverter() throws Throwable {
    Method method = MyParameterizedController.class.getMethod("handleDto", Identifiable.class);
    HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method);
    MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
    String content = "{\"name\" : \"Jad\"}";
    this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    HttpMessageConverter<Object> target = new MappingJackson2HttpMessageConverter();
    HttpMessageConverter<?> proxy = ProxyFactory.getProxy(HttpMessageConverter.class, new SingletonTargetSource(target));
    converters.add(proxy);
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    SimpleBean result = (SimpleBean) processor.resolveArgument(request, new ResolvableMethodParameter(methodParam));
    assertThat(result).isNotNull();
    assertThat(result.getName()).isEqualTo("Jad");
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)10 Test (org.junit.jupiter.api.Test)6 RetryContext (cn.taketoday.retry.RetryContext)4 RetryListener (cn.taketoday.retry.RetryListener)4 HotSwappableTargetSource (cn.taketoday.aop.target.HotSwappableTargetSource)3 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)3 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)3 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)2 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)2 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)2 EntityManager (jakarta.persistence.EntityManager)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Before (org.junit.Before)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 NopInterceptor (cn.taketoday.aop.NopInterceptor)1 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)1 Advised (cn.taketoday.aop.framework.Advised)1 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)1 MethodParameter (cn.taketoday.core.MethodParameter)1 AllEncompassingFormHttpMessageConverter (cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter)1