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    import java.io.ByteArrayInputStream;
014    import java.io.ByteArrayOutputStream;
015    import java.io.IOException;
016    import java.io.ObjectInputStream;
017    import java.io.ObjectOutputStream;
018    
019    import org.picocontainer.MutablePicoContainer;
020    import org.picocontainer.PicoContainer;
021    import org.picocontainer.PicoException;
022    import org.picocontainer.DefaultPicoContainer;
023    import org.picocontainer.tck.AbstractPicoContainerTestCase;
024    
025    /**
026     * @author Thomas Heller
027     * @author Paul Hammant
028     */
029    public class DefaultPicoContainerTreeSerializationTestCase extends AbstractPicoContainerTestCase {
030        protected MutablePicoContainer createPicoContainer(PicoContainer parent) {
031            return new DefaultPicoContainer(parent);
032        }
033    
034        public void testContainerIsDeserializableWithParent() throws PicoException,
035                                                                     IOException, ClassNotFoundException {
036    
037            PicoContainer parent = createPicoContainer(null);
038            MutablePicoContainer child = createPicoContainer(parent);
039    
040            ByteArrayOutputStream baos = new ByteArrayOutputStream();
041            ObjectOutputStream oos = new ObjectOutputStream(baos);
042    
043            oos.writeObject(child);
044    
045            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
046            child = (MutablePicoContainer) ois.readObject();
047            assertNotNull(child.getParent());
048        }
049    }