001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.thread;
018:
019: import org.apache.avalon.framework.configuration.Configuration;
020: import org.apache.avalon.framework.configuration.ConfigurationException;
021: import org.easymock.MockControl;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import junit.framework.TestCase;
028:
029: /**
030: * A {@link TestCase}with convenience methods to ease creation of Avalon mock
031: * classes.
032: *
033: * @author <a href="mailto:giacomo.at.apache.org">Giacomo Pati </a>
034: * @version $Id$
035: */
036: public class AbstractTestCase extends TestCase {
037: //~ Instance fields --------------------------------------------------------
038:
039: /**
040: * The {@link List}of {@link MockControl}s creted by the
041: * <code>create...Control</code> methods
042: */
043: private List m_controls;
044:
045: //~ Constructors -----------------------------------------------------------
046:
047: /**
048: * Constructor
049: *
050: * @param name
051: */
052: public AbstractTestCase(String name) {
053: super (name);
054: }
055:
056: /**
057: * Constructor
058: */
059: public AbstractTestCase() {
060: super ();
061: }
062:
063: //~ Methods ----------------------------------------------------------------
064:
065: /**
066: * Create an empty list for {@link MockControl}s created by
067: * <code>create...Control</code> methods
068: *
069: * @throws Exception
070: */
071: protected void setUp() throws Exception {
072: super .setUp();
073: m_controls = new ArrayList();
074: }
075:
076: /**
077: * Create a mock {@link Configuration}instance that has a boolean value
078: *
079: * @param value The value to return
080: * @param defaultValue The value accepted as the default value
081: *
082: * @return A mock <code>Configuration</code>
083: */
084: protected Configuration createBooleanConfigMock(
085: final boolean value, final boolean defaultValue) {
086: final MockControl valueConfigControl = createStrictControl(Configuration.class);
087: final Configuration valueConfig = (Configuration) valueConfigControl
088: .getMock();
089: valueConfig.getValueAsBoolean(defaultValue);
090: valueConfigControl.setReturnValue(value);
091: valueConfigControl.replay();
092:
093: return valueConfig;
094: }
095:
096: /**
097: * Create a mock {@link Configuration}instance that has a boolean value
098: *
099: * @param value The value to return
100: *
101: * @return A mock <code>Configuration</code>
102: *
103: * @throws ConfigurationException
104: */
105: protected Configuration createBooleanConfigMock(final boolean value)
106: throws ConfigurationException {
107: final MockControl valueConfigControl = createStrictControl(Configuration.class);
108: final Configuration valueConfig = (Configuration) valueConfigControl
109: .getMock();
110: valueConfig.getValueAsBoolean();
111: valueConfigControl.setReturnValue(value);
112: valueConfigControl.replay();
113:
114: return valueConfig;
115: }
116:
117: /**
118: * Create a {@link Configuration}instance that has a child
119: *
120: * @param name The value accepted as the name for the child
121: * @param value The value to return
122: *
123: * @return A mock <code>Configuration</code>
124: */
125: protected Configuration createChildConfigMock(final String name,
126: final Configuration value) {
127: final MockControl childConfigControl = createStrictControl(Configuration.class);
128: final Configuration childConfig = (Configuration) childConfigControl
129: .getMock();
130: childConfig.getChild(name);
131: childConfigControl.setReturnValue(value);
132: childConfigControl.replay();
133:
134: return childConfig;
135: }
136:
137: /**
138: * Create a {@link Configuration}instance that has a boolean value
139: *
140: * @param name The value accepted as the name for the children
141: * @param value The value to return
142: *
143: * @return A mock <code>Configuration</code>
144: */
145: protected Configuration createChildrenConfigMock(final String name,
146: final Configuration[] value) {
147: final MockControl childrenConfigControl = createStrictControl(Configuration.class);
148: final Configuration childrenConfig = (Configuration) childrenConfigControl
149: .getMock();
150: childrenConfig.getChildren(name);
151: childrenConfigControl.setReturnValue(value);
152: childrenConfigControl.replay();
153:
154: return childrenConfig;
155: }
156:
157: /**
158: * Create a {@link Configuration}instance that has a int value
159: *
160: * @param value The value to return
161: * @param defaultValue The value accepted as the default value
162: *
163: * @return A mock <code>Configuration</code>
164: */
165: protected Configuration createIntegerConfigMock(final int value,
166: final int defaultValue) {
167: final MockControl valueConfigControl = createStrictControl(Configuration.class);
168: final Configuration valueConfig = (Configuration) valueConfigControl
169: .getMock();
170: valueConfig.getValueAsInteger(defaultValue);
171: valueConfigControl.setReturnValue(value);
172: valueConfigControl.replay();
173:
174: return valueConfig;
175: }
176:
177: /**
178: * Create a {@link Configuration}instance that has a int value
179: *
180: * @param value The value to return
181: *
182: * @return A mock <code>Configuration</code>
183: *
184: * @throws ConfigurationException
185: */
186: protected Configuration createIntegerConfigMock(final int value)
187: throws ConfigurationException {
188: final MockControl valueConfigControl = createStrictControl(Configuration.class);
189: final Configuration valueConfig = (Configuration) valueConfigControl
190: .getMock();
191: valueConfig.getValueAsInteger();
192: valueConfigControl.setReturnValue(value);
193: valueConfigControl.replay();
194:
195: return valueConfig;
196: }
197:
198: /**
199: * Create a {@link Configuration}instance that has a long value
200: *
201: * @param value The value to return
202: * @param defaultValue The value accepted as the default value
203: *
204: * @return A mock <code>Configuration</code>
205: */
206: protected Configuration createLongConfigMock(final long value,
207: final long defaultValue) {
208: final MockControl valueConfigControl = createStrictControl(Configuration.class);
209: final Configuration valueConfig = (Configuration) valueConfigControl
210: .getMock();
211: valueConfig.getValueAsLong(defaultValue);
212: valueConfigControl.setReturnValue(value);
213: valueConfigControl.replay();
214:
215: return valueConfig;
216: }
217:
218: /**
219: * Create a {@link Configuration}instance that has a long value
220: *
221: * @param value The value to return
222: *
223: * @return A mock <code>Configuration</code>
224: *
225: * @throws ConfigurationException
226: */
227: protected Configuration createLongConfigMock(final long value)
228: throws ConfigurationException {
229: final MockControl valueConfigControl = createStrictControl(Configuration.class);
230: final Configuration valueConfig = (Configuration) valueConfigControl
231: .getMock();
232: valueConfig.getValueAsLong();
233: valueConfigControl.setReturnValue(value);
234: valueConfigControl.replay();
235:
236: return valueConfig;
237: }
238:
239: /**
240: * Create a strict mock control
241: *
242: * @param clazz The interface class the mock object should represent
243: *
244: * @return The mock instance
245: */
246: protected MockControl createStrictControl(final Class clazz) {
247: final MockControl control = MockControl
248: .createStrictControl(clazz);
249: m_controls.add(control);
250:
251: return control;
252: }
253:
254: /**
255: * Create a {@link Configuration}instance that has a string value
256: *
257: * @param value The value to return
258: * @param defaultValue The value accepted as the default value
259: *
260: * @return A mock <code>Configuration</code>
261: */
262: protected Configuration createValueConfigMock(final String value,
263: final String defaultValue) {
264: final MockControl valueConfigControl = createStrictControl(Configuration.class);
265: final Configuration valueConfig = (Configuration) valueConfigControl
266: .getMock();
267: valueConfig.getValue(defaultValue);
268: valueConfigControl.setReturnValue(value);
269: valueConfigControl.replay();
270:
271: return valueConfig;
272: }
273:
274: /**
275: * Create a {@link Configuration}instance that has a string value
276: *
277: * @param value The value to return
278: *
279: * @return A mock <code>Configuration</code>
280: *
281: * @throws ConfigurationException
282: */
283: protected Configuration createValueConfigMock(final String value)
284: throws ConfigurationException {
285: final MockControl valueConfigControl = createStrictControl(Configuration.class);
286: final Configuration valueConfig = (Configuration) valueConfigControl
287: .getMock();
288: valueConfig.getValue();
289: valueConfigControl.setReturnValue(value);
290: valueConfigControl.replay();
291:
292: return valueConfig;
293: }
294:
295: /**
296: * @see TestCase#tearDown()
297: */
298: protected void tearDown() throws Exception {
299: super .tearDown();
300: m_controls = null;
301: }
302:
303: /**
304: * Verify all <code>MockCOntrol</code>s
305: */
306: protected void verify() {
307: for (Iterator i = m_controls.iterator(); i.hasNext();) {
308: final MockControl control = (MockControl) i.next();
309: control.verify();
310: }
311: }
312: }
|