    001    /*****************************************************************************
    002     * Copyright (c) PicoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
    009     *****************************************************************************/
    010
    011    package org.picocontainer.defaults;
    012
    013
    014    import static org.junit.Assert.assertNotNull;
    015
    016    import java.io.ByteArrayInputStream;
    017    import java.io.ByteArrayOutputStream;
    018    import java.io.IOException;
    019    import java.io.ObjectInputStream;
    020    import java.io.ObjectOutputStream;
    021    import java.util.Properties;
    022
    023    import org.junit.Test;
    024    import org.picocontainer.DefaultPicoContainer;
    025    import org.picocontainer.MutablePicoContainer;
    026    import org.picocontainer.PicoContainer;
    027    import org.picocontainer.PicoException;
    028    import org.picocontainer.tck.AbstractPicoContainerTest;
    029
    030
    031    /**
    032     * @author Thomas Heller
    033     * @author Paul Hammant
    034     */
    035    public class DefaultPicoContainerTreeSerializationTestCase extends AbstractPicoContainerTest {
    036
    037        protected MutablePicoContainer createPicoContainer(PicoContainer parent) {
    038            return new DefaultPicoContainer(parent);
    039        }
    040
    041        protected Properties[] getProperties() {
    042            return new Properties[0];
    043        }
    044
    045        @Test public void testContainerIsDeserializableWithParent() throws PicoException,
    046                                                                     IOException, ClassNotFoundException {
    047
    048            PicoContainer parent = createPicoContainer(null);
    049            MutablePicoContainer child = createPicoContainer(parent);
    050
    051            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    052            ObjectOutputStream oos = new ObjectOutputStream(baos);
    053
    054            oos.writeObject(child);
    055
    056            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    057            child = (MutablePicoContainer) ois.readObject();
    058            assertNotNull(child.getParent());
    059        }
    060    }