KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > tck > AbstractLazyInstantiationTestCase


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by *
9  *****************************************************************************/

10 package org.picocontainer.tck;
11
12 import junit.framework.TestCase;
13 import org.picocontainer.MutablePicoContainer;
14 import org.picocontainer.PicoException;
15 import org.picocontainer.PicoRegistrationException;
16
17 /**
18  * @author Aslak Hellesøy
19  * @version $Revision: 940 $
20  */

21 public abstract class AbstractLazyInstantiationTestCase extends TestCase {
22
23     protected abstract MutablePicoContainer createPicoContainer();
24
25     public static class Kilroy {
26         public Kilroy(Havana havana) {
27             havana.graffiti("Kilroy was here");
28         }
29     }
30
31     public static class Havana {
32         public String JavaDoc paint = "Clean wall";
33
34         public void graffiti(String JavaDoc paint) {
35             this.paint = paint;
36         }
37     }
38
39     public void testLazyInstantiation() throws PicoException, PicoRegistrationException {
40         MutablePicoContainer pico = createPicoContainer();
41
42         pico.registerComponentImplementation(Kilroy.class);
43         pico.registerComponentImplementation(Havana.class);
44
45         assertSame(pico.getComponentInstance(Havana.class), pico.getComponentInstance(Havana.class));
46         assertNotNull(pico.getComponentInstance(Havana.class));
47         assertEquals("Clean wall", ((Havana) pico.getComponentInstance(Havana.class)).paint);
48         assertNotNull(pico.getComponentInstance(Kilroy.class));
49         assertEquals("Kilroy was here", ((Havana) pico.getComponentInstance(Havana.class)).paint);
50     }
51 }
52
Popular Tags