01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.test.validator;
13:
14: import java.io.ByteArrayInputStream;
15: import java.io.ByteArrayOutputStream;
16: import java.io.IOException;
17: import java.io.ObjectInputStream;
18: import java.io.ObjectOutputStream;
19: import java.io.Serializable;
20: import java.util.List;
21:
22: import junit.framework.TestCase;
23: import net.sf.oval.ConstraintViolation;
24: import net.sf.oval.Validator;
25: import net.sf.oval.constraint.Length;
26:
27: /**
28: * @author Sebastian Thomschke
29: */
30: public class SerializationTest extends TestCase {
31: private static class Person implements Serializable {
32: private static final long serialVersionUID = 1L;
33:
34: @Length(max=5)
35: public String firstName;
36: }
37:
38: @SuppressWarnings("unchecked")
39: public void testSerialization() throws IOException,
40: ClassNotFoundException {
41: final Validator validator = new Validator();
42:
43: final Person p = new Person();
44: p.firstName = "123456";
45: List<ConstraintViolation> violations = validator.validate(p);
46: assertTrue(violations.size() == 1);
47:
48: // serialize the violations
49: ByteArrayOutputStream bos = new ByteArrayOutputStream();
50: ObjectOutputStream oos = new ObjectOutputStream(bos);
51: oos.writeObject(violations);
52: oos.flush();
53: byte[] bytes = bos.toByteArray();
54:
55: // deserialize the violations
56: ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
57: ObjectInputStream ois = new ObjectInputStream(bis);
58: assertTrue(ois.readObject() instanceof List);
59: }
60: }
|