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 package org.apache.commons.collections4;
18
19 import static org.junit.jupiter.api.Assertions.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 public final class TestUtils {
29
30 /**
31 * Asserts that deserialization of the object returns the same object as the
32 * one that was serialized.
33 * <p>
34 * Effect of method call is the same as:
35 * {@code assertSameAfterSerialization(null, o)}.
36 * </p>
37 *
38 * @param o object that will be tested.
39 * @throws IOException Thrown on test failure.
40 * @throws ClassNotFoundException Thrown on test failure.
41 * @see #assertSameAfterSerialization(String, Object)
42 */
43 public static void assertSameAfterSerialization(final Object o) throws IOException, ClassNotFoundException {
44 assertSameAfterSerialization(null, o);
45 }
46
47 /**
48 * Asserts that deserialization of the object returns the same object as the
49 * one that was serialized. Object is first serialized, then deserialized
50 * and finally check is performed to see if original and deserialized
51 * object references are the same.
52 * <p>
53 * This method is especially good for testing singleton pattern on classes
54 * that support serialization.
55 * </p>
56 *
57 * @param msg the identifying message for the {@code AssertionError}.
58 * @param o object that will be tested.
59 * @throws IOException Thrown on test failure.
60 * @throws ClassNotFoundException Thrown on test failure.
61 * @see #assertSameAfterSerialization(Object)
62 */
63 public static void assertSameAfterSerialization(final String msg, final Object o) throws IOException, ClassNotFoundException {
64 // write object to byte buffer
65 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
66 final ObjectOutputStream oos = new ObjectOutputStream(baos);
67 oos.writeObject(o);
68 oos.close();
69
70 // read same object from byte buffer
71 final InputStream is = new ByteArrayInputStream(baos.toByteArray());
72 final ObjectInputStream ois = new ObjectInputStream(is);
73 final Object object = ois.readObject();
74 ois.close();
75
76 // assert that original object and deserialized objects are the same
77 assertSame(o, object, msg);
78 }
79
80 private TestUtils() {
81 // empty
82 }
83
84 }