use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testGetObjectTypeWithDirectTarget.
@Test
public void testGetObjectTypeWithDirectTarget() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS));
// We have a counting before advice here
CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice");
assertThat(cba.getCalls()).isEqualTo(0);
ITestBean tb = (ITestBean) bf.getBean("directTarget");
assertThat(tb.getName().equals("Adam")).isTrue();
assertThat(cba.getCalls()).isEqualTo(1);
ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget");
assertThat(TestBean.class.isAssignableFrom(pfb.getObjectType())).as("Has correct object type").isTrue();
}
use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testProxyConfigString.
/**
* Check that the string is informative.
*/
@Test
public void testProxyConfigString() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
MethodBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba);
pc.addAdvisor(advisor);
ITestBean proxied = (ITestBean) createProxy(pc);
String proxyConfigString = ((Advised) proxied).toProxyConfigString();
assertThat(proxyConfigString.contains(advisor.toString())).isTrue();
assertThat(proxyConfigString.contains("1 interface")).isTrue();
}
use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice 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.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCanPreventCastToAdvisedUsingOpaque.
@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
TestBean target = new TestBean();
ProxyFactory pc = new ProxyFactory(target);
pc.setInterfaces(ITestBean.class);
pc.addAdvice(new NopInterceptor());
CountingBeforeAdvice mba = new CountingBeforeAdvice();
Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
pc.addAdvisor(advisor);
assertThat(pc.isOpaque()).as("Opaque defaults to false").isFalse();
pc.setOpaque(true);
assertThat(pc.isOpaque()).as("Opaque now true for this config").isTrue();
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.setAge(10);
assertThat(proxied.getAge()).isEqualTo(10);
assertThat(mba.getCalls()).isEqualTo(1);
boolean condition = proxied instanceof Advised;
assertThat(condition).as("Cannot be cast to Advised").isFalse();
}
use of org.springframework.aop.testfixture.advice.CountingBeforeAdvice in project spring-framework by spring-projects.
the class SelectivePrototypeTargetSourceCreator method testWithOptimizedProxy.
@Test
public void testWithOptimizedProxy() throws Exception {
BeanFactory beanFactory = new ClassPathXmlApplicationContext(OPTIMIZED_CONTEXT, CLASS);
ITestBean testBean = (ITestBean) beanFactory.getBean("optimizedTestBean");
assertThat(AopUtils.isAopProxy(testBean)).isTrue();
CountingBeforeAdvice beforeAdvice = (CountingBeforeAdvice) beanFactory.getBean("countingAdvice");
testBean.setAge(23);
testBean.getAge();
assertThat(beforeAdvice.getCalls()).as("Incorrect number of calls to proxy").isEqualTo(2);
}
Aggregations