Search in sources :

Example 6 with SerializablePerson

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

the class DataBinderTests method testBindExceptionSerializable.

@Test
public 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();
    assertTrue(ex2.hasGlobalErrors());
    assertEquals("invalid", ex2.getGlobalError().getCode());
    assertTrue(ex2.hasFieldErrors("age"));
    assertEquals("invalidField", ex2.getFieldError("age").getCode());
    assertEquals(new Integer(99), ex2.getFieldValue("age"));
    ex2.rejectValue("name", "invalidField", "someMessage");
    assertTrue(ex2.hasFieldErrors("name"));
    assertEquals("invalidField", ex2.getFieldError("name").getCode());
    assertEquals("myName", ex2.getFieldValue("name"));
}
Also used : SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 7 with SerializablePerson

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

the class CommonsPool2TargetSourceTests method testProxySerializableWithoutConfigMixin.

@Test
public void testProxySerializableWithoutConfigMixin() throws Exception {
    Person pooled = (Person) beanFactory.getBean("pooledPerson");
    //System.out.println(((Advised) pooled).toProxyConfigString());
    assertTrue(((Advised) pooled).getTargetSource() instanceof CommonsPool2TargetSource);
    //((Advised) pooled).setTargetSource(new SingletonTargetSource(new SerializablePerson()));
    Person serialized = (Person) SerializationTestUtils.serializeAndDeserialize(pooled);
    assertTrue(((Advised) serialized).getTargetSource() instanceof SingletonTargetSource);
    serialized.setAge(25);
    assertEquals(25, serialized.getAge());
}
Also used : Advised(org.springframework.aop.framework.Advised) SerializablePerson(org.springframework.tests.sample.beans.SerializablePerson) Person(org.springframework.tests.sample.beans.Person) Test(org.junit.Test)

Aggregations

SerializablePerson (org.springframework.tests.sample.beans.SerializablePerson)7 Test (org.junit.Test)6 SerializableNopInterceptor (org.springframework.tests.aop.interceptor.SerializableNopInterceptor)5 Person (org.springframework.tests.sample.beans.Person)4 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 Advised (org.springframework.aop.framework.Advised)2 CountingAfterReturningAdvice (org.springframework.tests.aop.advice.CountingAfterReturningAdvice)2 CountingBeforeAdvice (org.springframework.tests.aop.advice.CountingBeforeAdvice)2 NopInterceptor (org.springframework.tests.aop.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 Before (org.junit.Before)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