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.0 KiB

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