View Javadoc

1   package org.apache.fulcrum.testcontainer;
2   /*
3    * Copyright 2004 The Apache Software Foundation.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License")
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  import junit.framework.TestCase;
18  
19  import org.apache.avalon.framework.component.ComponentException;
20  /**
21   * Base class for unit tests for components. This version doesn't load the container until the
22   * first request for a component. This allows the tester to populate the configurationFileName and
23   * roleFileName, possible one per test.
24   * 
25   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
26   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
27   * @version $Id: BaseUnitTest.java 223140 2004-11-01 13:29:25Z epugh $
28   */
29  public class BaseUnitTest extends TestCase
30  {
31    
32      public static final String CONTAINER_ECM="CONTAINER_ECM";
33      public static final String CONTAINER_YAAFI="CONTAINER_YAAFI";
34      /** Key used in the context for defining the application root */
35      public static String COMPONENT_APP_ROOT = Container.COMPONENT_APP_ROOT;
36  
37      /** Pick the default container to be Yaafi **/
38      public static String containerType = CONTAINER_YAAFI;
39      /** Container for the components */
40      private Container container;
41      /** Setup our default configurationFileName */
42      private String configurationFileName = "src/test/TestComponentConfig.xml";
43      /** Setup our default roleFileName */
44      private String roleFileName = "src/test/TestRoleConfig.xml";
45      /** Setup our default parameterFileName */
46      private String parameterFileName = null;
47      
48      /**
49  	 * Gets the configuration file name for the container should use for this test. By default it
50  	 * is src/test/TestComponentConfig.
51  	 * 
52  	 * @param configurationFileName
53  	 */
54      protected void setConfigurationFileName(String configurationFileName)
55      {
56          this.configurationFileName = configurationFileName;
57      }
58  
59      /**
60  	 * Override the role file name for the container should use for this test. By default it is
61  	 * src/test/TestRoleConfig.
62  	 * 
63  	 * @param roleFileName
64  	 */
65      protected void setRoleFileName(String roleFileName)
66      {
67          this.roleFileName = roleFileName;
68      }
69  
70      /**
71  	 * Constructor for test.
72  	 * 
73  	 * @param testName name of the test being executed
74  	 */
75      public BaseUnitTest(String testName)
76      {
77          super(testName);
78      }
79      
80      /**
81  	 * Clean up after each test is run.
82  	 */
83      protected void tearDown()
84      {
85          if (container != null)
86          {
87              container.dispose();
88          }
89          container = null;
90      }
91      /**
92  	 * Gets the configuration file name for the container should use for this test.
93  	 * 
94  	 * @return The filename of the configuration file
95  	 */
96      protected String getConfigurationFileName()
97      {
98          return configurationFileName;
99      }
100     /**
101 	 * Gets the role file name for the container should use for this test.
102 	 * 
103 	 * @return The filename of the role configuration file
104 	 */
105     protected String getRoleFileName()
106     {
107         return roleFileName;
108     }
109     /**
110      * Gets the parameter file name for the container should use for this test.
111      * 
112      * @return The filename of the role configuration file
113      */
114     protected String getParameterFileName()
115     {
116         return parameterFileName;
117     }    
118     /**
119 	 * Returns an instance of the named component. Starts the container if it hasn't been started.
120 	 * 
121 	 * @param roleName Name of the role the component fills.
122 	 * @throws ComponentException generic exception
123 	 */
124     protected Object lookup(String roleName) throws ComponentException
125     {
126         if (container == null)
127         {
128             if(containerType.equals(CONTAINER_ECM)){
129                 container = new ECMContainer();
130             }
131             else {
132                 container = new YAAFIContainer();
133             }
134             container.startup(getConfigurationFileName(), getRoleFileName(),getParameterFileName());
135         }
136         return container.lookup(roleName);
137     }
138     
139     /**
140      * Helper method for converting to and from Merlin Unit TestCase.
141      * @param roleName
142      * @return
143      * @throws ComponentException
144      */
145     protected Object resolve(String roleName) throws ComponentException
146     {
147         return lookup(roleName);
148     }    
149     /**
150 	 * Releases the component
151 	 * 
152 	 * @param component
153 	 */
154     protected void release(Object component)
155     {
156         if (container != null)
157         {
158             container.release(component);
159         }
160     }
161 }