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
diff --git a/changes.xml b/changes.xml
index 1b9e595..d836905 100644
--- a/changes.xml
+++ b/changes.xml
@@ -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">
diff --git a/docs/assertions.html b/docs/assertions.html
index da53a51..e56d04f 100644
--- a/docs/assertions.html
+++ b/docs/assertions.html
@@ -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 @@
</au:assertTrue>
</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>
+ <file file="${ant.home}/lib/ant.jar" id="ant.jar"/>
+ <assertRefResourceExists refid="ant.jar"/>
+ </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>
+ <assertNestedResourceExists>
+ <file file="${ant.home}/lib/ant.jar"/>
+ </assertNestedResourceExists>
+ </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 @@
</au:assertFalse>
</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>
+ <file file="${ant.home}/lib/ant.jar" id="ant.jar"/>
+ <assertRefResourceDoesntExist refid="ant.jar"/>
+ </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>
+ <assertNestedResourceDoesntExist>
+ <file file="${ant.home}/lib/ant.jar"/>
+ </assertNestedResourceDoesntExist>
+ </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>
diff --git a/src/main/org/apache/ant/antunit/ResourceExists.java b/src/main/org/apache/ant/antunit/ResourceExists.java
index 4284abc..53d9e56 100644
--- a/src/main/org/apache/ant/antunit/ResourceExists.java
+++ b/src/main/org/apache/ant/antunit/ResourceExists.java
@@ -24,6 +24,7 @@
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 @@
*/
public class ResourceExists extends ProjectComponent implements Condition {
private Resource resource;
+ private String refid;
/**
* The resource to check as attribute.
@@ -40,13 +42,20 @@
* 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.
*
* <p>Exactly one resource must be specfied either as attribute or
@@ -57,9 +66,39 @@
}
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;
}
}
diff --git a/src/main/org/apache/ant/antunit/antlib.xml b/src/main/org/apache/ant/antunit/antlib.xml
index 932258c..dce4f70 100644
--- a/src/main/org/apache/ant/antunit/antlib.xml
+++ b/src/main/org/apache/ant/antunit/antlib.xml
@@ -197,6 +197,81 @@
</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"/>
diff --git a/src/tests/antunit/assertResourceContains-test.xml b/src/tests/antunit/assertResourceContains-test.xml
new file mode 100644
index 0000000..4892625
--- /dev/null
+++ b/src/tests/antunit/assertResourceContains-test.xml
@@ -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>
diff --git a/src/tests/antunit/assertResourceExists-test.xml b/src/tests/antunit/assertResourceExists-test.xml
index 3810828..b7b3306 100644
--- a/src/tests/antunit/assertResourceExists-test.xml
+++ b/src/tests/antunit/assertResourceExists-test.xml
@@ -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>
diff --git a/src/tests/antunit/resourceexists-test.xml b/src/tests/antunit/resourceexists-test.xml
index 40f58da..a2360c4 100644
--- a/src/tests/antunit/resourceexists-test.xml
+++ b/src/tests/antunit/resourceexists-test.xml
@@ -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>