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.PicoInitializationException;
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 DefaultPicoContainer child = new DefaultPicoContainer(parent);
032 return child;
033 }
034
035 public void testContainerIsDeserializableWithParent() throws PicoException, PicoInitializationException,
036 IOException, ClassNotFoundException {
037
038 PicoContainer parent = createPicoContainer(null);
039 MutablePicoContainer child = createPicoContainer(parent);
040
041 ByteArrayOutputStream baos = new ByteArrayOutputStream();
042 ObjectOutputStream oos = new ObjectOutputStream(baos);
043
044 oos.writeObject(child);
045
046 child = null;
047 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
048 child = (MutablePicoContainer) ois.readObject();
049 assertNotNull(child.getParent());
050 }
051 }