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>
|
---|
13 | The <tt>assertions</tt> type enables or disables the Java 1.4 assertions feature,
|
---|
14 | on a whole Java program, or components of a program. It can be used
|
---|
15 | in <a href="../CoreTasks/java.html"><java></a> and
|
---|
16 | <a href="../OptionalTasks/junit.html"><junit></a> to add extra validation to code.
|
---|
17 |
|
---|
18 | <p>
|
---|
19 | Assertions 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>,
|
---|
21 | and the
|
---|
22 | <a href="http://java.sun.com/docs/books/jls/assert-spec.html">Java Language Specification</a>.
|
---|
23 |
|
---|
24 | <p>
|
---|
25 | The key points to note are that a <tt>java.lang.AssertionError</tt>
|
---|
26 | is thrown when an assertion fails, and that the facility is only available
|
---|
27 | on Java 1.4 and later. To enable assertions one must set <tt>source="1.4"</tt>
|
---|
28 | (or later) in <tt><javac></tt> when the source is being compiled, and
|
---|
29 | that the code must contain <tt>assert</tt> statements to be tested. The
|
---|
30 | result of such an action is code that neither compiles or runs on earlier
|
---|
31 | versions of Java. For this reason Ant itself currently contains no assertions.
|
---|
32 | <p>
|
---|
33 |
|
---|
34 | When assertions are enabled (or disabled) in a task through nested
|
---|
35 | assertions elements, the class loader or command line is modified with the
|
---|
36 | appropriate options. This means that the JVM executed must be a Java 1.4
|
---|
37 | or later JVM, even if there are no assertions in the code. Attempting to
|
---|
38 | enable assertions on earlier VMs will result in an "Unrecognized option"
|
---|
39 | error 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>
|
---|
60 | When system assertions have been neither enabled nor disabled, then
|
---|
61 | the JVM is not given any assertion information - the default action of the
|
---|
62 | current JVMs is to disable system assertions.
|
---|
63 | <p>
|
---|
64 | Note also that there is no apparent documentation for what parts of the
|
---|
65 | JRE come with useful assertions.
|
---|
66 |
|
---|
67 | <h3>Nested elements</h3>
|
---|
68 |
|
---|
69 | <h4>enable</h4>
|
---|
70 | <p>
|
---|
71 | Enable assertions in portions of code.
|
---|
72 | If 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>
|
---|
97 | Disable 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 |
|
---|
122 | Because assertions are disabled by default, it only makes sense to disable
|
---|
123 | assertions 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 |
|
---|
130 | All classes not in the JRE (i.e. all non-system classes) will have assertions turned on.
|
---|
131 | <pre>
|
---|
132 | <assertions>
|
---|
133 | <enable/>
|
---|
134 | </assertions>
|
---|
135 | </pre>
|
---|
136 |
|
---|
137 | <h5>Example: enable a single class</h5>
|
---|
138 |
|
---|
139 | Enable assertions in a class called Test
|
---|
140 | <pre>
|
---|
141 | <assertions>
|
---|
142 | <enable class="Test"/>
|
---|
143 | </assertions>
|
---|
144 | </pre>
|
---|
145 |
|
---|
146 | <h5>Example: enable a package</h5>
|
---|
147 |
|
---|
148 | Enable assertions in the <tt>org.apache</tt> package
|
---|
149 | and all packages starting with the <tt>org.apache.</tt> prefix
|
---|
150 | <pre>
|
---|
151 | <assertions>
|
---|
152 | <enable package="org.apache"/>
|
---|
153 | </assertions>
|
---|
154 | </pre>
|
---|
155 |
|
---|
156 | <h5>Example: System assertions</h5>
|
---|
157 |
|
---|
158 | Example: enable system assertions and assertions in all <tt>org.apache</tt> packages except
|
---|
159 | for Ant (but including <tt>org.apache.tools.ant.Main</tt>)
|
---|
160 | <pre>
|
---|
161 | <assertions enableSystemAssertions="true">
|
---|
162 | <enable package="org.apache"/>
|
---|
163 | <disable package="org.apache.tools.ant"/>
|
---|
164 | <enable class="org.apache.tools.ant.Main"/>
|
---|
165 | </assertions>
|
---|
166 | </pre>
|
---|
167 |
|
---|
168 | <h5>Example: disabled and anonymous package assertions</h5>
|
---|
169 |
|
---|
170 | Disable system assertions; enable those in the anonymous package
|
---|
171 | <pre>
|
---|
172 | <assertions enableSystemAssertions="false">
|
---|
173 | <enable package="..."/>
|
---|
174 | </assertions>
|
---|
175 | </pre>
|
---|
176 |
|
---|
177 |
|
---|
178 | <h5>Example: referenced assertions</h5>
|
---|
179 |
|
---|
180 | This type is a datatype, so you can declare assertions and use them later
|
---|
181 |
|
---|
182 | <pre>
|
---|
183 | <assertions id="project.assertions">
|
---|
184 | <enable package="org.apache.test"/>
|
---|
185 | </assertions>
|
---|
186 |
|
---|
187 | <assertions refid="project.assertions"/>
|
---|
188 | </pre>
|
---|
189 |
|
---|
190 | <hr>
|
---|
191 | <p align="center">Copyright © 2003-2004 The Apache Software Foundation. All rights
|
---|
192 | Reserved.</p>
|
---|
193 | </body>
|
---|
194 | </html>
|
---|