Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1777 | jmachado | 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 very similar to the Java <tt class="computeroutput"> |
||
29 | assert</tt> |
||
30 | 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. |
||
31 | </p> |
||
32 | <p> |
||
33 | |||
34 | The Assert task verifies that a given property has a |
||
35 | given value and throws a BuildException if the property value is not as expected |
||
36 | or the property does not exist. |
||
37 | </p> |
||
38 | <p> |
||
39 | |||
40 | Also like Java's <tt class="computeroutput"> |
||
41 | assert</tt> |
||
42 | keyword, the Assert task must be 'turned on' using the property <tt class="computeroutput"> |
||
43 | ant.enable.asserts</tt> |
||
44 | . If not set, or is set to <tt class="computeroutput"> |
||
45 | false</tt> |
||
46 | , the Assert task works exactly like the Sequential task. If the <a href="variable_task.html" title="Variable Task"> |
||
47 | Variable task</a> |
||
48 | is used to define this property, then it can be turned on and off as needed throughout a build. |
||
49 | </p> |
||
50 | <p> |
||
51 | |||
52 | This task can hold other tasks including Assert. |
||
53 | </p> |
||
54 | <p> |
||
55 | |||
56 | The Assert task may contain one 'bool' element. The 'bool' element is identical to the ConditionTask, but unlike the ConditionTask, is actually a Task. The 'bool' element can contain all the conditions permitted by the ConditionTask, plus the <a href="bk03ch05s02.html" title="More Conditions"> |
||
57 | IsPropertyTrue</a> |
||
58 | , <a href="more_conditions.html" title="More Conditions"> |
||
59 | IsPropertyFalse</a> |
||
60 | , |
||
61 | <a href="more_conditions.html" title="More Conditions"> |
||
62 | StartsWith</a> |
||
63 | , |
||
64 | <a href="more_conditions.html" title="More Conditions"> |
||
65 | EndsWith</a> |
||
66 | , |
||
67 | <a href="more_conditions.html" title="More Conditions"> |
||
68 | IsGreaterThan</a> |
||
69 | , |
||
70 | <a href="more_conditions.html" title="More Conditions"> |
||
71 | IsLessThan</a> |
||
72 | and conditions. |
||
73 | See the If task for examples of using these conditionals. |
||
74 | </p> |
||
75 | |||
76 | <p> |
||
77 | |||
78 | <div class="table"> |
||
79 | <a name="N10583"> |
||
80 | </a> |
||
81 | <p class="title"> |
||
82 | <b> |
||
83 | Table 4.1. Assert Task Attributes</b> |
||
84 | </p> |
||
85 | <table summary="Assert Task Attributes" border="1"> |
||
86 | <colgroup> |
||
87 | <col> |
||
88 | <col> |
||
89 | <col> |
||
90 | <col> |
||
91 | </colgroup> |
||
92 | <thead> |
||
93 | <tr> |
||
94 | <th> |
||
95 | Attribute</th> |
||
96 | <th> |
||
97 | Description</th> |
||
98 | <th> |
||
99 | Default</th> |
||
100 | <th> |
||
101 | Required</th> |
||
102 | </tr> |
||
103 | </thead> |
||
104 | <tbody> |
||
105 | <tr> |
||
106 | <td> |
||
107 | name</td> |
||
108 | <td> |
||
109 | The name of the property to test for.</td> |
||
110 | <td> |
||
111 | none</td> |
||
112 | <td> |
||
113 | Yes</td> |
||
114 | </tr> |
||
115 | <tr> |
||
116 | <td> |
||
117 | exists</td> |
||
118 | <td> |
||
119 | Test for existence or non-existence of the property.</td> |
||
120 | <td> |
||
121 | True</td> |
||
122 | <td> |
||
123 | No</td> |
||
124 | </tr> |
||
125 | <tr> |
||
126 | <td> |
||
127 | value</td> |
||
128 | <td> |
||
129 | The value to test for, implies 'exists=true'. If the value in the project is different than this value, a BuildException will be thrown and the build will stop.</td> |
||
130 | <td> |
||
131 | none</td> |
||
132 | <td> |
||
133 | No</td> |
||
134 | </tr> |
||
135 | <tr> |
||
136 | <td> |
||
137 | execute</td> |
||
138 | <td> |
||
139 | Should the tasks contained in this task be executed? It may be useful to set this to false when testing build files.</td> |
||
140 | <td> |
||
141 | True</td> |
||
142 | <td> |
||
143 | No</td> |
||
144 | </tr> |
||
145 | <tr> |
||
146 | <td> |
||
147 | failonerror</td> |
||
148 | <td> |
||
149 | 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> |
||
150 | <td> |
||
151 | True</td> |
||
152 | <td> |
||
153 | No</td> |
||
154 | </tr> |
||
155 | </tbody> |
||
156 | </table> |
||
157 | </div> |
||
158 | |||
159 | |||
160 | </p> |
||
161 | <p> |
||
162 | |||
163 | As stated above, the Assert task may contain a nested "bool" task, otherwise, |
||
164 | the Assert task does not support any nested |
||
165 | elements apart from Ant tasks. Any valid Ant task may be embedded within the |
||
166 | assert task. |
||
167 | </p> |
||
168 | <p> |
||
169 | |||
170 | In the following example, the first <tt class="computeroutput"> |
||
171 | assert</tt> |
||
172 | task checks that the <tt class="computeroutput"> |
||
173 | wait</tt> |
||
174 | property exists and does not execute the <tt class="computeroutput"> |
||
175 | echo</tt> |
||
176 | and <tt class="computeroutput"> |
||
177 | sleep</tt> |
||
178 | tasks. The second <tt class="computeroutput"> |
||
179 | assert</tt> |
||
180 | task checks that the <tt class="computeroutput"> |
||
181 | wait</tt> |
||
182 | property exists, has a value of 2, and executes the <tt class="computeroutput"> |
||
183 | echo</tt> |
||
184 | task. |
||
185 | </p> |
||
186 | <p> |
||
187 | |||
188 | <table border="0" bgcolor="#E0E0E0"> |
||
189 | <tr> |
||
190 | <td> |
||
191 | <pre class="programlisting"> |
||
192 | |||
193 | |||
194 | <property name="wait" value="2"/> |
||
195 | <assert name="wait" execute="false"> |
||
196 | <echo> |
||
197 | Waiting ${wait} seconds... |
||
198 | Click the red button to stop waiting. |
||
199 | </echo> |
||
200 | <sleep seconds="${wait}"/> |
||
201 | </assert> |
||
202 | <assert name="wait" value="2" execute="true"> |
||
203 | <echo>done waiting!</echo> |
||
204 | </assert> |
||
205 | |||
206 | </pre> |
||
207 | </td> |
||
208 | </tr> |
||
209 | </table> |
||
210 | |||
211 | </p> |
||
212 | <p> |
||
213 | |||
214 | The next example shows Assert being used in a unit test for the "limit" task: |
||
215 | <table border="0" bgcolor="#E0E0E0"> |
||
216 | <tr> |
||
217 | <td> |
||
218 | <pre class="programlisting"> |
||
219 | |||
220 | |||
221 | <property name="ant.enable.asserts" value="true"/> |
||
222 | <target name="test2"> |
||
223 | <!-- should not stop 'sleep' task, should print out '_passed_' --> |
||
224 | <stopwatch name="timer"/> |
||
225 | <limit maxwait="5"> |
||
226 | <sleep seconds="1"/> |
||
227 | <echo>_passed_</echo> |
||
228 | </limit> |
||
229 | <stopwatch name="timer" action="total"/> |
||
230 | <assert message="Too much time."> |
||
231 | <bool> |
||
232 | <islessthan arg1="${timer}" arg2="2"/> |
||
233 | </bool> |
||
234 | </assert> |
||
235 | </target> |
||
236 | |||
237 | </pre> |
||
238 | </td> |
||
239 | </tr> |
||
240 | </table> |
||
241 | |||
242 | </p> |
||
243 | <p> |
||
244 | |||
245 | If the <tt class="computeroutput"> |
||
246 | ant.enable.asserts</tt> |
||
247 | property is set to false, then in the above example, the <tt class="computeroutput"> |
||
248 | echo</tt> |
||
249 | , <tt class="computeroutput"> |
||
250 | sleep</tt> |
||
251 | , and <tt class="computeroutput"> |
||
252 | echo</tt> |
||
253 | tasks will all execute. |
||
254 | </p> |
||
255 | <hr> |
||
256 | <p align="center">Copyright © 2003 Ant-Contrib Project. All |
||
257 | rights Reserved.</p> |
||
258 | </div> |
||
259 | </body> |
||
260 | </html> |