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