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.

AssertsCv.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import org.aspectj.testing.Tester;
  2. public class AssertsCv {
  3. public static void main(String[] args) {
  4. // programmatic enablement does not work after class is initialized
  5. AssertsCv.class.getClassLoader().setClassAssertionStatus("AssertTest", true);
  6. warnGlobalAssertions();
  7. AssertTest.main(args);
  8. }
  9. static void warnGlobalAssertions() {
  10. boolean pass = true;
  11. try { assert false; }
  12. catch (AssertionError e) { pass = false; }
  13. finally {
  14. if (!pass) {
  15. System.err.println("WARNING: assertions enabled in AssertsCv");
  16. }
  17. }
  18. }
  19. }
  20. class AssertTest {
  21. int x;
  22. public static void main(String[] args) {
  23. requireAssertions();
  24. testExpressions();
  25. testContexts();
  26. }
  27. static void testContexts() {
  28. InnerTester.testExpressions();
  29. (new AssertTest()).new InnerInstanceTester().testExpressions();
  30. // static initialization
  31. boolean pass = false;
  32. try { String s = InnerStaticInitFailure.class.getName(); }
  33. catch (AssertionError e) { pass = true; }
  34. catch (ExceptionInInitializerError e) {
  35. Throwable t = e.getException(); // use getCause() in 1.4
  36. pass = (null != t) && (t instanceof AssertionError);
  37. }
  38. finally { Tester.check(pass, "no assertion in static initializer"); }
  39. AssertTest enclosing = new AssertTest();
  40. // field initialization of inner class
  41. pass = false;
  42. try { enclosing.new InnerFieldInitFailure(); }
  43. catch (AssertionError e) { pass = true; }
  44. finally { Tester.check(pass, "no assertion in field initializer"); }
  45. // instance initialization of inner class
  46. pass = false;
  47. try { enclosing.new InnerInitFailure(); }
  48. catch (AssertionError e) { pass = true; }
  49. finally { Tester.check(pass, "no assertion in constructor"); }
  50. // anonymous inner class - method
  51. pass = false;
  52. boolean doingRun = false;
  53. try {
  54. enclosing.x = 1;
  55. final int i = enclosing.x;
  56. Runnable r = new Runnable() {
  57. public void run() {
  58. assert i != 1;
  59. }
  60. };
  61. doingRun = true;
  62. r.run();
  63. } catch (AssertionError e) { pass = true; }
  64. finally {
  65. Tester.check(doingRun, "premature assertion in anonymous inner");
  66. Tester.check(pass, "no assertion in anonymous inner");
  67. }
  68. enclosing.x = 0;
  69. if (false) { // disabled now as current javac/j2se bug XXX
  70. // anonymous inner class - field initializer
  71. pass = false;
  72. try {
  73. enclosing.x = 1;
  74. Runnable r = new Runnable() {
  75. int j = throwAssert();
  76. public void run() { }
  77. int throwAssert() { assert false; return -100; }
  78. };
  79. doingRun = true;
  80. r.run();
  81. }
  82. catch (AssertionError e) { pass = true; }
  83. finally {
  84. Tester.check(!doingRun, "missed assertion in anonymous inner");
  85. Tester.check(pass, "no assertion in anonymous inner");
  86. }
  87. enclosing.x = 0;
  88. }
  89. }
  90. static void requireAssertions() {
  91. boolean pass = false;
  92. try { assert false; }
  93. catch (AssertionError e) { pass = true; }
  94. finally { Tester.check(pass, "assertions not enabled"); }
  95. }
  96. static void testExpressions() {
  97. // ---------- tests of not throwing assertions
  98. // first is any boolean expression
  99. int x = 0;
  100. assert x < 2;
  101. assert // parser questions
  102. x<2
  103. ;
  104. assert (x<2);
  105. assert x < 2 ? true : false ;
  106. assert x < 2 ? true : false : false ;
  107. assert x < 2 ? true : false : "bug if assert thrown" ;
  108. // If the first expression evaluates to true,
  109. // the second expression is not evaluated
  110. assert x == 0 : MyError.throwError("bug if error thrown");
  111. assert x < 2 ? true : false : MyError.throwError("bug if error thrown");
  112. // ---------- tests of throwing assertions
  113. // (if these fail, it may be Sun's bug...)
  114. // touch expression2 type bases
  115. try { assert x > 0 : 3; }
  116. catch (AssertionError e) { check(e, "3"); }
  117. try { assert x > 0 : 3f; }
  118. catch (AssertionError e) { check(e, "3"); }
  119. try { assert x > 0 : 3d; }
  120. catch (AssertionError e) { check(e, "3"); }
  121. try { assert x > 0 : 3l; }
  122. catch (AssertionError e) { check(e, "3"); }
  123. try { assert x > 0 : 'c'; }
  124. catch (AssertionError e) { check(e, "c"); }
  125. try { assert x > 0 : "String"; }
  126. catch (AssertionError e) { check(e, "String"); }
  127. try { assert x > 0 : new StringWrapper("String"); }
  128. catch (AssertionError e) { check(e, "String"); }
  129. try { assert x > 0 : result(true); }
  130. catch (AssertionError e) { check(e, "true"); }
  131. try { assert x > 0 : Boolean.FALSE ; }
  132. catch (AssertionError e) { check(e, "false"); }
  133. try { assert x > 0 : Boolean.FALSE ; }
  134. catch (AssertionError e) { check(e, "false"); }
  135. // If an exception is thrown while either expression is being evaluated,
  136. // the assert statement completes abruptly, throwing this exception
  137. try { assert MyError.throwError("throwit"); }
  138. catch (MyError e) { check(e, "throwit"); }
  139. catch (AssertionError e) { Tester.check(false, "expected throwit-1"); }
  140. try { assert x > 0 : MyError.throwError("throwit"); }
  141. catch (MyError e) { check(e, "throwit"); }
  142. catch (AssertionError e) { Tester.check(false, "expected throwit-2"); }
  143. try { assert MyInnerError.throwError("throwit"); }
  144. catch (MyInnerError e) { check(e, "throwit"); }
  145. catch (AssertionError e) { Tester.check(false, "expected throwit-1"); }
  146. try { assert x > 0 : MyInnerError.throwError("throwit"); }
  147. catch (MyInnerError e) { check(e, "throwit"); }
  148. catch (AssertionError e) { Tester.check(false, "expected throwit-2"); }
  149. }
  150. static void check(Error e, String prefix) {
  151. String m = e.getMessage();
  152. Tester.check(m.startsWith(prefix),
  153. "expected " + m
  154. + " to start with " + prefix);
  155. }
  156. static boolean result(boolean b ) {
  157. return b;
  158. }
  159. private static class MyInnerError extends Error {
  160. MyInnerError(String s) { super(s); }
  161. static boolean throwError(String s) {
  162. throw new MyInnerError(s);
  163. }
  164. }
  165. private static class InnerTester {
  166. // copy/paste from above
  167. static void testExpressions() {
  168. // ---------- tests of not throwing assertions
  169. // first is any boolean expression
  170. int x = 0;
  171. assert x < 2;
  172. assert // parser questions
  173. x<2
  174. ;
  175. assert (x<2);
  176. assert x < 2 ? true : false ;
  177. assert x < 2 ? true : false : false ;
  178. assert x < 2 ? true : false : "bug if assert thrown" ;
  179. // If the first expression evaluates to true,
  180. // the second expression is not evaluated
  181. assert x == 0 : MyError.throwError("bug if error thrown");
  182. assert x < 2 ? true : false : MyError.throwError("bug if error thrown");
  183. // ---------- tests of throwing assertions
  184. // (if these fail, it may be Sun's bug...)
  185. // touch expression2 type bases
  186. try { assert x > 0 : 3; }
  187. catch (AssertionError e) { check(e, "3"); }
  188. try { assert x > 0 : 3f; }
  189. catch (AssertionError e) { check(e, "3"); }
  190. try { assert x > 0 : 3d; }
  191. catch (AssertionError e) { check(e, "3"); }
  192. try { assert x > 0 : 3l; }
  193. catch (AssertionError e) { check(e, "3"); }
  194. try { assert x > 0 : 'c'; }
  195. catch (AssertionError e) { check(e, "c"); }
  196. try { assert x > 0 : "String"; }
  197. catch (AssertionError e) { check(e, "String"); }
  198. try { assert x > 0 : new StringWrapper("String"); }
  199. catch (AssertionError e) { check(e, "String"); }
  200. try { assert x > 0 : result(true); }
  201. catch (AssertionError e) { check(e, "true"); }
  202. try { assert x > 0 : Boolean.FALSE ; }
  203. catch (AssertionError e) { check(e, "false"); }
  204. try { assert x > 0 : Boolean.FALSE ; }
  205. catch (AssertionError e) { check(e, "false"); }
  206. // If an exception is thrown while either expression is being evaluated,
  207. // the assert statement completes abruptly, throwing this exception
  208. try { assert MyError.throwError("throwit"); }
  209. catch (MyError e) { check(e, "throwit"); }
  210. catch (AssertionError e) { Tester.check(false, "expected throwit-1"); }
  211. try { assert x > 0 : MyError.throwError("throwit"); }
  212. catch (MyError e) { check(e, "throwit"); }
  213. catch (AssertionError e) { Tester.check(false, "expected throwit-2"); }
  214. try { assert MyInnerError.throwError("throwit"); }
  215. catch (MyInnerError e) { check(e, "throwit"); }
  216. catch (AssertionError e) { Tester.check(false, "expected throwit-1"); }
  217. try { assert x > 0 : MyInnerError.throwError("throwit"); }
  218. catch (MyInnerError e) { check(e, "throwit"); }
  219. catch (AssertionError e) { Tester.check(false, "expected throwit-2"); }
  220. }
  221. }
  222. private class InnerInstanceTester {
  223. // copy/paste from above, with exception of x variable
  224. void testExpressions() {
  225. // ---------- tests of not throwing assertions
  226. // first is any boolean expression
  227. x = 0; // variable from enclosing instance
  228. assert x < 2;
  229. assert // parser questions
  230. x<2
  231. ;
  232. assert (x<2);
  233. assert x < 2 ? true : false ;
  234. assert x < 2 ? true : false : false ;
  235. assert x < 2 ? true : false : "bug if assert thrown" ;
  236. // If the first expression evaluates to true,
  237. // the second expression is not evaluated
  238. assert x == 0 : MyError.throwError("bug if error thrown");
  239. assert x < 2 ? true : false : MyError.throwError("bug if error thrown");
  240. // ---------- tests of throwing assertions
  241. // (if these fail, it may be Sun's bug...)
  242. // touch expression2 type bases
  243. try { assert x > 0 : 3; }
  244. catch (AssertionError e) { check(e, "3"); }
  245. try { assert x > 0 : 3f; }
  246. catch (AssertionError e) { check(e, "3"); }
  247. try { assert x > 0 : 3d; }
  248. catch (AssertionError e) { check(e, "3"); }
  249. try { assert x > 0 : 3l; }
  250. catch (AssertionError e) { check(e, "3"); }
  251. try { assert x > 0 : 'c'; }
  252. catch (AssertionError e) { check(e, "c"); }
  253. try { assert x > 0 : "String"; }
  254. catch (AssertionError e) { check(e, "String"); }
  255. try { assert x > 0 : new StringWrapper("String"); }
  256. catch (AssertionError e) { check(e, "String"); }
  257. try { assert x > 0 : result(true); }
  258. catch (AssertionError e) { check(e, "true"); }
  259. try { assert x > 0 : Boolean.FALSE ; }
  260. catch (AssertionError e) { check(e, "false"); }
  261. try { assert x > 0 : Boolean.FALSE ; }
  262. catch (AssertionError e) { check(e, "false"); }
  263. // If an exception is thrown while either expression is being evaluated,
  264. // the assert statement completes abruptly, throwing this exception
  265. try { assert MyError.throwError("throwit"); }
  266. catch (MyError e) { check(e, "throwit"); }
  267. catch (AssertionError e) { Tester.check(false, "expected throwit-1"); }
  268. try { assert x > 0 : MyError.throwError("throwit"); }
  269. catch (MyError e) { check(e, "throwit"); }
  270. catch (AssertionError e) { Tester.check(false, "expected throwit-2"); }
  271. try { assert MyInnerError.throwError("throwit"); }
  272. catch (MyInnerError e) { check(e, "throwit"); }
  273. catch (AssertionError e) { Tester.check(false, "expected throwit-1"); }
  274. try { assert x > 0 : MyInnerError.throwError("throwit"); }
  275. catch (MyInnerError e) { check(e, "throwit"); }
  276. catch (AssertionError e) { Tester.check(false, "expected throwit-2"); }
  277. }
  278. }
  279. static class InnerStaticInitFailure {
  280. static {
  281. int x = 0;
  282. assert x > 1 : "throwing assert during class init";
  283. }
  284. }
  285. class InnerFieldInitFailure {
  286. int i = getValue();
  287. int getValue () {
  288. int x = 0;
  289. assert x > 1 : "throwing assert during field init";
  290. return -100;
  291. }
  292. }
  293. class InnerInitFailure {
  294. InnerInitFailure() {
  295. int x = 0;
  296. assert x > 1 : "throwing assert during instance init";
  297. }
  298. }
  299. }
  300. class MyError extends Error {
  301. MyError(String s) { super(s); }
  302. static boolean throwError(String s) {
  303. throw new MyError(s);
  304. }
  305. }
  306. class StringWrapper {
  307. private String string;
  308. StringWrapper(String s) { string = s; }
  309. public String toString() { return string; }
  310. }