new assertResourceExists and assertResourceDoesntExist conditions

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@1063181 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stefan Bodewig 2011-01-25 09:29:02 +00:00
parent 685acd90c6
commit 7a4cc46b0a
7 changed files with 199 additions and 1 deletions

2
NOTICE
View File

@ -1,5 +1,5 @@
Apache AntUnit
Copyright 2005-2007 The Apache Software Foundation
Copyright 2005-2011 The Apache Software Foundation
This product includes software developed by
The Apache Software Foundation (http://www.apache.org/).

View File

@ -38,6 +38,10 @@
</properties>
<release version="1.2" date="not-released">
<action type="add">
New assertion assertResourceExists and assertResourceDoesntExist
have been added.
</action>
<action type="fix" breaks-bwc="true">
The XSLT stylesheets now create HTML files instead of plain text
for logs sent to System.err and System.out. You can always get

View File

@ -285,6 +285,80 @@
&lt;assertFileDoesntExist file="${ant.home}/lib/ant.jar"/&gt;
</pre>
<h2><a name="assertResourceExists">assertResourceExists</a></h2>
<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>
<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">resource</td>
<td valign="top">Location of the resource.</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>resource</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;assertResourceExists resource="${ant.home}/lib/ant.jar"/&gt;
</pre>
<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>
<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">resource</td>
<td valign="top">Location of the resource.</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>resource</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;assertResourceDoesntExist resource="${ant.home}/lib/ant.jar"/&gt;
</pre>
<h2><a name="assertDestIsUptodate">assertDestIsUptodate</a></h2>
<p>Asserts that a dest file is more recent than the source

View File

@ -136,6 +136,22 @@ under the License.
<au:assertFileDoesntExist file="assert.xml"/>
</target>
<target name="assertResourceExistsPass">
<au:assertResourceExists resource="assert.xml"/>
</target>
<target name="assertResourceExistsFail">
<au:assertResourceExists resource="assert.txt"/>
</target>
<target name="assertResourceDoesntExistPass">
<au:assertResourceDoesntExist resource="assert.txt"/>
</target>
<target name="assertResourceDoesntExistFail">
<au:assertResourceDoesntExist resource="assert.xml"/>
</target>
<target name="assertDestIsUptodatePass">
<au:assertDestIsUptodate
src="../../main/org/apache/ant/antunit/AssertTask.java"

View File

@ -0,0 +1,65 @@
/*
* 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.
*
*/
package org.apache.ant.antunit;
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;
/**
* A condition that tests whether a given resource exists.
*
* @since AntUnit 1.2
*/
public class ResourceExists extends ProjectComponent implements Condition {
private Resource resource;
/**
* The resource to check as attribute.
*
* <p>Exactly one resource must be specfied either as attribute or
* nested element.</p>
*/
public void setResource(Resource r) {
if (resource != null) {
throw new BuildException("Only one resource can be tested.");
}
resource = r;
}
/**
* The resource to check as nested element.
*
* <p>Exactly one resource must be specfied either as attribute or
* nested element.</p>
*/
public void add(Resource r) {
setResource(r);
}
public boolean eval() {
if (resource == null) {
throw new BuildException("You must specify a resource.");
}
return resource.isExists();
}
}

View File

@ -45,6 +45,9 @@ under the License.
<typedef name="logcontent"
classname="org.apache.ant.antunit.LogContent" />
<typedef name="resourcExists"
classname="org.apache.ant.antunit.ResourceExists"/>
<!-- Actually just an alias of fail that expects a condition -->
<macrodef name="assertTrue" backtrace="false">
<attribute name="message" default="Assertion failed"/>
@ -143,6 +146,28 @@ under the License.
</sequential>
</macrodef>
<macrodef name="assertResourceExists" backtrace="false">
<attribute name="resource"/>
<attribute name="message"
default="Expected resource '@{resource}' to exist"/>
<sequential>
<au:fail message="@{message}">
<au:resourceExists resource="@{resource}"/>
</au:fail>
</sequential>
</macrodef>
<macrodef name="assertResourceDoesntExist" backtrace="false">
<attribute name="resource"/>
<attribute name="message"
default="Didn't expect resource '@{resource}' to exist"/>
<sequential>
<au:assertFalse message="@{message}">
<au:resourceExists resource="@{resource}"/>
</au:assertFalse>
</sequential>
</macrodef>
<macrodef name="assertResourceContains">
<attribute name="resource"/>
<attribute name="value"/>

View File

@ -64,6 +64,12 @@ public class AssertTest extends BuildFileTest {
public void testFileDoesntExistPass() {
testPass("assertFileDoesntExistPass");
}
public void testResourceExistsPass() {
testPass("assertResourceExistsPass");
}
public void testResourceDoesntExistPass() {
testPass("assertResourceDoesntExistPass");
}
public void testDestIsUptodatePass() {
testPass("assertDestIsUptodatePass");
}
@ -121,6 +127,14 @@ public class AssertTest extends BuildFileTest {
testFail("assertFileDoesntExistFail",
"Didn't expect file 'assert.xml' to exist");
}
public void testResourceExistsFail() {
testFail("assertResourceExistsFail",
"Expected resource 'assert.txt' to exist");
}
public void testResourceDoesntExistFail() {
testFail("assertResourceDoesntExistFail",
"Didn't expect resource 'assert.xml' to exist");
}
public void testDestIsUptodateFail() {
testFail("assertDestIsUptodateFail",
"Expected '../../main/org/apache/ant/antunit/AssertTask.java' to be more recent than '../../../build/classes/org/apache/ant/antunit/AssertTask.class'");