KickJava   Java API By Example, From Geeks To Geeks.

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


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 Joerg Schaible *
9  *****************************************************************************/

10 package org.picocontainer.defaults;
11
12 import org.picocontainer.ComponentAdapter;
13 import org.picocontainer.Disposable;
14 import org.picocontainer.MutablePicoContainer;
15 import org.picocontainer.PicoContainer;
16 import org.picocontainer.Startable;
17 import org.picocontainer.tck.AbstractComponentAdapterTestCase;
18 import org.picocontainer.testmodel.NullLifecycle;
19 import org.picocontainer.testmodel.SimpleTouchable;
20 import org.picocontainer.testmodel.Touchable;
21
22 import java.util.Map JavaDoc;
23
24 /**
25  * Test the InstanceComponentAdapter.
26  *
27  * @author Jörg Schaible
28  * @since 1.1
29  */

30 public class InstanceComponentAdapterTestCase
31         extends AbstractComponentAdapterTestCase {
32
33     public void testComponentAdapterReturnsSame() {
34         final Touchable touchable = new SimpleTouchable();
35         final ComponentAdapter componentAdapter = new InstanceComponentAdapter(Touchable.class, touchable);
36         assertSame(touchable, componentAdapter.getComponentInstance(null));
37     }
38
39     public void testDefaultLifecycleStrategy() {
40         LifecycleComponent component = new LifecycleComponent();
41         InstanceComponentAdapter componentAdapter =
42             new InstanceComponentAdapter(LifecycleComponent.class, component);
43         PicoContainer pico = new DefaultPicoContainer();
44         componentAdapter.start(pico);
45         componentAdapter.stop(pico);
46         componentAdapter.dispose(pico);
47         assertEquals("start>stop>dispose>", component.buffer.toString());
48         componentAdapter.start(component);
49         componentAdapter.stop(component);
50         componentAdapter.dispose(component);
51         assertEquals("start>stop>dispose>start>stop>dispose>", component.buffer.toString());
52     }
53
54     private static class LifecycleComponent implements Startable, Disposable {
55         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
56
57         public void start() {
58             buffer.append("start>");
59         }
60
61         public void stop() {
62             buffer.append("stop>");
63         }
64
65         public void dispose() {
66             buffer.append("dispose>");
67         }
68     }
69
70     public void testCustomLifecycleCanBeInjected() {
71         NullLifecycle component = new NullLifecycle();
72         RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer JavaDoc());
73         InstanceComponentAdapter componentAdapter = new InstanceComponentAdapter(NullLifecycle.class, component, strategy);
74         PicoContainer pico = new DefaultPicoContainer();
75         componentAdapter.start(pico);
76         componentAdapter.stop(pico);
77         componentAdapter.dispose(pico);
78         assertEquals("<start<stop<dispose", strategy.recording());
79         componentAdapter.start(component);
80         componentAdapter.stop(component);
81         componentAdapter.dispose(component);
82         assertEquals("<start<stop<dispose<start<stop<dispose", strategy.recording());
83     }
84
85     public void testComponentAdapterCanIgnoreLifecycle() {
86         final Touchable touchable = new SimpleTouchable();
87         InstanceComponentAdapter componentAdapter = new InstanceComponentAdapter(Touchable.class, touchable);
88         PicoContainer pico = new DefaultPicoContainer();
89         componentAdapter.start(pico);
90         componentAdapter.stop(pico);
91         componentAdapter.dispose(pico);
92         componentAdapter.start(touchable);
93         componentAdapter.stop(touchable);
94         componentAdapter.dispose(touchable);
95     }
96
97     public void testGuardAgainstNullInstance() {
98         try {
99             new InstanceComponentAdapter(Map JavaDoc.class, null);
100             fail("should have barfed");
101         } catch (NullPointerException JavaDoc e) {
102             assertEquals("componentInstance cannot be null", e.getMessage());
103         }
104     }
105
106
107     /**
108      * {@inheritDoc}
109      * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#getComponentAdapterType()
110      */

111     protected Class JavaDoc getComponentAdapterType() {
112         return InstanceComponentAdapter.class;
113     }
114
115     /**
116      * {@inheritDoc}
117      * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#getComponentAdapterNature()
118      */

119     protected int getComponentAdapterNature() {
120         return super.getComponentAdapterNature() & ~(RESOLVING | VERIFYING | INSTANTIATING );
121     }
122
123     /**
124      * {@inheritDoc}
125      * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_verifyWithoutDependencyWorks(org.picocontainer.MutablePicoContainer)
126      */

127     protected ComponentAdapter prepDEF_verifyWithoutDependencyWorks(MutablePicoContainer picoContainer) {
128         return new InstanceComponentAdapter("foo", "bar");
129     }
130
131     /**
132      * {@inheritDoc}
133      * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_verifyDoesNotInstantiate(org.picocontainer.MutablePicoContainer)
134      */

135     protected ComponentAdapter prepDEF_verifyDoesNotInstantiate(
136             MutablePicoContainer picoContainer) {
137         return new InstanceComponentAdapter("Key", new Integer JavaDoc(4711));
138     }
139
140     /**
141      * {@inheritDoc}
142      * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_visitable()
143      */

144     protected ComponentAdapter prepDEF_visitable() {
145         return new InstanceComponentAdapter("Key", new Integer JavaDoc(4711));
146     }
147
148     /**
149      * {@inheritDoc}
150      * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepSER_isSerializable(org.picocontainer.MutablePicoContainer)
151      */

152     protected ComponentAdapter prepSER_isSerializable(MutablePicoContainer picoContainer) {
153         return new InstanceComponentAdapter("Key", new Integer JavaDoc(4711));
154     }
155
156     /**
157      * {@inheritDoc}
158      * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepSER_isXStreamSerializable(org.picocontainer.MutablePicoContainer)
159      */

160     protected ComponentAdapter prepSER_isXStreamSerializable(MutablePicoContainer picoContainer) {
161         return new InstanceComponentAdapter("Key", new Integer JavaDoc(4711));
162     }
163
164 }
165
Popular Tags