Author: bodewig Date: Mon Jul 4 05:07:49 2005 New Revision: 209062 URL: http://svn.apache.org/viewcvs?rev=209062&view=rev Log: New task expectfailure Added: ant/sandbox/antlibs/antunit/trunk/docs/expectfailure.html (with props) ant/sandbox/antlibs/antunit/trunk/src/etc/testcases/expectfailure.xml (with props) ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/ExpectFailureTask.java (with props) ant/sandbox/antlibs/antunit/trunk/src/testcases/org/apache/ant/antunit/ExpectFailureTest.java (with props) Modified: ant/sandbox/antlibs/antunit/trunk/docs/index.hmtl ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/antlib.xml Added: ant/sandbox/antlibs/antunit/trunk/docs/expectfailure.html URL: http://svn.apache.org/viewcvs/ant/sandbox/antlibs/antunit/trunk/docs/expectfailure.html?rev=209062&view=auto ============================================================================== --- ant/sandbox/antlibs/antunit/trunk/docs/expectfailure.html (added) +++ ant/sandbox/antlibs/antunit/trunk/docs/expectfailure.html Mon Jul 4 05:07:49 2005 @@ -0,0 +1,67 @@ + + + + expectfailure Task + + + +

expectfailure

+ +

Description

+ +

Asserts that a number of nested tasks throws a BuildException.

+ +

Parameters

+ + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
messageMessage for the exception if the condition + doesn't hold true. Defaults to "Expected build failure" if + expectedMessage hasn't been specified or "Expected build + failure with message 'expectedMessage' but was + 'actualMessage'" if it has been specified and a + different exception has been thrown.No.
expectedMessageUsed to assert a specific exception message.No.
+ +

Parameters specified as nested elements

+

all Ant tasks

+ +

You can use all supported tasks and types as nested element of + this task.

+ +

Examples

+ +

Make the build fail with the message "expected build failure + with message='failed' but was 'passed'":

+ +
+      <expectfailure expectedMessage="failed">
+        <fail>passed<fail>
+      </expectfailure>
+    
+ +

Make the build fail with the message "expected build failure" + because the nested task doesn't cause a build failure:

+ +
+      <expectfailure>
+        <echo>BUILD FAILED</echo>
+      </expectfailure>
+    
+ +
+

Copyright © 2005 The Apache Software Foundation. All rights Reserved.

