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