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>
|
---|
29 | The <tt>assertions</tt> type enables or disables the Java 1.4 assertions feature,
|
---|
30 | on a whole Java program, or components of a program. It can be used
|
---|
31 | in <a href="../CoreTasks/java.html"><code><java></code></a> and
|
---|
32 | <a href="../OptionalTasks/junit.html"><code><junit></code></a> to add extra validation to code.
|
---|
33 |
|
---|
34 | <p>
|
---|
35 | Assertions 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>,
|
---|
37 | and the
|
---|
38 | <a href="http://java.sun.com/docs/books/jls/assert-spec.html">Java Language Specification</a>.
|
---|
39 |
|
---|
40 | <p>
|
---|
41 | The key points to note are that a <tt>java.lang.AssertionError</tt>
|
---|
42 | is thrown when an assertion fails, and that the facility is only available
|
---|
43 | on Java 1.4 and later. To enable assertions one must set <tt>source="1.4"</tt>
|
---|
44 | (or later) in <tt><javac></tt> when the source is being compiled, and
|
---|
45 | that the code must contain <tt>assert</tt> statements to be tested. The
|
---|
46 | result of such an action is code that neither compiles or runs on earlier
|
---|
47 | versions of Java. For this reason Ant itself currently contains no assertions.
|
---|
48 | <p>
|
---|
49 |
|
---|
50 | When assertions are enabled (or disabled) in a task through nested
|
---|
51 | assertions elements, the class loader or command line is modified with the
|
---|
52 | appropriate options. This means that the JVM executed must be a Java 1.4
|
---|
53 | or later JVM, even if there are no assertions in the code. Attempting to
|
---|
54 | enable assertions on earlier VMs will result in an "Unrecognized option"
|
---|
55 | error 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>
|
---|
76 | When system assertions have been neither enabled nor disabled, then
|
---|
77 | the JVM is not given any assertion information - the default action of the
|
---|
78 | current JVMs is to disable system assertions.
|
---|
79 | <p>
|
---|
80 | Note also that there is no apparent documentation for what parts of the
|
---|
81 | JRE come with useful assertions.
|
---|
82 |
|
---|
83 | <h3>Nested elements</h3>
|
---|
84 |
|
---|
85 | <h4>enable</h4>
|
---|
86 | <p>
|
---|
87 | Enable assertions in portions of code.
|
---|
88 | If 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>
|
---|
113 | Disable 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 |
|
---|
138 | Because assertions are disabled by default, it only makes sense to disable
|
---|
139 | assertions 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 |
|
---|
146 | All classes not in the JRE (i.e. all non-system classes) will have assertions turned on.
|
---|
147 | <pre>
|
---|
148 | <assertions>
|
---|
149 | <enable/>
|
---|
150 | </assertions>
|
---|
151 | </pre>
|
---|
152 |
|
---|
153 | <h5>Example: enable a single class</h5>
|
---|
154 |
|
---|
155 | Enable assertions in a class called Test
|
---|
156 | <pre>
|
---|
157 | <assertions>
|
---|
158 | <enable class="Test"/>
|
---|
159 | </assertions>
|
---|
160 | </pre>
|
---|
161 |
|
---|
162 | <h5>Example: enable a package</h5>
|
---|
163 |
|
---|
164 | Enable assertions in the <tt>org.apache</tt> package
|
---|
165 | and all packages starting with the <tt>org.apache.</tt> prefix
|
---|
166 | <pre>
|
---|
167 | <assertions>
|
---|
168 | <enable package="org.apache"/>
|
---|
169 | </assertions>
|
---|
170 | </pre>
|
---|
171 |
|
---|
172 | <h5>Example: System assertions</h5>
|
---|
173 |
|
---|
174 | Example: enable system assertions and assertions in all <tt>org.apache</tt> packages except
|
---|
175 | for Ant (but including <tt>org.apache.tools.ant.Main</tt>)
|
---|
176 | <pre>
|
---|
177 | <assertions enableSystemAssertions="true">
|
---|
178 | <enable package="org.apache"/>
|
---|
179 | <disable package="org.apache.tools.ant"/>
|
---|
180 | <enable class="org.apache.tools.ant.Main"/>
|
---|
181 | </assertions>
|
---|
182 | </pre>
|
---|
183 |
|
---|
184 | <h5>Example: disabled and anonymous package assertions</h5>
|
---|
185 |
|
---|
186 | Disable system assertions; enable those in the anonymous package
|
---|
187 | <pre>
|
---|
188 | <assertions enableSystemAssertions="false">
|
---|
189 | <enable package="..."/>
|
---|
190 | </assertions>
|
---|
191 | </pre>
|
---|
192 |
|
---|
193 |
|
---|
194 | <h5>Example: referenced assertions</h5>
|
---|
195 |
|
---|
196 | This type is a datatype, so you can declare assertions and use them later
|
---|
197 |
|
---|
198 | <pre>
|
---|
199 | <assertions id="project.assertions">
|
---|
200 | <enable package="org.apache.test"/>
|
---|
201 | </assertions>
|
---|
202 |
|
---|
203 | <assertions refid="project.assertions"/>
|
---|
204 | </pre>
|
---|
205 |
|
---|
206 |
|
---|
207 | </body>
|
---|
208 | </html>
|
---|