KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > thread > AbstractTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.thread;
17
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.easymock.MockControl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import junit.framework.TestCase;
27
28
29 /**
30  * A {@link TestCase}with convenience methods to ease creation of Avalon mock
31  * classes.
32  *
33  * @author <a HREF="mailto:giacomo.at.apache.org">Giacomo Pati </a>
34  * @version $Id$
35  */

36 public class AbstractTestCase
37     extends TestCase
38 {
39     //~ Instance fields --------------------------------------------------------
40

41     /**
42      * The {@link List}of {@link MockControl}s creted by the
43      * <code>create...Control</code> methods
44      */

45     private List JavaDoc m_controls;
46
47     //~ Constructors -----------------------------------------------------------
48

49     /**
50      * Constructor
51      *
52      * @param name
53      */

54     public AbstractTestCase( String JavaDoc name )
55     {
56         super( name );
57     }
58
59     /**
60      * Constructor
61      */

62     public AbstractTestCase( )
63     {
64         super( );
65     }
66
67     //~ Methods ----------------------------------------------------------------
68

69     /**
70      * Create an empty list for {@link MockControl}s created by
71      * <code>create...Control</code> methods
72      *
73      * @throws Exception
74      */

75     protected void setUp( )
76         throws Exception JavaDoc
77     {
78         super.setUp( );
79         m_controls = new ArrayList JavaDoc( );
80     }
81
82     /**
83      * Create a mock {@link Configuration}instance that has a boolean value
84      *
85      * @param value The value to return
86      * @param defaultValue The value accepted as the default value
87      *
88      * @return A mock <code>Configuration</code>
89      */

90     protected Configuration createBooleanConfigMock( final boolean value,
91                                                      final boolean defaultValue )
92     {
93         final MockControl valueConfigControl =
94             createStrictControl( Configuration.class );
95         final Configuration valueConfig =
96             (Configuration)valueConfigControl.getMock( );
97         valueConfig.getValueAsBoolean( defaultValue );
98         valueConfigControl.setReturnValue( value );
99         valueConfigControl.replay( );
100
101         return valueConfig;
102     }
103
104     /**
105      * Create a mock {@link Configuration}instance that has a boolean value
106      *
107      * @param value The value to return
108      *
109      * @return A mock <code>Configuration</code>
110      *
111      * @throws ConfigurationException
112      */

113     protected Configuration createBooleanConfigMock( final boolean value )
114         throws ConfigurationException
115     {
116         final MockControl valueConfigControl =
117             createStrictControl( Configuration.class );
118         final Configuration valueConfig =
119             (Configuration)valueConfigControl.getMock( );
120         valueConfig.getValueAsBoolean( );
121         valueConfigControl.setReturnValue( value );
122         valueConfigControl.replay( );
123
124         return valueConfig;
125     }
126
127     /**
128      * Create a {@link Configuration}instance that has a child
129      *
130      * @param name The value accepted as the name for the child
131      * @param value The value to return
132      *
133      * @return A mock <code>Configuration</code>
134      */

135     protected Configuration createChildConfigMock( final String JavaDoc name,
136                                                    final Configuration value )
137     {
138         final MockControl childConfigControl =
139             createStrictControl( Configuration.class );
140         final Configuration childConfig =
141             (Configuration)childConfigControl.getMock( );
142         childConfig.getChild( name );
143         childConfigControl.setReturnValue( value );
144         childConfigControl.replay( );
145
146         return childConfig;
147     }
148
149     /**
150      * Create a {@link Configuration}instance that has a boolean value
151      *
152      * @param name The value accepted as the name for the children
153      * @param value The value to return
154      *
155      * @return A mock <code>Configuration</code>
156      */

157     protected Configuration createChildrenConfigMock( final String JavaDoc name,
158                                                       final Configuration [] value )
159     {
160         final MockControl childrenConfigControl =
161             createStrictControl( Configuration.class );
162         final Configuration childrenConfig =
163             (Configuration)childrenConfigControl.getMock( );
164         childrenConfig.getChildren( name );
165         childrenConfigControl.setReturnValue( value );
166         childrenConfigControl.replay( );
167
168         return childrenConfig;
169     }
170
171     /**
172      * Create a {@link Configuration}instance that has a int value
173      *
174      * @param value The value to return
175      * @param defaultValue The value accepted as the default value
176      *
177      * @return A mock <code>Configuration</code>
178      */

179     protected Configuration createIntegerConfigMock( final int value,
180                                                      final int defaultValue )
181     {
182         final MockControl valueConfigControl =
183             createStrictControl( Configuration.class );
184         final Configuration valueConfig =
185             (Configuration)valueConfigControl.getMock( );
186         valueConfig.getValueAsInteger( defaultValue );
187         valueConfigControl.setReturnValue( value );
188         valueConfigControl.replay( );
189
190         return valueConfig;
191     }
192
193     /**
194      * Create a {@link Configuration}instance that has a int value
195      *
196      * @param value The value to return
197      *
198      * @return A mock <code>Configuration</code>
199      *
200      * @throws ConfigurationException
201      */

202     protected Configuration createIntegerConfigMock( final int value )
203         throws ConfigurationException
204     {
205         final MockControl valueConfigControl =
206             createStrictControl( Configuration.class );
207         final Configuration valueConfig =
208             (Configuration)valueConfigControl.getMock( );
209         valueConfig.getValueAsInteger( );
210         valueConfigControl.setReturnValue( value );
211         valueConfigControl.replay( );
212
213         return valueConfig;
214     }
215
216     /**
217      * Create a {@link Configuration}instance that has a long value
218      *
219      * @param value The value to return
220      * @param defaultValue The value accepted as the default value
221      *
222      * @return A mock <code>Configuration</code>
223      */

224     protected Configuration createLongConfigMock( final long value,
225                                                   final long defaultValue )
226     {
227         final MockControl valueConfigControl =
228             createStrictControl( Configuration.class );
229         final Configuration valueConfig =
230             (Configuration)valueConfigControl.getMock( );
231         valueConfig.getValueAsLong( defaultValue );
232         valueConfigControl.setReturnValue( value );
233         valueConfigControl.replay( );
234
235         return valueConfig;
236     }
237
238     /**
239      * Create a {@link Configuration}instance that has a long value
240      *
241      * @param value The value to return
242      *
243      * @return A mock <code>Configuration</code>
244      *
245      * @throws ConfigurationException
246      */

247     protected Configuration createLongConfigMock( final long value )
248         throws ConfigurationException
249     {
250         final MockControl valueConfigControl =
251             createStrictControl( Configuration.class );
252         final Configuration valueConfig =
253             (Configuration)valueConfigControl.getMock( );
254         valueConfig.getValueAsLong( );
255         valueConfigControl.setReturnValue( value );
256         valueConfigControl.replay( );
257
258         return valueConfig;
259     }
260
261     /**
262      * Create a strict mock control
263      *
264      * @param clazz The interface class the mock object should represent
265      *
266      * @return The mock instance
267      */

268     protected MockControl createStrictControl( final Class JavaDoc clazz )
269     {
270         final MockControl control = MockControl.createStrictControl( clazz );
271         m_controls.add( control );
272
273         return control;
274     }
275
276     /**
277      * Create a {@link Configuration}instance that has a string value
278      *
279      * @param value The value to return
280      * @param defaultValue The value accepted as the default value
281      *
282      * @return A mock <code>Configuration</code>
283      */

284     protected Configuration createValueConfigMock( final String JavaDoc value,
285                                                    final String JavaDoc defaultValue )
286     {
287         final MockControl valueConfigControl =
288             createStrictControl( Configuration.class );
289         final Configuration valueConfig =
290             (Configuration)valueConfigControl.getMock( );
291         valueConfig.getValue( defaultValue );
292         valueConfigControl.setReturnValue( value );
293         valueConfigControl.replay( );
294
295         return valueConfig;
296     }
297
298     /**
299      * Create a {@link Configuration}instance that has a string value
300      *
301      * @param value The value to return
302      *
303      * @return A mock <code>Configuration</code>
304      *
305      * @throws ConfigurationException
306      */

307     protected Configuration createValueConfigMock( final String JavaDoc value )
308         throws ConfigurationException
309     {
310         final MockControl valueConfigControl =
311             createStrictControl( Configuration.class );
312         final Configuration valueConfig =
313             (Configuration)valueConfigControl.getMock( );
314         valueConfig.getValue( );
315         valueConfigControl.setReturnValue( value );
316         valueConfigControl.replay( );
317
318         return valueConfig;
319     }
320
321     /**
322      * @see TestCase#tearDown()
323      */

324     protected void tearDown( )
325         throws Exception JavaDoc
326     {
327         super.tearDown();
328         m_controls = null;
329     }
330
331     /**
332      * Verify all <code>MockCOntrol</code>s
333      */

334     protected void verify( )
335     {
336         for( Iterator JavaDoc i = m_controls.iterator( ); i.hasNext( ); )
337         {
338             final MockControl control = (MockControl)i.next( );
339             control.verify( );
340         }
341     }
342 }
343
Popular Tags