use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testBeforeAdvisorIsInvoked.
@Test
public void testBeforeAdvisorIsInvoked() {
CountingBeforeAdvice cba = new CountingBeforeAdvice();
@SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getParameterCount() == 0;
}
};
TestBean target = new TestBean();
target.setAge(80);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesNoArgs);
assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
ITestBean proxied = (ITestBean) createProxy(pf);
assertEquals(0, cba.getCalls());
assertEquals(0, cba.getCalls("getAge"));
assertEquals(target.getAge(), proxied.getAge());
assertEquals(1, cba.getCalls());
assertEquals(1, cba.getCalls("getAge"));
assertEquals(0, cba.getCalls("setAge"));
// Won't be advised
proxied.setAge(26);
assertEquals(1, cba.getCalls());
assertEquals(26, proxied.getAge());
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testValuesStick.
/**
* Simple test that if we set values we can get them out again.
*/
@Test
public void testValuesStick() {
int age1 = 33;
int age2 = 37;
String name = "tony";
TestBean target1 = new TestBean();
target1.setAge(age1);
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor()));
ITestBean tb = (ITestBean) pf1.getProxy();
assertEquals(age1, tb.getAge());
tb.setAge(age2);
assertEquals(age2, tb.getAge());
assertNull(tb.getName());
tb.setName(name);
assertEquals(name, tb.getName());
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
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);
assertEquals(name, proxied.getName());
TimeStamped intro = (TimeStamped) proxied;
assertEquals(ts, intro.getTimeStamp());
}
use of org.springframework.tests.aop.interceptor.NopInterceptor 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);
assertFalse("Opaque defaults to false", pc.isOpaque());
pc.setOpaque(true);
assertTrue("Opaque now true for this config", pc.isOpaque());
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.setAge(10);
assertEquals(10, proxied.getAge());
assertEquals(1, mba.getCalls());
assertFalse("Cannot be cast to Advised", proxied instanceof Advised);
}
use of org.springframework.tests.aop.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class UnsupportedInterceptor method testMethodInvocationDuringConstructor.
@Test
public void testMethodInvocationDuringConstructor() {
CglibTestBean bean = new CglibTestBean();
bean.setName("Rob Harrop");
AdvisedSupport as = new AdvisedSupport();
as.setTarget(bean);
as.addAdvice(new NopInterceptor());
AopProxy aop = new CglibAopProxy(as);
CglibTestBean proxy = (CglibTestBean) aop.getProxy();
assertEquals("The name property has been overwritten by the constructor", "Rob Harrop", proxy.getName());
}
Aggregations