Search in sources :

Example 26 with Advised

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

the class EnableAsyncTests method customAsyncAnnotationIsPropagated.

@Test
public void customAsyncAnnotationIsPropagated() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(CustomAsyncAnnotationConfig.class, CustomAsyncBean.class);
    ctx.refresh();
    Object bean = ctx.getBean(CustomAsyncBean.class);
    assertThat(AopUtils.isAopProxy(bean)).isTrue();
    boolean isAsyncAdvised = false;
    for (Advisor advisor : ((Advised) bean).getAdvisors()) {
        if (advisor instanceof AsyncAnnotationAdvisor) {
            isAsyncAdvised = true;
            break;
        }
    }
    assertThat(isAsyncAdvised).as("bean was not async advised as expected").isTrue();
    ctx.close();
}
Also used : AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) Advised(cn.taketoday.aop.framework.Advised) Advisor(cn.taketoday.aop.Advisor) Test(org.junit.jupiter.api.Test)

Example 27 with Advised

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

the class RetryOperationsInterceptorTests method testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation.

@Test
public void testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation() throws Exception {
    final String label = "FOO";
    final String classTagName = "class";
    final String methodTagName = "method";
    final String labelTagName = "label";
    final Map<String, String> monitoringTags = new HashMap<String, String>();
    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(new SimpleRetryPolicy(2));
    template.registerListener(new MethodInvocationRetryListenerSupport() {

        @Override
        protected <T, E extends Throwable> void doClose(RetryContext context, MethodInvocationRetryCallback<T, E> callback, Throwable throwable) {
            monitoringTags.put(labelTagName, callback.getLabel());
            Method method = callback.getInvocation().getMethod();
            monitoringTags.put(classTagName, method.getDeclaringClass().getSimpleName());
            monitoringTags.put(methodTagName, method.getName());
        }
    });
    this.interceptor.setLabel(label);
    this.interceptor.setRetryOperations(template);
    ((Advised) this.service).addAdvice(this.interceptor);
    this.service.service();
    assertEquals(2, count);
    assertEquals(3, monitoringTags.entrySet().size());
    assertThat(monitoringTags.get(labelTagName), equalTo(label));
    assertThat(monitoringTags.get(classTagName), equalTo(Service.class.getSimpleName()));
    assertThat(monitoringTags.get(methodTagName), equalTo("service"));
}
Also used : MethodInvocationRetryListenerSupport(cn.taketoday.retry.listener.MethodInvocationRetryListenerSupport) HashMap(java.util.HashMap) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Method(java.lang.reflect.Method) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.Test)

Example 28 with Advised

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

the class StatefulRetryOperationsInterceptorTests method testTransformerRecoveryAfterTooManyAttempts.

@Test
public void testTransformerRecoveryAfterTooManyAttempts() throws Exception {
    ((Advised) transformer).addAdvice(interceptor);
    interceptor.setRetryOperations(retryTemplate);
    retryTemplate.setRetryPolicy(new NeverRetryPolicy());
    try {
        transformer.transform("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
    interceptor.setRecoverer((MethodInvocationRecoverer<Collection<String>>) (data, cause) -> {
        count++;
        return Collections.singleton((String) data[0]);
    });
    Collection<String> result = transformer.transform("foo");
    assertEquals(2, count);
    assertEquals(1, result.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) Collection(java.util.Collection) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 29 with Advised

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

the class StatefulRetryOperationsInterceptorTests method testDefaultInterceptorAlwaysRetry.

@Test
public void testDefaultInterceptorAlwaysRetry() throws Exception {
    retryTemplate.setRetryPolicy(new AlwaysRetryPolicy());
    interceptor.setRetryOperations(retryTemplate);
    ((Advised) service).addAdvice(interceptor);
    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);
}
Also used : Advised(cn.taketoday.aop.framework.Advised) AlwaysRetryPolicy(cn.taketoday.retry.policy.AlwaysRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 30 with Advised

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

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