KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > SetterInjectionComponentAdapterFactoryTestCase


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  * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9  *****************************************************************************/

10 package org.picocontainer.defaults;
11
12 import org.picocontainer.Parameter;
13 import org.picocontainer.PicoInitializationException;
14 import org.picocontainer.tck.AbstractComponentAdapterFactoryTestCase;
15 import org.picocontainer.tck.AbstractComponentAdapterTestCase.RecordingLifecycleStrategy;
16 import org.picocontainer.testmodel.NullLifecycle;
17 import org.picocontainer.testmodel.RecordingLifecycle;
18 import org.picocontainer.testmodel.RecordingLifecycle.One;
19
20 /**
21  * @author J&ouml;rg Schaible</a>
22  * @version $Revision: 3139 $
23  */

24 public class SetterInjectionComponentAdapterFactoryTestCase extends AbstractComponentAdapterFactoryTestCase {
25     protected void setUp() throws Exception JavaDoc {
26         picoContainer = new DefaultPicoContainer(createComponentAdapterFactory());
27     }
28
29     protected ComponentAdapterFactory createComponentAdapterFactory() {
30         return new SetterInjectionComponentAdapterFactory();
31     }
32
33     public static interface Bean {
34     }
35
36     public static class NamedBean implements Bean {
37         private String JavaDoc name;
38
39         public String JavaDoc getName() {
40             return name;
41         }
42
43         public void setName(String JavaDoc name) {
44             this.name = name;
45         }
46     }
47
48     public static class NamedBeanWithPossibleDefault extends NamedBean {
49         private boolean byDefault;
50
51         public NamedBeanWithPossibleDefault() {
52         }
53
54         public NamedBeanWithPossibleDefault(String JavaDoc name) {
55             setName(name);
56             byDefault = true;
57         }
58
59         public boolean getByDefault() {
60             return byDefault;
61         }
62     }
63
64     public static class NoBean extends NamedBean {
65         public NoBean(String JavaDoc name) {
66             setName(name);
67         }
68     }
69
70     public void testContainerUsesStandardConstructor() {
71         picoContainer.registerComponentImplementation(Bean.class, NamedBeanWithPossibleDefault.class);
72         picoContainer.registerComponentInstance("Tom");
73         NamedBeanWithPossibleDefault bean = (NamedBeanWithPossibleDefault) picoContainer.getComponentInstance(Bean.class);
74         assertFalse(bean.getByDefault());
75     }
76
77     public void testContainerUsesOnlyStandardConstructor() {
78         picoContainer.registerComponentImplementation(Bean.class, NoBean.class);
79         picoContainer.registerComponentInstance("Tom");
80         try {
81             picoContainer.getComponentInstance(Bean.class);
82             fail("Instantiation should have failed.");
83         } catch (PicoInitializationException e) {
84         }
85     }
86
87     public void testCustomLifecycleCanBeInjected() throws NoSuchMethodException JavaDoc {
88         RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer JavaDoc());
89         SetterInjectionComponentAdapterFactory caf = new SetterInjectionComponentAdapterFactory(false, strategy);
90         SetterInjectionComponentAdapter sica = (SetterInjectionComponentAdapter)caf.createComponentAdapter(NullLifecycle.class, NullLifecycle.class, new Parameter[0]);
91         One one = new RecordingLifecycle.One(new StringBuffer JavaDoc());
92         sica.start(one);
93         sica.stop(one);
94         sica.dispose(one);
95         assertEquals("<start<stop<dispose", strategy.recording());
96     }
97 }
Popular Tags