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