KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.picocontainer.defaults;
9
10 import org.jmock.Mock;
11 import org.jmock.MockObjectTestCase;
12 import org.picocontainer.ComponentAdapter;
13 import org.picocontainer.PicoContainer;
14 import org.picocontainer.MutablePicoContainer;
15 import org.picocontainer.testmodel.SimpleTouchable;
16 import org.picocontainer.testmodel.Touchable;
17
18
19 /**
20  * @author Mauro Talevi
21  * @version $Revision: $
22  */

23 public class CachingComponentAdapterTestCase extends MockObjectTestCase {
24
25     public void testComponentIsNotStartedWhenCachedAndCanBeStarted() {
26         CachingComponentAdapter adapter = new CachingComponentAdapter(
27                 mockComponentAdapterSupportingLifecycleStrategy(true, false, false));
28         PicoContainer pico = new DefaultPicoContainer();
29         adapter.getComponentInstance(pico);
30         adapter.start(pico);
31     }
32
33     public void testComponentCanBeStartedAgainAfterBeingStopped() {
34         CachingComponentAdapter adapter = new CachingComponentAdapter(
35                 mockComponentAdapterSupportingLifecycleStrategy(true, true, false));
36         PicoContainer pico = new DefaultPicoContainer();
37         adapter.start(pico);
38         Object JavaDoc instanceAfterFirstStart = adapter.getComponentInstance(pico);
39         adapter.stop(pico);
40         adapter.start(pico);
41         Object JavaDoc instanceAfterSecondStart = adapter.getComponentInstance(pico);
42         assertSame(instanceAfterFirstStart, instanceAfterSecondStart);
43     }
44
45     public void testComponentCannotBeStartedIfDisposed() {
46         CachingComponentAdapter adapter = new CachingComponentAdapter(
47                 mockComponentAdapterSupportingLifecycleStrategy(false, false, true));
48         PicoContainer pico = new DefaultPicoContainer();
49         adapter.dispose(pico);
50         try {
51             adapter.start(pico);
52             fail("IllegalStateException expected");
53         } catch (Exception JavaDoc e) {
54             assertEquals("Already disposed", e.getMessage());
55         }
56     }
57
58     public void testComponentCannotBeStartedIfAlreadyStarted() {
59         CachingComponentAdapter adapter = new CachingComponentAdapter(
60                 mockComponentAdapterSupportingLifecycleStrategy(true, false, false));
61         PicoContainer pico = new DefaultPicoContainer();
62         adapter.start(pico);
63         try {
64             adapter.start(pico);
65             fail("IllegalStateException expected");
66         } catch (Exception JavaDoc e) {
67             assertEquals("Already started", e.getMessage());
68         }
69     }
70
71     public void testComponentCannotBeStoppeddIfDisposed() {
72         CachingComponentAdapter adapter = new CachingComponentAdapter(
73                 mockComponentAdapterSupportingLifecycleStrategy(false, false, true));
74         PicoContainer pico = new DefaultPicoContainer();
75         adapter.dispose(pico);
76         try {
77             adapter.stop(pico);
78             fail("IllegalStateException expected");
79         } catch (Exception JavaDoc e) {
80             assertEquals("Already disposed", e.getMessage());
81         }
82     }
83
84     public void testComponentCannotBeStoppedIfNotStarted() {
85         CachingComponentAdapter adapter = new CachingComponentAdapter(
86                 mockComponentAdapterSupportingLifecycleStrategy(true, true, false));
87         PicoContainer pico = new DefaultPicoContainer();
88         adapter.start(pico);
89         adapter.stop(pico);
90         try {
91         adapter.stop(pico);
92             fail("IllegalStateException expected");
93         } catch (Exception JavaDoc e) {
94             assertEquals("Not started", e.getMessage());
95         }
96     }
97
98     public void testComponentCannotBeDisposedIfAlreadyDisposed() {
99         CachingComponentAdapter adapter = new CachingComponentAdapter(
100                 mockComponentAdapterSupportingLifecycleStrategy(true, true, true));
101         PicoContainer pico = new DefaultPicoContainer();
102         adapter.start(pico);
103         adapter.stop(pico);
104         adapter.dispose(pico);
105         try {
106             adapter.dispose(pico);
107             fail("IllegalStateException expected");
108         } catch (Exception JavaDoc e) {
109             assertEquals("Already disposed", e.getMessage());
110         }
111     }
112
113     public void testComponentIsStoppedAndDisposedIfStartedWhenFlushed() {
114         CachingComponentAdapter adapter = new CachingComponentAdapter(
115                 mockComponentAdapterSupportingLifecycleStrategy(true, true, true));
116         PicoContainer pico = new DefaultPicoContainer();
117         adapter.start(pico);
118         adapter.flush();
119     }
120
121     public void testComponentIsNotStoppedAndDisposedWhenFlushedIfNotStarted() {
122         CachingComponentAdapter adapter = new CachingComponentAdapter(
123                 mockComponentAdapterSupportingLifecycleStrategy(false, false, false));
124         adapter.flush();
125     }
126
127     public void testComponentIsNotStoppedAndDisposedWhenFlushedIfDelegateDoesNotSupportLifecycle() {
128         CachingComponentAdapter adapter = new CachingComponentAdapter(
129                 mockComponentAdapterNotSupportingLifecycleStrategy());
130         adapter.flush();
131     }
132
133     public void testLifecycleIsIgnoredIfDelegateDoesNotSupportIt() {
134         CachingComponentAdapter adapter = new CachingComponentAdapter(
135                 mockComponentAdapterNotSupportingLifecycleStrategy());
136         PicoContainer pico = new DefaultPicoContainer();
137         adapter.start(pico);
138         adapter.stop(pico);
139         adapter.dispose(pico);
140     }
141
142     public void testCanStopAComponentThatWasNeverStartedBecauseItHasNoLifecycle() {
143         MutablePicoContainer pico = new DefaultPicoContainer();
144
145         pico.registerComponentImplementation(StringBuffer JavaDoc.class);
146
147         pico.start();
148
149         assertNotNull(pico.getComponentInstance(StringBuffer JavaDoc.class));
150
151         pico.stop();
152         pico.dispose();
153     }
154
155     private ComponentAdapter mockComponentAdapterNotSupportingLifecycleStrategy() {
156         Mock mock = mock(ComponentAdapter.class);
157         return (ComponentAdapter)mock.proxy();
158     }
159
160     private ComponentAdapter mockComponentAdapterSupportingLifecycleStrategy(
161             boolean start, boolean stop, boolean dispose) {
162         boolean hasLifecycle = start || stop || dispose;
163         Mock mock = mock(ComponentAdapterSupportingLifecycleStrategy.class);
164         if (start) {
165             mock.expects(atLeastOnce()).method("start").with(isA(Touchable.class));
166         }
167         if (stop) {
168             mock.expects(once()).method("stop").with(isA(Touchable.class));
169         }
170         if (dispose) {
171             mock.expects(once()).method("dispose").with(isA(Touchable.class));
172         }
173         if (hasLifecycle) {
174             mock.stubs().method("getComponentInstance").with(isA(PicoContainer.class)).will(
175                     returnValue(new SimpleTouchable()));
176         }
177         mock.expects(once()).method("getComponentImplementation").will(
178                 returnValue(SimpleTouchable.class));
179         mock.expects(once()).method("hasLifecycle").with(same(SimpleTouchable.class)).will(
180                 returnValue(hasLifecycle));
181         return (ComponentAdapter)mock.proxy();
182     }
183
184     static interface ComponentAdapterSupportingLifecycleStrategy extends ComponentAdapter,
185             LifecycleStrategy {
186     }
187 }
Popular Tags