PR 53383 add new assertions that really work with non-file resoucres

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@1591586 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stefan Bodewig 2014-05-01 09:57:49 +00:00
parent 039c52761e
commit 612c532036
7 changed files with 448 additions and 23 deletions

View File

@ -38,6 +38,14 @@
</properties>
<release version="1.3" date="not-released">
<action type="add" issue="53383">
New assertion assertRefResourceExists,
assertRefResourceDoesntExist, assertRefResourceContains,
assertRefResourceDoesntContain, assertNestedResourceExists,
assertNestedResourceDoesntExist have been added. These new
assertions work for non-filesystem resources unlike their
existing cousins of AntUnit 1.2.
</action>
</release>
<release version="1.2" date="2011-08-16">

View File

@ -289,9 +289,12 @@
<p><em>Since AntUnit 1.2</em></p>
<p>Asserts that a given resource exists. This is a
generalization of assertFileExists and allows to test for
arbitrary resources.</p>
<p>Asserts that a given resource exists. This was intended as a
generalization of <code>assertFileExists</code> but still really
only works for file resources.
Use <code>assertNestedResourceExists</code>
or <code>assertRefResourceExists</code> for non-filesystem
resources.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@ -337,13 +340,94 @@
&lt;/au:assertTrue&gt;
</pre>
<h2><a name="assertRefResourceExists">assertRefResourceExists</a></h2>
<p><em>Since AntUnit 1.3</em></p>
<p>Asserts that a resource given by its id exists.</p>
<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">refid</td>
<td valign="top">Reference to a resource defined inside the project.</td>
<td valign="top" align="center">Yes</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
resource '<em>refid</em>' to exist".</td>
<td align="center">No.</td>
</tr>
</table>
<h3>Examples</h3>
<p>Make the build fail if the resource ${ant.home}/lib/ant.jar doesn't
exist:</p>
<pre>
&lt;file file="${ant.home}/lib/ant.jar" id="ant.jar"/&gt;
&lt;assertRefResourceExists refid="ant.jar"/&gt;
</pre>
<h3>Ant 1.7.x Note</h3>
<p><code>assertRefResourceExists</code> relies on Ant 1.8.0 or later,
it doesn't work with Ant 1.7.x.</p>
<h2><a name="assertNestedResourceExists">assertNestedResourceExists</a></h2>
<p><em>Since AntUnit 1.3</em></p>
<p>Asserts that a resource given as nested element exists.</p>
<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 resource to exist".</td>
<td align="center">No.</td>
</tr>
</table>
<h3>Examples</h3>
<p>Make the build fail if the resource ${ant.home}/lib/ant.jar doesn't
exist:</p>
<pre>
&lt;assertNestedResourceExists&gt;
&lt;file file="${ant.home}/lib/ant.jar"/&gt;
&lt;/assertNestedResourceExists&gt;
</pre>
<h3>Ant 1.7.x Note</h3>
<p><code>assertNestedResourceExists</code> relies on Ant 1.8.0 or later,
it doesn't work with Ant 1.7.x.</p>
<h2><a name="assertResourceDoesntExist">assertResourceDoesntExist</a></h2>
<p><em>Since AntUnit 1.2</em></p>
<p>Asserts that a given resource does not exist. This is a
generalization of assertFileDoesntExist and allows to test for
arbitrary resources.</p>
<p>Asserts that a given resource does not exist. This was
intended as a generalization
of <code>assertFileDoesntExist</code> but still really only
works for file resources.
Use <code>assertNestedResourceDoesntExist</code>
or <code>assertRefResourceDoesntExist</code> for non-filesystem
resources.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@ -389,6 +473,84 @@
&lt;/au:assertFalse&gt;
</pre>
<h2><a name="assertRefResourceDoesntExist">assertRefResourceDoesntExist</a></h2>
<p><em>Since AntUnit 1.3</em></p>
<p>Asserts that a resource given by its id exists.</p>
<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">refid</td>
<td valign="top">Reference to a resource defined inside the project.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">message</td>
<td valign="top">Message for the exception if the condition
doesn't hold true. Defaults to "Didn't expect
resource '<em>refid</em>' to exist".</td>
<td align="center">No.</td>
</tr>
</table>
<h3>Examples</h3>
<p>Make the build fail if the resource ${ant.home}/lib/ant.jar
exists:</p>
<pre>
&lt;file file="${ant.home}/lib/ant.jar" id="ant.jar"/&gt;
&lt;assertRefResourceDoesntExist refid="ant.jar"/&gt;
</pre>
<h3>Ant 1.7.x Note</h3>
<p><code>assertRefResourceDoesntExist</code> relies on Ant 1.8.0 or later,
it doesn't work with Ant 1.7.x.</p>
<h2><a name="assertNestedResourceDoesntExist">assertNestedResourceDoesntExist</a></h2>
<p><em>Since AntUnit 1.3</em></p>
<p>Asserts that a resource given as nested element exists.</p>
<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 "Didn't expect
resource to exist".</td>
<td align="center">No.</td>
</tr>
</table>
<h3>Examples</h3>
<p>Make the build fail if the resource ${ant.home}/lib/ant.jar
exists:</p>
<pre>
&lt;assertNestedResourceDoesntExist&gt;
&lt;file file="${ant.home}/lib/ant.jar"/&gt;
&lt;/assertNestedResourceDoesntExist&gt;
</pre>
<h3>Ant 1.7.x Note</h3>
<p><code>assertNestedResourceDoesntExist</code> relies on Ant 1.8.0 or later,
it doesn't work with Ant 1.7.x.</p>
<h2><a name="assertDestIsUptodate">assertDestIsUptodate</a></h2>
<p>Asserts that a dest file is more recent than the source
@ -786,7 +948,10 @@
<h2><a name="assertResourceContains">assertResourceContains</a></h2>
<p>Asserts that a resource's content includes a given string.</p>
<p>Asserts that a resource's content includes a given string.
This task anly really works for filesystem resources.
Use <code>assertRefResourceContains</code> when using non-file
resources.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@ -822,9 +987,51 @@
</tr>
</table>
<h2><a name="assertRefResourceContains">assertRefResourceContains</a></h2>
<p><em>Since AntUnit 1.3</em></p>
<p>Asserts that a resource's content includes a given string.</p>
<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">refid</td>
<td valign="top">Reference to a resource defined inside the project.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">value</td>
<td valign="top">The text to search for.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">casesensitive</td>
<td valign="top">
Perform a case sensitive match.
Default is true.
</td>
<td align="center">No.</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 resource
'<em>refeid</em>' to contain value '<em>value</em>'."</td>
<td align="center">No.</td>
</tr>
</table>
<h2><a name="assertResourceDoesntContain">assertResourceDoesntContain</a></h2>
<p>Asserts that a resource's content doesn't include a given string.</p>
<p>Asserts that a resource's content doesn't include a given string.
This task anly really works for filesystem resources.
Use <code>assertRefResourceDoesntContain</code> when using non-file
resources.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@ -860,6 +1067,45 @@
</tr>
</table>
<h2><a name="assertRefResourceDoesntContain">assertRefResourceDoesntContain</a></h2>
<p><em>Since AntUnit 1.3</em></p>
<p>Asserts that a resource's content doesn't include a given string.</p>
<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">refid</td>
<td valign="top">Reference to a resource defined inside the project.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">value</td>
<td valign="top">The text to search for.</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">casesensitive</td>
<td valign="top">
Perform a case sensitive match.
Default is true.
</td>
<td align="center">No.</td>
</tr>
<tr>
<td valign="top">message</td>
<td valign="top">Message for the exception if the condition
doesn't hold true. Defaults to "Didn't expect resource
'<em>refid</em>' to contain value '<em>value</em>'".</td>
<td align="center">No.</td>
</tr>
</table>
<hr/>
</body>
</html>

