Search in sources :

Example 6 with SerializablePerson

use of org.springframework.beans.testfixture.beans.SerializablePerson in project spring-framework by spring-projects.

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

Example 7 with SerializablePerson

use of org.springframework.beans.testfixture.beans.SerializablePerson in project spring-framework by spring-projects.

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(org.springframework.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)

Aggregations

SerializablePerson (org.springframework.beans.testfixture.beans.SerializablePerson)7 Test (org.junit.jupiter.api.Test)6 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)5 Person (org.springframework.beans.testfixture.beans.Person)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 CountingAfterReturningAdvice (org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice)2 CountingBeforeAdvice (org.springframework.aop.testfixture.advice.CountingBeforeAdvice)2 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Advice (org.aopalliance.aop.Advice)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 AfterReturningAdvice (org.springframework.aop.AfterReturningAdvice)1 DynamicIntroductionAdvice (org.springframework.aop.DynamicIntroductionAdvice)1 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)1 ThrowsAdvice (org.springframework.aop.ThrowsAdvice)1 Advised (org.springframework.aop.framework.Advised)1