Search in sources :

Example 6 with Person

use of org.springframework.tests.sample.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 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied);
    NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
    p2.getName();
    assertEquals(2, nop2.getCount());
    p2.echo(null);
    assertEquals(3, nop2.getCount());
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) Person(org.springframework.tests.sample.beans.Person) Test(org.junit.Test)

Example 7 with Person

use of org.springframework.tests.sample.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");
    assertEquals(0, nop.getCount());
    int newAge = 12;
    // Not advised
    assertEquals(0, p.getAge());
    assertEquals(0, nop.getCount());
    // This is proxied
    p.setAge(newAge);
    assertEquals(1, nop.getCount());
    p.setAge(newAge);
    assertEquals(newAge, p.getAge());
    // Only setter fired
    assertEquals(2, nop.getCount());
    // Serialize and continue...
    p = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(newAge, p.getAge());
    // Remembers count, but we need to get a new reference to nop...
    nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
    assertEquals(2, nop.getCount());
    assertEquals("serializableSettersAdvised", p.getName());
    p.setAge(newAge + 1);
    assertEquals(3, nop.getCount());
    assertEquals(newAge + 1, p.getAge());
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.tests.aop.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.tests.sample.beans.Person) Test(org.junit.Test)

Example 8 with Person

use of org.springframework.tests.sample.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");
    assertSame("Should be a Singleton", p, bf.getBean("serializableSingleton"));
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(p, p2);
    assertNotSame(p, p2);
    assertEquals("serializableSingleton", p2.getName());
    // Add unserializable advice
    Advice nop = new NopInterceptor();
    ((Advised) p).addAdvice(nop);
    // Check it still works
    assertEquals(p2.getName(), p2.getName());
    assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p));
    // Remove offending interceptor...
    assertTrue(((Advised) p).removeAdvice(nop));
    assertTrue("Serializable again because offending interceptor was removed", SerializationTestUtils.isSerializable(p));
}
Also used : NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Advice(org.aopalliance.aop.Advice) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Person(org.springframework.tests.sample.beans.Person) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 9 with Person

use of org.springframework.tests.sample.beans.Person in project spring-framework by spring-projects.

the class CommonsPool2TargetSourceTests method testProxySerializableWithoutConfigMixin.

@Test
public void testProxySerializableWithoutConfigMixin() throws Exception {
    Person pooled = (Person) beanFactory.getBean("pooledPerson");
    //System.out.println(((Advised) pooled).toProxyConfigString());
    assertTrue(((Advised) pooled).getTargetSource() instanceof CommonsPool2TargetSource);
    //((Advised) pooled).setTargetSource(new SingletonTargetSource(new SerializablePerson()));
    Person serialized = (Person) SerializationTestUtils.serializeAndDeserialize(pooled);
    assertTrue(((Advised) serialized).getTargetSource() instanceof SingletonTargetSource);
    serialized.setAge(25);
    assertEquals(25, serialized.getAge());
}
Also used : Advised(org.springframework.aop.framework.Advised) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) Person(org.springframework.tests.sample.beans.Person) Test(org.junit.Test)

Example 10 with Person

use of org.springframework.tests.sample.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");
    assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p));
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Person(org.springframework.tests.sample.beans.Person) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

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