1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>
Assert Task</title>
<meta content="DocBook XSL Stylesheets V1.60.1" name="generator">
<link rel="home" href="index.html" title="Antelope Users Guide">
<link rel="up" href="bk03.html" title="Additional Ant Tasks">
<link rel="previous" href="bk03ch03.html" title="Chapter 3. Installation">
<link rel="next" href="bk03ch05.html" title="Chapter 5. If Task">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="chapter" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title">
<a name="Assert">
</a>
Assert Task</h2>
</div>
</div>
<div>
</div>
</div>
<p>
The Assert task adds an assertion capability to Ant projects. This task works in a manner
very similar to the Java <tt class="computeroutput">assert</tt> keyword, and provides a
limited "design by contract" facility to Ant. This is very useful for testing build
scripts prior to putting them into production.
</p>
<p>
The Assert task verifies that a particular boolean condition is met, and throws a BuildException
if it is not.
</p>
<p>
Also like Java's <tt class="computeroutput">assert</tt> keyword, the Assert task must be
'turned on' using the property <tt class="computeroutput">ant.enable.asserts</tt>
. If not set, or is set to <tt class="computeroutput"> false</tt>
, the Assert task works exactly like the Sequential task. If the <a href="variable_task.html" title="Variable Task">
Variable task</a>
is used to define this property, then it can be turned on and off as needed throughout a
build.
</p>
<p>
This task can hold other tasks including Assert.
</p>
<p>
Thie assert task may contain a single conditional element known by ANT, or one of
the following additional conditions:
<a href="more_conditions.html" title="More Conditions">
IsPropertyTrue</a>
, <a href="more_conditions.html" title="More Conditions">
IsPropertyFalse</a>
,
<a href="more_conditions.html" title="More Conditions">
StartsWith</a>
,
<a href="more_conditions.html" title="More Conditions">
EndsWith</a>
,
<a href="more_conditions.html" title="More Conditions">
IsGreaterThan</a>
,
<a href="more_conditions.html" title="More Conditions">
IsLessThan</a>
and conditions.
See the If task for examples of using these conditionals.
</p>
<p>
<div class="table">
<a name="N10583">
</a>
<p class="title">
<b>
Table 4.1. Assert Task Attributes</b>
</p>
<table summary="Assert Task Attributes" border="1">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>
Attribute</th>
<th>
Description</th>
<th>
Default</th>
<th>
Required</th>
</tr>
</thead>
<tbody>
<tr>
<td>
name</td>
<td>
The name of the property to test for. This is a shortcut for specifying an <equals>
condition.</td>
<td>
none</td>
<td>
No. However, if specified, the 'value' attribute must also be present</td>
</tr>
<tr>
<td>
value</td>
<td>
The value to test for, implies . If the value in the project is different than this value,
a BuildException will be thrown and the build will stop.</td>
<td>
none</td>
<td>
No, unless the 'name' attribute is specified.</td>
</tr>
<tr>
<td>
execute</td>
<td>
Should the tasks contained in this task be executed? It may be useful to set this to false when testing build files.</td>
<td>
True</td>
<td>
No</td>
</tr>
<tr>
<td>
failonerror</td>
<td>
Should the build halt if the assertion fails? Setting this to false is contrary to the intented use of assertions, but may be useful in certain situations. </td>
<td>
True</td>
<td>
No</td>
</tr>
</tbody>
</table>
</div>
</p>
<h2>
Examples
</h2>
<p>
In the following example, the first <tt class="computeroutput">
assert</tt>
task checks that the <tt class="computeroutput">
wait</tt>
property exists and does not execute the <tt class="computeroutput">
echo</tt>
and <tt class="computeroutput">
sleep</tt>
tasks. The second <tt class="computeroutput">
assert</tt>
task checks that the <tt class="computeroutput">
wait</tt>
property exists, has a value of 2, and executes the <tt class="computeroutput">
echo</tt>
task.
</p>
<p>
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
<property name="wait" value="2"/>
<assert execute="false">
<isset property="wait" />
<sequential>
<echo>
Waiting ${wait} seconds...
Click the red button to stop waiting.
</echo>
<sleep seconds="${wait}"/>
</sequential>
</assert>
<assert name="wait" value="2" execute="true">
<sequential>
<echo>done waiting!</echo>
</sequential>
</assert>
</pre>
</td>
</tr>
</table>
</p>
<p>
The next example shows Assert being used in a unit test for the "limit" task:
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
<property name="ant.enable.asserts" value="true"/>
<target name="test2">
<!-- should not stop 'sleep' task, should print out '_passed_' -->
<stopwatch name="timer"/>
<limit maxwait="5">
<sleep seconds="1"/>
<echo>_passed_</echo>
</limit>
<stopwatch name="timer" action="total"/>
<assert message="Too much time.">
<islessthan arg1="${timer}" arg2="2"/>
</assert>
</target>
</pre>
</td>
</tr>
</table>
</p>
<p>
If the <tt class="computeroutput">
ant.enable.asserts</tt>
property is set to false, then in the above example, the <tt class="computeroutput">
echo</tt>
, <tt class="computeroutput">
sleep</tt>
, and <tt class="computeroutput">
echo</tt>
tasks will all execute.
</p>
<hr>
<p align="center">Copyright © 2003 Ant-Contrib Project. All
rights Reserved.</p>
</div>
</body>
</html>
|