use of org.springframework.beans.testfixture.beans.Person in project spring-framework by spring-projects.
the class NameMatchMethodPointcutTests method testSerializable.
@Test
public void testSerializable() throws Throwable {
testSets();
// Count is now 2
Person p2 = SerializationTestUtils.serializeAndDeserialize(proxied);
NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
p2.getName();
assertThat(nop2.getCount()).isEqualTo(2);
p2.echo(null);
assertThat(nop2.getCount()).isEqualTo(3);
}
use of org.springframework.beans.testfixture.beans.Person 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.beans.testfixture.beans.Person in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testProxyNotSerializableBecauseOfAdvice.
@Test
public void testProxyNotSerializableBecauseOfAdvice() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
Person p = (Person) bf.getBean("interceptorNotSerializableSingleton");
assertThat(SerializationTestUtils.isSerializable(p)).as("Not serializable because an interceptor isn't serializable").isFalse();
}
use of org.springframework.beans.testfixture.beans.Person 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.beans.testfixture.beans.Person in project spring-framework by spring-projects.
the class CommonsPool2TargetSourceTests method testProxySerializableWithoutConfigMixin.
@Test
void testProxySerializableWithoutConfigMixin() throws Exception {
Person pooled = (Person) beanFactory.getBean("pooledPerson");
boolean condition1 = ((Advised) pooled).getTargetSource() instanceof CommonsPool2TargetSource;
assertThat(condition1).isTrue();
// ((Advised) pooled).setTargetSource(new SingletonTargetSource(new SerializablePerson()));
Person serialized = SerializationTestUtils.serializeAndDeserialize(pooled);
boolean condition = ((Advised) serialized).getTargetSource() instanceof SingletonTargetSource;
assertThat(condition).isTrue();
serialized.setAge(25);
assertThat(serialized.getAge()).isEqualTo(25);
}
Aggregations