001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: SerializableHelper.java 28515 2008-01-06 20:37:29Z sfermigier $
020 */
021
022package org.nuxeo.common.utils;
023
024import java.io.ByteArrayInputStream;
025import java.io.ByteArrayOutputStream;
026import java.io.IOException;
027import java.io.ObjectInputStream;
028import java.io.ObjectOutputStream;
029import java.io.Serializable;
030
031/**
032 * Helper to test object serialization. Used only in tests.
033 *
034 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
035 */
036public final class SerializableHelper {
037
038    // This is an utility class.
039    private SerializableHelper() {
040    }
041
042    /**
043     * Checks if a given object is serializable.
044     *
045     * @param ob the actual object we want to test
046     * @return true if the object is serializable.
047     */
048    // XXX AT: since class loader isolation, this module is not aware anymore of
049    // nuxeo.ear classes => ClassCastException can be thrown is tested object is
050    // a DocumentModel for instance.
051    public static boolean isSerializable(Object ob) {
052        if (!(ob instanceof Serializable)) {
053            return false;
054        }
055        try {
056            ob = serializeUnserialize(ob);
057            return ob != null;
058        } catch (IOException e) {
059            return false;
060        } catch (ClassNotFoundException e) {
061            return false;
062        }
063    }
064
065    /**
066     * Serializes and unserializes back an object to test whether it is correctly rebuilt (to be used in unit tests as
067     * sanity checks).
068     *
069     * @param ob the actual object we want to test
070     * @return true if the object is serializable.
071     */
072    public static Object serializeUnserialize(Object ob) throws IOException, ClassNotFoundException {
073        Serializable in = (Serializable) ob;
074        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
075        ObjectOutputStream outStream = new ObjectOutputStream(byteOutStream);
076        outStream.writeObject(in);
077        ByteArrayInputStream byteInStream = new ByteArrayInputStream(byteOutStream.toByteArray());
078        ObjectInputStream inStream = new ObjectInputStream(byteInStream);
079        return inStream.readObject();
080    }
081
082}