Search in sources :

Example 16 with Advised

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

the class StatefulRetryOperationsInterceptorTests method testInterceptorChainWithRetry.

@Test
public void testInterceptorChainWithRetry() throws Exception {
    ((Advised) service).addAdvice(interceptor);
    final List<String> list = new ArrayList<>();
    ((Advised) service).addAdvice((MethodInterceptor) invocation -> {
        list.add("chain");
        return invocation.proceed();
    });
    interceptor.setRetryOperations(retryTemplate);
    retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2));
    try {
        service.service("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
    service.service("foo");
    assertEquals(2, count);
    assertEquals(2, list.size());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) AlwaysRetryPolicy(cn.taketoday.retry.policy.AlwaysRetryPolicy) RetryListener(cn.taketoday.retry.RetryListener) Advised(cn.taketoday.aop.framework.Advised) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ArgumentCaptor(org.mockito.ArgumentCaptor) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) RetryContext(cn.taketoday.retry.RetryContext) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Assert.fail(org.junit.Assert.fail) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryCallback(cn.taketoday.retry.RetryCallback) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Collection(java.util.Collection) RetryOperations(cn.taketoday.retry.RetryOperations) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.when(org.mockito.Mockito.when) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Advised(cn.taketoday.aop.framework.Advised) ArrayList(java.util.ArrayList) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 17 with Advised

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

the class CircuitBreakerTests method vanilla.

@Test
public void vanilla() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
    Service service = context.getBean(Service.class);
    assertTrue(AopUtils.isAopProxy(service));
    try {
        service.service();
        fail("Expected exception");
    } catch (Exception e) {
    }
    assertFalse((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
    try {
        service.service();
        fail("Expected exception");
    } catch (Exception e) {
    }
    assertFalse((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
    try {
        service.service();
        fail("Expected exception");
    } catch (Exception e) {
    }
    assertTrue((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN));
    assertEquals(3, service.getCount());
    try {
        service.service();
        fail("Expected exception");
    } catch (Exception e) {
    }
    // Not called again once circuit is open
    assertEquals(3, service.getCount());
    service.expressionService();
    assertEquals(4, service.getCount());
    Advised advised = (Advised) service;
    Advisor advisor = advised.getAdvisors()[0];
    Map<?, ?> delegates = (Map<?, ?>) new DirectFieldAccessor(advisor).getPropertyValue("advice.delegates");
    assertTrue(delegates.size() == 1);
    Map<?, ?> methodMap = (Map<?, ?>) delegates.values().iterator().next();
    MethodInterceptor interceptor = (MethodInterceptor) methodMap.get(Service.class.getDeclaredMethod("expressionService"));
    DirectFieldAccessor accessor = new DirectFieldAccessor(interceptor);
    assertEquals(8, accessor.getPropertyValue("retryOperations.retryPolicy.delegate.maxAttempts"));
    assertEquals(19000L, accessor.getPropertyValue("retryOperations.retryPolicy.openTimeout"));
    assertEquals(20000L, accessor.getPropertyValue("retryOperations.retryPolicy.resetTimeout"));
    assertEquals("#root instanceof RuntimeExpression", accessor.getPropertyValue("retryOperations.retryPolicy.delegate.expression.expression"));
    context.close();
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Advised(cn.taketoday.aop.framework.Advised) DirectFieldAccessor(cn.taketoday.beans.DirectFieldAccessor) Advisor(cn.taketoday.aop.Advisor) Map(java.util.Map) Test(org.junit.Test)

Example 18 with Advised

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

the class AbstractAopProxyTests method testCanCastProxyToProxyConfig.

@Test
public void testCanCastProxyToProxyConfig() throws Throwable {
    TestBean tb = new TestBean();
    ProxyFactory pc = new ProxyFactory(tb);
    NopInterceptor di = new NopInterceptor();
    pc.addAdvice(0, di);
    ITestBean t = (ITestBean) createProxy(pc);
    assertThat(di.getCount()).isEqualTo(0);
    t.setAge(23);
    assertThat(t.getAge()).isEqualTo(23);
    assertThat(di.getCount()).isEqualTo(2);
    Advised advised = (Advised) t;
    assertThat(advised.getAdvisors().length).as("Have 1 advisor").isEqualTo(1);
    assertThat(advised.getAdvisors()[0].getAdvice()).isEqualTo(di);
    NopInterceptor di2 = new NopInterceptor();
    advised.addAdvice(1, di2);
    t.getName();
    assertThat(di.getCount()).isEqualTo(3);
    assertThat(di2.getCount()).isEqualTo(1);
    // will remove di
    advised.removeAdvisor(0);
    t.getAge();
    // Unchanged
    assertThat(di.getCount()).isEqualTo(3);
    assertThat(di2.getCount()).isEqualTo(2);
    CountingBeforeAdvice cba = new CountingBeforeAdvice();
    assertThat(cba.getCalls()).isEqualTo(0);
    advised.addAdvice(cba);
    t.setAge(16);
    assertThat(t.getAge()).isEqualTo(16);
    assertThat(cba.getCalls()).isEqualTo(2);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) 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) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.jupiter.api.Test)

Example 19 with Advised

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

the class AbstractAopProxyTests method testCanPreventCastToAdvisedUsingOpaque.

@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
    TestBean target = new TestBean();
    ProxyFactory pc = new ProxyFactory(target);
    pc.setInterfaces(ITestBean.class);
    pc.addAdvice(new NopInterceptor());
    CountingBeforeAdvice mba = new CountingBeforeAdvice();
    Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
    pc.addAdvisor(advisor);
    assertThat(pc.isOpaque()).as("Opaque defaults to false").isFalse();
    pc.setOpaque(true);
    assertThat(pc.isOpaque()).as("Opaque now true for this config").isTrue();
    ITestBean proxied = (ITestBean) createProxy(pc);
    proxied.setAge(10);
    assertThat(proxied.getAge()).isEqualTo(10);
    assertThat(mba.getCalls()).isEqualTo(1);
    boolean condition = proxied instanceof Advised;
    assertThat(condition).as("Cannot be cast to Advised").isFalse();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) 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) Advised(cn.taketoday.aop.framework.Advised) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Advisor(cn.taketoday.aop.Advisor) LockMixinAdvisor(cn.taketoday.aop.mixin.LockMixinAdvisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) NameMatchMethodPointcut(cn.taketoday.aop.support.NameMatchMethodPointcut) Test(org.junit.jupiter.api.Test)

Example 20 with Advised

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

Aggregations

Advised (cn.taketoday.aop.framework.Advised)85 Test (org.junit.jupiter.api.Test)58 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)40 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)23 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)18 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)18 Advisor (cn.taketoday.aop.Advisor)13 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)12 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)12 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)12 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)10 Test (org.junit.Test)10 NopInterceptor (cn.taketoday.aop.NopInterceptor)9 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)9 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)8 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)7 MethodInvocation (org.aopalliance.intercept.MethodInvocation)7 RetryContext (cn.taketoday.retry.RetryContext)6 AlwaysRetryPolicy (cn.taketoday.retry.policy.AlwaysRetryPolicy)6 ArrayList (java.util.ArrayList)6