diff --git a/changes.xml b/changes.xml index 1b9e595..d836905 100644 --- a/changes.xml +++ b/changes.xml @@ -38,6 +38,14 @@ + + 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. + 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 @@

Since AntUnit 1.2

-

Asserts that a given resource exists. This is a - generalization of assertFileExists and allows to test for - arbitrary resources.

+

Asserts that a given resource exists. This was intended as a + generalization of assertFileExists but still really + only works for file resources. + Use assertNestedResourceExists + or assertRefResourceExists for non-filesystem + resources.

@@ -337,13 +340,94 @@ </au:assertTrue> +

assertRefResourceExists

+ +

Since AntUnit 1.3

+ +

Asserts that a resource given by its id exists.

+ +
+ + + + + + + + + + + + + + + +
AttributeDescriptionRequired
refidReference to a resource defined inside the project.Yes
messageMessage for the exception if the condition + doesn't hold true. Defaults to "Expected + resource 'refid' to exist".No.
+ +

Examples

+ +

Make the build fail if the resource ${ant.home}/lib/ant.jar doesn't + exist:

+ +
+      <file file="${ant.home}/lib/ant.jar" id="ant.jar"/>
+      <assertRefResourceExists refid="ant.jar"/>
+    
+ +

Ant 1.7.x Note

+ +

assertRefResourceExists relies on Ant 1.8.0 or later, + it doesn't work with Ant 1.7.x.

+ +

assertNestedResourceExists

+ +

Since AntUnit 1.3

+ +

Asserts that a resource given as nested element exists.

+ + + + + + + + + + + + +
AttributeDescriptionRequired
messageMessage for the exception if the condition + doesn't hold true. Defaults to "Expected resource to exist".No.
+ +

Examples

+ +

Make the build fail if the resource ${ant.home}/lib/ant.jar doesn't + exist:

+ +
+      <assertNestedResourceExists>
+        <file file="${ant.home}/lib/ant.jar"/>
+      </assertNestedResourceExists>
+    
+ +

Ant 1.7.x Note

+ +

assertNestedResourceExists relies on Ant 1.8.0 or later, + it doesn't work with Ant 1.7.x.

+

assertResourceDoesntExist

Since AntUnit 1.2

-

Asserts that a given resource does not exist. This is a - generalization of assertFileDoesntExist and allows to test for - arbitrary resources.

+

Asserts that a given resource does not exist. This was + intended as a generalization + of assertFileDoesntExist but still really only + works for file resources. + Use assertNestedResourceDoesntExist + or assertRefResourceDoesntExist for non-filesystem + resources.

@@ -389,6 +473,84 @@ </au:assertFalse> +

assertRefResourceDoesntExist

+ +

Since AntUnit 1.3

+ +

Asserts that a resource given by its id exists.

+ +
+ + + + + + + + + + + + + + + +
AttributeDescriptionRequired
refidReference to a resource defined inside the project.Yes
messageMessage for the exception if the condition + doesn't hold true. Defaults to "Didn't expect + resource 'refid' to exist".No.
+ +

Examples

+ +

Make the build fail if the resource ${ant.home}/lib/ant.jar + exists:

+ +
+      <file file="${ant.home}/lib/ant.jar" id="ant.jar"/>
+      <assertRefResourceDoesntExist refid="ant.jar"/>
+    
+ +

Ant 1.7.x Note

+ +

assertRefResourceDoesntExist relies on Ant 1.8.0 or later, + it doesn't work with Ant 1.7.x.

+ +

assertNestedResourceDoesntExist

+ +

Since AntUnit 1.3

+ +

Asserts that a resource given as nested element exists.

+ + + + + + + + + + + + +
AttributeDescriptionRequired
messageMessage for the exception if the condition + doesn't hold true. Defaults to "Didn't expect + resource to exist".No.
+ +

Examples

+ +

Make the build fail if the resource ${ant.home}/lib/ant.jar + exists:

+ +
+      <assertNestedResourceDoesntExist>
+        <file file="${ant.home}/lib/ant.jar"/>
+      </assertNestedResourceDoesntExist>
+    
+ +

Ant 1.7.x Note

+ +

assertNestedResourceDoesntExist relies on Ant 1.8.0 or later, + it doesn't work with Ant 1.7.x.

+

assertDestIsUptodate

Asserts that a dest file is more recent than the source @@ -786,7 +948,10 @@

assertResourceContains

-

Asserts that a resource's content includes a given string.

+

Asserts that a resource's content includes a given string. + This task anly really works for filesystem resources. + Use assertRefResourceContains when using non-file + resources.

@@ -822,9 +987,51 @@
+

assertRefResourceContains

+ +

Since AntUnit 1.3

+ +

Asserts that a resource's content includes a given string.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
refidReference to a resource defined inside the project.Yes
valueThe text to search for.Yes
casesensitive + Perform a case sensitive match. + Default is true. + No.
messageMessage for the exception if the condition + doesn't hold true. Defaults to "Expected resource + 'refeid' to contain value 'value'."No.
+

assertResourceDoesntContain

-

Asserts that a resource's content doesn't include a given string.

+

Asserts that a resource's content doesn't include a given string. + This task anly really works for filesystem resources. + Use assertRefResourceDoesntContain when using non-file + resources.

@@ -860,6 +1067,45 @@
+

assertRefResourceDoesntContain

+ +

Since AntUnit 1.3

+ +

Asserts that a resource's content doesn't include a given string.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
refidReference to a resource defined inside the project.Yes
valueThe text to search for.Yes
casesensitive + Perform a case sensitive match. + Default is true. + No.
messageMessage for the exception if the condition + doesn't hold true. Defaults to "Didn't expect resource + 'refid' to contain value 'value'".No.
+
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.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.

*/ 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; } } 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 @@ under the License. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 @@ + + + + + + + + + + + + + + + + + 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 @@ - - + + + - - + + + + + + + + + + + + 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 @@ + + + + + + + + + + + + + + @@ -43,12 +57,6 @@ - - - - - -