    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.tck;
    011
    012    import static org.junit.Assert.assertEquals;
    013    import static org.junit.Assert.assertNotNull;
    014    import static org.junit.Assert.assertSame;
    015
    016    import org.junit.Test;
    017    import org.picocontainer.Characteristics;
    018    import org.picocontainer.MutablePicoContainer;
    019    import org.picocontainer.PicoException;
    020
    021    /**
    022     * @author Aslak Helles&oslash;y
    023     */
    024    public abstract class AbstractLazyInstantiationTest {
    025
    026        protected abstract MutablePicoContainer createPicoContainer();
    027
    028        public static class Kilroy {
    029            public Kilroy(Havana havana) {
    030                havana.graffiti("Kilroy was here");
    031            }
    032        }
    033
    034        public static class Havana {
    035            public String paint = "Clean wall";
    036
    037            public void graffiti(String paint) {
    038                this.paint = paint;
    039            }
    040        }
    041
    042        @Test public void testLazyInstantiation() throws PicoException {
    043            MutablePicoContainer pico = createPicoContainer();
    044
    045            pico.as(Characteristics.CACHE).addComponent(Kilroy.class);
    046            pico.as(Characteristics.CACHE).addComponent(Havana.class);
    047
    048            assertSame(pico.getComponent(Havana.class), pico.getComponent(Havana.class));
    049            assertNotNull(pico.getComponent(Havana.class));
    050            assertEquals("Clean wall", pico.getComponent(Havana.class).paint);
    051            assertNotNull(pico.getComponent(Kilroy.class));
    052            assertEquals("Kilroy was here", pico.getComponent(Havana.class).paint);
    053        }
    054    }