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/conditions/test IsSetConditionTestCase.java IsTrueConditionTestCase.java NotConditionTestCase.java isset.ant istrue.ant not.ant
Date Wed, 05 Jun 2002 07:45:37 GMT
adammurdoch    2002/06/05 00:45:36

  Modified:    framework/src/test/org/apache/myrmidon/framework/conditions/test
                        IsSetConditionTestCase.java
                        IsTrueConditionTestCase.java
                        NotConditionTestCase.java isset.ant istrue.ant
                        not.ant
  Added:       framework/src/test/org/apache/myrmidon/framework
                        ExpectExceptionTask.java
                        ExpectExceptionTaskTestCase.java
                        ExpectedException.java FailTask.java
                        expect-error.ant
  Log:
  * Added an <expect-error> task, that the unit tests can use to verify that
    a set of nested tasks fails as expected.
  
  * Changed the condition unit tests to use this new task.
  
  Revision  Changes    Path
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/ExpectExceptionTask.java
  
  Index: ExpectExceptionTask.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.api.TaskException;
  import org.apache.myrmidon.api.metadata.ModelElement;
  import org.apache.avalon.framework.ExceptionUtil;
  import java.util.ArrayList;
  
  /**
   * A unit test task that checks that an expected exception is thrown when
   * a set of tasks is executed.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/05 07:45:36 $
   *
   * @ant.task name="expect-error"
   */
  public class ExpectExceptionTask
      extends AbstractContainerTask
  {
      private TaskList m_tasks;
      private ArrayList m_exceptions = new ArrayList();
  
      /**
       * The tasks to execute.
       */
      public void addTasks( final TaskList taskList ) throws TaskException
      {
          if( m_tasks != null )
          {
              throw new TaskException( "Cannot have more than one <tasks> element."
);
          }
          m_tasks = taskList;
      }
  
      /**
       * An expected error message.
       */
      public void addExc( final ExpectedException exc )
      {
          m_exceptions.add( exc );
      }
  
      /**
       * Execute task.
       */
      public void execute()
          throws TaskException
      {
          try
          {
              final ModelElement[] tasks = m_tasks.getTasks();
              executeTasks( tasks );
          }
          catch( final TaskException exc )
          {
              // Skip the generic 'task <blah> failed' error
              Throwable t = exc.getCause();
  
              final int i = m_exceptions.size();
              for( int q = 0; q < i; q++, t = ExceptionUtil.getCause( t, true ) )
              {
                  final ExpectedException expected = (ExpectedException)m_exceptions.get(
q );
                  expected.match( t );
              }
  
              return;
          }
  
          throw new TaskException( "Tasks did not fail as expected." );
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/ExpectExceptionTaskTestCase.java
  
  Index: ExpectExceptionTaskTestCase.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.AbstractTaskTestCase;
  import java.io.File;
  
  /**
   * Test cases for the <expect-error> task.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/05 07:45:36 $
   */
  public class ExpectExceptionTaskTestCase
      extends AbstractTaskTestCase
  {
      public ExpectExceptionTaskTestCase( final String name )
      {
          super( name );
      }
  
      /**
       * Checks that the task succeeds when the nested tasks throw the expected
       * exception.
       */
      public void testExpectedExc() throws Exception
      {
          final File projectFile = getTestResource( "expect-error.ant" );
          executeTarget( projectFile, "expected-exc" );
          executeTarget( projectFile, "expected-exc-chain" );
      }
  
      /**
       * Checks that the error message can be ignored.
       */
      public void testIgnoreMessage() throws Exception
      {
          final File projectFile = getTestResource( "expect-error.ant" );
          executeTarget( projectFile, "ignore-message" );
          executeTarget( projectFile, "ignore-message-chain" );
          executeTarget( projectFile, "ignore-message-no-msg" );
      }
  
      /**
       * Checks that the task fails when the nested tasks do not throw an exception.
       */
      public void testNoException() throws Exception
      {
          final File projectFile = getTestResource( "expect-error.ant" );
          executeTargetExpectError( projectFile,
                                    "no-exception",
                                    "Tasks did not fail as expected." );
          executeTargetExpectError( projectFile,
                                    "no-exception-no-msg",
                                    "Tasks did not fail as expected." );
      }
  
      /**
       * Checks that the task fails when an exception is thrown with an unexpected
       * error message.
       */
      public void testUnexpectedExc() throws Exception
      {
          final File projectFile = getTestResource( "expect-error.ant" );
          executeTargetExpectError( projectFile,
                                    "unexpected-exc",
                                    "Was expecting error <some error message> but found
<error 1>" );
          executeTargetExpectError( projectFile,
                                    "unexpected-exc-chain",
                                    "Was expecting error <some error message> but found
<error 2>" );
          executeTargetExpectError( projectFile,
                                    "exc-chain-too-short",
                                    "Was expecting error <error 2>" );
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/ExpectedException.java
  
  Index: ExpectedException.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.api.TaskException;
  
  /**
   * Information about an expected exception.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/05 07:45:36 $
   */
  public class ExpectedException
  {
      private String m_message;
  
      public String getMessage()
      {
          return m_message;
      }
  
      /**
       * The expected error message.
       */
      public void addContent( final String message )
      {
          m_message = message;
      }
  
      /**
       * Determines if a throwable matches the expected exception.
       */
      public void match( final Throwable throwable ) throws TaskException
      {
          if( throwable == null )
          {
              if( m_message != null )
              {
                  throw new TaskException( "Was expecting error <" + m_message + ">"
);
              }
              else
              {
                  throw new TaskException( "Was expecting error." );
              }
          }
  
          if( m_message != null && ! m_message.equals( throwable.getMessage() ) )
          {
              throw new TaskException( "Was expecting error <" + m_message + "> but
found <" + throwable.getMessage() + ">",
                                       throwable );
          }
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/FailTask.java
  
  Index: FailTask.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.api.AbstractTask;
  import org.apache.myrmidon.api.TaskException;
  
  /**
   * A task that throws an exception.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/06/05 07:45:36 $
   *
   * @ant.task name="fail"
   */
  public class FailTask
      extends AbstractTask
  {
      private int m_depth = 1;
  
      public void setDepth( final int depth )
      {
          m_depth = depth;
      }
  
      /**
       * Execute task.
       */
      public void execute()
          throws TaskException
      {
          TaskException t = null;
          for( int i = m_depth; i > 0; i-- )
          {
              t = new TaskException( "error " + i, t );
          }
          throw t;
      }
  }
  
  
  
  1.1                  jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/expect-error.ant
  
  Index: expect-error.ant
  ===================================================================
  <project version="2.0">
      <!-- Test when the expected exception is thrown -->
      <target name="expected-exc">
          <expect-error>
              <exc>error 1</exc>
              <tasks>
                  <fail/>
              </tasks>
          </expect-error>
      </target>
  
      <target name="expected-exc-chain">
          <expect-error>
              <exc>error 1</exc>
              <exc>error 2</exc>
              <tasks>
                  <fail depth="2"/>
              </tasks>
          </expect-error>
      </target>
  
      <!-- Test ignoring the error message -->
  
      <target name="ignore-message">
          <expect-error>
              <exc/>
              <tasks>
                  <fail/>
              </tasks>
          </expect-error>
      </target>
  
      <target name="ignore-message-chain">
          <expect-error>
              <exc/>
              <exc>error 2</exc>
              <tasks>
                  <fail depth="2"/>
              </tasks>
          </expect-error>
      </target>
  
      <target name="ignore-message-no-msg">
          <expect-error>
              <tasks>
                  <fail/>
              </tasks>
          </expect-error>
      </target>
  
      <!-- Test when nested tasks do not throw exception -->
  
      <target name="no-exception">
          <expect-error>
              <exc>some error mesage.</exc>
              <tasks>
                  <unit-test-log>No error here.</unit-test-log>
              </tasks>
          </expect-error>
      </target>
  
      <target name="no-exception-no-msg">
          <expect-error>
              <tasks>
                  <unit-test-log>No error here.</unit-test-log>
              </tasks>
          </expect-error>
      </target>
  
      <!-- Test when nested tasks throw unexpected exception -->
  
      <target name="unexpected-exc">
          <expect-error>
              <exc>some error message</exc>
              <tasks>
                  <fail/>
              </tasks>
          </expect-error>
      </target>
  
      <target name="unexpected-exc-chain">
          <expect-error>
              <exc>error 1</exc>
              <exc>some error message</exc>
              <tasks>
                  <fail depth="2"/>
              </tasks>
          </expect-error>
      </target>
  
      <target name="exc-chain-too-short">
          <expect-error>
              <exc>error 1</exc>
              <exc>error 2</exc>
              <exc>error 3</exc>
              <tasks>
                  <fail/>
              </tasks>
          </expect-error>
      </target>
  
  </project>
  
  
  1.4       +2 -9      jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/IsSetConditionTestCase.java
  
  Index: IsSetConditionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/IsSetConditionTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- IsSetConditionTestCase.java	4 Jun 2002 12:44:54 -0000	1.3
  +++ IsSetConditionTestCase.java	5 Jun 2002 07:45:36 -0000	1.4
  @@ -8,15 +8,13 @@
   package org.apache.myrmidon.framework.conditions.test;
   
   import java.io.File;
  -import org.apache.avalon.excalibur.i18n.Resources;
   import org.apache.myrmidon.AbstractTaskTestCase;
  -import org.apache.myrmidon.framework.conditions.IsSetCondition;
   
   /**
    * Test cases for the <is-set> condition.
    *
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
  - * @version $Revision: 1.3 $ $Date: 2002/06/04 12:44:54 $
  + * @version $Revision: 1.4 $ $Date: 2002/06/05 07:45:36 $
    */
   public class IsSetConditionTestCase
       extends AbstractTaskTestCase
  @@ -36,12 +34,7 @@
           executeTarget( projectFile, "set2true" );
           executeTarget( projectFile, "set2false" );
           executeTarget( projectFile, "not-set" );
  -
  -        Resources res = getResourcesForTested( IsSetCondition.class );
  -        final String[] messages = {
  -            res.getString( "isset.no-property.error" )
  -        };
  -        executeTargetExpectError( projectFile, "no-prop-name", messages );
  +        executeTarget( projectFile, "no-prop-name" );
       }
   
   }
  
  
  
  1.4       +3 -14     jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/IsTrueConditionTestCase.java
  
  Index: IsTrueConditionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/IsTrueConditionTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- IsTrueConditionTestCase.java	4 Jun 2002 12:44:54 -0000	1.3
  +++ IsTrueConditionTestCase.java	5 Jun 2002 07:45:36 -0000	1.4
  @@ -8,15 +8,13 @@
   package org.apache.myrmidon.framework.conditions.test;
   
   import java.io.File;
  -import org.apache.avalon.excalibur.i18n.Resources;
   import org.apache.myrmidon.AbstractTaskTestCase;
  -import org.apache.myrmidon.framework.conditions.IsTrueCondition;
   
   /**
    * Test cases for the <is-true> condition.
    *
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
  - * @version $Revision: 1.3 $ $Date: 2002/06/04 12:44:54 $
  + * @version $Revision: 1.4 $ $Date: 2002/06/05 07:45:36 $
    */
   public class IsTrueConditionTestCase
       extends AbstractTaskTestCase
  @@ -35,16 +33,7 @@
           executeTarget( projectFile, "set2true" );
           executeTarget( projectFile, "set2false" );
           executeTarget( projectFile, "not-set" );
  -
  -        // TODO - check error message
  -        String[] messages = {};
  -        executeTargetExpectError( projectFile, "set", messages );
  -
  -        final Resources res = getResourcesForTested( IsTrueCondition.class );
  -        messages = new String[] {
  -            res.getString( "istrue.no-property.error" )
  -        };
  -        executeTargetExpectError( projectFile, "no-prop-name", messages );
  +        executeTarget( projectFile, "set" );
  +        executeTarget( projectFile, "no-prop-name" );
       }
  -
   }
  
  
  
  1.3       +3 -5      jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/NotConditionTestCase.java
  
  Index: NotConditionTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/NotConditionTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NotConditionTestCase.java	11 May 2002 12:44:01 -0000	1.2
  +++ NotConditionTestCase.java	5 Jun 2002 07:45:36 -0000	1.3
  @@ -14,7 +14,7 @@
    * Test cases for the <not> condition.
    *
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
  - * @version $Revision: 1.2 $ $Date: 2002/05/11 12:44:01 $
  + * @version $Revision: 1.3 $ $Date: 2002/06/05 07:45:36 $
    */
   public class NotConditionTestCase
       extends AbstractTaskTestCase
  @@ -31,9 +31,7 @@
       {
           final File projectFile = getTestResource( "not.ant" );
           executeTarget( projectFile, "truth-table" );
  -
  -        // TODO - check error messages
  -        executeTargetExpectError( projectFile, "empty", new String[0] );
  -        executeTargetExpectError( projectFile, "too-many-nested", new String[0] );
  +        executeTarget( projectFile, "empty" );
  +        executeTarget( projectFile, "too-many-nested" );
       }
   }
  
  
  
  1.4       +8 -3      jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/isset.ant
  
  Index: isset.ant
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/isset.ant,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- isset.ant	27 May 2002 13:06:07 -0000	1.3
  +++ isset.ant	5 Jun 2002 07:45:36 -0000	1.4
  @@ -34,9 +34,14 @@
   
       <!-- No property name -->
       <target name="no-prop-name">
  -        <assert>
  -            <is-set/>
  -        </assert>
  +        <expect-error>
  +            <exc>No property specified to test.</exc>
  +            <tasks>
  +                <assert>
  +                    <is-set/>
  +                </assert>
  +            </tasks>
  +        </expect-error>
       </target>
   
   </project>
  
  
  
  1.3       +17 -7     jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/istrue.ant
  
  Index: istrue.ant
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/istrue.ant,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- istrue.ant	17 May 2002 07:48:10 -0000	1.2
  +++ istrue.ant	5 Jun 2002 07:45:36 -0000	1.3
  @@ -3,10 +3,15 @@
   
       <!-- Set to some arbirary value -->
       <target name="set">
  -        <path id="test-prop" location="some-location"/>
  -        <assert>
  -            <is-true property="test-prop"/>
  -        </assert>
  +        <expect-error>
  +            <exc>Could not convert from org.apache.myrmidon.framework.file.Path to
java.lang.Boolean.</exc>
  +            <tasks>
  +                <path id="test-prop" location="some-location"/>
  +                <assert>
  +                    <is-true property="test-prop"/>
  +                </assert>
  +            </tasks>
  +        </expect-error>
       </target>
   
       <!-- Set to true -->
  @@ -34,9 +39,14 @@
   
       <!-- No property name -->
       <target name="no-prop-name">
  -        <assert>
  -            <is-true/>
  -        </assert>
  +        <expect-error>
  +            <exc>No property specified to test.</exc>
  +            <tasks>
  +                <assert>
  +                    <is-true/>
  +                </assert>
  +            </tasks>
  +        </expect-error>
       </target>
   
   </project>
  
  
  
  1.2       +17 -6     jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/not.ant
  
  Index: not.ant
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/framework/src/test/org/apache/myrmidon/framework/conditions/test/not.ant,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- not.ant	14 Apr 2002 09:33:13 -0000	1.1
  +++ not.ant	5 Jun 2002 07:45:36 -0000	1.2
  @@ -3,9 +3,14 @@
   
       <!-- An empty <or> condition -->
       <target name="empty">
  -        <assert>
  -            <not/>
  -        </assert>
  +        <expect-error>
  +            <exc>No condition specified.</exc>
  +            <tasks>
  +                <assert>
  +                    <not/>
  +                </assert>
  +            </tasks>
  +        </expect-error>
       </target>
   
       <!-- The truth-table -->
  @@ -20,9 +25,15 @@
   
       <!-- Too many nested conditions -->
       <target name="too-many-nested">
  -        <assert>
  -            <not><true/><false/></not>
  -        </assert>
  +        <expect-error>
  +            <exc>Could not configure element &lt;false&gt;.</exc>
  +            <exc>Too many conditions specified.</exc>
  +            <tasks>
  +                <assert>
  +                    <not><true/><false/></not>
  +                </assert>
  +            </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