KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > yaafi > testcontainer > BaseUnitTest


1 package org.apache.fulcrum.yaafi.testcontainer;
2 /*
3  * ==================================================================== The Apache Software
4  * License, Version 1.1
5  *
6  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification, are permitted
9  * provided that the following conditions are met:
10  * 1. Redistributions of source code must retain the above copyright notice, this list of
11  * conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions and the following disclaimer in the documentation and/or other materials provided
14  * with the distribution.
15  * 3. The end-user documentation included with the redistribution, if any, must include the
16  * following acknowledgment: "This product includes software developed by the Apache Software
17  * Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the
18  * software itself, if and wherever such third-party acknowledgments normally appear.
19  * 4. The names "Apache" and "Apache Software Foundation" and "Apache Turbine" must not be used to
20  * endorse or promote products derived from this software without prior written permission. For
21  * written permission, please contact apache@apache.org.
22  * 5. Products derived from this software may not be called "Apache", "Apache Turbine", nor may
23  * "Apache" appear in their name, without prior written permission of the Apache Software
24  * Foundation.
25  *
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR
29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  * ====================================================================
35  *
36  * This software consists of voluntary contributions made by many individuals on behalf of the
37  * Apache Software Foundation. For more information on the Apache Software Foundation, please see
38  * <http://www.apache.org/> .
39  */

40 import junit.framework.TestCase;
41
42 import org.apache.avalon.framework.component.Component;
43 import org.apache.avalon.framework.component.ComponentException;
44
45 /**
46  * Base class for unit tests for components. This version doesn't load the container until the
47  * first request for a component. This allows the tester to populate the configurationFileName and
48  * roleFileName, possible one per test.
49  *
50  * @author <a HREF="mailto:epugh@upstate.com">Eric Pugh</a>
51  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
52  * @version $Id: BaseUnitTest.java,v 1.1 2004/10/19 22:03:50 epugh Exp $
53  */

54 public abstract class BaseUnitTest extends TestCase
55 {
56     /** Key used in the context for defining the application root */
57     public static String JavaDoc COMPONENT_APP_ROOT = Container.COMPONENT_APP_ROOT;
58     /** YaffiContainer for the components */
59     private Container container;
60     /** Setup our default configurationFileName */
61     private String JavaDoc configurationFileName = "src/test/TestComponentConfig.xml";
62     /** Setup our default roleFileName */
63     private String JavaDoc roleFileName = "src/test/TestRoleConfig.xml";
64     /** Setup our paramterFileName */
65     private String JavaDoc parameterFileName = "src/test/TestParameters.properties";
66
67     /**
68      * Gets the configuration file name for the container should use for this test. By default it
69      * is src/test/TestComponentConfig.
70      *
71      * @param configurationFileName
72      */

73     protected void setConfigurationFileName(String JavaDoc configurationFileName)
74     {
75         this.configurationFileName = configurationFileName;
76     }
77
78     /**
79      * Override the role file name for the container should use for this test. By default it is
80      * src/test/TestRoleConfig.
81      *
82      * @param roleFileName
83      */

84     protected void setRoleFileName(String JavaDoc roleFileName)
85     {
86         this.roleFileName = roleFileName;
87     }
88
89     /**
90      * Override the parameter file name for the container should use for this test. By default it is
91      * src/test/TestRoleConfig.
92      *
93      * @param roleFileName
94      */

95     protected void setParameterFileName(String JavaDoc parameterFileName)
96     {
97         this.parameterFileName = parameterFileName;
98     }
99
100     /**
101      * Constructor for test.
102      *
103      * @param testName name of the test being executed
104      */

105     public BaseUnitTest(String JavaDoc testName)
106     {
107         super(testName);
108     }
109     
110     /**
111      * Clean up after each test is run.
112      */

113     protected void tearDown() throws Exception JavaDoc
114     {
115         if (container != null)
116         {
117             container.dispose();
118         }
119         container = null;
120     }
121     /**
122      * Gets the configuration file name for the container should use for this test.
123      *
124      * @return The filename of the configuration file
125      */

126     protected String JavaDoc getConfigurationFileName()
127     {
128         return configurationFileName;
129     }
130     /**
131      * Gets the role file name for the container should use for this test.
132      *
133      * @return The filename of the role configuration file
134      */

135     protected String JavaDoc getRoleFileName()
136     {
137         return roleFileName;
138     }
139     /**
140      * Gets the parameter file name for the container should use for this test.
141      *
142      * @return The filename of the parameter file
143      */

144     protected String JavaDoc getParameterFileName()
145     {
146         return parameterFileName;
147     }
148     /**
149      * Returns an instance of the named component. Starts the container if it hasn't been started.
150      *
151      * @param roleName Name of the role the component fills.
152      * @throws ComponentException generic exception
153      */

154     protected Object JavaDoc lookup(String JavaDoc roleName) throws ComponentException
155     {
156         if (container == null)
157         {
158             container = new Container();
159             container.startup(getConfigurationFileName(), getRoleFileName(), getParameterFileName());
160         }
161         return container.lookup(roleName);
162     }
163     /**
164      * Releases the component
165      *
166      * @param component
167      */

168     protected void release(Component component)
169     {
170         if (container != null)
171         {
172             container.release(component);
173         }
174     }
175     /**
176      * Releases the component
177      *
178      * @param component
179      */

180     protected void release(Object JavaDoc component)
181     {
182         if (container != null)
183         {
184             container.release(component);
185         }
186     }
187 }
188
Popular Tags