1 17 18 package org.apache.avalon.fortress.testcase; 19 20 import java.io.InputStream ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Modifier ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 28 import org.apache.avalon.excalibur.logger.LoggerManager; 29 import org.apache.avalon.fortress.impl.DefaultContainer; 30 import org.apache.avalon.fortress.impl.DefaultContainerManager; 31 import org.apache.avalon.fortress.util.FortressConfig; 32 import org.apache.avalon.fortress.util.OverridableContext; 33 import org.apache.avalon.framework.activity.Disposable; 34 import org.apache.avalon.framework.activity.Initializable; 35 import org.apache.avalon.framework.configuration.Configuration; 36 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 37 import org.apache.avalon.framework.container.ContainerUtil; 38 import org.apache.avalon.framework.context.Context; 39 import org.apache.avalon.framework.context.DefaultContext; 40 import org.apache.avalon.framework.logger.Logger; 41 import org.apache.avalon.framework.logger.LogKitLogger; 42 import org.apache.avalon.framework.service.ServiceException; 43 import org.apache.avalon.framework.service.ServiceManager; 44 45 import junit.framework.AssertionFailedError; 46 import junit.framework.TestCase; 47 import junit.framework.TestResult; 48 49 import org.apache.log.Hierarchy; 50 import org.apache.log.LogTarget; 51 import org.apache.log.Priority; 52 import org.apache.log.format.PatternFormatter; 53 import org.apache.log.output.io.StreamTarget; 54 55 230 public class FortressTestCase extends TestCase 231 { 232 233 private static final String FORMAT = 235 "%7.7{priority} %23.23{time:yyyy-MM-dd' 'HH:mm:ss.SSS} [%30.30{category}] " + 236 "(%{context}): %{message}\n%{throwable}"; 237 238 239 private Logger m_logger; 241 242 private DefaultContainerManager m_containerManager; 244 245 private DefaultContainer m_container; 247 248 private LoggerManager m_loggerManager; 249 private ServiceManager m_serviceManager; 250 251 252 private static HashMap m_tests = new HashMap (); 253 254 255 public FortressTestCase( final String name ) 256 { 257 super( name ); 258 259 ArrayList methodList = (ArrayList )FortressTestCase.m_tests.get( getClass() ); 260 261 Method [] methods = getClass().getMethods(); 262 263 if( null == methodList ) 264 { 265 methodList = new ArrayList ( methods.length ); 266 267 for( int i = 0; i < methods.length; i++ ) 268 { 269 String methodName = methods[ i ].getName(); 270 if( methodName.startsWith( "test" ) && 271 ( Modifier.isPublic( methods[ i ].getModifiers() ) ) && 272 ( methods[ i ].getReturnType().equals( Void.TYPE ) ) && 273 ( methods[ i ].getParameterTypes().length == 0 ) ) 274 { 275 methodList.add( methodName ); 276 } 277 } 278 279 FortressTestCase.m_tests.put( getClass(), methodList ); 280 } 281 } 282 283 protected final boolean hasService( final String key ) 284 { 285 return m_serviceManager.hasService( key ); 286 } 287 288 protected final Object lookup( final String key ) 289 throws ServiceException 290 { 291 return m_serviceManager.lookup( key ); 292 } 293 294 protected final void release( final Object object ) 295 { 296 m_serviceManager.release( object ); 297 } 298 299 300 protected Logger getLogger() 301 { 302 return m_logger; 303 } 304 305 306 309 public final void run( TestResult result ) 310 { 311 ArrayList methodList = (ArrayList )FortressTestCase.m_tests.get( getClass() ); 312 313 if( null == methodList || methodList.isEmpty() ) 314 { 315 return; } 317 318 setCurrentLogger( getBaseClassName( getClass() ) ); 320 321 try 322 { 323 324 prepare(); 325 326 if( this instanceof Initializable ) 327 { 328 ( (Initializable)this ).initialize(); 329 } 330 331 Iterator tests = methodList.iterator(); 332 333 while( tests.hasNext() ) 334 { 335 String methodName = (String )tests.next(); 336 setName( methodName ); 337 setCurrentLogger( methodName ); 338 339 if( getLogger().isDebugEnabled() ) 340 { 341 getLogger().debug( "" ); 342 getLogger().debug( "========================================" ); 343 getLogger().debug( " begin test: " + methodName ); 344 getLogger().debug( "========================================" ); 345 } 346 347 super.run( result ); 348 349 if( getLogger().isDebugEnabled() ) 350 { 351 getLogger().debug( "========================================" ); 352 getLogger().debug( " end test: " + methodName ); 353 getLogger().debug( "========================================" ); 354 getLogger().debug( "" ); 355 } 356 } 357 358 } 359 catch( Exception e ) 360 { 361 System.out.println( e ); 362 e.printStackTrace(); 363 result.addError( this, e ); 364 } 365 finally 366 { 367 done(); 368 369 if( this instanceof Disposable ) 370 { 371 try 372 { 373 ( (Disposable)this ).dispose(); 374 } 375 catch( Exception e ) 376 { 377 result.addFailure( this, new AssertionFailedError( "Disposal Error" ) ); 378 } 379 } 380 } 381 382 methodList.clear(); 383 FortressTestCase.m_tests.put( getClass(), methodList ); 384 } 385 386 392 protected void prepare() throws Exception 393 { 394 setCurrentLogger( "prepare" ); 395 396 final String resourceName = getClass().getName().replace( '.', '/' ) + ".xtest"; 397 URL resource = getClass().getClassLoader().getResource( resourceName ); 398 399 if( resource != null ) 400 { 401 getLogger().debug( "Loading resource " + resourceName ); 402 prepare( resource.openStream() ); 403 } 404 else 405 { 406 getLogger().warn( "Resource not found " + resourceName ); 407 } 408 } 409 410 421 protected final void prepare( final InputStream testconf ) 422 throws Exception 423 { 424 getLogger().debug( "FortressTestCase.initialize" ); 425 426 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 427 final Configuration conf = builder.build( testconf ); 428 429 String annotation = conf.getChild( "annotation" ).getValue( null ); 430 431 if( ( null != annotation ) && !( "".equals( annotation ) ) ) 432 { 433 m_logger.info( annotation ); 434 } 435 436 final FortressConfig config = new FortressConfig(); 437 config.setContainerClass( DefaultContainer.class ); 438 439 config.setContextDirectory( "./" ); 440 config.setWorkDirectory( "./" ); 441 config.setLoggerCategory( "fortress" ); 442 443 config.setLoggerManagerConfiguration( conf.getChild( "logger" ) ); 444 config.setRoleManagerConfiguration( conf.getChild( "roles" ) ); 445 config.setContainerConfiguration( conf.getChild( "components" ) ); 446 447 m_containerManager = new DefaultContainerManager( 448 setupContext( conf.getChild( "context", true ), config.getContext() ) ); 449 ContainerUtil.initialize( m_containerManager ); 450 451 m_container = (DefaultContainer) m_containerManager.getContainer(); 452 m_serviceManager = m_container.getServiceManager(); 453 m_loggerManager = (LoggerManager) m_serviceManager.lookup( LoggerManager.class.getName() ); 454 } 455 456 459 private void setCurrentLogger( String name ) 460 { 461 if( m_loggerManager == null ) 462 { 463 org.apache.log.Logger logger; 464 465 logger = Hierarchy.getDefaultHierarchy().getLoggerFor( name ); 467 logger.setPriority( Priority.INFO ); 468 469 PatternFormatter formatter = new PatternFormatter( FORMAT ); 470 StreamTarget target = new StreamTarget( System.out, formatter ); 471 logger.setLogTargets( new LogTarget[]{target} ); 472 m_logger = new LogKitLogger( logger ); 473 } 474 else 475 { 476 m_logger = m_loggerManager.getLoggerForCategory( "test." + name ); 477 } 478 } 479 480 487 private Context setupContext( final Configuration conf, final Context parentContext ) 488 throws Exception 489 { 490 final DefaultContext context = new OverridableContext( parentContext ); 493 final Configuration[] confs = conf.getChildren( "entry" ); 494 for( int i = 0; i < confs.length; i++ ) 495 { 496 final String key = confs[ i ].getAttribute( "name" ); 497 final String value = confs[ i ].getAttribute( "value", null ); 498 if( value == null ) 499 { 500 String clazz = confs[ i ].getAttribute( "class" ); 501 Object obj = getClass().getClassLoader().loadClass( clazz ).newInstance(); 502 context.put( key, obj ); 503 if( getLogger().isInfoEnabled() ) 504 { 505 getLogger().info( "FortressTestCase: added an instance of class " 506 + clazz + " to context entry " + key ); 507 } 508 509 } 510 else 511 { 512 context.put( key, value ); 513 if( getLogger().isInfoEnabled() ) 514 { 515 getLogger().info( "FortressTestCase: added value \"" + value 516 + "\" to context entry " + key ); 517 } 518 519 } 520 } 521 addContext( context ); 522 context.makeReadOnly(); 523 return context; 524 } 525 526 530 protected void addContext( DefaultContext context ) 531 { 532 } 533 534 535 538 private String getBaseClassName( Class clazz ) 539 { 540 String name = clazz.getName(); 541 int pos = name.lastIndexOf( '.' ); 542 if( pos >= 0 ) 543 { 544 name = name.substring( pos + 1 ); 545 } 546 return name; 547 } 548 549 550 553 private void done() 554 { 555 ContainerUtil.dispose( m_containerManager ); 556 } 557 558 } 559 | Popular Tags |