use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAdviceSupportListeners.
@Test
public void testAdviceSupportListeners() throws Throwable {
TestBean target = new TestBean();
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
CountingAdvisorListener l = new CountingAdvisorListener(pc);
pc.addListener(l);
RefreshCountingAdvisorChainFactory acf = new RefreshCountingAdvisorChainFactory();
// Should be automatically added as a listener
pc.addListener(acf);
assertThat(pc.isActive()).isFalse();
assertThat(l.activates).isEqualTo(0);
assertThat(acf.refreshes).isEqualTo(0);
ITestBean proxied = (ITestBean) createProxy(pc);
assertThat(acf.refreshes).isEqualTo(1);
assertThat(l.activates).isEqualTo(1);
assertThat(pc.isActive()).isTrue();
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(l.adviceChanges).isEqualTo(0);
NopInterceptor di = new NopInterceptor();
pc.addAdvice(0, di);
assertThat(l.adviceChanges).isEqualTo(1);
assertThat(acf.refreshes).isEqualTo(2);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
pc.removeAdvice(di);
assertThat(l.adviceChanges).isEqualTo(2);
assertThat(acf.refreshes).isEqualTo(3);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
pc.getProxy();
assertThat(l.activates).isEqualTo(1);
pc.removeListener(l);
assertThat(l.adviceChanges).isEqualTo(2);
pc.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
// No longer counting
assertThat(l.adviceChanges).isEqualTo(2);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testCannotAddInterceptorWhenFrozen.
@Test
public void testCannotAddInterceptorWhenFrozen() 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);
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add interceptor when frozen").isThrownBy(() -> pc.addAdvice(0, new NopInterceptor())).withMessageContaining("frozen");
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(((Advised) proxied).getAdvisors().length).isEqualTo(1);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testSerializableTargetAndAdvice.
@Test
public void testSerializableTargetAndAdvice() throws Throwable {
SerializablePerson personTarget = new SerializablePerson();
personTarget.setName("jim");
personTarget.setAge(26);
assertThat(SerializationTestUtils.isSerializable(personTarget)).isTrue();
ProxyFactory pf = new ProxyFactory(personTarget);
CountingThrowsAdvice cta = new CountingThrowsAdvice();
pf.addAdvice(new SerializableNopInterceptor());
// Try various advice types
pf.addAdvice(new CountingBeforeAdvice());
pf.addAdvice(new CountingAfterReturningAdvice());
pf.addAdvice(cta);
Person p = (Person) createAopProxy(pf).getProxy();
p.echo(null);
assertThat(cta.getCalls()).isEqualTo(0);
try {
p.echo(new IOException());
} catch (IOException ex) {
/* expected */
}
assertThat(cta.getCalls()).isEqualTo(1);
// Will throw exception if it fails
Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
assertThat(p2).isNotSameAs(p);
assertThat(p2.getName()).isEqualTo(p.getName());
assertThat(p2.getAge()).isEqualTo(p.getAge());
assertThat(AopUtils.isAopProxy(p2)).as("Deserialized object is an AOP proxy").isTrue();
Advised a1 = (Advised) p;
Advised a2 = (Advised) p2;
// Check we can manipulate state of p2
assertThat(a2.getAdvisors().length).isEqualTo(a1.getAdvisors().length);
// This should work as SerializablePerson is equal
assertThat(p2).as("Proxies should be equal, even after one was serialized").isEqualTo(p);
assertThat(p).as("Proxies should be equal, even after one was serialized").isEqualTo(p2);
// Check we can add a new advisor to the target
NopInterceptor ni = new NopInterceptor();
p2.getAge();
assertThat(ni.getCount()).isEqualTo(0);
a2.addAdvice(ni);
p2.getAge();
assertThat(ni.getCount()).isEqualTo(1);
cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
p2.echo(null);
assertThat(cta.getCalls()).isEqualTo(1);
try {
p2.echo(new IOException());
} catch (IOException ex) {
}
assertThat(cta.getCalls()).isEqualTo(2);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class CreatesTestBean method jdkAssertions.
private void jdkAssertions(ITestBean tb, int nopInterceptorCount) {
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("nopInterceptor");
assertThat(nop.getCount()).isEqualTo(0);
assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
int age = 5;
tb.setAge(age);
assertThat(tb.getAge()).isEqualTo(age);
assertThat(nop.getCount()).isEqualTo((2 * nopInterceptorCount));
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class CreatesTestBean method jdkIntroduction.
@Test
void jdkIntroduction() {
ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertThat(nop.getCount()).isEqualTo(0);
assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
int age = 5;
tb.setAge(age);
assertThat(tb.getAge()).isEqualTo(age);
boolean condition = tb instanceof TimeStamped;
assertThat(condition).as("Introduction was made").isTrue();
assertThat(((TimeStamped) tb).getTimeStamp()).isEqualTo(0);
assertThat(nop.getCount()).isEqualTo(3);
assertThat(tb.getName()).isEqualTo("introductionUsingJdk");
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertThat(lockable1.locked()).isFalse();
assertThat(lockable2.locked()).isFalse();
tb.setAge(65);
assertThat(tb.getAge()).isEqualTo(65);
lockable1.lock();
assertThat(lockable1.locked()).isTrue();
// Shouldn't affect second
assertThat(lockable2.locked()).isFalse();
// Can still mod second object
tb2.setAge(12);
// But can't mod first
assertThatExceptionOfType(LockedException.class).as("mixin should have locked this object").isThrownBy(() -> tb.setAge(6));
}
Aggregations