New task expectfailure
git-svn-id: https://svn.apache.org/repos/asf/ant/sandbox/antlibs/antunit/trunk@209062 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
112898e2ed
commit
74933f8b7e
|
|
@ -0,0 +1,67 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Language" content="en-us"></meta>
|
||||
<title>expectfailure Task</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2><a name="expectfailure">expectfailure</a></h2>
|
||||
|
||||
<h3>Description</h3>
|
||||
|
||||
<p>Asserts that a number of nested tasks throws a BuildException.</p>
|
||||
|
||||
<h3>Parameters</h3>
|
||||
<table border="1" cellpadding="2" cellspacing="0">
|
||||
<tr>
|
||||
<td valign="top"><b>Attribute</b></td>
|
||||
<td valign="top"><b>Description</b></td>
|
||||
<td align="center" valign="top"><b>Required</b></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td valign="top">message</td>
|
||||
<td valign="top">Message 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 '<em>expectedMessage</em>' but was
|
||||
'<em>actualMessage</em>'" if it has been specified and a
|
||||
different exception has been thrown.</td>
|
||||
<td align="center">No.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td valign="top">expectedMessage</td>
|
||||
<td valign="top">Used to assert a specific exception message.</td>
|
||||
<td align="center">No.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h3>Parameters specified as nested elements</h3>
|
||||
<h4>all Ant tasks</h4>
|
||||
|
||||
<p>You can use all supported tasks and types as nested element of
|
||||
this task.</p>
|
||||
|
||||
<h3>Examples</h3>
|
||||
|
||||
<p>Make the build fail with the message "expected build failure
|
||||
with message='failed' but was 'passed'":</p>
|
||||
|
||||
<pre>
|
||||
<expectfailure expectedMessage="failed">
|
||||
<fail>passed<fail>
|
||||
</expectfailure>
|
||||
</pre>
|
||||
|
||||
<p>Make the build fail with the message "expected build failure"
|
||||
because the nested task doesn't cause a build failure:</p>
|
||||
|
||||
<pre>
|
||||
<expectfailure>
|
||||
<echo>BUILD FAILED</echo>
|
||||
</expectfailure>
|
||||
</pre>
|
||||
|
||||
<hr/>
|
||||
<p align="center">Copyright © 2005 The Apache Software Foundation. All rights Reserved.</p>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -103,6 +103,10 @@
|
|||
<li><a href="assertions.html">more assertions</a> - useful
|
||||
specialized versions of assertTrue.</li>
|
||||
|
||||
<li><a href="expectfailure.html">expectfailure</a> - a task
|
||||
container useful for tests that assert build failures.</li>
|
||||
</ul>
|
||||
|
||||
<hr/>
|
||||
<p align="center">Copyright © 2005 The Apache Software Foundation. All rights Reserved.</p>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
<?xml version="1.0"?>
|
||||
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
|
||||
<project name="antunit-test"
|
||||
default="all" xmlns:au="antlib:org.apache.ant.antunit">
|
||||
|
||||
<target name="passNoMessage">
|
||||
<au:expectfailure>
|
||||
<fail>foo</fail>
|
||||
</au:expectfailure>
|
||||
</target>
|
||||
|
||||
<target name="passMessage">
|
||||
<au:expectfailure expectedMessage="foo">
|
||||
<fail>foo</fail>
|
||||
</au:expectfailure>
|
||||
</target>
|
||||
|
||||
<target name="failNoMessage">
|
||||
<au:expectfailure>
|
||||
<echo>foo</echo>
|
||||
</au:expectfailure>
|
||||
</target>
|
||||
|
||||
<target name="failWrongMessage">
|
||||
<au:expectfailure expectedMessage="bar">
|
||||
<fail>foo</fail>
|
||||
</au:expectfailure>
|
||||
</target>
|
||||
|
||||
<target name="failNoMessageMessageSet">
|
||||
<au:expectfailure message="oops">
|
||||
<echo>foo</echo>
|
||||
</au:expectfailure>
|
||||
</target>
|
||||
|
||||
<target name="failWrongMessageMessageSet">
|
||||
<au:expectfailure expectedMessage="bar" message="oops">
|
||||
<fail>foo</fail>
|
||||
</au:expectfailure>
|
||||
</target>
|
||||
|
||||
</project>
|
||||
|
|
@ -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.
|
||||
*
|
||||
* <p>Throws a AssertFailedException if the nested tasks do not throw
|
||||
* the expected BuildException.</p>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,9 @@
|
|||
<taskdef name="assertTrue"
|
||||
classname="org.apache.ant.antunit.AssertTask"/>
|
||||
|
||||
<taskdef name="expectfailure"
|
||||
classname="org.apache.ant.antunit.ExpectFailureTask"/>
|
||||
|
||||
<typedef name="plainlistener"
|
||||
classname="org.apache.ant.antunit.PlainAntUnitListener"/>
|
||||
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue