use of cn.taketoday.aop.Advisor in project today-infrastructure by TAKETODAY.
the class ProxyFactoryTests method testRemoveAdvisorByReference.
@Test
public void testRemoveAdvisorByReference() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(cba);
pf.addAdvice(nop);
pf.addAdvisor(advisor);
ITestBean proxied = (ITestBean) pf.getProxy();
proxied.setAge(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(1);
assertThat(pf.removeAdvisor(advisor)).isTrue();
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(pf.removeAdvisor(new DefaultPointcutAdvisor(null))).isFalse();
}
use of cn.taketoday.aop.Advisor in project today-infrastructure by TAKETODAY.
the class ProxyFactoryTests method testIndexOfMethods.
@Test
public void testIndexOfMethods() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
Advised advised = (Advised) pf.getProxy();
// Can use advised and ProxyFactory interchangeably
advised.addAdvice(nop);
pf.addAdvisor(advisor);
assertThat(pf.indexOf(new NopInterceptor())).isEqualTo(-1);
assertThat(pf.indexOf(nop)).isEqualTo(0);
assertThat(pf.indexOf(advisor)).isEqualTo(1);
assertThat(advised.indexOf(new DefaultPointcutAdvisor(null))).isEqualTo(-1);
}
use of cn.taketoday.aop.Advisor in project today-infrastructure by TAKETODAY.
the class ProxyFactoryTests method testRemoveAdvisorByIndex.
@Test
public void testRemoveAdvisorByIndex() {
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
NopInterceptor nop = new NopInterceptor();
CountingBeforeAdvice cba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(cba);
pf.addAdvice(nop);
pf.addAdvisor(advisor);
NopInterceptor nop2 = new NopInterceptor();
pf.addAdvice(nop2);
ITestBean proxied = (ITestBean) pf.getProxy();
proxied.setAge(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(1);
assertThat(nop2.getCount()).isEqualTo(1);
// Removes counting before advisor
pf.removeAdvisor(1);
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(nop2.getCount()).isEqualTo(2);
// Removes Nop1
pf.removeAdvisor(0);
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(nop.getCount()).isEqualTo(2);
assertThat(nop2.getCount()).isEqualTo(3);
// Check out of bounds
try {
pf.removeAdvisor(-1);
} catch (AopConfigException ex) {
// Ok
}
try {
pf.removeAdvisor(2);
} catch (AopConfigException ex) {
// Ok
}
assertThat(proxied.getAge()).isEqualTo(5);
assertThat(nop2.getCount()).isEqualTo(4);
}
use of cn.taketoday.aop.Advisor in project today-infrastructure by TAKETODAY.
the class SimpleBeforeAdviceInterceptor method getAdviceImpl.
private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) {
Advised advised = (Advised) tb;
Advisor advisor = advised.getAdvisors()[0];
return (SimpleBeforeAdviceImpl) advisor.getAdvice();
}
use of cn.taketoday.aop.Advisor 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();
}
Aggregations