Search in sources :

Example 21 with NopInterceptor

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");
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) HashMap(java.util.HashMap) IJmxTestBean(cn.taketoday.jmx.IJmxTestBean) JmxTestBean(cn.taketoday.jmx.JmxTestBean) IJmxTestBean(cn.taketoday.jmx.IJmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Example 22 with NopInterceptor

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();
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) MBeanInfo(javax.management.MBeanInfo) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) HashMap(java.util.HashMap) MBeanExporter(cn.taketoday.jmx.export.MBeanExporter) IJmxTestBean(cn.taketoday.jmx.IJmxTestBean) Test(org.junit.jupiter.api.Test)

Example 23 with NopInterceptor

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");
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) HashMap(java.util.HashMap) IJmxTestBean(cn.taketoday.jmx.IJmxTestBean) JmxTestBean(cn.taketoday.jmx.JmxTestBean) IJmxTestBean(cn.taketoday.jmx.IJmxTestBean) ObjectName(javax.management.ObjectName) Test(org.junit.jupiter.api.Test)

Example 24 with NopInterceptor

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();
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) IOther(cn.taketoday.beans.testfixture.beans.IOther) Test(org.junit.jupiter.api.Test)

Example 25 with NopInterceptor

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);
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) MarshalException(java.rmi.MarshalException) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) MyThrowsHandler(cn.taketoday.aop.MyThrowsHandler) AopConfigException(cn.taketoday.aop.framework.AopConfigException) LockedException(cn.taketoday.aop.mixin.LockedException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (cn.taketoday.aop.NopInterceptor)47 Test (org.junit.jupiter.api.Test)46 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)39 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)30 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)29 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)29 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)14 DefaultIntroductionAdvisor (cn.taketoday.aop.support.DefaultIntroductionAdvisor)11 Advisor (cn.taketoday.aop.Advisor)10 Advised (cn.taketoday.aop.framework.Advised)9 AopConfigException (cn.taketoday.aop.framework.AopConfigException)9 AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)8 CglibAopProxy (cn.taketoday.aop.framework.CglibAopProxy)8 StaticMethodMatcherPointcutAdvisor (cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor)7 AopProxy (cn.taketoday.aop.framework.AopProxy)6 LockMixinAdvisor (cn.taketoday.aop.mixin.LockMixinAdvisor)6 Method (java.lang.reflect.Method)5 HashMap (java.util.HashMap)5 CountingAfterReturningAdvice (cn.taketoday.aop.CountingAfterReturningAdvice)4 IJmxTestBean (cn.taketoday.jmx.IJmxTestBean)4