Search in sources :

Example 1 with SerializablePerson

use of cn.taketoday.beans.testfixture.beans.SerializablePerson in project today-infrastructure 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 ex) {
    }
    assertThat(cta.getCalls()).isEqualTo(2);
}
Also used : CountingAfterReturningAdvice(cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice) NopInterceptor(cn.taketoday.aop.testfixture.interceptor.NopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) SerializableNopInterceptor(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) IOException(java.io.IOException) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) Person(cn.taketoday.beans.testfixture.beans.Person) CountingBeforeAdvice(cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice) Test(org.junit.jupiter.api.Test)

Example 2 with SerializablePerson

use of cn.taketoday.beans.testfixture.beans.SerializablePerson in project today-infrastructure by TAKETODAY.

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(cn.taketoday.beans.testfixture.beans.Person) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) Test(org.junit.jupiter.api.Test)

Example 3 with SerializablePerson

use of cn.taketoday.beans.testfixture.beans.SerializablePerson in project today-infrastructure by TAKETODAY.

the class DataBinderTests method bindExceptionSerializable.

@Test
void bindExceptionSerializable() throws Exception {
    SerializablePerson tb = new SerializablePerson();
    tb.setName("myName");
    tb.setAge(99);
    BindException ex = new BindException(tb, "tb");
    ex.reject("invalid", "someMessage");
    ex.rejectValue("age", "invalidField", "someMessage");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(ex);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    BindException ex2 = (BindException) ois.readObject();
    assertThat(ex2.hasGlobalErrors()).isTrue();
    assertThat(ex2.getGlobalError().getCode()).isEqualTo("invalid");
    assertThat(ex2.hasFieldErrors("age")).isTrue();
    assertThat(ex2.getFieldError("age").getCode()).isEqualTo("invalidField");
    assertThat(ex2.getFieldValue("age")).isEqualTo(99);
    ex2.rejectValue("name", "invalidField", "someMessage");
    assertThat(ex2.hasFieldErrors("name")).isTrue();
    assertThat(ex2.getFieldError("name").getCode()).isEqualTo("invalidField");
    assertThat(ex2.getFieldValue("name")).isEqualTo("myName");
}
Also used : SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Example 4 with SerializablePerson

use of cn.taketoday.beans.testfixture.beans.SerializablePerson in project today-infrastructure by TAKETODAY.

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(cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with SerializablePerson

use of cn.taketoday.beans.testfixture.beans.SerializablePerson in project today-framework by TAKETODAY.

the class DataBinderTests method testBindExceptionSerializable.

@Test
void testBindExceptionSerializable() throws Exception {
    SerializablePerson tb = new SerializablePerson();
    tb.setName("myName");
    tb.setAge(99);
    BindException ex = new BindException(tb, "tb");
    ex.reject("invalid", "someMessage");
    ex.rejectValue("age", "invalidField", "someMessage");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(ex);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    BindException ex2 = (BindException) ois.readObject();
    assertThat(ex2.hasGlobalErrors()).isTrue();
    assertThat(ex2.getGlobalError().getCode()).isEqualTo("invalid");
    assertThat(ex2.hasFieldErrors("age")).isTrue();
    assertThat(ex2.getFieldError("age").getCode()).isEqualTo("invalidField");
    assertThat(ex2.getFieldValue("age")).isEqualTo(99);
    ex2.rejectValue("name", "invalidField", "someMessage");
    assertThat(ex2.hasFieldErrors("name")).isTrue();
    assertThat(ex2.getFieldError("name").getCode()).isEqualTo("invalidField");
    assertThat(ex2.getFieldValue("name")).isEqualTo("myName");
}
Also used : SerializablePerson(cn.taketoday.beans.testfixture.beans.SerializablePerson) ByteArrayInputStream(java.io.ByteArrayInputStream) BindException(cn.taketoday.validation.BindException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

SerializablePerson (cn.taketoday.beans.testfixture.beans.SerializablePerson)15 Test (org.junit.jupiter.api.Test)13 SerializableNopInterceptor (cn.taketoday.aop.testfixture.interceptor.SerializableNopInterceptor)10 Person (cn.taketoday.beans.testfixture.beans.Person)8 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)6 CountingAfterReturningAdvice (cn.taketoday.aop.testfixture.advice.CountingAfterReturningAdvice)4 CountingBeforeAdvice (cn.taketoday.aop.testfixture.advice.CountingBeforeAdvice)4 NopInterceptor (cn.taketoday.aop.testfixture.interceptor.NopInterceptor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 AfterReturningAdvice (cn.taketoday.aop.AfterReturningAdvice)2 DynamicIntroductionAdvice (cn.taketoday.aop.DynamicIntroductionAdvice)2 MethodBeforeAdvice (cn.taketoday.aop.MethodBeforeAdvice)2 ThrowsAdvice (cn.taketoday.aop.ThrowsAdvice)2 Advised (cn.taketoday.aop.framework.Advised)2 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)2 TimeStamped (cn.taketoday.core.testfixture.TimeStamped)2 IOException (java.io.IOException)2