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 5.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 id="assertions">Assertions</h2>
  23. <p>
  24. The <code>assertions</code> type enables or disables the Java 1.4 assertions feature, on a whole
  25. Java program, or components of a program. It can be used
  26. in <a href="../Tasks/java.html"><code>&lt;java&gt;</code></a>
  27. and <a href="../Tasks/junit.html"><code>&lt;junit&gt;</code></a> to add extra validation to
  28. code.
  29. <p>
  30. Assertions are covered in
  31. the <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html">Java
  32. SE documentation</a>, and
  33. the <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.10">Java
  34. Language Specification</a>.
  35. <p>
  36. The key points to note are that a <code>java.lang.AssertionError</code> is thrown when an
  37. assertion fails, and that the facility is only available on Java 1.4 and later. To enable
  38. assertions one must set <var>source</var>=<q>1.4</q> (or later) in <code>&lt;javac&gt;</code>
  39. when the source is being compiled, and that the code must contain <code>assert</code> statements
  40. to be tested. The result of such an action is code that neither compiles or runs on earlier
  41. versions of Java. For this reason Apache Ant itself currently contains no assertions.
  42. <p>
  43. When assertions are enabled (or disabled) in a task through nested assertions elements, the
  44. class loader or command line is modified with the appropriate options. This means that the JVM
  45. executed must be of version 1.4 or later, even if there are no assertions in the
  46. code. Attempting to enable assertions on earlier JVMs will result in an "Unrecognized option"
  47. error and the JVM will not start.
  48. <p>
  49. <h4>Attributes</h4>
  50. <table class="attr">
  51. <tr>
  52. <th>Attribute</th>
  53. <th>Description</th>
  54. <th>Required</th>
  55. </tr>
  56. <tr>
  57. <td>enableSystemAssertions</td>
  58. <td>Flag to turn system assertions on or off.</td>
  59. <td>No; default is <q>unspecified</q></td>
  60. </tr>
  61. </table>
  62. <p>
  63. When system assertions have been neither enabled nor disabled, then the JVM is not given any
  64. assertion information&mdash;the default action of the current JVMs is to disable system
  65. assertions.
  66. <p>
  67. Note also that there is no apparent documentation for what parts of the JRE come with useful
  68. assertions.
  69. <h3>Nested elements</h3>
  70. <h4>enable</h4>
  71. <p>
  72. Enable assertions in portions of code. If neither a package nor class is specified, assertions
  73. are turned on in <em>all</em> (user) code.
  74. </p>
  75. <table class="attr">
  76. <tr>
  77. <th>Attribute</th>
  78. <th>Description</th>
  79. <th>Required</th>
  80. </tr>
  81. <tr>
  82. <td>class</td>
  83. <td>The name of a class on which to enable assertions.</td>
  84. <td>No</td>
  85. </tr>
  86. <tr>
  87. <td>package</td>
  88. <td>
  89. The name of a package in which to enable assertions on all classes. (Includes subpackages.)
  90. Use <q>...</q> for the anonymous package.
  91. </td>
  92. <td>No</td>
  93. </tr>
  94. </table>
  95. <h4>disable</h4>
  96. <p>
  97. Disable assertions in portions of code.
  98. </p>
  99. <table class="attr">
  100. <tr>
  101. <th>Attribute</th>
  102. <th>Description</th>
  103. <th>Required</th>
  104. </tr>
  105. <tr>
  106. <td>class</td>
  107. <td>The name of a class on which to disable assertions.</td>
  108. <td>No</td>
  109. </tr>
  110. <tr>
  111. <td>package</td>
  112. <td>
  113. The name of a package in which to disable assertions on all classes. (Includes subpackages.)
  114. Use <q>...</q> for the anonymous package.
  115. </td>
  116. <td>No</td>
  117. </tr>
  118. </table>
  119. <p>
  120. Because assertions are disabled by default, it only makes sense to disable assertions where they
  121. have been enabled in a parent package.
  122. </p>
  123. <h4>Examples</h4>
  124. <h5>Example: enable assertions in all user classes</h5>
  125. All classes not in the JRE (i.e. all non-system classes) will have assertions turned on.
  126. <pre>
  127. &lt;assertions&gt;
  128. &lt;enable/&gt;
  129. &lt;/assertions&gt;
  130. </pre>
  131. <h5>Example: enable a single class</h5>
  132. Enable assertions in a class called Test
  133. <pre>
  134. &lt;assertions&gt;
  135. &lt;enable class="Test"/&gt;
  136. &lt;/assertions&gt;
  137. </pre>
  138. <h5>Example: enable a package</h5>
  139. Enable assertions in the <code>org.apache</code> package and all packages starting with
  140. the <code>org.apache.</code> prefix
  141. <pre>
  142. &lt;assertions&gt;
  143. &lt;enable package="org.apache"/&gt;
  144. &lt;/assertions&gt;
  145. </pre>
  146. <h5>Example: System assertions</h5>
  147. Enable system assertions and assertions in all <code>org.apache</code> packages except for Ant
  148. (but including <code>org.apache.tools.ant.Main</code>)
  149. <pre>
  150. &lt;assertions enableSystemAssertions="true"&gt;
  151. &lt;enable package="org.apache"/&gt;
  152. &lt;disable package="org.apache.tools.ant"/&gt;
  153. &lt;enable class="org.apache.tools.ant.Main"/&gt;
  154. &lt;/assertions&gt;
  155. </pre>
  156. <h5>Example: disabled and anonymous package assertions</h5>
  157. Disable system assertions; enable those in the anonymous package
  158. <pre>
  159. &lt;assertions enableSystemAssertions="false"&gt;
  160. &lt;enable package="..."/&gt;
  161. &lt;/assertions&gt;
  162. </pre>
  163. <h5>Example: referenced assertions</h5>
  164. This type is a datatype, so you can declare assertions and use them later
  165. <pre>
  166. &lt;assertions id="project.assertions"&gt;
  167. &lt;enable package="org.apache.test"/&gt;
  168. &lt;/assertions&gt;
  169. &lt;assertions refid="project.assertions"/&gt;
  170. </pre>
  171. </body>
  172. </html>