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