use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class RegexpMethodPointcutAdvisorIntegrationTests method testSerialization.
@Test
public void testSerialization() throws Throwable {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
// This is a CGLIB proxy, so we can proxy it to the target class
Person p = (Person) bf.getBean("serializableSettersAdvised");
// Interceptor behind regexp advisor
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
assertThat(nop.getCount()).isEqualTo(0);
int newAge = 12;
// Not advised
assertThat(p.getAge()).isEqualTo(0);
assertThat(nop.getCount()).isEqualTo(0);
// This is proxied
p.setAge(newAge);
assertThat(nop.getCount()).isEqualTo(1);
p.setAge(newAge);
assertThat(p.getAge()).isEqualTo(newAge);
// Only setter fired
assertThat(nop.getCount()).isEqualTo(2);
// Serialize and continue...
p = SerializationTestUtils.serializeAndDeserialize(p);
assertThat(p.getAge()).isEqualTo(newAge);
// Remembers count, but we need to get a new reference to nop...
nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
assertThat(nop.getCount()).isEqualTo(2);
assertThat(p.getName()).isEqualTo("serializableSettersAdvised");
p.setAge(newAge + 1);
assertThat(nop.getCount()).isEqualTo(3);
assertThat(p.getAge()).isEqualTo((newAge + 1));
}
use of org.springframework.aop.testfixture.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((TimeStamped) () -> ts));
ITestBean proxied = (ITestBean) createProxy(pc);
assertThat(proxied.getName()).isEqualTo(name);
TimeStamped intro = (TimeStamped) proxied;
assertThat(intro.getTimeStamp()).isEqualTo(ts);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotRemoveAdvisorWhenFrozen.
@Test
public void testCannotRemoveAdvisorWhenFrozen() 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);
Advised advised = (Advised) proxied;
assertThat(pc.isFrozen()).isTrue();
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to remove Advisor when frozen").isThrownBy(() -> advised.removeAdvisor(0)).withMessageContaining("frozen");
// Didn't get removed
assertThat(advised.getAdvisors().length).isEqualTo(1);
pc.setFrozen(false);
// Can now remove it
advised.removeAdvisor(0);
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(advised.getAdvisors().length).isEqualTo(0);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters.
@Test
public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
// Could apply dynamically to getAge/setAge but not to getName
TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age");
pc.addAdvisor(dp);
this.mockTargetSource.setTarget(tb);
pc.setTargetSource(mockTargetSource);
ITestBean it = (ITestBean) createProxy(pc);
assertThat(dp.count).isEqualTo(0);
it.getAge();
// Statically vetoed
assertThat(dp.count).isEqualTo(0);
it.setAge(11);
assertThat(it.getAge()).isEqualTo(11);
assertThat(dp.count).isEqualTo(1);
// Applies statically but not dynamically
it.setName("joe");
assertThat(dp.count).isEqualTo(1);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAddThrowsAdviceWithoutAdvisor.
@Test
public void testAddThrowsAdviceWithoutAdvisor() throws Throwable {
// Reacts to ServletException and RemoteException
MyThrowsHandler th = new MyThrowsHandler();
Echo target = new Echo();
target.setA(16);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvice(th);
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);
// Subclass of RemoteException
MarshalException mex = new MarshalException("");
assertThatExceptionOfType(MarshalException.class).isThrownBy(() -> proxied.echoException(1, mex)).matches(mex::equals);
assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
Aggregations