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