You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

assertions.html 6.5 KiB

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