1 package org.apache.fulcrum.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 org.apache.avalon.framework.component.ComponentException; 23 import org.apache.avalon.framework.logger.ConsoleLogger; 24 25 import junit.framework.TestCase; 26 27 /** 28 * Base class for unit tests for components. This version doesn't load the 29 * container until the first request for a component. This allows the tester to 30 * populate the configurationFileName and roleFileName, possible one per test. 31 * 32 * This class uses JUnit 3. 33 * 34 * @see BaseUnit4Test 35 * 36 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> 37 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> 38 * @version $Id$ 39 */ 40 public class BaseUnitTest extends TestCase { 41 public static final String CONTAINER_ECM = "CONTAINER_ECM"; 42 public static final String CONTAINER_YAAFI = "CONTAINER_YAAFI"; 43 44 /** Key used in the context for defining the application root */ 45 public static final String COMPONENT_APP_ROOT = Container.COMPONENT_APP_ROOT; 46 47 /** Pick the default container to be Yaafi **/ 48 public static final String containerType = CONTAINER_YAAFI; 49 50 /** Use INFO for ConsoleLogger */ 51 public static final int defaultLogLevel = ConsoleLogger.LEVEL_INFO; 52 53 /** Container for the components */ 54 private Container container; 55 56 /** Setup our default configurationFileName */ 57 private String configurationFileName = "src/test/TestComponentConfig.xml"; 58 59 /** Setup our default roleFileName */ 60 private String roleFileName = "src/test/TestRoleConfig.xml"; 61 62 /** Setup our default parameterFileName */ 63 private String parameterFileName = null; 64 65 /** Set the log level (only works for YAAFI container) */ 66 private int logLevel = defaultLogLevel; 67 68 /** 69 * Gets the configuration file name for the container should use for this test. 70 * By default it is src/test/TestComponentConfig. 71 * 72 * @param configurationFileName the location of the config file 73 */ 74 protected void setConfigurationFileName(String configurationFileName) 75 { 76 this.configurationFileName = configurationFileName; 77 } 78 79 /** 80 * Override the role file name for the container should use for this test. By 81 * default it is src/test/TestRoleConfig. 82 * 83 * @param roleFileName location of the role file 84 */ 85 protected void setRoleFileName(String roleFileName) 86 { 87 this.roleFileName = roleFileName; 88 } 89 90 /** 91 * Set the console logger level 92 * 93 * @see org.apache.avalon.framework.logger.ConsoleLogger for debugging levels 94 * @param logLevel set valid logging level 95 */ 96 protected void setLogLevel(int logLevel) 97 { 98 this.logLevel = logLevel; 99 } 100 101 /** 102 * Constructor for test. 103 * 104 * @param testName name of the test being executed 105 */ 106 public BaseUnitTest(String testName) 107 { 108 super(testName); 109 } 110 111 /** 112 * Clean up after each test is run. 113 */ 114 protected void tearDown() 115 { 116 if (container != null) 117 { 118 container.dispose(); 119 } 120 container = null; 121 } 122 123 /** 124 * Gets the configuration file name for the container should use for this test. 125 * 126 * @return The filename of the configuration file 127 */ 128 protected String getConfigurationFileName() 129 { 130 return configurationFileName; 131 } 132 133 /** 134 * Gets the role file name for the container should use for this test. 135 * 136 * @return The filename of the role configuration file 137 */ 138 protected String getRoleFileName() 139 { 140 return roleFileName; 141 } 142 143 /** 144 * Gets the parameter file name for the container should use for this test. 145 * 146 * @return The filename of the role configuration file 147 */ 148 protected String getParameterFileName() 149 { 150 return parameterFileName; 151 } 152 153 /** 154 * Returns an instance of the named component. This method will also start the 155 * container if it has not been started already 156 * 157 * @param roleName Name of the role the component fills. 158 * @return instance of the component 159 * @throws ComponentException generic exception 160 */ 161 protected Object lookup(String roleName) throws ComponentException 162 { 163 if (container == null) { 164 if (containerType.equals(CONTAINER_ECM)) 165 { 166 container = new ECMContainer(); 167 } 168 else 169 { 170 container = new YAAFIContainer(logLevel); 171 } 172 container.startup(getConfigurationFileName(), getRoleFileName(), getParameterFileName()); 173 } 174 return container.lookup(roleName); 175 } 176 177 /** 178 * Helper method for converting to and from Merlin Unit TestCase. 179 * 180 * @param roleName the role name to resolve 181 * @return the component matching the role 182 * @throws ComponentException generic exception 183 */ 184 protected Object resolve(String roleName) throws ComponentException 185 { 186 return lookup(roleName); 187 } 188 189 /** 190 * Releases the component. 191 * 192 * @param component component to be released 193 */ 194 protected void release(Object component) 195 { 196 if (container != null) 197 { 198 container.release(component); 199 } 200 } 201 }