+ + \ No newline at end of file Propchange: ant/sandbox/antlibs/antunit/trunk/docs/expectfailure.html ------------------------------------------------------------------------------ svn:eol-style = native Modified: ant/sandbox/antlibs/antunit/trunk/docs/index.hmtl URL: http://svn.apache.org/viewcvs/ant/sandbox/antlibs/antunit/trunk/docs/index.hmtl?rev=209062&r1=209061&r2=209062&view=diff ============================================================================== --- ant/sandbox/antlibs/antunit/trunk/docs/index.hmtl (original) +++ ant/sandbox/antlibs/antunit/trunk/docs/index.hmtl Mon Jul 4 05:07:49 2005 @@ -103,6 +103,10 @@
  • more assertions - useful specialized versions of assertTrue.
  • +
  • expectfailure - a task + container useful for tests that assert build failures.
  • + +

    Copyright © 2005 The Apache Software Foundation. All rights Reserved.

    Added: ant/sandbox/antlibs/antunit/trunk/src/etc/testcases/expectfailure.xml URL: http://svn.apache.org/viewcvs/ant/sandbox/antlibs/antunit/trunk/src/etc/testcases/expectfailure.xml?rev=209062&view=auto ============================================================================== --- ant/sandbox/antlibs/antunit/trunk/src/etc/testcases/expectfailure.xml (added) +++ ant/sandbox/antlibs/antunit/trunk/src/etc/testcases/expectfailure.xml Mon Jul 4 05:07:49 2005 @@ -0,0 +1,58 @@ + + + + + + + + + foo + + + + + + foo + + + + + + foo + + + + + + foo + + + + + + foo + + + + + + foo + + + + \ No newline at end of file Propchange: ant/sandbox/antlibs/antunit/trunk/src/etc/testcases/expectfailure.xml ------------------------------------------------------------------------------ svn:eol-style = native Added: ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/ExpectFailureTask.java URL: http://svn.apache.org/viewcvs/ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/ExpectFailureTask.java?rev=209062&view=auto ============================================================================== --- ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/ExpectFailureTask.java (added) +++ ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/ExpectFailureTask.java Mon Jul 4 05:07:49 2005 @@ -0,0 +1,78 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.ant.antunit; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.taskdefs.Sequential; + +/** + * Expects the nested tasks to throw a BuildException and optinally + * asserts the message of that exception. + * + *

    Throws a AssertFailedException if the nested tasks do not throw + * the expected BuildException.

    + */ +public class ExpectFailureTask extends Sequential { + + private String expectedMessage; + private String message; + + /** + * The exception message to expect. + */ + public void setExpectedMessage(String m) { + expectedMessage = m; + } + + /** + * The message to use in the AssertinFailedException if the nested + * tasks fail to raise the "correct" exception. + */ + public void setMessage(String m) { + message = m; + } + + public void execute() { + boolean thrown = false; + try { + super.execute(); + } catch (BuildException e) { + thrown = true; + if (expectedMessage != null + && !expectedMessage.equals(e.getMessage())) { + if (message == null) { + throw new AssertionFailedException("Expected build failure " + + "with message '" + + expectedMessage + + "' but was '" + + e.getMessage() + "'"); + } else { + throw new AssertionFailedException(message); + } + } + } + + if (!thrown) { + if (message == null) { + throw new AssertionFailedException("Expected build failure"); + } else { + throw new AssertionFailedException(message); + } + } + } +} \ No newline at end of file Propchange: ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/ExpectFailureTask.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/antlib.xml URL: http://svn.apache.org/viewcvs/ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/antlib.xml?rev=209062&r1=209061&r2=209062&view=diff ============================================================================== --- ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/antlib.xml (original) +++ ant/sandbox/antlibs/antunit/trunk/src/main/org/apache/ant/antunit/antlib.xml Mon Jul 4 05:07:49 2005 @@ -21,6 +21,9 @@ + + Added: ant/sandbox/antlibs/antunit/trunk/src/testcases/org/apache/ant/antunit/ExpectFailureTest.java URL: http://svn.apache.org/viewcvs/ant/sandbox/antlibs/antunit/trunk/src/testcases/org/apache/ant/antunit/ExpectFailureTest.java?rev=209062&view=auto ============================================================================== --- ant/sandbox/antlibs/antunit/trunk/src/testcases/org/apache/ant/antunit/ExpectFailureTest.java (added) +++ ant/sandbox/antlibs/antunit/trunk/src/testcases/org/apache/ant/antunit/ExpectFailureTest.java Mon Jul 4 05:07:49 2005 @@ -0,0 +1,64 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.ant.antunit; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.BuildFileTest; + +public class ExpectFailureTest extends BuildFileTest { + + public ExpectFailureTest(String name) { + super(name); + } + + public void setUp() { + configureProject("src/etc/testcases/expectfailure.xml"); + } + + public void testPassNoMessage() { + testPass("passNoMessage"); + } + public void testPassMessage() { + testPass("passMessage"); + } + + public void testFailNoMessage() { + testFail("failNoMessage", "Expected build failure"); + } + public void testFailMessage() { + testFail("failWrongMessage", "Expected build failure with message 'bar'" + + " but was 'foo'"); + } + public void testFailNoMessageMessageSet() { + testFail("failNoMessageMessageSet", "oops"); + } + public void testFailMessageMessageSet() { + testFail("failWrongMessageMessageSet", "oops"); + } + + private void testPass(String target) { + executeTarget(target); + } + private void testFail(String target, String message) { + try { + executeTarget(target); + fail("Expected an exception"); + } catch (AssertionFailedException e) { + assertEquals(message, e.getMessage()); + } + } +} Propchange: ant/sandbox/antlibs/antunit/trunk/src/testcases/org/apache/ant/antunit/ExpectFailureTest.java ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org For additional commands, e-mail: dev-help@ant.apache.org