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 * 37 * @param o object that will be tested. 38 * @see #assertSameAfterSerialization(String, Object) 39 */ 40 public static void assertSameAfterSerialization(final Object o) { 41 assertSameAfterSerialization(null, o); 42 } 43 44 /** 45 * Asserts that deserialization of the object returns the same object as the 46 * one that was serialized. Object is first serialized, then deserialized 47 * and finally check is performed to see if original and deserialized 48 * object references are the same. 49 * <p> 50 * This method is especially good for testing singleton pattern on classes 51 * that support serialization. 52 * 53 * @param msg the identifying message for the {@code AssertionError}. 54 * @param o object that will be tested. 55 * @see #assertSameAfterSerialization(Object) 56 */ 57 public static void assertSameAfterSerialization(final String msg, final Object o) { 58 try { 59 // write object to byte buffer 60 final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 61 final ObjectOutputStream oos = new ObjectOutputStream(baos); 62 oos.writeObject(o); 63 oos.close(); 64 65 // read same object from byte buffer 66 final InputStream is = new ByteArrayInputStream(baos.toByteArray()); 67 final ObjectInputStream ois = new ObjectInputStream(is); 68 final Object object = ois.readObject(); 69 ois.close(); 70 71 // assert that original object and deserialized objects are the same 72 assertSame(o, object, msg); 73 } catch (final IOException | ClassNotFoundException e) { 74 // should never happen 75 throw new RuntimeException(e); 76 } 77 } 78 79 private TestUtils() {} 80 }