use of cn.taketoday.beans.testfixture.beans.Person in project today-infrastructure by TAKETODAY.
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 cn.taketoday.beans.testfixture.beans.Person in project today-infrastructure by TAKETODAY.
the class ProxyFactoryBeanTests method testProxyNotSerializableBecauseOfAdvice.
@Test
public void testProxyNotSerializableBecauseOfAdvice() throws Exception {
StandardBeanFactory bf = new StandardBeanFactory();
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 cn.taketoday.beans.testfixture.beans.Person in project today-infrastructure by TAKETODAY.
the class ProxyFactoryBeanTests method testSerializableSingletonProxy.
@Test
public void testSerializableSingletonProxy() throws Exception {
StandardBeanFactory bf = new StandardBeanFactory();
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 cn.taketoday.beans.testfixture.beans.Person in project today-infrastructure by TAKETODAY.
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);
}
use of cn.taketoday.beans.testfixture.beans.Person in project today-framework by TAKETODAY.
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