use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testSerializableSingletonProxy.
@Test
public void testSerializableSingletonProxy() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
Person p = (Person) bf.getBean("serializableSingleton");
assertThat(bf.getBean("serializableSingleton")).as("Should be a Singleton").isSameAs(p);
Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
assertThat(p2).isEqualTo(p);
assertThat(p2).isNotSameAs(p);
assertThat(p2.getName()).isEqualTo("serializableSingleton");
// Add unserializable advice
Advice nop = new NopInterceptor();
((Advised) p).addAdvice(nop);
// Check it still works
assertThat(p2.getName()).isEqualTo(p2.getName());
assertThat(SerializationTestUtils.isSerializable(p)).as("Not serializable because an interceptor isn't serializable").isFalse();
// Remove offending interceptor...
assertThat(((Advised) p).removeAdvice(nop)).isTrue();
assertThat(SerializationTestUtils.isSerializable(p)).as("Serializable again because offending interceptor was removed").isTrue();
}
use of org.springframework.aop.testfixture.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();
assertThat(tb.getAge()).isEqualTo(age1);
tb.setAge(age2);
assertThat(tb.getAge()).isEqualTo(age2);
assertThat(tb.getName()).isNull();
tb.setName(name);
assertThat(tb.getName()).isEqualTo(name);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testEquals.
@Test
public void testEquals() {
IOther a = new AllInstancesAreEqual();
IOther b = new AllInstancesAreEqual();
NopInterceptor i1 = new NopInterceptor();
NopInterceptor i2 = new NopInterceptor();
ProxyFactory pfa = new ProxyFactory(a);
pfa.addAdvice(i1);
ProxyFactory pfb = new ProxyFactory(b);
pfb.addAdvice(i2);
IOther proxyA = (IOther) createProxy(pfa);
IOther proxyB = (IOther) createProxy(pfb);
assertThat(pfb.getAdvisors().length).isEqualTo(pfa.getAdvisors().length);
assertThat(b).isEqualTo(a);
assertThat(i2).isEqualTo(i1);
assertThat(proxyB).isEqualTo(proxyA);
assertThat(proxyB.hashCode()).isEqualTo(proxyA.hashCode());
assertThat(proxyA.equals(a)).isFalse();
// Equality checks were handled by the proxy
assertThat(i1.getCount()).isEqualTo(0);
// When we invoke A, it's NopInterceptor will have count == 1
// and won't think it's equal to B's NopInterceptor
proxyA.absquatulate();
assertThat(i1.getCount()).isEqualTo(1);
assertThat(proxyA.equals(proxyB)).isFalse();
}
use of org.springframework.aop.testfixture.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);
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.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testSerializationAdviceAndTargetNotSerializable.
@Test
public void testSerializationAdviceAndTargetNotSerializable() throws Exception {
TestBean tb = new TestBean();
assertThat(SerializationTestUtils.isSerializable(tb)).isFalse();
ProxyFactory pf = new ProxyFactory(tb);
pf.addAdvice(new NopInterceptor());
ITestBean proxy = (ITestBean) createAopProxy(pf).getProxy();
assertThat(SerializationTestUtils.isSerializable(proxy)).isFalse();
}
Aggregations