    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     * Original code by                                                          *
    009     *****************************************************************************/
    010    package org.picocontainer.defaults;
    011
    012    import static org.junit.Assert.assertEquals;
    013
    014    import org.junit.Test;
    015    import org.picocontainer.DefaultPicoContainer;
    016    import org.picocontainer.MutablePicoContainer;
    017    import org.picocontainer.PicoContainer;
    018    import org.picocontainer.testmodel.DependsOnTouchable;
    019    import org.picocontainer.testmodel.SimpleTouchable;
    020
    021    import com.thoughtworks.xstream.XStream;
    022    import com.thoughtworks.xstream.core.JVM;
    023    import com.thoughtworks.xstream.io.xml.XppDriver;
    024
    025    /**
    026     * @author Aslak Helles&oslash;y
    027     */
    028    public final class XStreamSerialisationTestCase {
    029        private final XStream xStream = new XStream(new XppDriver());
    030
    031        @Test public void testShouldBeAbleToSerialiseEmptyPico() {
    032            if (JVM.is14()) {
    033                MutablePicoContainer pico = new DefaultPicoContainer();
    034                String picoXml = xStream.toXML(pico);
    035                PicoContainer serializedPico = (PicoContainer) xStream.fromXML(picoXml);
    036
    037                assertEquals(0, serializedPico.getComponents().size());
    038            }
    039        }
    040
    041        @Test public void testShouldBeAbleToSerialisePicoWithUninstantiatedComponents() {
    042            if (JVM.is14()) {
    043                MutablePicoContainer pico = new DefaultPicoContainer();
    044                pico.addComponent(SimpleTouchable.class);
    045                pico.addComponent(DependsOnTouchable.class);
    046                String picoXml = xStream.toXML(pico);
    047                PicoContainer serializedPico = (PicoContainer) xStream.fromXML(picoXml);
    048
    049                assertEquals(2, serializedPico.getComponents().size());
    050            }
    051        }
    052
    053        @Test public void testShouldBeAbleToSerialisePicoWithInstantiatedComponents() {
    054            if (JVM.is14()) {
    055                MutablePicoContainer pico = new DefaultPicoContainer();
    056                pico.addComponent(SimpleTouchable.class);
    057                pico.addComponent(DependsOnTouchable.class);
    058                pico.getComponents();
    059                String picoXml = xStream.toXML(pico);
    060                PicoContainer serializedPico = (PicoContainer) xStream.fromXML(picoXml);
    061
    062                assertEquals(2, serializedPico.getComponents().size());
    063            }
    064        }
    065    }