View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.xds.core.metadata;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertSame;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.ObjectInputStream;
26  import java.io.ObjectOutputStream;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.junit.Test;
30  import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
31  import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
32  import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocument;
33  import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocumentSet;
34  
35  /**
36   * Tests that all requests/responses and metadata classes can be serialized.
37   * @author Jens Riemschneider
38   */
39  public class SerializationTest {
40      @Test
41      public void testProvideAndRegisterDocumentSet() throws Exception {
42          ProvideAndRegisterDocumentSet request = SampleData.createProvideAndRegisterDocumentSet();
43          checkSerialization(request);
44      }
45  
46      @Test
47      public void testQueryRegistry() throws Exception {
48          checkSerialization(SampleData.createFindDocumentsQuery());
49          checkSerialization(SampleData.createFindDocumentsForMultiplePatientsQuery());
50          checkSerialization(SampleData.createFindFoldersQuery());
51          checkSerialization(SampleData.createFindFoldersForMultiplePatientsQuery());
52          checkSerialization(SampleData.createFindSubmissionSetsQuery());
53          checkSerialization(SampleData.createGetAllQuery());
54          checkSerialization(SampleData.createGetAssociationsQuery());
55          checkSerialization(SampleData.createGetDocumentsAndAssociationsQuery());
56          checkSerialization(SampleData.createGetDocumentsQuery());
57          checkSerialization(SampleData.createGetFolderAndContentsQuery());
58          checkSerialization(SampleData.createGetFoldersForDocumentQuery());
59          checkSerialization(SampleData.createGetFoldersQuery());
60          checkSerialization(SampleData.createGetRelatedDocumentsQuery());
61          checkSerialization(SampleData.createGetSubmissionSetAndContentsQuery());
62          checkSerialization(SampleData.createGetSubmissionSetsQuery());
63      }
64      
65      @Test
66      public void testRegisterDocumentSet() throws Exception {
67          checkSerialization(SampleData.createRegisterDocumentSet());
68      }
69      
70      @Test
71      public void testRetrieveDocumentSet() throws Exception {
72          checkSerialization(SampleData.createRetrieveDocumentSet());
73      }
74  
75      @Test
76      public void testQueryResponse() throws Exception {
77          checkSerialization(SampleData.createQueryResponseWithLeafClass());
78          checkSerialization(SampleData.createQueryResponseWithObjRef());
79      }
80  
81      @Test
82      public void testRetrievedDocumentSet() throws Exception {
83          RetrievedDocumentSet response = SampleData.createRetrievedDocumentSet();
84          for (RetrievedDocument doc : response.getDocuments()) {
85              doc.setDataHandler(null);
86          }
87          checkSerialization(response);
88      }
89      
90      private void checkSerialization(Object original) throws IOException, ClassNotFoundException {
91          ByteArrayOutputStream out = new ByteArrayOutputStream();
92          ObjectOutputStream oos = new ObjectOutputStream(out);
93          ObjectInputStream ois = null;
94          try {
95              oos.writeObject(original);
96              InputStream in = new ByteArrayInputStream(out.toByteArray());
97              ois = new ObjectInputStream(in);   
98              Object copy = ois.readObject();
99              assertSame(original.getClass(), copy.getClass());
100             assertEquals(original, copy);
101         }
102         finally {
103             IOUtils.closeQuietly(oos);
104             IOUtils.closeQuietly(ois);
105         }
106     }
107 }