1 package org.apache.fulcrum.yaafi.testcontainer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }