1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package tests.util;
19 
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.io.OutputStream;
29 import java.net.URL;
30 
31 /**
32  * This class simplifies the serialization test.
33  */
34 public class SerializationTester {
SerializationTester()35     private SerializationTester() {
36 
37     }
38 
39     /**
40      * Serialize an object and then deserialize it.
41      *
42      * @param inputObject the input object
43      * @return the deserialized object
44      */
getDeserilizedObject(Object inputObject)45     public static Object getDeserilizedObject(Object inputObject)
46             throws IOException, ClassNotFoundException {
47         ByteArrayOutputStream bos = new ByteArrayOutputStream();
48         ObjectOutputStream oos = new ObjectOutputStream(bos);
49         oos.writeObject(inputObject);
50         oos.close();
51 
52         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
53         ObjectInputStream ois = new ObjectInputStream(bis);
54         Object outputObject = ois.readObject();
55         ois.close();
56         return outputObject;
57     }
58 
59     /**
60      * Tests the serialization and deserialization of const objects.
61      *
62      * @param inputObject A const object
63      * @return true if the deserialized object is the same as the input object,
64      *         otherwise false
65      * @throws Exception If any occurs.
66      */
assertSame(Object inputObject)67     public static boolean assertSame(Object inputObject) throws Exception {
68         return inputObject == getDeserilizedObject(inputObject);
69     }
70 
71     /**
72      * Tests the serialization and deserialization of instance objects.
73      *
74      * @param inputObject An object
75      * @return true if the deserialized object is equal to the input object,
76      *         otherwise false
77      * @throws Exception If any occurs.
78      */
assertEquals(Object inputObject)79     public static boolean assertEquals(Object inputObject) throws Exception {
80         return inputObject.equals(getDeserilizedObject(inputObject));
81     }
82 
83     /**
84      * Tests the serialization compatibility with reference const objects.
85      *
86      * @param obj      the object to be checked
87      * @param fileName the serialization output file generated by reference
88      * @return true if compatible, otherwise false
89      * @throws Exception If any occurs.
90      */
assertCompabilitySame(Object obj, String fileName)91     public static boolean assertCompabilitySame(Object obj, String fileName)
92             throws Exception {
93         return obj == readObject(obj, fileName);
94     }
95 
96     /**
97      * Tests the serialization compatibility with reference for instance
98      * objects.
99      *
100      * @param obj      the object to be checked
101      * @param fileName the serialization output file generated by reference
102      * @return true if compatible, otherwise false
103      * @throws Exception If any occurs.
104      */
assertCompabilityEquals(Object obj, String fileName)105     public static boolean assertCompabilityEquals(Object obj, String fileName)
106             throws Exception {
107         return obj.equals(readObject(obj, fileName));
108     }
109 
110     /**
111      * Deserialize an object from a file.
112      *
113      * @param obj      the object to be serialized if no serialization file is found
114      * @param fileName the serialization file
115      * @return the deserialized object
116      * @throws Exception If any occurs.
117      */
readObject(Object obj, String fileName)118     public static Object readObject(Object obj, String fileName)
119             throws Exception {
120         InputStream input = null;
121         ObjectInputStream oinput = null;
122         URL url = SerializationTester.class.getClassLoader().getResource(fileName);
123         if (null == url) {
124             // serialization file does not exist, create one in the current dir
125             writeObject(obj, new File(fileName).getName());
126             throw new Error(
127                     "Serialization file does not exist, created in the current dir.");
128         }
129         input = url.openStream();
130         try {
131             oinput = new ObjectInputStream(input);
132             Object newObj = oinput.readObject();
133             return newObj;
134         } finally {
135             try {
136                 if (null != oinput) {
137                     oinput.close();
138                 }
139             } catch (Exception e) {
140                 // ignore
141             }
142             try {
143                 if (null != input) {
144                     input.close();
145                 }
146             } catch (Exception e) {
147                 // ignore
148             }
149         }
150     }
151 
152     /*
153       * Creates a serialization output.
154       *
155       * @param obj the object to be serialized @param fileName the output file
156       * @throws Exception If any occurs.
157       */
writeObject(Object obj, String fileName)158     public static void writeObject(Object obj, String fileName)
159             throws Exception {
160         // String path = SerializationTester.class.getResource(".").getPath();
161         // if (path.endsWith(".")) {
162         // path = path.substring(0, path.length() - 1);
163         // }
164         // if (!path.endsWith("/")) {
165         // path += "/";
166         // }
167         // path += fileName;
168         // System.out.println(path);
169         OutputStream output = null;
170         ObjectOutputStream ooutput = null;
171         try {
172             output = new FileOutputStream(fileName);
173             ooutput = new ObjectOutputStream(output);
174             ooutput.writeObject(obj);
175         } finally {
176             try {
177                 if (null != ooutput) {
178                     ooutput.close();
179                 }
180             } catch (Exception e) {
181                 // ignore
182             }
183             try {
184                 if (null != output) {
185                     output.close();
186                 }
187             } catch (Exception e) {
188                 // ignore
189             }
190         }
191     }
192 }
193