use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class MBeanExporterTests method testExportJdkProxy.
@Test
void testExportJdkProxy() throws Exception {
JmxTestBean bean = new JmxTestBean();
bean.setName("Rob Harrop");
ProxyFactory factory = new ProxyFactory();
factory.setTarget(bean);
factory.addAdvice(new NopInterceptor());
factory.setInterfaces(IJmxTestBean.class);
IJmxTestBean proxy = (IJmxTestBean) factory.getProxy();
String name = "bean:mmm=whatever";
Map<String, Object> beans = new HashMap<>();
beans.put(name, proxy);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
exporter.setBeans(beans);
exporter.registerBeans();
ObjectName oname = ObjectName.getInstance(name);
Object nameValue = server.getAttribute(oname, "Name");
assertThat(nameValue).isEqualTo("Rob Harrop");
}
use of cn.taketoday.aop.NopInterceptor in project today-infrastructure 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();
}
use of cn.taketoday.aop.NopInterceptor in project today-infrastructure by TAKETODAY.
the class MBeanExporterTests method testExportJdkProxy.
@Test
void testExportJdkProxy() throws Exception {
JmxTestBean bean = new JmxTestBean();
bean.setName("Rob Harrop");
ProxyFactory factory = new ProxyFactory();
factory.setTarget(bean);
factory.addAdvice(new NopInterceptor());
factory.setInterfaces(IJmxTestBean.class);
IJmxTestBean proxy = (IJmxTestBean) factory.getProxy();
String name = "bean:mmm=whatever";
Map<String, Object> beans = new HashMap<>();
beans.put(name, proxy);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
exporter.setBeans(beans);
exporter.registerBeans();
ObjectName oname = ObjectName.getInstance(name);
Object nameValue = server.getAttribute(oname, "Name");
assertThat(nameValue).isEqualTo("Rob Harrop");
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testEquals.
@Test
public void testEquals() {
IOther a = new AllInstancesAreEqual();
IOther b = new AllInstancesAreEqual();
NopInterceptor i1 = new NopInterceptor();
NopInterceptor i2 = new NopInterceptor();
ProxyFactory pfa = new ProxyFactory(a);
pfa.addAdvice(i1);
ProxyFactory pfb = new ProxyFactory(b);
pfb.addAdvice(i2);
IOther proxyA = (IOther) createProxy(pfa);
IOther proxyB = (IOther) createProxy(pfb);
assertThat(pfb.getAdvisors().length).isEqualTo(pfa.getAdvisors().length);
assertThat(b).isEqualTo(a);
assertThat(i2).isEqualTo(i1);
assertThat(proxyB).isEqualTo(proxyA);
assertThat(proxyB.hashCode()).isEqualTo(proxyA.hashCode());
assertThat(proxyA.equals(a)).isFalse();
// Equality checks were handled by the proxy
assertThat(i1.getCount()).isEqualTo(0);
// When we invoke A, it's NopInterceptor will have count == 1
// and won't think it's equal to B's NopInterceptor
proxyA.absquatulate();
assertThat(i1.getCount()).isEqualTo(1);
assertThat(proxyA.equals(proxyB)).isFalse();
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testAddThrowsAdviceWithoutAdvisor.
@Test
public void testAddThrowsAdviceWithoutAdvisor() throws Throwable {
// Reacts to ServletException and RemoteException
MyThrowsHandler th = new MyThrowsHandler();
Echo target = new Echo();
target.setA(16);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvice(th);
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);
// Subclass of RemoteException
MarshalException mex = new MarshalException("");
assertThatExceptionOfType(MarshalException.class).isThrownBy(() -> proxied.echoException(1, mex)).matches(mex::equals);
assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
Aggregations