Search in sources :

Example 1 with SerializableNopInterceptor

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

the class HotSwappableTargetSourceTests method testSerialization.

@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();
    assertThat(p.getName()).isEqualTo(sp1.getName());
    hts.swap(sp2);
    assertThat(p.getName()).isEqualTo(sp2.getName());
    p = SerializationTestUtils.serializeAndDeserialize(p);
    // We need to get a reference to the client-side targetsource
    hts = (HotSwappableTargetSource) ((Advised) p).getTargetSource();
    assertThat(p.getName()).isEqualTo(sp2.getName());
    hts.swap(sp1);
    assertThat(p.getName()).isEqualTo(sp1.getName());
}
Also used : SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) Person(org.springframework.beans.testfixture.beans.Person) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) Test(org.junit.jupiter.api.Test)

Example 2 with SerializableNopInterceptor

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

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

Example 3 with SerializableNopInterceptor

use of org.springframework.aop.testfixture.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();
    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(org.springframework.core.testfixture.TimeStamped) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Person(org.springframework.beans.testfixture.beans.Person) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) Test(org.junit.jupiter.api.Test)

Example 4 with SerializableNopInterceptor

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

the class NameMatchMethodPointcutTests method setup.

/**
 * Create an empty pointcut, populating instance variables.
 */
@BeforeEach
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.aop.testfixture.interceptor.SerializableNopInterceptor) ProxyFactory(org.springframework.aop.framework.ProxyFactory) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with SerializableNopInterceptor

use of org.springframework.aop.testfixture.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");
    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)

Aggregations

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