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
18package tests.util;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.File;
23import java.io.FileOutputStream;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.ObjectInputStream;
27import java.io.ObjectOutputStream;
28import java.io.OutputStream;
29import java.net.URL;
30
31/**
32 * This class simplifies the serialization test.
33 *
34 */
35public class SerializationTester {
36
37	/*
38	 * --------------------------------------------------------------------
39	 * Class variables
40	 * --------------------------------------------------------------------
41	 */
42
43	// the last deserialized object
44	private static Object lastOutput = null;
45
46	/*
47	 * -------------------------------------------------------------------
48	 * Constructors
49	 * -------------------------------------------------------------------
50	 */
51
52	private SerializationTester() {
53
54	}
55
56	/*
57	 * -------------------------------------------------------------------
58	 * Methods
59	 * -------------------------------------------------------------------
60	 */
61
62	/**
63	 * Serialize an object and then deserialize it.
64	 *
65	 * @param inputObject
66	 *            the input object
67	 * @return the deserialized object
68	 */
69	public static Object getDeserilizedObject(Object inputObject)
70			throws IOException, ClassNotFoundException {
71		ByteArrayOutputStream bos = new ByteArrayOutputStream();
72		ObjectOutputStream oos = new ObjectOutputStream(bos);
73		oos.writeObject(inputObject);
74		oos.close();
75
76		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
77		ObjectInputStream ois = new ObjectInputStream(bis);
78		Object outputObject = ois.readObject();
79		lastOutput = outputObject;
80		ois.close();
81		return outputObject;
82	}
83
84	/**
85	 * Tests the serialization and deserialization of const objects.
86	 *
87	 * @param inputObject
88	 *            A const object
89	 * @return true if the deserialized object is the same as the input object,
90	 *         otherwise false
91	 * @throws Exception
92	 *             If any occurs.
93	 */
94	public static boolean assertSame(Object inputObject) throws Exception {
95		return inputObject == getDeserilizedObject(inputObject);
96	}
97
98	/**
99	 * Tests the serialization and deserialization of instance objects.
100	 *
101	 * @param inputObject
102	 *            An object
103	 * @return true if the deserialized object is equal to the input object,
104	 *         otherwise false
105	 * @throws Exception
106	 *             If any occurs.
107	 */
108	public static boolean assertEquals(Object inputObject) throws Exception {
109		return inputObject.equals(getDeserilizedObject(inputObject));
110	}
111
112	/**
113	 * Tests the serialization compatibility with reference const objects.
114	 *
115	 * @param obj
116	 *            the object to be checked
117	 * @param fileName
118	 *            the serialization output file generated by reference
119	 * @return true if compatible, otherwise false
120	 * @throws Exception
121	 *             If any occurs.
122	 */
123	public static boolean assertCompabilitySame(Object obj, String fileName)
124			throws Exception {
125		return obj == readObject(obj, fileName);
126	}
127
128	/**
129	 * Tests the serialization compatibility with reference for instance
130	 * objects.
131	 *
132	 * @param obj
133	 *            the object to be checked
134	 * @param fileName
135	 *            the serialization output file generated by reference
136	 * @return true if compatible, otherwise false
137	 * @throws Exception
138	 *             If any occurs.
139	 */
140	public static boolean assertCompabilityEquals(Object obj, String fileName)
141			throws Exception {
142		return obj.equals(readObject(obj, fileName));
143	}
144
145	/**
146	 * Deserialize an object from a file.
147	 *
148	 * @param obj
149	 *            the object to be serialized if no serialization file is found
150	 * @param fileName
151	 *            the serialization file
152	 * @return the deserialized object
153	 * @throws Exception
154	 *             If any occurs.
155	 */
156	public static Object readObject(Object obj, String fileName)
157			throws Exception {
158		InputStream input = null;
159		ObjectInputStream oinput = null;
160		URL url = SerializationTester.class.getClassLoader().getResource(
161				fileName);
162		if (null == url) {
163			// serialization file does not exist, create one in the current dir
164			writeObject(obj, new File(fileName).getName());
165			throw new Error(
166					"Serialization file does not exist, created in the current dir.");
167		}
168		input = url.openStream();
169		try {
170			oinput = new ObjectInputStream(input);
171			Object newObj = oinput.readObject();
172			return newObj;
173		} finally {
174			try {
175				if (null != oinput) {
176					oinput.close();
177				}
178			} catch (Exception e) {
179				// ignore
180			}
181			try {
182				if (null != input) {
183					input.close();
184				}
185			} catch (Exception e) {
186				// ignore
187			}
188		}
189	}
190
191	/*
192	 * Creates a serialization output.
193	 *
194	 * @param obj the object to be serialized @param fileName the output file
195	 * @throws Exception If any occurs.
196	 */
197	public static void writeObject(Object obj, String fileName)
198			throws Exception {
199		// String path = SerializationTester.class.getResource(".").getPath();
200		// if (path.endsWith(".")) {
201		// path = path.substring(0, path.length() - 1);
202		// }
203		// if (!path.endsWith("/")) {
204		// path += "/";
205		// }
206		// path += fileName;
207		// System.out.println(path);
208		OutputStream output = null;
209		ObjectOutputStream ooutput = null;
210		try {
211			output = new FileOutputStream(fileName);
212			ooutput = new ObjectOutputStream(output);
213			ooutput.writeObject(obj);
214		} finally {
215			try {
216				if (null != ooutput) {
217					ooutput.close();
218				}
219			} catch (Exception e) {
220				// ignore
221			}
222			try {
223				if (null != output) {
224					output.close();
225				}
226			} catch (Exception e) {
227				// ignore
228			}
229		}
230	}
231
232	/**
233	 * Gets the last deserialized object.
234	 *
235	 * @return the last deserialized object
236	 */
237	public static Object getLastOutput() {
238		return lastOutput;
239	}
240
241	/*
242	 * For test purpose.
243	 */
244	public static void main(String[] args) {
245	}
246}
247