1 8 package org.apache.avalon.excalibur.testcase; 9 10 import java.io.InputStream ; 11 12 import org.apache.avalon.framework.component.ComponentManager; 13 import org.apache.avalon.framework.configuration.Configuration; 14 import org.apache.avalon.framework.configuration.ConfigurationException; 15 import org.apache.avalon.framework.configuration.DefaultConfiguration; 16 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 17 import org.apache.avalon.framework.context.Context; 18 import org.apache.avalon.framework.context.ContextException; 19 import org.apache.avalon.framework.context.DefaultContext; 20 import org.apache.avalon.framework.activity.Initializable; 21 import org.apache.avalon.framework.activity.Disposable; 22 import org.apache.avalon.framework.logger.LogKitLogger; 23 import org.apache.log.Logger; 24 25 import org.apache.avalon.excalibur.component.DefaultRoleManager; 26 import org.apache.avalon.excalibur.component.ExcaliburComponentManager; 27 import org.apache.avalon.excalibur.logger.DefaultLogKitManager; 28 29 import org.apache.log.Hierarchy; 30 import org.apache.log.LogTarget; 31 import org.apache.log.Priority; 32 import org.apache.log.format.PatternFormatter; 33 import org.apache.log.output.io.StreamTarget; 34 35 import junit.framework.TestCase; 36 import junit.framework.TestResult; 37 import junit.framework.AssertionFailedError; 38 39 import java.lang.reflect.Method ; 40 import java.lang.reflect.Modifier ; 41 import java.util.ArrayList ; 42 import java.util.Iterator ; 43 import java.util.HashMap ; 44 import java.net.URL ; 45 46 151 public class ExcaliburTestCase 152 extends TestCase 153 { 154 private static final String FORMAT = 156 "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}"; 157 158 private Logger m_logger; 160 private LogKitLogger m_logEnabledLogger; 161 private ExcaliburComponentManager m_manager; 162 private static HashMap m_tests = new HashMap (); 163 164 protected Priority m_logPriority = Priority.DEBUG; 165 protected ComponentManager manager; 166 167 public ExcaliburTestCase( final String name ) 168 { 169 super( name ); 170 171 ArrayList methodList = (ArrayList ) ExcaliburTestCase.m_tests.get(this.getClass()); 172 173 Method [] methods = this.getClass().getDeclaredMethods(); 174 175 if ( null == methodList ) 176 { 177 methodList = new ArrayList ( methods.length ); 178 179 for ( int i = 0; i < methods.length; i++ ) 180 { 181 String methodName = methods[i].getName(); 182 if (methodName.startsWith( "test" ) && 183 ( Modifier.isPublic( methods[i].getModifiers() ) ) && 184 ( methods[i].getReturnType().equals( Void.TYPE ) ) && 185 ( methods[i].getParameterTypes().length == 0 ) ) 186 { 187 methodList.add(methodName); 188 } 189 } 190 191 ExcaliburTestCase.m_tests.put( this.getClass(), methodList ); 192 } 193 } 194 195 196 protected Logger getLogger() 197 { 198 if ( null == m_logger ) 199 { 200 m_logger = this.setupLogger(); 201 } 202 203 return m_logger; 204 } 205 206 207 protected LogKitLogger getLogEnabledLogger() 208 { 209 if ( null == m_logEnabledLogger ) 210 { 211 m_logEnabledLogger = new LogKitLogger( this.getLogger() ); 212 } 213 214 return m_logEnabledLogger; 215 } 216 217 223 protected void prepare() 224 throws Exception 225 { 226 final String resourceName = this.getClass().getName().replace( '.', '/' ) + ".xtest"; 227 URL resource = this.getClass().getClassLoader().getResource( resourceName ); 228 if ( resource != null ) { 229 getLogger().debug("Loading resource " + resourceName); 230 prepare( resource.openStream() ); 231 } 232 else 233 getLogger().debug("Resource not found " + resourceName); 234 } 235 236 247 protected final void prepare( final InputStream testconf ) 248 throws Exception 249 { 250 getLogger().debug( "ExcaliburTestCase.initialize" ); 251 252 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 253 final Configuration conf = builder.build( testconf ); 254 255 String annotation = conf.getChild( "annotation" ).getValue( null ); 256 257 if ( ( null != annotation ) || !( "".equals( annotation ) ) ) 258 { 259 m_logger.info( annotation ); 260 } 261 262 Context context = setupContext( conf.getChild( "context" ) ); 263 264 m_manager = setupComponentManager( conf.getChild( "components" ), 265 conf.getChild( "roles" ), 266 conf.getChild( "logkit" ), 267 context ); 268 manager = m_manager; 269 } 270 271 274 final private void done() 275 { 276 if ( null != m_manager ) 277 { 278 m_manager.dispose(); 279 } 280 281 m_manager = null; 282 } 283 284 287 final public void run(TestResult result) 288 { 289 ArrayList methodList = (ArrayList ) ExcaliburTestCase.m_tests.get( this.getClass() ); 290 291 if ( null == methodList || methodList.isEmpty() ) 292 { 293 return; } 295 296 try 297 { 298 if (this instanceof Initializable) 299 { 300 ((Initializable)this).initialize(); 301 } 302 303 this.prepare(); 304 305 Iterator tests = methodList.iterator(); 306 307 while ( tests.hasNext() ) 308 { 309 String methodName = (String ) tests.next(); 310 this.setName( methodName ); 311 m_logger = null; 312 313 if ( this.getLogger().isDebugEnabled() ) 314 { 315 this.getLogger().debug("Now running the following test: " + methodName); 316 } 317 318 super.run(result); 319 } 320 321 } 322 catch ( Exception e ) 323 { 324 result.addFailure(this, new AssertionFailedError()); 325 } 326 finally 327 { 328 this.done(); 329 330 if (this instanceof Disposable) 331 { 332 try 333 { 334 ((Disposable)this).dispose(); 335 } 336 catch( Exception e ) 337 { 338 result.addFailure(this, new AssertionFailedError("Disposal Error")); 339 } 340 } 341 } 342 343 methodList.clear(); 344 ExcaliburTestCase.m_tests.put( this.getClass(), methodList ); 345 } 346 347 350 final private Logger setupLogger() 351 { 352 final org.apache.log.Logger logger = Hierarchy.getDefaultHierarchy().getLoggerFor( getName() ); 356 logger.setPriority( m_logPriority ); 357 358 final PatternFormatter formatter = new PatternFormatter( FORMAT ); 359 final StreamTarget target = new StreamTarget( System.out, formatter ); 360 logger.setLogTargets( new LogTarget[] { target } ); 361 362 return logger; 363 } 364 365 372 final private Context setupContext( final Configuration conf ) 373 throws Exception 374 { 375 final DefaultContext context = new DefaultContext(); 378 final Configuration [] confs = conf.getChildren( "entry" ); 379 for (int i = 0; i < confs.length; i++) 380 { 381 final String key = confs[i].getAttribute( "name" ); 382 final String value = confs[i].getAttribute( "value", null ); 383 if (value == null) { 384 String clazz = confs[i].getAttribute( "class" ); 385 Object obj = this.getClass().getClassLoader().loadClass( clazz ).newInstance(); 386 context.put( key, obj ); 387 if (getLogger().isInfoEnabled()) 388 getLogger().info( "ExcaliburTestCase: added an instance of class " + clazz + " to context entry " + key); 389 } else { 390 context.put( key, value ); 391 if (getLogger().isInfoEnabled()) 392 getLogger().info( "ExcaliburTestCase: added value \"" + value + "\" to context entry " + key); 393 } 394 } 395 addContext( context ); 396 return( context ); 397 } 398 399 403 protected void addContext( DefaultContext context ) 404 { 405 } 406 407 final private ExcaliburComponentManager setupComponentManager ( final Configuration confCM, 408 final Configuration confRM, 409 final Configuration confLM, 410 final Context context ) 411 throws Exception 412 { 413 final DefaultRoleManager roleManager = new DefaultRoleManager(); 414 roleManager.setLogger( getLogger() ); 415 roleManager.configure( confRM ); 416 417 final DefaultLogKitManager logKitManager = new DefaultLogKitManager(); 418 logKitManager.enableLogging( getLogEnabledLogger() ); 419 logKitManager.contextualize( context ); 420 logKitManager.configure( confLM ); 421 422 final ExcaliburComponentManager manager = new ExcaliburComponentManager(); 423 manager.setLogger( getLogger() ); 424 manager.setRoleManager( roleManager ); 425 manager.contextualize( context ); 426 manager.setLogKitManager( logKitManager ); 427 manager.configure( confCM ); 428 manager.initialize(); 429 return manager; 430 } 431 } 432 | Popular Tags |