Search in sources :

Example 1 with SerializablePerson

use of cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson in project today-framework by TAKETODAY.

the class AbstractAopProxyTests method testSerializationAdviceNotSerializable.

@Test
public void testSerializationAdviceNotSerializable() throws Exception {
    SerializablePerson sp = new SerializablePerson();
    assertThat(SerializationTestUtils.isSerializable(sp)).isTrue();
    ProxyFactory pf = new ProxyFactory(sp);
    // This isn't serializable
    Advice i = new NopInterceptor();
    pf.addAdvice(i);
    assertThat(SerializationTestUtils.isSerializable(i)).isFalse();
    Object proxy = createAopProxy(pf).getProxy();
    assertThat(SerializationTestUtils.isSerializable(proxy)).isFalse();
}
Also used : NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) SerializablePerson(cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) MethodBeforeAdvice(cn.taketoday.aop.MethodBeforeAdvice) ThrowsAdvice(cn.taketoday.aop.ThrowsAdvice) AfterReturningAdvice(cn.taketoday.aop.AfterReturningAdvice) Advice(org.aopalliance.aop.Advice) DynamicIntroductionAdvice(cn.taketoday.aop.DynamicIntroductionAdvice) CountingAfterReturningAdvice(cn.taketoday.aop.CountingAfterReturningAdvice) Test(org.junit.jupiter.api.Test)

Example 2 with SerializablePerson

use of cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson in project today-framework by TAKETODAY.

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 ignored) {
    }
    assertThat(cta.getCalls()).isEqualTo(2);
}
Also used : CountingAfterReturningAdvice(cn.taketoday.aop.CountingAfterReturningAdvice) NopInterceptor(cn.taketoday.aop.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.SerializableNopInterceptor) SerializablePerson(cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) IOException(java.io.IOException) Person(cn.taketoday.aop.support.NameMatchMethodPointcutTests.Person) SerializablePerson(cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson) Test(org.junit.jupiter.api.Test)

Aggregations

CountingAfterReturningAdvice (cn.taketoday.aop.CountingAfterReturningAdvice)2 NopInterceptor (cn.taketoday.aop.NopInterceptor)2 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)2 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)2 SerializablePerson (cn.taketoday.aop.support.NameMatchMethodPointcutTests.SerializablePerson)2 Test (org.junit.jupiter.api.Test)2 AfterReturningAdvice (cn.taketoday.aop.AfterReturningAdvice)1 DynamicIntroductionAdvice (cn.taketoday.aop.DynamicIntroductionAdvice)1 MethodBeforeAdvice (cn.taketoday.aop.MethodBeforeAdvice)1 ThrowsAdvice (cn.taketoday.aop.ThrowsAdvice)1 Advised (cn.taketoday.aop.framework.Advised)1 Person (cn.taketoday.aop.support.NameMatchMethodPointcutTests.Person)1 IOException (java.io.IOException)1 Advice (org.aopalliance.aop.Advice)1