use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testThrowsAdvisorIsInvoked.
@Test
public void testThrowsAdvisorIsInvoked() throws Throwable {
// Reacts to ServletException and RemoteException
MyThrowsHandler th = new MyThrowsHandler();
@SuppressWarnings("serial") Advisor matchesEchoInvocations = new StaticMethodMatcherPointcutAdvisor(th) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().startsWith("echo");
}
};
Echo target = new Echo();
target.setA(16);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesEchoInvocations);
assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesEchoInvocations);
IEcho proxied = (IEcho) createProxy(pf);
assertThat(th.getCalls()).isEqualTo(0);
assertThat(proxied.getA()).isEqualTo(target.getA());
assertThat(th.getCalls()).isEqualTo(0);
Exception ex = new Exception();
// Will be advised but doesn't match
assertThatExceptionOfType(Exception.class).isThrownBy(() -> proxied.echoException(1, ex)).matches(ex::equals);
FileNotFoundException fex = new FileNotFoundException();
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> proxied.echoException(1, fex)).matches(fex::equals);
assertThat(th.getCalls("ioException")).isEqualTo(1);
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testCannotAddInterceptorWhenFrozen.
@Test
public void testCannotAddInterceptorWhenFrozen() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
assertThat(pc.isFrozen()).isFalse();
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add interceptor when frozen").isThrownBy(() -> pc.addAdvice(0, new NopInterceptor())).withMessageContaining("frozen");
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(((Advised) proxied).getAdvisors().length).isEqualTo(1);
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testDynamicMethodPointcutThatAlwaysAppliesStatically.
@Test
public void testDynamicMethodPointcutThatAlwaysAppliesStatically() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
TestDynamicPointcutAdvice dp = new TestDynamicPointcutAdvice(new NopInterceptor(), "getAge");
pc.addAdvisor(dp);
pc.setTarget(tb);
ITestBean it = (ITestBean) createProxy(pc);
assertThat(dp.count).isEqualTo(0);
it.getAge();
assertThat(dp.count).isEqualTo(1);
it.setAge(11);
assertThat(it.getAge()).isEqualTo(11);
assertThat(dp.count).isEqualTo(2);
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testAdviceImplementsIntroductionInfo.
@Test
public void testAdviceImplementsIntroductionInfo() throws Throwable {
TestBean tb = new TestBean();
String name = "tony";
tb.setName(name);
ProxyFactory pc = new ProxyFactory(tb);
NopInterceptor di = new NopInterceptor();
pc.addAdvice(di);
final long ts = 37;
pc.addAdvice(new DelegatingIntroductionInterceptor(new TimeStamped() {
@Override
public long getTimeStamp() {
return ts;
}
}));
ITestBean proxied = (ITestBean) createProxy(pc);
assertThat(proxied.getName()).isEqualTo(name);
TimeStamped intro = (TimeStamped) proxied;
assertThat(intro.getTimeStamp()).isEqualTo(ts);
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractMetadataAssemblerTests method testWithCglibProxy.
@Test
public void testWithCglibProxy() throws Exception {
IJmxTestBean tb = createJmxTestBean();
ProxyFactory pf = new ProxyFactory();
pf.setTarget(tb);
pf.addAdvice(new NopInterceptor());
Object proxy = pf.getProxy();
MetadataMBeanInfoAssembler assembler = (MetadataMBeanInfoAssembler) getAssembler();
MBeanExporter exporter = new MBeanExporter();
exporter.setBeanFactory(getContext());
exporter.setAssembler(assembler);
String objectName = "spring:bean=test,proxy=true";
Map<String, Object> beans = new HashMap<>();
beans.put(objectName, proxy);
exporter.setBeans(beans);
start(exporter);
MBeanInfo inf = getServer().getMBeanInfo(ObjectNameManager.getInstance(objectName));
assertThat(inf.getOperations().length).as("Incorrect number of operations").isEqualTo(getExpectedOperationCount());
assertThat(inf.getAttributes().length).as("Incorrect number of attributes").isEqualTo(getExpectedAttributeCount());
assertThat(assembler.includeBean(proxy.getClass(), "some bean name")).as("Not included in autodetection").isTrue();
}
Aggregations