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/framework ExpectOutputTask.java ExpectOutputTaskTestCase.java expect-output.ant
Date Thu, 06 Jun 2002 10:27:37 GMT
adammurdoch    2002/06/06 03:27:37

  Modified:    container/src/test/org/apache/myrmidon
                        LogMessageTracker.java
  Added:       framework/src/test/org/apache/myrmidon/framework
                        ExpectOutputTask.java ExpectOutputTaskTestCase.java
                        expect-output.ant
  Log:
  Added an <expect-output> task, which unit tests can use to verify the output
  of a set of tasks.
  
  Revision  Changes    Path
  1.7       +9 -6      jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/LogMessageTracker.java
  
  Index: LogMessageTracker.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/container/src/test/org/apache/myrmidon/LogMessageTracker.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- LogMessageTracker.java	27 May 2002 07:03:53 -0000	1.6
  +++ LogMessageTracker.java	6 Jun 2002 10:27:37 -0000	1.7
  @@ -17,7 +17,7 @@
    * correct order.
    *
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
  - * @version $Revision: 1.6 $ $Date: 2002/05/27 07:03:53 $
  + * @version $Revision: 1.7 $ $Date: 2002/06/06 10:27:37 $
    */
   public class LogMessageTracker
       extends TrackingTaskListener
  @@ -54,10 +54,10 @@
   
           // Pop the next expected message off the list, and make sure it
           // matches the message in the event
  -        assertTrue( "Unexpected log message \"" + event.getMessage() + "\"", m_tasks.size()
> 0 );
  -        assertEquals( "Incorrect task path", m_tasks.remove( 0 ), event.getPath() );
  -        assertEquals( "Incorrect log message", m_messages.remove( 0 ), event.getMessage()
);
  -        assertEquals( "Incorrect log level", m_levels.remove( 0 ), event.getLogLevel()
);
  +        assertTrue( "Unexpected log message \"" + event.getMessage() + "\".", m_tasks.size()
> 0 );
  +        assertEquals( "Incorrect task path,", m_tasks.remove( 0 ), event.getPath() );
  +        assertEquals( "Incorrect log message,", m_messages.remove( 0 ), event.getMessage()
);
  +        assertEquals( "Incorrect log level,", m_levels.remove( 0 ), event.getLogLevel()
);
       }
   
       /**
  @@ -68,7 +68,10 @@
           super.assertComplete();
   
           // Make sure that all log messages were delivered
  -        assertTrue( "Log message not delivered", m_tasks.size() == 0 && m_messages.size()
== 0 );
  +        if( m_tasks.size() != 0 || m_messages.size() != 0 )
  +        {
  +            fail( "Log message <" + m_messages.get( 0 ) + "> not output." );
  +        }
       }
   
       /**
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/ExpectOutputTask.java
  
  Index: ExpectOutputTask.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.framework;
  
  import org.apache.myrmidon.LogMessageTracker;
  import org.apache.myrmidon.interfaces.event.TaskEventManager;
  import org.apache.myrmidon.interfaces.executor.ExecutionFrame;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.myrmidon.api.metadata.ModelElement;
  
  /**
   * A task that unit tests can use to verify the output of a set of nested
   * tasks.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/06 10:27:37 $
   *
   * @ant.task name="expect-output"
   *
   * @todo Allow log levels to be specified.
   */
  public class ExpectOutputTask
      extends AbstractContainerTask
  {
      private TaskList m_tasks;
      private ModelElement m_output;
  
      /**
       * The tasks to execute.
       */
      public void addTasks( final TaskList taskList ) throws TaskException
      {
          if( m_tasks != null )
          {
              throw new TaskException( "Cannot have more than one nested <task> element."
);
          }
          m_tasks = taskList;
      }
  
      /**
       * The expected output.
       */
      public void addOut( final ModelElement outputModel ) throws TaskException
      {
          if( m_output != null )
          {
              throw new TaskException( "Cannot have more than one nested <out> element."
);
          }
          m_output = outputModel;
      }
  
      /**
       * Execute task.
       */
      public void execute()
          throws TaskException
      {
  
          // Create and configure a task listener to track output
          final LogMessageTracker listener = new LogMessageTracker();
          if( m_output != null )
          {
              final ExecutionFrame frame = (ExecutionFrame)getService( ExecutionFrame.class
);
              final String path = frame.getName();
              configureListener( listener, m_output, path );
          }
  
          // Install the listener, execute the tasks, and check all is good
          TaskEventManager eventManager = (TaskEventManager)getService( TaskEventManager.class
);
          eventManager.addTaskListener( listener );
          try
          {
              final ModelElement[] tasks = m_tasks.getTasks();
              executeTasks( tasks );
              listener.assertComplete();
          }
          finally
          {
              eventManager.removeTaskListener( listener );
          }
      }
  
      /**
       * Configures a listener that will track the log messages
       * generated by the tasks.
       */
      private void configureListener( final LogMessageTracker listener,
                                      final ModelElement model,
                                      final String prefix )
      {
          final ModelElement[] children = model.getChildren();
          for( int i = 0; i < children.length; i++ )
          {
              final ModelElement child = children[ i ];
              final String path = prefix + "/" + child.getName();
              if( child.getContent() != null )
              {
                  listener.addExpectedMessage( path, child.getContent() );
              }
              else
              {
                  configureListener( listener, child, path );
              }
          }
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/ExpectOutputTaskTestCase.java
  
  Index: ExpectOutputTaskTestCase.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.framework;
  
  import java.io.File;
  
  /**
   * Test cases for the <expect-output> task.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/06 10:27:37 $
   */
  public class ExpectOutputTaskTestCase
      extends AbstractTaskTestCase
  {
      public ExpectOutputTaskTestCase( final String name )
      {
          super( name );
      }
  
      /**
       * Checks task succeeds with expected output.
       */
      public void testExpectedOutput() throws Exception
      {
          final File projectFile = getTestResource( "expect-output.ant" );
          executeTarget( projectFile, "expected" );
          executeTarget( projectFile, "expected2" );
          executeTarget( projectFile, "none-expected" );
          executeTarget( projectFile, "nested" );
      }
  
      /**
       * Checks the task fails when extra messages appear in the output.
       */
      public void testExtraMessage() throws Exception
      {
          final File projectFile = getTestResource( "expect-output.ant" );
          executeTarget( projectFile, "extra" );
          executeTarget( projectFile, "extra2" );
      }
  
      /**
       * Checks the task fails when an expected message does not appear in the
       * output.
       */
      public void testMissingMessage() throws Exception
      {
          final File projectFile = getTestResource( "expect-output.ant" );
          executeTarget( projectFile, "missing" );
          executeTarget( projectFile, "missing2" );
      }
  
      /**
       * Checks the task fails when incorrect messages appear in the output.
       */
      public void testMismatchedMessage() throws Exception
      {
          final File projectFile = getTestResource( "expect-output.ant" );
          executeTarget( projectFile, "mismatched" );
          executeTarget( projectFile, "mismatched2" );
          executeTarget( projectFile, "bad-path" );
          executeTarget( projectFile, "bad-path-nested" );
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/expect-output.ant
  
  Index: expect-output.ant
  ===================================================================
  <project version="2.0">
  
      <!-- Test expected output -->
  
      <target name="expected">
          <expect-output>
              <out>
                  <unit-test-log>log message</unit-test-log>
              </out>
              <tasks>
                  <unit-test-log>log message</unit-test-log>
              </tasks>
          </expect-output>
      </target>
  
      <target name="expected2">
          <expect-output>
              <out>
                  <unit-test-log>log message 1</unit-test-log>
                  <unit-test-log>log message 2</unit-test-log>
              </out>
              <tasks>
                  <unit-test-log>log message 1</unit-test-log>
                  <unit-test-log>log message 2</unit-test-log>
              </tasks>
          </expect-output>
      </target>
  
      <target name="nested">
          <expect-output>
              <out>
                  <simple-target>
                      <unit-test-log>log message 1</unit-test-log>
                      <unit-test-log>log message 2</unit-test-log>
                  </simple-target>
              </out>
              <tasks>
                  <simple-target name="this is shonky">
                      <unit-test-log>log message 1</unit-test-log>
                      <unit-test-log>log message 2</unit-test-log>
                  </simple-target>
              </tasks>
          </expect-output>
      </target>
  
      <target name="none-expected">
          <expect-output>
              <tasks>
              </tasks>
          </expect-output>
      </target>
  
      <!-- Extra message -->
  
      <target name="extra">
          <expect-error>
              <exc>Unexpected log message "log message".</exc>
              <tasks>
                  <expect-output>
                      <tasks>
                          <unit-test-log>log message</unit-test-log>
                      </tasks>
                  </expect-output>
              </tasks>
          </expect-error>
      </target>
  
      <target name="extra2">
          <expect-error>
              <exc>Unexpected log message "extra message".</exc>
              <tasks>
                  <expect-output>
                      <out>
                          <unit-test-log>log message</unit-test-log>
                      </out>
                      <tasks>
                          <unit-test-log>log message</unit-test-log>
                          <unit-test-log>extra message</unit-test-log>
                      </tasks>
                  </expect-output>
              </tasks>
          </expect-error>
      </target>
  
      <!-- Missing message -->
  
      <target name="missing">
          <expect-error>
              <exc>Log message &lt;some missing message&gt; not output.</exc>
              <tasks>
                  <expect-output>
                      <out>
                          <sometask>some missing message</sometask>
                      </out>
                      <tasks>
                      </tasks>
                  </expect-output>
              </tasks>
          </expect-error>
      </target>
  
      <target name="missing2">
          <expect-error>
              <exc>Log message &lt;some missing message&gt; not output.</exc>
              <tasks>
                  <expect-output>
                      <out>
                          <unit-test-log>log message</unit-test-log>
                          <sometask>some missing message</sometask>
                      </out>
                      <tasks>
                          <unit-test-log>log message</unit-test-log>
                      </tasks>
                  </expect-output>
              </tasks>
          </expect-error>
      </target>
  
      <!-- Mismatched output -->
  
      <target name="mismatched">
          <expect-error>
              <exc>Incorrect log message, expected:&lt;some message&gt; but
was:&lt;log message&gt;</exc>
              <tasks>
                  <expect-output>
                      <out>
                          <unit-test-log>some message</unit-test-log>
                      </out>
                      <tasks>
                          <unit-test-log>log message</unit-test-log>
                      </tasks>
                  </expect-output>
              </tasks>
          </expect-error>
      </target>
  
      <target name="mismatched2">
          <expect-error>
              <exc>Incorrect log message, expected:&lt;some message&gt; but
was:&lt;log message&gt;</exc>
              <tasks>
                  <expect-output>
                      <out>
                          <unit-test-log>log message</unit-test-log>
                          <unit-test-log>some message</unit-test-log>
                      </out>
                      <tasks>
                          <unit-test-log>log message</unit-test-log>
                          <unit-test-log>log message</unit-test-log>
                      </tasks>
                  </expect-output>
              </tasks>
          </expect-error>
      </target>
  
      <target name="bad-path">
          <expect-error>
              <exc>Incorrect task path, expected:&lt;/expect-output/bad-path/expect-error/expect-output/sometask&gt;
but was:&lt;/expect-output/bad-path/expect-error/expect-output/unit-test-log&gt;</exc>
              <tasks>
                  <expect-output>
                      <out>
                          <sometask>log message</sometask>
                      </out>
                      <tasks>
                          <unit-test-log>log message</unit-test-log>
                      </tasks>
                  </expect-output>
              </tasks>
          </expect-error>
      </target>
  
      <target name="bad-path-nested">
          <expect-error>
              <exc>Incorrect task path, expected:&lt;/expect-output/bad-path-nested/expect-error/expect-output/a/b&gt;
but was:&lt;/expect-output/bad-path-nested/expect-error/expect-output/unit-test-log&gt;</exc>
              <tasks>
                  <expect-output>
                      <out>
                          <a>
                              <b>log message</b>
                          </a>
                      </out>
                      <tasks>
                          <unit-test-log>log message</unit-test-log>
                      </tasks>
                  </expect-output>
              </tasks>
          </expect-error>
      </target>
  
  </project>
  
  

--
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