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
011package org.picocontainer.defaults;
012
013
014import static org.junit.Assert.assertNotNull;
015
016import java.io.ByteArrayInputStream;
017import java.io.ByteArrayOutputStream;
018import java.io.IOException;
019import java.io.ObjectInputStream;
020import java.io.ObjectOutputStream;
021import java.util.Properties;
022
023import org.junit.Test;
024import org.picocontainer.DefaultPicoContainer;
025import org.picocontainer.MutablePicoContainer;
026import org.picocontainer.PicoContainer;
027import org.picocontainer.PicoException;
028import org.picocontainer.tck.AbstractPicoContainerTest;
029
030
031/**
032 * @author Thomas Heller
033 * @author Paul Hammant
034 */
035public 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}