View File

@ -24,6 +24,7 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.taskdefs.condition.Condition;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
/**
* A condition that tests whether a given resource exists.
@ -32,6 +33,7 @@ import org.apache.tools.ant.types.Resource;
*/
public class ResourceExists extends ProjectComponent implements Condition {
private Resource resource;
private String refid;
/**
* The resource to check as attribute.
@ -40,12 +42,19 @@ public class ResourceExists extends ProjectComponent implements Condition {
* nested element.</p>
*/
public void setResource(Resource r) {
if (resource != null) {
throw new BuildException("Only one resource can be tested.");
}
onlyOne();
resource = r;
}
/**
* The resource to check as a refid.
* @since AntUnit 1.3
*/
public void setRefid(String refid) {
onlyOne();
this.refid = refid;
}
/**
* The resource to check as nested element.
*
@ -57,9 +66,39 @@ public class ResourceExists extends ProjectComponent implements Condition {
}
public boolean eval() {
if (resource == null) {
if (resource == null && refid == null) {
throw new BuildException("You must specify a resource.");
}
return resource.isExists();
Resource r = resource != null ? resource : expandRefId();
return r.isExists();
}
private void onlyOne() {
if (resource != null || refid != null) {
throw new BuildException("Only one resource can be tested.");
}
}
// logic stolen from Ant's ResourceContains condition
private Resource expandRefId() {
if (getProject() == null) {
throw new BuildException("Cannot retrieve refid; project unset");
}
Object o = getProject().getReference(refid);
if (!(o instanceof Resource)) {
if (o instanceof ResourceCollection) {
ResourceCollection rc = (ResourceCollection) o;
if (rc.size() == 1) {
o = rc.iterator().next();
} else {
throw new BuildException("Referred resource collection must"
+ " contain exactly one resource.");
}
} else {
throw new BuildException("'" + refid + "' is not a resource but "
+ String.valueOf(o));
}
}
return (Resource) o;
}
}

View File

@ -197,6 +197,81 @@ under the License.
</sequential>
</macrodef>
<macrodef name="assertNestedResourceExists" backtrace="false">
<attribute name="message" default="Expected resource to exist"/>
<element name="nested-resource" implicit="true"/>
<sequential>
<au:fail message="@{message}">
<au:resourceExists>
<nested-resource/>
</au:resourceExists>
</au:fail>
</sequential>
</macrodef>
<macrodef name="assertNestedResourceDoesntExist" backtrace="false">
<attribute name="message" default="Didn't expect resource to exist"/>
<element name="nested-resource" implicit="true"/>
<sequential>
<au:assertFalse message="@{message}">
<au:resourceExists>
<nested-resource/>
</au:resourceExists>
</au:assertFalse>
</sequential>
</macrodef>
<macrodef name="assertRefResourceExists" backtrace="false">
<attribute name="refid"/>
<attribute name="message"
default="Expected resource '@{refid}' to exist"/>
<sequential>
<au:fail message="@{message}">
<au:resourceExists refid="@{refid}"/>
</au:fail>
</sequential>
</macrodef>
<macrodef name="assertRefResourceDoesntExist" backtrace="false">
<attribute name="refid"/>
<attribute name="message"
default="Didn't expect resource '@{refid}' to exist"/>
<sequential>
<au:assertFalse message="@{message}">
<au:resourceExists refid="@{refid}"/>
</au:assertFalse>
</sequential>
</macrodef>
<macrodef name="assertRefResourceContains">
<attribute name="refid"/>
<attribute name="value"/>
<attribute name="casesensitive" default="true"/>
<attribute name="message"
default="Expected resource '@{refid}' to contain value '@{value}'"/>
<sequential>
<au:assertRefResourceExists refid="@{refid}"/>
<au:fail message="@{message}">
<resourcecontains refid="@{refid}" substring="@{value}"
casesensitive="@{casesensitive}"/>
</au:fail>
</sequential>
</macrodef>
<macrodef name="assertRefResourceDoesntContain">
<attribute name="refid"/>
<attribute name="value"/>
<attribute name="casesensitive" default="true"/>
<attribute name="message"
default="Didn't expect resource '@{refid}' to contain value '@{value}'"/>
<sequential>
<au:assertFalse message="@{message}">
<resourcecontains refid="@{refid}" substring="@{value}"
casesensitive="@{casesensitive}"/>
</au:assertFalse>
</sequential>
</macrodef>
<macrodef name="assertDestIsUptodate" backtrace="false">
<attribute name="src"/>
<attribute name="dest"/>

View File

@ -0,0 +1,34 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 xmlns:au="antlib:org.apache.ant.antunit" default="antunit">
<import file="antunit-base.xml"/>
<url url="http://ant.apache.org/index.html" id="antsite"/>
<target name="testURLUsingRef"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=53383">
<au:assertRefResourceContains refid="antsite" value="Apache Ant"/>
</target>
<target name="testURLUsingRef-no"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=53383">
<au:assertRefResourceDoesntContain refid="antsite" value="Foo"/>
</target>
</project>

View File

@ -19,15 +19,30 @@
<import file="antunit-base.xml"/>
<target name="NOtestURL"
<target name="testURL"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=53383">
<au:assertResourceExists resource="http://ant.apache.org/index.html"/>
<au:assertNestedResourceExists>
<url url="http://ant.apache.org/index.html"/>
</au:assertNestedResourceExists>
</target>
<target name="NOtestURLUsingRef"
<target name="testURLUsingRef"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=53383">
<url url="http://ant.apache.org/index.html" id="antsite"/>
<au:assertResourceExists resource="${ant.refid:antsite}"/>
<au:assertRefResourceExists refid="antsite"/>
</target>
<target name="testURL-no"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=53383">
<au:assertNestedResourceDoesntExist>
<url url="http://ant.apache.org/foo.html"/>
</au:assertNestedResourceDoesntExist>
</target>
<target name="testURLUsingRef-no"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=53383">
<url url="http://ant.apache.org/foo.html" id="antsite-foo"/>
<au:assertRefResourceDoesntExist refid="antsite-foo"/>
</target>
</project>

View File

@ -27,6 +27,20 @@
</au:assertTrue>
</target>
<target name="test-refid-file">
<file file="resourceexists-test.xml" id="id1"/>
<au:assertTrue>
<au:resourceExists refid="id1"/>
</au:assertTrue>
</target>
<target name="test-refid-fileset">
<fileset file="resourceexists-test.xml" id="id2"/>
<au:assertTrue>
<au:resourceExists refid="id2"/>
</au:assertTrue>
</target>
<target name="test-no">
<au:assertFalse>
<au:resourceExists>
@ -43,12 +57,6 @@
</au:assertTrue>
</target>
<target name="NOtestURLAttribute">
<au:assertTrue>
<au:resourceExists resource="http://ant.apache.org/index.html"/>
</au:assertTrue>
</target>
<target name="testURLAttributeUsingRef">
<url url="http://ant.apache.org/index.html" id="antsite"/>
<au:assertTrue>