]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/TestUserSerialize.java
add: Implement (de)serialisation support for CertificateOwner and derived classes
[gigi.git] / tests / org / cacert / gigi / TestUserSerialize.java
1 package org.cacert.gigi;
2
3 import static org.junit.Assert.*;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
8 import java.io.ObjectInputStream;
9 import java.io.ObjectOutputStream;
10
11 import org.cacert.gigi.dbObjects.User;
12 import org.cacert.gigi.testUtils.BusinessTest;
13 import org.junit.Test;
14
15 public class TestUserSerialize extends BusinessTest {
16
17     private byte[] serialize(Object o) throws IOException {
18         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
19             try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
20                 oos.writeObject(o);
21                 oos.flush();
22             }
23             baos.flush();
24             return baos.toByteArray();
25         }
26     }
27
28     private Object deserialize(byte[] ba) throws IOException, ClassNotFoundException {
29         try (ByteArrayInputStream bais = new ByteArrayInputStream(ba)) {
30             try (ObjectInputStream ois = new ObjectInputStream(bais)) {
31                 Object o = ois.readObject();
32                 return o;
33             }
34         }
35     }
36
37     @Test
38     public void testSerializeUser() throws GigiApiException, IOException, ClassNotFoundException {
39         User u = createVerifiedUser();
40         byte[] ba = serialize(u);
41         Object uo = deserialize(ba);
42         assertSame("Original user and the deserialized object must be the same", u, uo);
43     }
44
45 }