Search in sources :

Example 1 with SerializableNopInterceptor

use of org.springframework.tests.aop.interceptor.SerializableNopInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testSerializationSerializableTargetAndAdvice.

@Test
public void testSerializationSerializableTargetAndAdvice() throws Throwable {
    SerializablePerson personTarget = new SerializablePerson();
    personTarget.setName("jim");
    personTarget.setAge(26);
    assertTrue(SerializationTestUtils.isSerializable(personTarget));
    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);
    assertEquals(0, cta.getCalls());
    try {
        p.echo(new IOException());
    } catch (IOException ex) {
    /* expected */
    }
    assertEquals(1, cta.getCalls());
    // Will throw exception if it fails
    Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertNotSame(p, p2);
    assertEquals(p.getName(), p2.getName());
    assertEquals(p.getAge(), p2.getAge());
    assertTrue("Deserialized object is an AOP proxy", AopUtils.isAopProxy(p2));
    Advised a1 = (Advised) p;
    Advised a2 = (Advised) p2;
    // Check we can manipulate state of p2
    assertEquals(a1.getAdvisors().length, a2.getAdvisors().length);
    // This should work as SerializablePerson is equal
    assertEquals("Proxies should be equal, even after one was serialized", p, p2);
    assertEquals("Proxies should be equal, even after one was serialized", p2, p);
    // Check we can add a new advisor to the target
    NopInterceptor ni = new NopInterceptor();
    p2.getAge();
    assertEquals(0, ni.getCount());
    a2.addAdvice(ni);
    p2.getAge();
    assertEquals(1, ni.getCount());
    cta = (CountingThrowsAdvice) a2.getAdvisors()[3].getAdvice();
    p2.echo(null);
    assertEquals(1, cta.getCalls());
    try {
        p2.echo(new IOException());
    } catch (IOException ex) {
    }
    assertEquals(2, cta.getCalls());
}
Also used : CountingAfterReturningAdvice(org.springframework.tests.aop.advice.CountingAfterReturningAdvice) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) NopInterceptor(org.springframework.tests.aop.interceptor.NopInterceptor) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) IOException(java.io.IOException) Person(org.springframework.tests.sample.beans.Person) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) CountingBeforeAdvice(org.springframework.tests.aop.advice.CountingBeforeAdvice) Test(org.junit.Test)

Example 2 with SerializableNopInterceptor

use of org.springframework.tests.aop.interceptor.SerializableNopInterceptor in project spring-framework by spring-projects.

the class HotSwappableTargetSourceTests method testSerialization.

// TODO test reject swap to wrong interface or class?
// how to decide what's valid?
@Test
public void testSerialization() throws Exception {
    SerializablePerson sp1 = new SerializablePerson();
    sp1.setName("Tony");
    SerializablePerson sp2 = new SerializablePerson();
    sp1.setName("Gordon");
    HotSwappableTargetSource hts = new HotSwappableTargetSource(sp1);
    ProxyFactory pf = new ProxyFactory();
    pf.addInterface(Person.class);
    pf.setTargetSource(hts);
    pf.addAdvisor(new DefaultPointcutAdvisor(new SerializableNopInterceptor()));
    Person p = (Person) pf.getProxy();
    assertEquals(sp1.getName(), p.getName());
    hts.swap(sp2);
    assertEquals(sp2.getName(), p.getName());
    p = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    // We need to get a reference to the client-side targetsource
    hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource();
    assertEquals(sp2.getName(), p.getName());
    hts.swap(sp1);
    assertEquals(sp1.getName(), p.getName());
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) Person(org.springframework.tests.sample.beans.Person) Test(org.junit.Test)

Example 3 with SerializableNopInterceptor

use of org.springframework.tests.aop.interceptor.SerializableNopInterceptor in project spring-framework by spring-projects.

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();
    assertEquals(name, p.getName());
    assertEquals(time, ((TimeStamped) p).getTimeStamp());
    Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
    assertEquals(name, p1.getName());
    assertEquals(time, ((TimeStamped) p1).getTimeStamp());
}
Also used : TimeStamped(org.springframework.tests.TimeStamped) SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) ProxyFactory(org.springframework.aop.framework.ProxyFactory) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) Person(org.springframework.tests.sample.beans.Person) Test(org.junit.Test)

Example 4 with SerializableNopInterceptor

use of org.springframework.tests.aop.interceptor.SerializableNopInterceptor in project spring-framework by spring-projects.

the class NameMatchMethodPointcutTests method setUp.

/**
	 * Create an empty pointcut, populating instance variables.
	 */
@Before
public void setUp() {
    ProxyFactory pf = new ProxyFactory(new SerializablePerson());
    nop = new SerializableNopInterceptor();
    pc = new NameMatchMethodPointcut();
    pf.addAdvisor(new DefaultPointcutAdvisor(pc, nop));
    proxied = (Person) pf.getProxy();
}
Also used : SerializableNopInterceptor(org.springframework.tests.aop.interceptor.SerializableNopInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) Before(org.junit.Before)

Example 5 with SerializableNopInterceptor

use of org.springframework.tests.aop.interceptor.SerializableNopInterceptor 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)

Aggregations

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