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