source: release-kits/lirk3/bin/ant-installer/web/manual1.7.0/manual/CoreTypes/assertions.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 6.5 KB
Line 
1<!--
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16-->
17<html>
18
19<head>
20<meta http-equiv="Content-Language" content="en-us">
21<link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
22<title>Assertions type</title>
23</head>
24
25<body>
26
27<h2><a name="assertions">Assertions</a></h2>
28<p>
29The <tt>assertions</tt> type enables or disables the Java 1.4 assertions feature,
30on a whole Java program, or components of a program. It can be used
31in <a href="../CoreTasks/java.html"><code>&lt;java&gt;</code></a> and
32<a href="../OptionalTasks/junit.html"><code>&lt;junit&gt;</code></a> to add extra validation to code.
33
34<p>
35Assertions are covered in the
36<a href="http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html">J2SDK 1.4 documentation</a>,
37and the
38<a href="http://java.sun.com/docs/books/jls/assert-spec.html">Java Language Specification</a>.
39
40<p>
41The key points to note are that a <tt>java.lang.AssertionError</tt>
42is thrown when an assertion fails, and that the facility is only available
43on Java 1.4 and later. To enable assertions one must set <tt>source="1.4"</tt>
44(or later) in <tt>&lt;javac&gt;</tt> when the source is being compiled, and
45that the code must contain <tt>assert</tt> statements to be tested. The
46result of such an action is code that neither compiles or runs on earlier
47versions of Java. For this reason Ant itself currently contains no assertions.
48<p>
49
50When assertions are enabled (or disabled) in a task through nested
51assertions elements, the class loader or command line is modified with the
52appropriate options. This means that the JVM executed must be a Java 1.4
53or later JVM, even if there are no assertions in the code. Attempting to
54enable assertions on earlier VMs will result in an "Unrecognized option"
55error and the JVM will not start.
56
57<p>
58<h4>Attributes</h4>
59<p>
60
61
62</p>
63<table border="1" cellpadding="2" cellspacing="0">
64 <tr>
65 <td valign="top"><b>Attribute</b></td>
66 <td valign="top"><b>Description</b></td>
67 <td align="center" valign="top"><b>Required</b></td>
68 </tr>
69 <tr>
70 <td valign="top">enableSystemAssertions</td>
71 <td valign="top">Flag to turn system assertions on or off.</td>
72 <td valign="top" align="center">No; default is "unspecified"</td>
73 </tr>
74</table>
75<p>
76When system assertions have been neither enabled nor disabled, then
77the JVM is not given any assertion information - the default action of the
78 current JVMs is to disable system assertions.
79<p>
80Note also that there is no apparent documentation for what parts of the
81JRE come with useful assertions.
82
83<h3>Nested elements</h3>
84
85<h4>enable</h4>
86<p>
87Enable assertions in portions of code.
88If neither a package nor class is specified, assertions are turned on in <i>all</i> (user) code.
89</p>
90<table border="1" cellpadding="2" cellspacing="0">
91 <tr>
92 <td valign="top"><b>Attribute</b></td>
93 <td valign="top"><b>Description</b></td>
94 <td align="center" valign="top"><b>Required</b></td>
95 </tr>
96 <tr>
97 <td valign="top">class</td>
98 <td valign="top">The name of a class on which to enable assertions.</td>
99 <td valign="top" align="center">No</td>
100 </tr>
101 <tr>
102 <td valign="top">package</td>
103 <td valign="top">
104 The name of a package in which to enable assertions on all classes. (Includes subpackages.)
105 Use "<tt>...</tt>" for the anonymous package.
106 </td>
107 <td valign="top" align="center">No</td>
108 </tr>
109</table>
110
111<h4>disable</h4>
112<p>
113Disable assertions in portions of code.
114
115</p>
116<table border="1" cellpadding="2" cellspacing="0">
117 <tr>
118 <td valign="top"><b>Attribute</b></td>
119 <td valign="top"><b>Description</b></td>
120 <td align="center" valign="top"><b>Required</b></td>
121 </tr>
122 <tr>
123 <td valign="top">class</td>
124 <td valign="top">The name of a class on which to disable assertions.</td>
125 <td valign="top" align="center">No</td>
126 </tr>
127 <tr>
128 <td valign="top">package</td>
129 <td valign="top">
130 The name of a package in which to disable assertions on all classes. (Includes subpackages.)
131 Use "<tt>...</tt>" for the anonymous package.
132 </td>
133 <td valign="top" align="center">No</td>
134 </tr>
135</table>
136<p>
137
138Because assertions are disabled by default, it only makes sense to disable
139assertions where they have been enabled in a parent package.
140
141
142<h3>Examples</h3>
143
144<h5>Example: enable assertions in all user classes</h5>
145
146All classes not in the JRE (i.e. all non-system classes) will have assertions turned on.
147<pre>
148&lt;assertions&gt;
149 &lt;enable/&gt;
150&lt;/assertions&gt;
151</pre>
152
153<h5>Example: enable a single class</h5>
154
155Enable assertions in a class called Test
156<pre>
157&lt;assertions&gt;
158 &lt;enable class="Test"/&gt;
159&lt;/assertions&gt;
160</pre>
161
162<h5>Example: enable a package</h5>
163
164Enable assertions in the <tt>org.apache</tt> package
165and all packages starting with the <tt>org.apache.</tt> prefix
166<pre>
167&lt;assertions&gt;
168 &lt;enable package="org.apache"/&gt;
169&lt;/assertions&gt;
170</pre>
171
172<h5>Example: System assertions</h5>
173
174Example: enable system assertions and assertions in all <tt>org.apache</tt> packages except
175for Ant (but including <tt>org.apache.tools.ant.Main</tt>)
176<pre>
177&lt;assertions enableSystemAssertions="true"&gt;
178 &lt;enable package="org.apache"/&gt;
179 &lt;disable package="org.apache.tools.ant"/&gt;
180 &lt;enable class="org.apache.tools.ant.Main"/&gt;
181&lt;/assertions&gt;
182</pre>
183
184<h5>Example: disabled and anonymous package assertions</h5>
185
186Disable system assertions; enable those in the anonymous package
187<pre>
188&lt;assertions enableSystemAssertions="false"&gt;
189 &lt;enable package="..."/&gt;
190&lt;/assertions&gt;
191</pre>
192
193
194<h5>Example: referenced assertions</h5>
195
196This type is a datatype, so you can declare assertions and use them later
197
198<pre>
199&lt;assertions id="project.assertions"&gt;
200 &lt;enable package="org.apache.test"/&gt;
201&lt;/assertions&gt;
202
203&lt;assertions refid="project.assertions"/&gt;
204</pre>
205
206
207</body>
208</html>
Note: See TracBrowser for help on using the repository browser.