Search in sources :

Example 6 with Person

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);
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) Person(org.springframework.beans.testfixture.beans.Person) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) Test(org.junit.jupiter.api.Test)

Example 7 with Person

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));
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) Advised(org.springframework.aop.framework.Advised) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Person(org.springframework.beans.testfixture.beans.Person) Test(org.junit.jupiter.api.Test)

Example 8 with Person

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();
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Person(org.springframework.beans.testfixture.beans.Person) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 9 with Person

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();
}
Also used : NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Advice(org.aopalliance.aop.Advice) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) Person(org.springframework.beans.testfixture.beans.Person) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 10 with Person

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);
}
Also used : Person(org.springframework.beans.testfixture.beans.Person) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) Test(org.junit.jupiter.api.Test)

Aggregations

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