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

AssertInOnePackage.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // attempts to be a comprehensive test of assert statements that have
  2. // only intra-package behaviour.
  3. import org.aspectj.testing.Tester;
  4. public class AssertInOnePackage {
  5. public static void main(String[] args) {
  6. turnOnAssertions();
  7. runTests();
  8. }
  9. // we first turn on assertions. This is run _before_ any other
  10. // method is run, so the only classes that are loaded are
  11. // AssertInOnePackage.
  12. static void turnOnAssertions() {
  13. ClassLoader cl = AssertInOnePackage.class.getClassLoader();
  14. cl.setClassAssertionStatus("StaticInitializerOnHelper", true);
  15. cl.setClassAssertionStatus("StaticInitializerOffHelper", false);
  16. cl.setClassAssertionStatus("ConstructorOnHelper", true);
  17. cl.setClassAssertionStatus("ConstructorOffHelper", false);
  18. cl.setClassAssertionStatus("InnerStaticInitializerOnHelper", true);
  19. cl.setClassAssertionStatus("InnerStaticInitializerOffHelper", false);
  20. cl.setClassAssertionStatus("InnerStaticInitializerOnHelperI", true);
  21. cl.setClassAssertionStatus("InnerStaticInitializerOffHelperI", false);
  22. cl.setClassAssertionStatus("CycleSubOn", true);
  23. cl.setClassAssertionStatus("CycleSubOff", false);
  24. }
  25. // In the following tests, the assignment to the static field
  26. // translates into a putstatic bytecode, which, by section 5.5 of
  27. // the JVM spec, will initialize the class.
  28. static void runTests() {
  29. check(true,
  30. "static initializer should throw",
  31. new Runnable() {
  32. void run() { StaticInitializerOnHelper.x = 3; }
  33. });
  34. check(false,
  35. "static initializer should not throw",
  36. new Runnable() {
  37. void run() { StaticInitializerOffHelper.x = 3; }
  38. });
  39. check(true,
  40. "constructor should throw",
  41. new Runnable() {
  42. void pre() { ConstructorOnHelper.x = 3; }
  43. void run() { new ConstructorOnHelper(); }
  44. });
  45. check(false,
  46. "static initializer should not throw",
  47. new Runnable() {
  48. void pre() { ConstructorOffHelper.x = 3; }
  49. void run() { new ConstructorOffHelper(); }
  50. });
  51. check(true,
  52. "inner static initializer should throw",
  53. new Runnable() {
  54. void run() { InnerStaticInitializerOnHelper.Inner.x = 3; }
  55. });
  56. check(false,
  57. "inner static initializer should not throw",
  58. new Runnable() {
  59. void run() { InnerStaticInitializerOffHelper.Inner.x = 3; }
  60. });
  61. check(true,
  62. "inner static initializer of interface should throw",
  63. new Runnable() {
  64. void run() { InnerStaticInitializerOnHelperI.Inner.x = 3; }
  65. });
  66. check(false,
  67. "inner static initializer of interface should not throw",
  68. new Runnable() {
  69. void run() { InnerStaticInitializerOffHelperI.Inner.x = 3; }
  70. });
  71. check(true,
  72. "static initializer in cyclic should throw",
  73. new Runnable() {
  74. void run() { CycleSubOn.x = 3; }
  75. });
  76. check(true,
  77. "static initializer in cyclic should always throw",
  78. new Runnable() {
  79. void run() { CycleSubOff.x = 3; }
  80. });
  81. }
  82. static void check(boolean shouldThrow, String message, Runnable r) {
  83. r.pre();
  84. boolean threw = false;
  85. try {
  86. r.run();
  87. } catch (AssertionError e) {
  88. threw = true;
  89. }
  90. if (threw != shouldThrow) {
  91. //System.err.println(message);
  92. Tester.check(false, message);
  93. }
  94. r.post();
  95. }
  96. static class Runnable {
  97. void pre() {}
  98. void run() {}
  99. void post() {}
  100. }
  101. }
  102. // ------------------------------
  103. // Asserts in a static initializer of a class
  104. class StaticInitializerOnHelper {
  105. static int x;
  106. static {
  107. assert false;
  108. }
  109. }
  110. class StaticInitializerOffHelper {
  111. static int x;
  112. static {
  113. assert false;
  114. }
  115. }
  116. // ------------------------------
  117. // Asserts in a constructor of a class. This stands in for all
  118. // "normal" assertion, so I'm not going to bother writing asserts in
  119. // other post-class-initialization contexts.
  120. class ConstructorOnHelper {
  121. static int x;
  122. ConstructorOnHelper() {
  123. assert false;
  124. }
  125. }
  126. class ConstructorOffHelper {
  127. static int x;
  128. ConstructorOffHelper() {
  129. assert false;
  130. }
  131. }
  132. // ------------------------------
  133. // Asserts in a static initializer of an inner class.
  134. class InnerStaticInitializerOnHelper {
  135. static class Inner {
  136. static int x;
  137. static {
  138. assert false;
  139. }
  140. }
  141. }
  142. class InnerStaticInitializerOffHelper {
  143. static class Inner {
  144. static int x;
  145. static {
  146. assert false;
  147. }
  148. }
  149. }
  150. // ------------------------------
  151. // Asserts in a static initializer of an inner class of an interface.
  152. interface InnerStaticInitializerOnHelperI {
  153. static class Inner {
  154. static int x;
  155. static {
  156. assert false;
  157. }
  158. }
  159. }
  160. interface InnerStaticInitializerOffHelperI {
  161. static class Inner {
  162. static int x;
  163. static {
  164. assert false;
  165. }
  166. }
  167. }
  168. // ------------------------------
  169. // Asserts in the subclass called in an initialization inversion
  170. class CycleSuperOn {
  171. static {
  172. CycleSubOn.foo();
  173. }
  174. }
  175. class CycleSubOn extends CycleSuperOn {
  176. static int x;
  177. static void foo() {
  178. assert false;
  179. }
  180. }
  181. class CycleSuperOff {
  182. static {
  183. CycleSubOff.foo();
  184. }
  185. }
  186. class CycleSubOff extends CycleSuperOff {
  187. static int x;
  188. static void foo() {
  189. assert false;
  190. }
  191. }