1 16 package org.apache.cocoon.core.container; 17 18 import java.io.InputStream ; 19 import java.net.URL ; 20 21 import junit.framework.TestCase; 22 23 import org.apache.avalon.excalibur.component.DefaultRoleManager; 24 import org.apache.avalon.excalibur.component.ExcaliburComponentManager; 25 import org.apache.avalon.excalibur.logger.LoggerManager; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 28 import org.apache.avalon.framework.container.ContainerUtil; 29 import org.apache.avalon.framework.context.Context; 30 import org.apache.avalon.framework.context.DefaultContext; 31 import org.apache.avalon.framework.logger.ConsoleLogger; 32 import org.apache.avalon.framework.logger.Logger; 33 import org.apache.avalon.framework.service.ServiceException; 34 import org.apache.avalon.framework.service.ServiceManager; 35 import org.apache.avalon.framework.service.WrapperServiceManager; 36 import org.apache.cocoon.util.Deprecation; 37 38 118 public class ContainerTestCase extends TestCase { 119 120 121 private Logger logger; 122 123 124 private ServiceManager manager; 125 126 127 protected Logger getLogger() { 128 return logger; 129 } 130 131 132 protected ServiceManager getManager() { 133 return this.manager; 134 } 135 136 139 protected void setUp() throws Exception { 140 super.setUp(); 141 142 String level = System.getProperty("junit.test.loglevel", "" + ConsoleLogger.LEVEL_WARN); 143 this.logger = new ConsoleLogger(Integer.parseInt(level)); 144 Deprecation.setLogger(this.logger); 145 prepare(); 146 } 147 148 154 protected void prepare() 155 throws Exception { 156 final String resourceName = getClass().getName().replace( '.', '/' ) + ".xtest"; 157 URL resource = getClass().getClassLoader().getResource( resourceName ); 158 159 if (resource != null) { 160 getLogger().debug("Loading resource " + resourceName); 161 prepare(resource.openStream()); 162 } else { 163 getLogger().debug("Resource not found " + resourceName); 164 } 165 } 166 167 178 protected final void prepare(final InputStream testconf) 179 throws Exception { 180 if (getLogger().isDebugEnabled()) { 181 getLogger().debug("ContainerTestCase.initialize"); 182 } 183 184 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 185 final Configuration conf = builder.build(testconf); 186 187 Context context = this.setupContext(conf.getChild("context")); 188 189 setupManagers(conf.getChild("components"), 190 conf.getChild("roles"), 191 context); 192 } 193 194 197 protected void tearDown() throws Exception { 198 done(); 199 super.tearDown(); 200 } 201 202 205 final private void done() { 206 if (manager != null) { 207 ContainerUtil.dispose(manager); 208 manager = null; 209 } 210 } 211 212 219 final private Context setupContext( final Configuration conf ) 220 throws Exception { 221 final DefaultContext context = new DefaultContext(); 222 final Configuration[] confs = conf.getChildren( "entry" ); 223 for (int i = 0; i < confs.length; i++) { 224 final String key = confs[i].getAttribute("name"); 225 final String value = confs[i].getAttribute("value", null); 226 if (value == null) { 227 String clazz = confs[i].getAttribute("class"); 228 Object obj = getClass().getClassLoader().loadClass(clazz).newInstance(); 229 context.put(key, obj); 230 if (getLogger().isInfoEnabled()) { 231 getLogger().info("ContainerTestCase: added an instance of class " + clazz + " to context entry " + key); 232 } 233 } else { 234 context.put(key, value); 235 if (getLogger().isInfoEnabled()) { 236 getLogger().info("ContainerTestCase: added value \"" + value + "\" to context entry " + key); 237 } 238 } 239 } 240 addContext(context); 241 return context ; 242 } 243 244 248 protected void addContext(DefaultContext context) { 249 } 250 251 final private void setupManagers(final Configuration confCM, 252 final Configuration confRM, 253 final Context context) 254 throws Exception { 255 DefaultRoleManager roleManager = new DefaultRoleManager(); 257 roleManager.enableLogging(getLogger()); 258 roleManager.configure(confRM); 259 260 ExcaliburComponentManager ecManager = new ExcaliburComponentManager(); 262 ecManager.enableLogging(getLogger()); 263 ecManager.contextualize(context); 264 ecManager.setRoleManager(roleManager); 265 ecManager.setLoggerManager(new DefaultLoggerManager(getLogger())); 266 ecManager.configure(confCM); 267 ecManager.initialize(); 268 this.manager = new WrapperServiceManager(ecManager); 269 } 270 271 protected final Object lookup(final String key) 272 throws ServiceException { 273 return manager.lookup(key); 274 } 275 276 protected final void release(final Object object) { 277 manager.release(object); 278 } 279 280 protected static class DefaultLoggerManager implements LoggerManager { 281 private Logger logger; 282 283 public DefaultLoggerManager(Logger logger) { 284 this.logger = logger; 285 } 286 289 public Logger getDefaultLogger() { 290 return this.logger; 291 } 292 295 public Logger getLoggerForCategory(String arg0) { 296 return this.logger; 297 } 298 } 299 } 300 | Popular Tags |