leif        02/02/25 03:18:14

  Modified:    src/java/org/apache/avalon/excalibur/testcase
                        ExcaliburTestCase.java
  Log:
  Added the ablity to fully control the loggers used by test cases and
  their components using only the config file.
  
  Revision  Changes    Path
  1.21      +187 -69   
jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/testcase/ExcaliburTestCase.java
  
  Index: ExcaliburTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/src/java/org/apache/avalon/excalibur/testcase/ExcaliburTestCase.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- ExcaliburTestCase.java    25 Feb 2002 04:50:18 -0000      1.20
  +++ ExcaliburTestCase.java    25 Feb 2002 11:18:14 -0000      1.21
  @@ -46,21 +46,16 @@
   /**
    * JUnit TestCase for Avalon Components
    * <p>
  - *   This class will extends the JUnit TestCase class to setup an environment
  - *   to easily test Avalon Components. The following methods and instance 
variables
  - *   are exposed for convenience testing:
  + *   This class extends the JUnit TestCase class to setup an environment 
which
  + *   makes it possible to easily test Avalon Components. The following 
methods
  + *   and instance variables are exposed for convenience testing:
    * </p>
    * <dl>
  - *   <dt>m_logPriority</dt>
  - *   <dd>
  - *     This variable contains the log priority of the default logger setup
  - *     at startup of the test. Overwrite the initialize() method to set a
  - *     different Priority.
  - *   </dd>
    *   <dt>manager</dt>
    *   <dd>
  - *     This instance variable contains a initialized ComponentManager
  - *     according to the test case configuration file (see below)
  + *     This instance variable contains an initialized ComponentManager which
  + *     can be used to lookup Components configured in the test configuration
  + *     file. (see below)
    *   </dd>
    *   <dt>getLogger()</dt>
    *   <dd>
  @@ -68,51 +63,53 @@
    *   </dd>
    * </dl>
    * <p>
  - *   The test case configuration file has the following structure:
  + *   The following test case configuration can be used as a basis for new 
tests.
  + *   Detailed are explanations of the configuration elements can be found 
after
  + *   the example.  The example will log all logger output to the console and 
to
  + *   a log file.
    * </p>
    * <pre>
    *   &lt;testcase&gt;
    *     &lt;annotation&gt;
    *       &lt;![CDATA[
  - *         &lt;title&gt;DataSourceComponent tests&lt;/title&gt;
  + *         &lt;title&gt;{Name of test}&lt;/title&gt;
    *         &lt;para&gt;
  - *           This is some &lt;emphasis&gt;text&lt;/emphasis&gt; to
  - *           describethe tests made herein. Unfortunately the
  - *           ConfigurationBuilder used to read the test case configuration
  - *           file doesn't allow to have mixed context. This should be fixed
  - *           in a future version .
  + *           {Description of test}
  + *           The configuration is specified in the file located in
  + *           &lt;parameter&gt;jakarta-avalon-excalibur/src/test/{path and 
name of conf file}.xtext&lt;/parameter&gt;.
    *         &lt;/para&gt;
    *       ]]&gt;
    *     &lt;/annotation&gt;
    *
  - *     &lt;logkit&gt;
  + *     &lt;logkit log-level="INFO"&gt;
    *       &lt;factories&gt;
  - *         &lt;factory type="file" 
class="org.apache.avalon.excalibur.logger.facatory.FileTargetFactory"/&gt;
  - *         &lt;factory type="property-filter" 
class="org.apache.avalon.excalibur.logger.facatory.FilterTargetFactory"/&gt;
  - *         &lt;factory type="servlet" 
class="org.apache.avalon.excalibur.logger.facatory.ServletTargetFactory"/&gt;
  - *         ...
  + *         &lt;factory type="stream" 
class="org.apache.avalon.excalibur.logger.factory.StreamTargetFactory"/&gt;
  + *         &lt;factory type="file" 
class="org.apache.avalon.excalibur.logger.factory.FileTargetFactory"/&gt;
    *       &lt;/factories&gt;
    *
    *       &lt;targets&gt;
  - *         &lt;file id="root"&gt;
  - *           &lt;filename&gt;dev/logs/main.log&lt;/filename&gt;
  + *         &lt;stream id="console"&gt;
  + *           &lt;stream&gt;System.out&lt;/stream&gt;
  + *           &lt;format type="avalon"&gt;
  + *             %7.7{priority} %19.19{time:yyyy-MM-dd' 'HH:mm:ss.SSS} 
[%30.30{category}] (%{context}): %{message}\n%{throwable}
  + *           &lt;/format&gt;
  + *         &lt;/stream&gt;
  + *         &lt;file id="log-file"&gt;
  + *           &lt;filename&gt;TEST-{full test class name}.log&lt;/filename&gt;
  + *           &lt;format type="avalon"&gt;
  + *             %7.7{priority} %19.19{time:yyyy-MM-dd' 'HH:mm:ss.SSS} 
[%30.30{category}] (%{context}): %{message}\n%{throwable}
  + *           &lt;/format&gt;
    *         &lt;/file&gt;
  - *         &lt;file id="classloader"&gt;
  - *           &lt;filename&gt;dev/logs/classloader.log&lt;/filename&gt;
  - *         &lt;/file&gt;
  - *         &lt;priority-filter id="servlet" log-level="ERROR"&gt;
  - *           &lt;servlet/&gt;
  - *         &lt;/priority-filter&gt;
    *       &lt;/targets&gt;
    *
    *       &lt;categories&gt;
  - *         &lt;category name="cocoon" log-level="INFO"&gt;
  - *           &lt;log-target id-ref="root"/&gt;
  - *           &lt;log-target id-ref="servlet"/&gt;
  - *
  - *           &lt;category name="classloader" log-level="DEBUG"&gt;
  - *             &lt;log-target id-ref="classloader"/&gt;
  - *           &lt;/category&gt;
  + *         &lt;category name="test" log-level="INFO"&gt;
  + *           &lt;log-target id-ref="console"/&gt;
  + *           &lt;log-target id-ref="log-file"/&gt;
  + *         &lt;/category&gt;
  + *         &lt;category name="jdbc" log-level="INFO"&gt;
  + *           &lt;log-target id-ref="console"/&gt;
  + *           &lt;log-target id-ref="log-file"/&gt;
    *         &lt;/category&gt;
    *       &lt;/categories&gt;
    *     &lt;/logkit&gt;
  @@ -132,7 +129,7 @@
    *
    *     &lt;components&gt;
    *       &lt;datasources&gt;
  - *         &lt;jdbc name="personell"&gt;
  + *         &lt;jdbc name="personell" logger="jdbc"&gt;
    *           &lt;pool-controller min="5" max="10"/&gt;
    *           &lt;jdbc name="personnel"/&gt;
    *           &lt;dburl&gt;jdbc:odbc:test&lt;/dburl&gt;
  @@ -144,24 +141,107 @@
    *     &lt;/components&gt;
    *   &lt;/testcase&gt;
    * </pre>
  + * <p>
  + * Element Explanation:
  + * <dl>
  + * <dt>testcase</dt>
  + * <dd>Defines a test case configuration.  Must contain one each of the
  + *  following elements: <code>annotation</code>, <code>logkit</code>,
  + *  <code>context</code>, <code>roles</code>, and <code>components</code>
  + *  </dd>.
  + *
  + * <dt>annotation</dt>
  + * <dd>Defines a test annotation.  This element should define a block of
  + *  XML enclosed within a CDATA element.  The XML should be made up of a
  + *  <code>title</code> element, naming the test, and a <code>para</code>
  + *  element which is used to describe the test.</dd>
  + *
  + * <dt>logkit</dt>
  + * <dd>Configures the logger used by the test cases and the components used
  + *  by the tests.  The <code>logkit</code> element takes two optional
  + *  attributes:
  + *      <dl>
  + *      <dt>logger</dt><dd>Uses to name the logger which is used to bootstrap
  + *       the LogKit logger.  (Defaults to <code>"lm"</code>)</dd>
  + *      <dt>log-level</dt><dd>Because the logger used by the LogKit must be
  + *       created before the Log Kit Manager is initialized, it must be fully
  + *       configured before the <code>logkit</code> element is parsed.  This
  + *       attribute allows the Log Kit's log priority to be set.  This log
  + *       level will also become the default for the Role Manager, Component
  + *       Manager, and all components if they do not have 
<code>category</code>
  + *       elements declated in the <code>logkit</code> element.
  + *       (Defaults to "INFO")</dd>
  + *      </dl>
  + *  The loggers used by test cases and components can be easily configured
  + *  from within this file.  The default test configuration, shown above,
  + *  includes a "test" category.  This category is used to configure the
  + *  default logger for all test cases.  If it is set to "DEBUG", then all
  + *  test debug logging will be enabled.  To enalble debug logging for a
  + *  single test case, a child category must be defined for the
  + *  "testCheckTotals" test case as follows:
  + *  <pre>
  + *       &lt;categories&gt;
  + *         &lt;category name="test" log-level="INFO"&gt;
  + *           &lt;log-target id-ref="console"/&gt;
  + *           &lt;log-target id-ref="log-file"/&gt;
  + *
  + *           &lt;category name="testCheckTotals" log-level="DEBUG"&gt;
  + *             &lt;log-target id-ref="console"/&gt;
  + *             &lt;log-target id-ref="log-file"/&gt;
  + *           &lt;/category&gt;
  + *         &lt;/category&gt;
  + *       &lt;/categories&gt;
  + *  </pre>
  + *  For general information on how to configure the LogKit Manager, please
  + *  refer to the Log Kit documentation.
  + * </dd>
  + *
  + * <dt>context</dt>
  + * <dd>Allows context properties to be set in the context passed to any
  + *  Contextualizable components.</dd>
  + *
  + * <dt>roles</dt>
  + * <dd>Roles configuration for the Components configured in the
  + *  <code>components</code> element.  The logger used by the RoleManager
  + *  can be configured using a <code>logger</code> attribute, which defaults
  + *  to "rm".  By default this logger will have the same log level and
  + *  formatting as the LogKit logger.  It can be configured by adding a
  + *  <code>category</code> within the <code>logkit</code> element.</dd>
  + *
  + * <dt>components</dt>
  + * <dd>Used to configure any Components used by the test cases.  The logger
  + *  used by the ComponentManager can be configured using a 
<code>logger</code>
  + *  attribute, which defaults to "cm".  By default this logger will have the
  + *  same log level and formatting as the LogKit logger.  It can be configured
  + *  by adding a <code>category</code> within the <code>logkit</code> element.
  + *  </dd>
  + *
  + * </dl>
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a>
  - * @version $Id: ExcaliburTestCase.java,v 1.20 2002/02/25 04:50:18 leif Exp $
  + * @version $Id: ExcaliburTestCase.java,v 1.21 2002/02/25 11:18:14 leif Exp $
    */
   public class ExcaliburTestCase
       extends TestCase
   {
       ///Format of default formatter
       private static final String  FORMAT =
  -        "%7.7{priority} %5.5{time}   [%8.8{category}] (%{context}): 
%{message}\\n%{throwable}";
  +        "%7.7{priority} %19.19{time:yyyy-MM-dd' 'HH:mm:ss.SSS} 
[%30.30{category}] (%{context}): %{message}\n%{throwable}";
  +        //"%7.7{priority} %5.5{time}   [%20.20{category}] (%{context}): 
%{message}\\n%{throwable}";
   
       //The default logger
       private Logger                      m_logger;
       private LogKitLogger                m_logEnabledLogger;
       private ExcaliburComponentManager   m_manager;
  +    private DefaultLogKitManager        m_logKitManager;
       private static HashMap              m_tests = new HashMap();
   
  -    protected Priority                  m_logPriority = Priority.DEBUG;
  +    /**
  +     * @deprecated The test log priority should now be set using the test 
category in the
  +     *             test case configuration file using the "test" category.
  +     */
  +    protected Priority                  m_logPriority = null;
  +    
       protected ComponentManager          manager;
   
       public ExcaliburTestCase( final String name )
  @@ -252,10 +332,8 @@
   
           Context context = setupContext( conf.getChild( "context" ) );
   
  -        m_manager = setupComponentManager( conf.getChild( "components" ),
  -                                           conf.getChild( "roles" ),
  -                                           conf.getChild( "logkit" ),
  -                                           context );
  +        setupManagers( conf.getChild( "components" ), conf.getChild( "roles" 
), 
  +                       conf.getChild( "logkit" ), context );
           manager = m_manager;
       }
   
  @@ -290,9 +368,6 @@
        */
       final public void run(TestResult result)
       {
  -        // Set the logger for the initialization phase.
  -        setCurrentLogger( getBaseClassName( getClass() ) );
  -        
           ArrayList methodList = (ArrayList) ExcaliburTestCase.m_tests.get( 
this.getClass() );
   
           if ( null == methodList || methodList.isEmpty() )
  @@ -300,6 +375,9 @@
               return; // The test was already run!  NOTE: this is a hack.
           }
   
  +        // Set the logger for the initialization phase.
  +        setCurrentLogger( getBaseClassName( getClass() ) );
  +        
           try
           {
               if (this instanceof Initializable)
  @@ -367,6 +445,35 @@
        */
       final private void setCurrentLogger( String name )
       {
  +        org.apache.log.Logger logger;
  +        if ( m_logKitManager == null )
  +        {
  +            // Logger for the portion of the configuration has been loaded.
  +            logger = Hierarchy.getDefaultHierarchy().getLoggerFor( name );
  +            if ( m_logPriority != null )
  +            {
  +                logger.setPriority( m_logPriority );
  +            }
  +            else
  +            {
  +                logger.setPriority( Priority.INFO );
  +            }
  +    
  +            PatternFormatter formatter = new PatternFormatter( FORMAT );
  +            StreamTarget target = new StreamTarget( System.out, formatter );
  +            logger.setLogTargets( new LogTarget[] { target } );
  +        } else {
  +            logger = m_logKitManager.getLogger( "test." + name );
  +        
  +            // Use the log priority in the conf file unless specified using 
the m_logPriority
  +            //  property. This must be kept around for backwards 
compatibility.
  +            if ( m_logPriority != null )
  +            {
  +                logger.setPriority( m_logPriority );
  +            }
  +        }
  +        
  +        /*
           //FIXME(GP): This method should setup a LogConfigurator and 
LogManager
           //           according to the configuration spec. not yet 
completed/implemented
           //           It will return a default logger for now.
  @@ -376,6 +483,7 @@
           final PatternFormatter formatter = new PatternFormatter( FORMAT );
           final StreamTarget target = new StreamTarget( System.out, formatter 
);
           logger.setLogTargets( new LogTarget[] { target } );
  +        */
   
           m_logger = logger;
           m_logEnabledLogger = new LogKitLogger( m_logger );
  @@ -423,32 +531,42 @@
       {
       }
   
  -    final private ExcaliburComponentManager setupComponentManager ( final 
Configuration confCM,
  -                                                                    final 
Configuration confRM,
  -                                                                    final 
Configuration confLM,
  -                                                                    final 
Context context )
  +    final private void setupManagers ( final Configuration confCM,
  +                                       final Configuration confRM,
  +                                       final Configuration confLM,
  +                                       final Context context )
           throws Exception
       {
  -        // Create a loggers for each of the managers so that they will get 
the correct logger.
  -        setCurrentLogger( "rm" );
  -        final DefaultRoleManager roleManager = new DefaultRoleManager();
  -        roleManager.setLogger( getLogger() );
  -        roleManager.configure( confRM );
  -
  -        setCurrentLogger( "lm" );
  -        final DefaultLogKitManager logKitManager = new 
DefaultLogKitManager();
  -        logKitManager.enableLogging( getLogEnabledLogger() );
  +        // Setup the log manager.  Get the logger name and log level from 
attributes
  +        //  in the <logkit> node
  +        String lmLoggerName = confLM.getAttribute( "logger", "lm" );
  +        String lmLogLevel = confLM.getAttribute( "log-level", "INFO" );
  +        Priority lmPriority = Priority.getPriorityForName( lmLogLevel );
  +        DefaultLogKitManager logKitManager = new DefaultLogKitManager();
  +        Logger lmLogger = Hierarchy.getDefaultHierarchy().getLoggerFor( 
lmLoggerName );
  +        lmLogger.setPriority( lmPriority );
  +        logKitManager.enableLogging( new LogKitLogger( lmLogger ) );
           logKitManager.contextualize( context );
           logKitManager.configure( confLM );
  -
  -        setCurrentLogger( "cm" );
  -        final ExcaliburComponentManager manager = new 
ExcaliburComponentManager();
  -        manager.setLogger( getLogger() );
  -        manager.setRoleManager( roleManager );
  -        manager.contextualize( context );
  +        Hierarchy h = logKitManager.getHierarchy();
  +        h.setDefaultPriority( lmPriority );
  +        m_logKitManager = logKitManager;
  +        
  +        // Setup the RoleManager
  +        String rmLoggerName = confRM.getAttribute( "logger", "rm" );
  +        DefaultRoleManager roleManager = new DefaultRoleManager();
  +        roleManager.setLogger( logKitManager.getLogger( rmLoggerName ) );
  +        roleManager.configure( confRM );
  +        
  +        // Set up the ComponentManager
  +        String cmLoggerName = confCM.getAttribute( "logger", "cm" );
  +        ExcaliburComponentManager manager = new ExcaliburComponentManager();
  +        manager.setLogger( logKitManager.getLogger( cmLoggerName ) );
           manager.setLogKitManager( logKitManager );
  +        manager.contextualize( context );
  +        manager.setRoleManager( roleManager );
           manager.configure( confCM );
           manager.initialize();
  -        return manager;
  +        m_manager = manager;
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to