2006-09-11 04:19:00 +00:00
|
|
|
<!--
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
-->
|
2003-08-21 00:34:27 +00:00
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Language" content="en-us">
|
2005-04-29 18:58:16 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
|
2003-08-21 00:34:27 +00:00
|
|
|
<title>Assertions type</title>
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<h2><a name="assertions">Assertions</a></h2>
|
|
|
|
<p>
|
2004-02-02 14:33:08 +00:00
|
|
|
The <tt>assertions</tt> type enables or disables the Java 1.4 assertions feature,
|
|
|
|
on a whole Java program, or components of a program. It can be used
|
2010-05-26 20:33:51 +00:00
|
|
|
in <a href="../Tasks/java.html"><code><java></code></a> and
|
|
|
|
<a href="../Tasks/junit.html"><code><junit></code></a> to add extra validation to code.
|
2003-08-21 00:34:27 +00:00
|
|
|
|
|
|
|
<p>
|
|
|
|
Assertions are covered in the
|
2013-09-13 13:40:23 +00:00
|
|
|
<a href="http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html">Java SE documentation</a>,
|
2004-02-02 14:33:08 +00:00
|
|
|
and the
|
2013-09-13 13:40:23 +00:00
|
|
|
<a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.10">Java Language Specification</a>.
|
2003-08-21 00:34:27 +00:00
|
|
|
|
|
|
|
<p>
|
2004-02-02 14:33:08 +00:00
|
|
|
The key points to note are that a <tt>java.lang.AssertionError</tt>
|
2003-08-21 00:34:27 +00:00
|
|
|
is thrown when an assertion fails, and that the facility is only available
|
2004-02-02 14:33:08 +00:00
|
|
|
on Java 1.4 and later. To enable assertions one must set <tt>source="1.4"</tt>
|
|
|
|
(or later) in <tt><javac></tt> when the source is being compiled, and
|
2003-08-21 00:34:27 +00:00
|
|
|
that the code must contain <tt>assert</tt> statements to be tested. The
|
|
|
|
result of such an action is code that neither compiles or runs on earlier
|
2010-11-11 17:04:16 +00:00
|
|
|
versions of Java. For this reason Apache Ant itself currently contains no assertions.
|
2003-08-21 00:34:27 +00:00
|
|
|
<p>
|
|
|
|
|
|
|
|
When assertions are enabled (or disabled) in a task through nested
|
2004-02-02 14:33:08 +00:00
|
|
|
assertions elements, the class loader or command line is modified with the
|
|
|
|
appropriate options. This means that the JVM executed must be a Java 1.4
|
2003-08-21 00:34:27 +00:00
|
|
|
or later JVM, even if there are no assertions in the code. Attempting to
|
|
|
|
enable assertions on earlier VMs will result in an "Unrecognized option"
|
|
|
|
error and the JVM will not start.
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<h4>Attributes</h4>
|
|
|
|
<p>
|
|
|
|
|
|
|
|
|
|
|
|
</p>
|
|
|
|
<table border="1" cellpadding="2" cellspacing="0">
|
|
|
|
<tr>
|
|
|
|
<td valign="top"><b>Attribute</b></td>
|
|
|
|
<td valign="top"><b>Description</b></td>
|
|
|
|
<td align="center" valign="top"><b>Required</b></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td valign="top">enableSystemAssertions</td>
|
|
|
|
<td valign="top">Flag to turn system assertions on or off.</td>
|
2004-02-02 14:33:08 +00:00
|
|
|
<td valign="top" align="center">No; default is "unspecified"</td>
|
2003-08-21 00:34:27 +00:00
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<p>
|
2004-02-02 14:33:08 +00:00
|
|
|
When system assertions have been neither enabled nor disabled, then
|
|
|
|
the JVM is not given any assertion information - the default action of the
|
2003-08-21 00:34:27 +00:00
|
|
|
current JVMs is to disable system assertions.
|
|
|
|
<p>
|
|
|
|
Note also that there is no apparent documentation for what parts of the
|
2004-02-02 14:33:08 +00:00
|
|
|
JRE come with useful assertions.
|
2003-08-21 00:34:27 +00:00
|
|
|
|
|
|
|
<h3>Nested elements</h3>
|
|
|
|
|
|
|
|
<h4>enable</h4>
|
|
|
|
<p>
|
|
|
|
Enable assertions in portions of code.
|
2004-02-02 14:33:08 +00:00
|
|
|
If neither a package nor class is specified, assertions are turned on in <i>all</i> (user) code.
|
2003-08-21 00:34:27 +00:00
|
|
|
</p>
|
|
|
|
<table border="1" cellpadding="2" cellspacing="0">
|
|
|
|
<tr>
|
|
|
|
<td valign="top"><b>Attribute</b></td>
|
|
|
|
<td valign="top"><b>Description</b></td>
|
|
|
|
<td align="center" valign="top"><b>Required</b></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td valign="top">class</td>
|
2004-02-02 14:33:08 +00:00
|
|
|
<td valign="top">The name of a class on which to enable assertions.</td>
|
2003-08-21 00:34:27 +00:00
|
|
|
<td valign="top" align="center">No</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td valign="top">package</td>
|
|
|
|
<td valign="top">
|
2004-02-02 14:33:08 +00:00
|
|
|
The name of a package in which to enable assertions on all classes. (Includes subpackages.)
|
|
|
|
Use "<tt>...</tt>" for the anonymous package.
|
2003-08-21 00:34:27 +00:00
|
|
|
</td>
|
|
|
|
<td valign="top" align="center">No</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<h4>disable</h4>
|
|
|
|
<p>
|
|
|
|
Disable assertions in portions of code.
|
|
|
|
|
|
|
|
</p>
|
|
|
|
<table border="1" cellpadding="2" cellspacing="0">
|
|
|
|
<tr>
|
|
|
|
<td valign="top"><b>Attribute</b></td>
|
|
|
|
<td valign="top"><b>Description</b></td>
|
|
|
|
<td align="center" valign="top"><b>Required</b></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td valign="top">class</td>
|
2004-02-02 14:33:08 +00:00
|
|
|
<td valign="top">The name of a class on which to disable assertions.</td>
|
2003-08-21 00:34:27 +00:00
|
|
|
<td valign="top" align="center">No</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td valign="top">package</td>
|
|
|
|
<td valign="top">
|
2004-02-02 14:33:08 +00:00
|
|
|
The name of a package in which to disable assertions on all classes. (Includes subpackages.)
|
|
|
|
Use "<tt>...</tt>" for the anonymous package.
|
2003-08-21 00:34:27 +00:00
|
|
|
</td>
|
|
|
|
<td valign="top" align="center">No</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<p>
|
|
|
|
|
|
|
|
Because assertions are disabled by default, it only makes sense to disable
|
|
|
|
assertions where they have been enabled in a parent package.
|
|
|
|
|
|
|
|
|
|
|
|
<h4>Examples</h4>
|
|
|
|
|
2004-02-02 14:33:08 +00:00
|
|
|
<h5>Example: enable assertions in all user classes</h5>
|
|
|
|
|
|
|
|
All classes not in the JRE (i.e. all non-system classes) will have assertions turned on.
|
|
|
|
<pre>
|
|
|
|
<assertions>
|
|
|
|
<enable/>
|
|
|
|
</assertions>
|
|
|
|
</pre>
|
|
|
|
|
2003-08-21 00:34:27 +00:00
|
|
|
<h5>Example: enable a single class</h5>
|
|
|
|
|
|
|
|
Enable assertions in a class called Test
|
|
|
|
<pre>
|
2004-02-02 14:33:08 +00:00
|
|
|
<assertions>
|
|
|
|
<enable class="Test"/>
|
2003-08-21 00:34:27 +00:00
|
|
|
</assertions>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<h5>Example: enable a package</h5>
|
|
|
|
|
2004-02-02 14:33:08 +00:00
|
|
|
Enable assertions in the <tt>org.apache</tt> package
|
|
|
|
and all packages starting with the <tt>org.apache.</tt> prefix
|
2003-08-21 00:34:27 +00:00
|
|
|
<pre>
|
2004-02-02 14:33:08 +00:00
|
|
|
<assertions>
|
|
|
|
<enable package="org.apache"/>
|
2003-08-21 00:34:27 +00:00
|
|
|
</assertions>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<h5>Example: System assertions</h5>
|
|
|
|
|
2004-02-02 14:33:08 +00:00
|
|
|
Example: enable system assertions and assertions in all <tt>org.apache</tt> packages except
|
|
|
|
for Ant (but including <tt>org.apache.tools.ant.Main</tt>)
|
2003-08-21 00:34:27 +00:00
|
|
|
<pre>
|
2004-02-02 14:33:08 +00:00
|
|
|
<assertions enableSystemAssertions="true">
|
|
|
|
<enable package="org.apache"/>
|
|
|
|
<disable package="org.apache.tools.ant"/>
|
2003-08-21 00:34:27 +00:00
|
|
|
<enable class="org.apache.tools.ant.Main"/>
|
|
|
|
</assertions>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<h5>Example: disabled and anonymous package assertions</h5>
|
|
|
|
|
|
|
|
Disable system assertions; enable those in the anonymous package
|
|
|
|
<pre>
|
2004-02-02 14:33:08 +00:00
|
|
|
<assertions enableSystemAssertions="false">
|
|
|
|
<enable package="..."/>
|
2003-08-21 00:34:27 +00:00
|
|
|
</assertions>
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
|
|
|
|
<h5>Example: referenced assertions</h5>
|
|
|
|
|
|
|
|
This type is a datatype, so you can declare assertions and use them later
|
|
|
|
|
|
|
|
<pre>
|
2004-02-02 14:33:08 +00:00
|
|
|
<assertions id="project.assertions">
|
|
|
|
<enable package="org.apache.test"/>
|
2003-08-21 00:34:27 +00:00
|
|
|
</assertions>
|
|
|
|
|
2004-02-02 14:33:08 +00:00
|
|
|
<assertions refid="project.assertions"/>
|
2003-08-21 00:34:27 +00:00
|
|
|
</pre>
|
2003-08-22 09:20:12 +00:00
|
|
|
|
2006-09-11 04:33:25 +00:00
|
|
|
|
2003-08-22 09:20:12 +00:00
|
|
|
</body>
|
|
|
|
</html>
|