Search in sources :

Example 16 with NopInterceptor

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);
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) FileNotFoundException(java.io.FileNotFoundException) DefaultPointcutAdvisor(cn.taketoday.aop.support.DefaultPointcutAdvisor) DefaultIntroductionAdvisor(cn.taketoday.aop.support.DefaultIntroductionAdvisor) Advisor(cn.taketoday.aop.Advisor) LockMixinAdvisor(cn.taketoday.aop.mixin.LockMixinAdvisor) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Method(java.lang.reflect.Method) 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) MyThrowsHandler(cn.taketoday.aop.MyThrowsHandler) StaticMethodMatcherPointcutAdvisor(cn.taketoday.aop.support.StaticMethodMatcherPointcutAdvisor) Test(org.junit.jupiter.api.Test)

Example 17 with NopInterceptor

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

Example 18 with NopInterceptor

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

Example 19 with NopInterceptor

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

Example 20 with NopInterceptor

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

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