Search in sources :

Example 56 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testExistingProxyChangesTarget.

@Test
public void testExistingProxyChangesTarget() throws Throwable {
    TestBean tb1 = new TestBean();
    tb1.setAge(33);
    TestBean tb2 = new TestBean();
    tb2.setAge(26);
    tb2.setName("Juergen");
    TestBean tb3 = new TestBean();
    tb3.setAge(37);
    ProxyFactory pc = new ProxyFactory(tb1);
    NopInterceptor nop = new NopInterceptor();
    pc.addAdvice(nop);
    ITestBean proxy = (ITestBean) createProxy(pc);
    assertThat(0).isEqualTo(nop.getCount());
    assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
    assertThat(1).isEqualTo(nop.getCount());
    // Change to a new static target
    pc.setTarget(tb2);
    assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
    assertThat(2).isEqualTo(nop.getCount());
    // Change to a new dynamic target
    HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
    pc.setTargetSource(hts);
    assertThat(proxy.getAge()).isEqualTo(tb3.getAge());
    assertThat(3).isEqualTo(nop.getCount());
    hts.swap(tb1);
    assertThat(proxy.getAge()).isEqualTo(tb1.getAge());
    tb1.setName("Colin");
    assertThat(proxy.getName()).isEqualTo(tb1.getName());
    assertThat(5).isEqualTo(nop.getCount());
    // Change back, relying on casting to Advised
    Advised advised = (Advised) proxy;
    assertThat(advised.getTargetSource()).isSameAs(hts);
    SingletonTargetSource sts = new SingletonTargetSource(tb2);
    advised.setTargetSource(sts);
    assertThat(proxy.getName()).isEqualTo(tb2.getName());
    assertThat(advised.getTargetSource()).isSameAs(sts);
    assertThat(proxy.getAge()).isEqualTo(tb2.getAge());
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) Test(org.junit.jupiter.api.Test)

Example 57 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class CreatesTestBean method cglibAssertions.

/**
 * Also has counting before advice.
 */
private void cglibAssertions(TestBean tb) {
    CountingBeforeAdvice cba = (CountingBeforeAdvice) beanFactory.getBean("countingBeforeAdvice");
    NopInterceptor nop = (NopInterceptor) beanFactory.getBean("nopInterceptor");
    assertThat(cba.getCalls()).isEqualTo(0);
    assertThat(nop.getCount()).isEqualTo(0);
    assertThat(AopUtils.isCglibProxy(tb)).isTrue();
    int age = 5;
    tb.setAge(age);
    assertThat(tb.getAge()).isEqualTo(age);
    assertThat(nop.getCount()).isEqualTo(2);
    assertThat(cba.getCalls()).isEqualTo(2);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice)

Example 58 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class SelectivePrototypeTargetSourceCreator method testCommonInterceptorAndAdvisor.

/**
 * Check that we can provide a common interceptor that will
 * appear in the chain before "specific" interceptors,
 * which are sourced from matching advisors
 */
@Test
public void testCommonInterceptorAndAdvisor() throws Exception {
    BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
    ITestBean test1 = (ITestBean) bf.getBean("test1");
    assertThat(AopUtils.isAopProxy(test1)).isTrue();
    Lockable lockable1 = (Lockable) test1;
    NopInterceptor nop1 = (NopInterceptor) bf.getBean("nopInterceptor");
    NopInterceptor nop2 = (NopInterceptor) bf.getBean("pointcutAdvisor", Advisor.class).getAdvice();
    ITestBean test2 = (ITestBean) bf.getBean("test2");
    Lockable lockable2 = (Lockable) test2;
    // Locking should be independent; nop is shared
    assertThat(lockable1.locked()).isFalse();
    assertThat(lockable2.locked()).isFalse();
    // equals 2 calls on shared nop, because it's first and sees calls
    // against the Lockable interface introduced by the specific advisor
    assertThat(nop1.getCount()).isEqualTo(2);
    assertThat(nop2.getCount()).isEqualTo(0);
    lockable1.lock();
    assertThat(lockable1.locked()).isTrue();
    assertThat(lockable2.locked()).isFalse();
    assertThat(nop1.getCount()).isEqualTo(5);
    assertThat(nop2.getCount()).isEqualTo(0);
    PackageVisibleMethod packageVisibleMethod = (PackageVisibleMethod) bf.getBean("packageVisibleMethod");
    assertThat(nop1.getCount()).isEqualTo(5);
    assertThat(nop2.getCount()).isEqualTo(0);
    packageVisibleMethod.doSomething();
    assertThat(nop1.getCount()).isEqualTo(6);
    assertThat(nop2.getCount()).isEqualTo(1);
    boolean condition = packageVisibleMethod instanceof Lockable;
    assertThat(condition).isTrue();
    Lockable lockable3 = (Lockable) packageVisibleMethod;
    lockable3.lock();
    assertThat(lockable3.locked()).isTrue();
    lockable3.unlock();
    assertThat(lockable3.locked()).isFalse();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BeanFactory(org.springframework.beans.factory.BeanFactory) Lockable(test.mixin.Lockable) Test(org.junit.jupiter.api.Test)

Example 59 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testReentrance.

@Test
public void testReentrance() {
    int age1 = 33;
    TestBean target1 = new TestBean();
    ProxyFactory pf1 = new ProxyFactory(target1);
    NopInterceptor di1 = new NopInterceptor();
    pf1.addAdvice(0, di1);
    ITestBean advised1 = (ITestBean) createProxy(pf1);
    // = 1 invocation
    advised1.setAge(age1);
    // = 2 invocations
    advised1.setSpouse(advised1);
    assertThat(di1.getCount()).as("one was invoked correct number of times").isEqualTo(2);
    // = 3 invocations
    assertThat(advised1.getAge()).as("Advised one has correct age").isEqualTo(age1);
    assertThat(di1.getCount()).as("one was invoked correct number of times").isEqualTo(3);
    // = 5 invocations, as reentrant call to spouse is advised also
    assertThat(advised1.getSpouse().getAge()).as("Advised spouse has correct age").isEqualTo(age1);
    assertThat(di1.getCount()).as("one was invoked correct number of times").isEqualTo(5);
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 60 with NopInterceptor

use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.

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(org.springframework.aop.testfixture.interceptor.NopInterceptor) MBeanInfo(javax.management.MBeanInfo) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ProxyFactory(org.springframework.aop.framework.ProxyFactory) HashMap(java.util.HashMap) MBeanExporter(org.springframework.jmx.export.MBeanExporter) IJmxTestBean(org.springframework.jmx.IJmxTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)60 Test (org.junit.jupiter.api.Test)57 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)39 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)33 TestBean (org.springframework.beans.testfixture.beans.TestBean)32 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)14 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)13 DefaultIntroductionAdvisor (org.springframework.aop.support.DefaultIntroductionAdvisor)11 Advisor (org.springframework.aop.Advisor)10 StaticMethodMatcherPointcutAdvisor (org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor)7 Method (java.lang.reflect.Method)6 LockMixinAdvisor (test.mixin.LockMixinAdvisor)6 IOException (java.io.IOException)4 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)4 CountingAfterReturningAdvice (org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 Person (org.springframework.beans.testfixture.beans.Person)4 Nullable (org.springframework.lang.Nullable)4