ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon TestCaseProjectBuilder.java TestCaseTargetTask.java AbstractTaskTestCase.java
Date Tue, 04 Jun 2002 12:45:47 GMT
adammurdoch    2002/06/04 05:45:47

  Modified:    framework/src/test/org/apache/myrmidon
                        AbstractTaskTestCase.java
  Added:       framework/src/test/org/apache/myrmidon
                        TestCaseProjectBuilder.java TestCaseTargetTask.java
  Log:
  Reimplemented AbstractTaskTestCase so that it uses EmbeddedAnt
  and a simplified project builder (this time for real).
  
  Revision  Changes    Path
  1.5       +86 -79    jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/AbstractTaskTestCase.java
  
  Index: AbstractTaskTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/AbstractTaskTestCase.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- AbstractTaskTestCase.java	18 May 2002 05:05:31 -0000	1.4
  +++ AbstractTaskTestCase.java	4 Jun 2002 12:45:47 -0000	1.5
  @@ -8,28 +8,18 @@
   package org.apache.myrmidon;
   
   import java.io.File;
  +import java.util.Iterator;
  +import java.util.Map;
  +import org.apache.avalon.framework.ExceptionUtil;
   import org.apache.myrmidon.api.event.TaskListener;
  -import org.apache.myrmidon.api.metadata.ModelElement;
   import org.apache.myrmidon.components.AbstractComponentTest;
  -import org.apache.myrmidon.interfaces.model.ModelElementUtil;
  -import org.apache.myrmidon.interfaces.event.TaskEventManager;
  -import org.apache.myrmidon.interfaces.executor.ExecutionFrame;
  -import org.apache.myrmidon.interfaces.executor.Executor;
  -import org.apache.myrmidon.interfaces.library.Library;
  -import org.apache.myrmidon.interfaces.library.LibraryManager;
  -import org.apache.myrmidon.interfaces.deployer.Deployer;
  -import org.apache.myrmidon.interfaces.deployer.TypeLibraryDeployer;
  -import org.apache.myrmidon.interfaces.property.PropertyStore;
  -import org.apache.avalon.framework.ExceptionUtil;
  +import org.apache.myrmidon.interfaces.EmbeddedAnt;
   
   /**
    * A base class for test cases which need to execute tasks.
    *
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
  - * @version $Revision: 1.4 $ $Date: 2002/05/18 05:05:31 $
  - *
  - * @todo - Move the guts of AbstractProjectTestCase back to this class,
  - *         and add a specialised project builder.
  + * @version $Revision: 1.5 $ $Date: 2002/06/04 12:45:47 $
    */
   public class AbstractTaskTestCase
       extends AbstractComponentTest
  @@ -40,6 +30,15 @@
       }
   
       /**
  +     * Executes a target in a project, and asserts that it fails.
  +     */
  +    protected void executeTargetExpectError( final File projectFile,
  +                                             final String targetName )
  +    {
  +        executeTargetExpectError( projectFile, targetName, new String[ 0 ] );
  +    }
  +
  +    /**
        * Executes a target in a project, and asserts that it fails with the
        * given error message.
        */
  @@ -60,19 +59,35 @@
       {
           try
           {
  -            executeTarget( projectFile, targetName, null, null );
  +            doExecuteTarget( projectFile, targetName, null, null );
               fail( "target execution did not fail" );
           }
  -        catch( Exception e )
  +        catch( final Exception e )
           {
  -            assertSameMessage( messages, e );
  +            System.out.println( "expect:" );
  +            for( int i = 0; i < messages.length; i++ )
  +            {
  +                final String message = messages[ i ];
  +                System.out.println( "    (" + message + ")" );
  +            }
  +            System.out.println( "got:" );
  +            for( Throwable t = e; t != null; t = ExceptionUtil.getCause( t, true ) )
  +            {
  +                System.out.println( "    (" + t.getMessage() + ")" );
  +            }
  +
  +            // Add in the 'could not execute task <blah>' message
  +            final String[] allMessages = new String[ 1 + messages.length ];
  +            System.arraycopy( messages, 0, allMessages, 1, messages.length );
  +            assertSameMessage( allMessages, e );
           }
       }
   
       /**
        * Executes a target in a project, and asserts that it does not fail.
        */
  -    protected void executeTarget( final File projectFile, final String targetName )
  +    protected void executeTarget( final File projectFile,
  +                                  final String targetName )
           throws Exception
       {
           executeTarget( projectFile, targetName, null, null );
  @@ -83,7 +98,7 @@
        */
       protected void executeTarget( final File projectFile,
                                     final String targetName,
  -                                  final PropertyStore props )
  +                                  final Map props )
           throws Exception
       {
           executeTarget( projectFile, targetName, props, null );
  @@ -105,90 +120,82 @@
        */
       protected void executeTarget( final File projectFile,
                                     final String targetName,
  -                                  final PropertyStore props,
  +                                  final Map props,
                                     final TaskListener listener )
           throws Exception
       {
  -        // TODO - implement this as a ProjectBuilder, and use EmbeddedAnt
  -        // (or an Embeddor at least) instead.
  -
  -        // Load model
  -        final String systemId = projectFile.toURL().toString();
  -        final ModelElement model = ModelElementUtil.loadModel( systemId );
  -
  -        // Locate and execute the target
  -        final ModelElement[] targets = model.getChildren( "target" );
  -        for( int i = 0; i < targets.length; i++ )
  +        try
  +        {
  +            doExecuteTarget( projectFile, targetName, props, listener );
  +        }
  +        catch( final Exception e )
           {
  -            final ModelElement target = targets[ i ];
  -            if( targetName.equals( target.getAttribute( "name" ) ) )
  +            for( Throwable t = e; t != null; t = ExceptionUtil.getCause( t, true ) )
               {
  -                try
  -                {
  -                    executeTarget( target, props, listener );
  -                    return;
  -                }
  -                catch( Exception e )
  -                {
  -                    System.out.println( ExceptionUtil.printStackTrace( e, 999, true, true
) );
  -                    throw e;
  -                }
  +                getLogger().error( t.getMessage(), t );
               }
  +            throw e;
           }
  -
  -        // Target not found
  -        throw new Exception( "Target \"" + targetName + "\" not found in project " + projectFile
+ "." );
       }
   
       /**
  -     * Executes a target.
  +     * Executes a target in a project, and asserts that it does not fail.
        */
  -    private void executeTarget( final ModelElement target,
  -                                final PropertyStore props,
  -                                final TaskListener listener )
  +    private void doExecuteTarget( final File projectFile,
  +                                  final String targetName,
  +                                  final Map props,
  +                                  final TaskListener listener )
           throws Exception
       {
  -        // Register all the types visible in the classloader
  -        final LibraryManager libraryManager = (LibraryManager)getServiceManager().lookup(
LibraryManager.ROLE );
  -        final Library lib = libraryManager.createLibrary( getClass().getClassLoader() );
  -        final Deployer deployer = (Deployer)getServiceManager().lookup( Deployer.ROLE );
  -        final TypeLibraryDeployer typeLibraryDeployer = deployer.createDeployer( lib, "test"
);
  -        typeLibraryDeployer.deployAll();
  -
  -        final TaskEventManager eventManager = (TaskEventManager)getServiceManager().lookup(
TaskEventManager.ROLE );
  -        final Executor executor = (Executor)getServiceManager().lookup( Executor.ROLE );
  -        final ExecutionFrame frame = createExecutionFrame( props );
  -
  -        // Install listener
  -        if( listener != null )
  -        {
  -            eventManager.addTaskListener( listener );
  -        }
  +        final EmbeddedAnt embeddor = new EmbeddedAnt();
  +        final TrackingTaskListener tracker = new TrackingTaskListener();
   
           try
           {
  -            // Execute each of the tasks
  -            final ModelElement[] tasks = target.getChildren();
  -            for( int i = 0; i < tasks.length; i++ )
  +            // Setup a dummy install
  +            final File distDir = getTestDirectory( "dist" );
  +            getTestDirectory( "dist/lib/core" );
  +            getTestDirectory( "dist/ext" );
  +
  +            // Configure embeddor
  +            embeddor.setHomeDirectory( distDir );
  +            embeddor.enableLogging( getLogger() );
  +            embeddor.setSharedClassLoader( getClass().getClassLoader() );
  +            embeddor.setContainerClassLoader( getClass().getClassLoader() );
  +            embeddor.setProjectFile( projectFile.getAbsolutePath() );
  +            embeddor.setProjectType( "simple" );
  +            if( props != null )
  +            {
  +                for( Iterator iterator = props.entrySet().iterator(); iterator.hasNext();
)
  +                {
  +                    final Map.Entry entry = (Map.Entry)iterator.next();
  +                    embeddor.setWorkspaceProperty( (String)entry.getKey(), entry.getValue()
);
  +                }
  +            }
  +            embeddor.setEmbeddorProperty( "myrmidon.ext.path", System.getProperty( "test.libdir"
) );
  +
  +            // Add a listener to make sure all is good
  +            embeddor.addTaskListener( tracker );
  +
  +            // Add supplied listener
  +            if( listener != null )
               {
  -                final ModelElement task = tasks[ i ];
  -                final ExecutionFrame taskFrame = frame.createChildFrame( task.getName(),
null, null, false );
  -                executor.execute( task, taskFrame );
  +                embeddor.addTaskListener( listener );
               }
  +
  +            // Now execute the target
  +            embeddor.executeTargets( new String[]{targetName} );
           }
           finally
           {
  -            if( listener != null )
  -            {
  -                // Uninstall listener
  -                eventManager.removeTaskListener( listener );
  -            }
  +            embeddor.stop();
           }
   
  +        // Make sure all expected events were delivered
  +        tracker.assertComplete();
           if( listener instanceof TrackingTaskListener )
           {
  -            TrackingTaskListener taskListener = (TrackingTaskListener)listener;
  -            taskListener.assertComplete();
  +            ( (TrackingTaskListener)listener ).assertComplete();
           }
       }
   }
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/TestCaseProjectBuilder.java
  
  Index: TestCaseProjectBuilder.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon;
  
  import org.apache.myrmidon.components.builder.DefaultProjectBuilder;
  import org.apache.myrmidon.components.builder.DefaultProject;
  import org.apache.myrmidon.interfaces.oldmodel.Target;
  import org.apache.myrmidon.interfaces.model.ModelElementUtil;
  import org.apache.myrmidon.api.metadata.ModelElement;
  
  /**
   * A project builder for unit test projects.  Uses a simplified project file
   * format, which does not allow project references, or dependencies between
   * targets.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/04 12:45:47 $
   *
   * @ant.type type="project-builder" name="simple"
   */
  public class TestCaseProjectBuilder
      extends DefaultProjectBuilder
  {
      /**
       * Creates a target from its model.
       */
      protected Target createTarget( final DefaultProject project,
                                     final ModelElement model )
      {
          final ModelElement newModel =
              new ModelElement( "simple-target", model.getLocation() );
          ModelElementUtil.copyModel( model, newModel );
  
          return super.createTarget( project, newModel );
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/TestCaseTargetTask.java
  
  Index: TestCaseTargetTask.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.myrmidon;
  
  import org.apache.myrmidon.framework.AbstractContainerTask;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.myrmidon.api.metadata.ModelElement;
  import java.util.ArrayList;
  
  /**
   * A simple <target> implementation.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/04 12:45:47 $
   *
   * @ant.task name="simple-target"
   */
  public class TestCaseTargetTask
      extends AbstractContainerTask
  {
      final ArrayList m_tasks = new ArrayList();
  
      /**
       * The task name.
       */
      public void setName( final String name )
      {
          // Ignore
      }
  
      /**
       * Adds a nested task to execute.
       */
      public void add( final ModelElement taskModel )
      {
          m_tasks.add( taskModel );
      }
  
      /**
       * Executes this target.
       */
      public void execute()
          throws TaskException
      {
          final int i = m_tasks.size();
          for( int j = 0; j < i; j++ )
          {
              final ModelElement taskModel = (ModelElement)m_tasks.get( j );
              executeTask( taskModel );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message