Search in sources :

Example 11 with Person

use of cn.taketoday.beans.testfixture.beans.Person in project today-framework by TAKETODAY.

the class RegexpMethodPointcutAdvisorIntegrationTests method testSerialization.

@Test
public void testSerialization() throws Throwable {
    StandardBeanFactory bf = new StandardBeanFactory();
    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));
}
Also used : NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) Advised(cn.taketoday.aop.framework.Advised) XmlBeanDefinitionReader(cn.taketoday.beans.factory.xml.XmlBeanDefinitionReader) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Person(cn.taketoday.beans.testfixture.beans.Person) Test(org.junit.jupiter.api.Test)

Example 12 with Person

use of cn.taketoday.beans.testfixture.beans.Person in project today-framework by TAKETODAY.

the class DelegatingIntroductionInterceptorTests method testSerializableDelegatingIntroductionInterceptorSerializable.

@Test
public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception {
    SerializablePerson serializableTarget = new SerializablePerson();
    String name = "Tony";
    serializableTarget.setName("Tony");
    ProxyFactory factory = new ProxyFactory(serializableTarget);
    factory.addInterface(Person.class);
    long time = 1000;
    TimeStamped ts = new SerializableTimeStamped(time);
    factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
    factory.addAdvice(new SerializableNopInterceptor());
    Person p = (Person) factory.getProxy();
    assertThat(p.getName()).isEqualTo(name);
    assertThat(((TimeStamped) p).getTimeStamp()).isEqualTo(time);
    Person p1 = SerializationTestUtils.serializeAndDeserialize(p);
    assertThat(p1.getName()).isEqualTo(name);
    assertThat(((TimeStamped) p1).getTimeStamp()).isEqualTo(time);
}
Also used : TimeStamped(cn.taketoday.core.testfixture.TimeStamped) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Person(cn.taketoday.beans.testfixture.beans.Person) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) Test(org.junit.jupiter.api.Test)

Example 13 with Person

use of cn.taketoday.beans.testfixture.beans.Person in project today-framework by TAKETODAY.

the class ProxyFactoryBeanTests method testSerializableSingletonProxyFactoryBean.

@Test
public void testSerializableSingletonProxyFactoryBean() throws Exception {
    StandardBeanFactory bf = new StandardBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
    Person p = (Person) bf.getBean("serializableSingleton");
    ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&serializableSingleton");
    ProxyFactoryBean pfb2 = SerializationTestUtils.serializeAndDeserialize(pfb);
    Person p2 = (Person) pfb2.getObject();
    assertThat(p2).isEqualTo(p);
    assertThat(p2).isNotSameAs(p);
    assertThat(p2.getName()).isEqualTo("serializableSingleton");
}
Also used : XmlBeanDefinitionReader(cn.taketoday.beans.factory.xml.XmlBeanDefinitionReader) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Person(cn.taketoday.beans.testfixture.beans.Person) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 14 with Person

use of cn.taketoday.beans.testfixture.beans.Person in project today-framework by TAKETODAY.

the class ProxyFactoryBeanTests method testSerializablePrototypeProxy.

@Test
public void testSerializablePrototypeProxy() throws Exception {
    StandardBeanFactory bf = new StandardBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
    Person p = (Person) bf.getBean("serializablePrototype");
    assertThat(bf.getBean("serializablePrototype")).as("Should not be a Singleton").isNotSameAs(p);
    Person p2 = SerializationTestUtils.serializeAndDeserialize(p);
    assertThat(p2).isEqualTo(p);
    assertThat(p2).isNotSameAs(p);
    assertThat(p2.getName()).isEqualTo("serializablePrototype");
}
Also used : XmlBeanDefinitionReader(cn.taketoday.beans.factory.xml.XmlBeanDefinitionReader) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Person(cn.taketoday.beans.testfixture.beans.Person) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 15 with Person

use of cn.taketoday.beans.testfixture.beans.Person in project today-infrastructure by TAKETODAY.

the class ProxyFactoryBeanTests method testSerializableSingletonProxyFactoryBean.

@Test
public void testSerializableSingletonProxyFactoryBean() throws Exception {
    StandardBeanFactory bf = new StandardBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS));
    Person p = (Person) bf.getBean("serializableSingleton");
    ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&serializableSingleton");
    ProxyFactoryBean pfb2 = SerializationTestUtils.serializeAndDeserialize(pfb);
    Person p2 = (Person) pfb2.getObject();
    assertThat(p2).isEqualTo(p);
    assertThat(p2).isNotSameAs(p);
    assertThat(p2.getName()).isEqualTo("serializableSingleton");
}
Also used : XmlBeanDefinitionReader(cn.taketoday.beans.factory.xml.XmlBeanDefinitionReader) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Person(cn.taketoday.beans.testfixture.beans.Person) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Aggregations

Person (cn.taketoday.beans.testfixture.beans.Person)20 Test (org.junit.jupiter.api.Test)20 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)10 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)10 XmlBeanDefinitionReader (cn.taketoday.beans.factory.xml.XmlBeanDefinitionReader)10 SerializablePerson (cn.taketoday.beans.testfixture.beans.SerializablePerson)10 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)8 ClassPathResource (cn.taketoday.core.io.ClassPathResource)8 Advised (cn.taketoday.aop.framework.Advised)4 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)4 CountingBeforeAdvice (cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice)4 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)2 CountingAfterReturningAdvice (cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice)2 TimeStamped (cn.taketoday.core.testfixture.TimeStamped)2 IOException (java.io.IOException)2 Advice (org.aopalliance.aop.Advice)2