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());
}
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();
}
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);
}
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();
}
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());
}
Aggregations