1 /*******************************************************************************
2  * Copyright (c) 2000, 2014 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.core.tests.compiler.regression;
15 
16 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
17 
18 import junit.framework.Test;
19 
20 @SuppressWarnings({ "rawtypes" })
21 public class AssertionTest extends AbstractRegressionTest {
22 //	 Static initializer to specify tests subset using TESTS_* static variables
23 //	 All specified tests which does not belong to the class are skipped...
24 	static {
25 //		TESTS_NAMES = new String[] { "test000" };
26 //		TESTS_NUMBERS = new int[] { 13, 14 };
27 //		TESTS_RANGE = new int[] { 11, -1 };
28 	}
AssertionTest(String name)29 	public AssertionTest(String name) {
30 		super(name);
31 	}
32 
suite()33 	public static Test suite() {
34 		return buildMinimalComplianceTestSuite(testClass(), F_1_4);
35 	}
36 
testClass()37 	public static Class testClass() {
38 		return AssertionTest.class;
39 	}
40 
test001()41 	public void test001() {
42 		this.runNegativeTest(
43 			new String[] {
44 				"assert.java",
45 				"public class assert {}\n",
46 			},
47 			"----------\n" +
48 			"1. ERROR in assert.java (at line 1)\n" +
49 			"	public class assert {}\n" +
50 			"	             ^^^^^^\n" +
51 			"Syntax error on token \"assert\", Identifier expected\n" +
52 			"----------\n");
53 	}
54 
test002()55 	public void test002() {
56 		this.runConformTest(new String[] {
57 			"A4.java",
58 			"public class A4 { \n"
59 			+ "	public static void main(String[] args) {\n"
60 			+ "   try {	\n"
61 			+ "    int i = 4;\n"
62 			+ "    assert i != 4;\n"
63 			+ "	   System.out.println(i);\n"
64 			+ "	  } catch(AssertionError e){	\n"
65 			+ "		System.out.print(\"SUCCESS\");	\n"
66 			+ "	  } \n"
67 			+ "	} \n"
68 			+ "} \n" },
69 		"SUCCESS", //expected display
70 		null, // use default class-path
71 		true, // flush previous output dir content
72 		new String[] {"-ea"});
73 	}
74 
test003()75 	public void test003() {
76 		this.runConformTest(new String[] {
77 			"A4.java",
78 			"public class A4 { \n"
79 			+ "	public static void main(String[] args) {\n"
80 			+ "    int i = 4;\n"
81 			+ "    assert i != 4;\n"
82 			+ "	   System.out.println(i);\n"
83 			+ "	} \n"
84 			+ "} \n" },
85 		"4",
86 		null, // use default class-path
87 		true, // flush previous output dir content
88 		new String[] {"-da"});
89 	}
test004()90 	public void test004() {
91 		this.runConformTest(new String[] {
92 			"A4.java",
93 			"public class A4 { \n"
94 			+ "	public static void main(String[] args) {\n"
95 			+ "   try {	\n"
96 			+ "		assert false : \"SUC\";	\n"
97 			+ "	  } catch(AssertionError e){	\n"
98 			+ "		System.out.print(e.getMessage());	\n"
99 			+ "	  }	\n"
100 			+ "	  try {	\n"
101 			+ "		assert false : new Object(){ public String toString(){ return \"CESS\";}};	\n"
102 			+ "	  } catch(AssertionError e){	\n"
103 			+ "		System.out.println(e.getMessage());	\n"
104 			+ "	  }	\n"
105 			+ "  }	\n"
106 			+ "} \n" },
107 		"SUCCESS", //expected display
108 		null, // use default class-path
109 		true, // flush previous output dir content
110 		new String[] {"-ea"});
111 	}
test005()112 	public void test005() {
113 		this.runConformTest(new String[] {
114 			"A4.java",
115 			"public class A4 { \n"
116 			+ "	public static void main(String[] args) {\n"
117 			+ "   try {	\n"
118 			+ "		assert false : 1;	\n"
119 			+ "	  } catch(AssertionError e){	\n"
120 			+ "		System.out.print(e.getMessage());	\n"
121 			+ "	  }	\n"
122 			+ "	  try {	\n"
123 			+ "		int i = 2;	\n"
124 			+ "		assert false : i;	\n"
125 			+ "	  } catch(AssertionError e){	\n"
126 			+ "		System.out.println(e.getMessage());	\n"
127 			+ "	  }	\n"
128 			+ "  }	\n"
129 			+ "} \n" },
130 		"12", //expected display
131 		null, // use default class-path
132 		true, // flush previous output dir content
133 		new String[] {"-ea"});
134 	}
test006()135 	public void test006() {
136 		this.runNegativeTest(new String[] {
137 			"A4.java",
138 			"public class A4 { \n"
139 			+ "	public static void main(String[] args) {\n"
140 			+ "	  try {	\n"
141 			+ "		assert false : unbound;	\n"
142 			+ "	  } catch(AssertionError e){	\n"
143 			+ "		System.out.println(e.getMessage());	\n"
144 			+ "	  }	\n"
145 			+ "  }	\n"
146 			+ "} \n" },
147 		"----------\n" +
148 		"1. ERROR in A4.java (at line 4)\n" +
149 		"	assert false : unbound;	\n" +
150 		"	               ^^^^^^^\n" +
151 		"unbound cannot be resolved to a variable\n" +
152 		"----------\n");
153 	}
test007()154 	public void test007() {
155 		this.runConformTest(new String[] {
156 			"A4.java",
157 			"public class A4 { \n"
158 			+ "	public static void main(String[] args) {\n"
159 			+ "   try {	\n"
160 			+ "		assert false : 1L;	\n"
161 			+ "	  } catch(AssertionError e){	\n"
162 			+ "		System.out.print(e.getMessage());	\n"
163 			+ "	  }	\n"
164 			+ "   try {	\n"
165 			+ "		assert false : 0L;	\n" // 0L isn't 0
166 			+ "	  } catch(AssertionError e){	\n"
167 			+ "		System.out.print(e.getMessage());	\n"
168 			+ "	  }	\n"
169 			+ "	  try {	\n"
170 			+ "		long l = 2L;	\n"
171 			+ "		assert false : l;	\n"
172 			+ "	  } catch(AssertionError e){	\n"
173 			+ "		System.out.println(e.getMessage());	\n"
174 			+ "	  }	\n"
175 			+ "  }	\n"
176 			+ "} \n" },
177 		"102", //expected display
178 		null, // use default class-path
179 		true, // flush previous output dir content
180 		new String[] {"-ea"});
181 	}
test008()182 	public void test008() {
183 		this.runConformTest(new String[] {
184 			"A4.java",
185 			"public class A4 { \n"
186 			+ "	public static void main(String[] args) {\n"
187 			+ "   try {	\n"
188 			+ "		assert false : 1.0f;	\n"
189 			+ "	  } catch(AssertionError e){	\n"
190 			+ "		System.out.print(e.getMessage());	\n"
191 			+ "	  }	\n"
192 			+ "	  try {	\n"
193 			+ "		float f = 2.0f;	\n"
194 			+ "		assert false : f;	\n"
195 			+ "	  } catch(AssertionError e){	\n"
196 			+ "		System.out.println(e.getMessage());	\n"
197 			+ "	  }	\n"
198 			+ "  }	\n"
199 			+ "} \n" },
200 		"1.02.0", //expected display
201 		null, // use default class-path
202 		true, // do not flush previous output dir content
203 		new String[] {"-ea"});
204 	}
test009()205 	public void test009() {
206 		this.runConformTest(new String[] {
207 			"A4.java",
208 			"public class A4 { \n"
209 			+ "	public static void main(String[] args) {\n"
210 			+ "   try {	\n"
211 			+ "		assert false : 1.0;	\n"
212 			+ "	  } catch(AssertionError e){	\n"
213 			+ "		System.out.print(e.getMessage());	\n"
214 			+ "	  }	\n"
215 			+ "	  try {	\n"
216 			+ "		double d = 2.0;	\n"
217 			+ "		assert false : d;	\n"
218 			+ "	  } catch(AssertionError e){	\n"
219 			+ "		System.out.println(e.getMessage());	\n"
220 			+ "	  }	\n"
221 			+ "  }	\n"
222 			+ "} \n" },
223 		"1.02.0", //expected display
224 		null, // use default class-path
225 		true, // flush previous output dir content
226 		new String[] {"-ea"});
227 	}
228 	// http://dev.eclipse.org/bugs/show_bug.cgi?id=22334
test010()229 	public void test010() {
230 		this.runConformTest(new String[] {
231 			"X.java",
232 			"public class X { \n" +
233 			"	public static void main(String[] args) { \n" +
234 			"		I.Inner inner = new I.Inner(); \n" +
235 			"		try { \n" +
236 			"			inner.test(); \n" +
237 			"			System.out.println(\"FAILED\"); \n" +
238 			"		} catch(AssertionError e){ \n" +
239 			"			System.out.println(\"SUCCESS\"); \n" +
240 			"		} \n" +
241 			"	} \n" +
242 			"} \n" +
243 			"interface I { \n" +
244 			"  public static class Inner { \n" +
245 			"    public void test() { \n" +
246 			"      assert false; \n" +
247 			"    } \n" +
248 			"  } \n" +
249 			"} \n" },
250 		"SUCCESS",
251 		null, // use default classpath
252 		true, // flush previous output dir content
253 		new String[] {"-ea"});
254 	}
255 
256 	/**
257 	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=28750
258 	 */
test011()259 	public void test011() {
260 		this.runConformTest(
261 			new String[] {
262 				"AssertTest.java",
263 				"public class AssertTest {\n" +
264 				"   public AssertTest() {}\n" +
265 				"   public class InnerClass {\n" +
266 				"      InnerClass() {\n" +
267 				"        assert(false);\n" +
268 				"      }\n" +
269 				"   }\n" +
270 				"   \n" +
271 				"   public static void main(String[] args) {	\n" +
272 				"        System.out.print(\"SUCCESS\");	\n" +
273 				"	}	\n" +
274 				"}"
275 			},
276 			"SUCCESS"); // expected output
277 	}
278 	/**
279 	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=57743
280 	 */
test012()281 	public void test012() {
282 		this.runConformTest(
283 			new String[] {
284 				"X.java",
285 				"public class X {\n" +
286 				"    public static void main( String[] args ) {\n" +
287 				"        try {\n" +
288 				"            throw new Throwable( \"This is a test\");\n" +
289 				"        }\n" +
290 				"        catch( Throwable ioe ) {\n" +
291 				"            assert false : ioe;\n" +
292 				"        }\n" +
293 				"        System.out.print(\"SUCCESS\");	\n" +
294 				"    }\n" +
295 				"}\n"
296 			},
297 			"SUCCESS"); // expected output
298 	}
299 
300 	/**
301 	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=157389
302 	 */
test013()303 	public void test013() {
304 		this.runConformTest(
305 			new String[] {
306 				"X.java",
307 				"public class X {\n" +
308 				"        static class Y {\n" +
309 				"                public static void test() {\n" +
310 				"                        assert false;\n" +
311 				"                        System.out.println(\"SUCCESS\");\n" +
312 				"                }\n" +
313 				"        }\n" +
314 				"        public static void main(String[] args) {\n" +
315 				"                ClassLoader classLoader = new X().getClass().getClassLoader();\n" +
316 				"                // enable assertion for X.Y\n" +
317 				"                classLoader.setClassAssertionStatus(\"X$Y\", true);\n" +
318 				"                X.Y.test();\n" +
319 				"        }\n" +
320 				"}"
321 			},
322 			"SUCCESS"); // expected output
323 	}
324 
325 	/**
326 	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=163600
327 	 */
test014()328 	public void test014() {
329 		this.runConformTest(
330 			new String[] {
331 				"X.java",
332 				"public class X {\n" +
333 				"\n" +
334 				"	public static class Foo {\n" +
335 				"		public void myMethod(boolean trash) {\n" +
336 				"			System.out.println(\"Expecting class Foo\");\n" +
337 				"			Class c = Foo.class;\n" +
338 				"			System.out.println(\"Got the class \" + c);\n" +
339 				"		}\n" +
340 				"	}\n" +
341 				"	public static class Bar {\n" +
342 				"		public void myMethod(boolean doAssert) {\n" +
343 				"			System.out.println(\"Expecting class Bar\");\n" +
344 				"			Class c = Bar.class;\n" +
345 				"			System.out.println(\"Got the class \" + c);\n" +
346 				"			assert c.getName().endsWith(\"Bar\");\n" +
347 				"		}\n" +
348 				"	}\n" +
349 				"	public static void main(String[] args) {\n" +
350 				"		new Foo().myMethod(false);\n" +
351 				"		new Bar().myMethod(false);\n" +
352 				"	}\n" +
353 				"}"
354 			},
355 			"Expecting class Foo\n" +
356 			"Got the class class X$Foo\n" +
357 			"Expecting class Bar\n" +
358 			"Got the class class X$Bar"); // expected output
359 	}
360 
361 	/**
362 	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=163600
363 	 */
test015()364 	public void test015() {
365 		this.runConformTest(
366 			new String[] {
367 				"X.java",
368 				"public class X {\n" +
369 				"\n" +
370 				"	public static class Foo {\n" +
371 				"		public void myMethod(boolean trash) {\n" +
372 				"			System.out.println(\"Expecting class Foo\");\n" +
373 				"			Class c = Foo.class;\n" +
374 				"			System.out.println(\"Got the class \" + c);\n" +
375 				"		}\n" +
376 				"	}\n" +
377 				"	public static class Bar {\n" +
378 				"		public void myMethod(boolean doAssert) {\n" +
379 				"			System.out.println(\"Expecting class Bar\");\n" +
380 				"			Class c = Bar.class;\n" +
381 				"			try {\n" +
382 				"				assert c.getName().endsWith(\"Bar2\");\n" +
383 				"			} catch(AssertionError e) {\n" +
384 				"				System.out.println(\"SUCCESS\");\n" +
385 				"			}\n" +
386 				"			System.out.println(\"Got the class \" + c);\n" +
387 				"		}\n" +
388 				"	}\n" +
389 				"	public static void main(String[] args) {\n" +
390 				"		new Foo().myMethod(false);\n" +
391 				"		new Bar().myMethod(false);\n" +
392 				"	}\n" +
393 				"}"
394 			},
395 			"Expecting class Foo\n" +
396 			"Got the class class X$Foo\n" +
397 			"Expecting class Bar\n" +
398 			"SUCCESS\n" +
399 			"Got the class class X$Bar",
400 			null, // use default classpath
401 			true, // flush previous output dir content
402 			new String[] {"-ea"});
403 	}
404 
405 	/**
406 	 * http://dev.eclipse.org/bugs/show_bug.cgi?id=163600
407 	 */
test016()408 	public void test016() {
409 		this.runConformTest(
410 			new String[] {
411 				"X.java",
412 				"public class X {\n" +
413 				"\n" +
414 				"	public static class Foo {\n" +
415 				"		public void myMethod(boolean trash) {\n" +
416 				"			System.out.println(\"Expecting class Foo\");\n" +
417 				"			Class c = Foo.class;\n" +
418 				"			System.out.println(\"Got the class \" + c);\n" +
419 				"		}\n" +
420 				"	}\n" +
421 				"	public static class Bar {\n" +
422 				"		public void myMethod(boolean doAssert) {\n" +
423 				"			System.out.println(\"Expecting class Bar\");\n" +
424 				"			Class c = Bar.class;\n" +
425 				"			try {\n" +
426 				"				assert c.getName().endsWith(\"Bar2\");\n" +
427 				"				System.out.println(\"SUCCESS\");\n" +
428 				"			} catch(AssertionError e) {\n" +
429 				"				System.out.println(\"FAILED\");\n" +
430 				"			}\n" +
431 				"			System.out.println(\"Got the class \" + c);\n" +
432 				"		}\n" +
433 				"	}\n" +
434 				"	public static void main(String[] args) {\n" +
435 				"		new Foo().myMethod(false);\n" +
436 				"		new Bar().myMethod(false);\n" +
437 				"	}\n" +
438 				"}"
439 			},
440 			"Expecting class Foo\n" +
441 			"Got the class class X$Foo\n" +
442 			"Expecting class Bar\n" +
443 			"SUCCESS\n" +
444 			"Got the class class X$Bar",
445 			null, // use default classpath
446 			true, // flush previous output dir content
447 			new String[] {"-da"});
448 	}
449 	//https://bugs.eclipse.org/bugs/show_bug.cgi?id=255008
test017()450 	public void test017() {
451 		runNegativeTest(
452 			new String[] { /* test files */
453 				"X.java",
454 				"public class X {\n" +
455 				"	protected void transform1(boolean srcPts) {\n" +
456 				"		final float error1;\n" +
457 				"		assert !(srcPts && (error1 = maxError()) > 0) : error1;\n" +
458 				"	}\n" +
459 				"	float foo1(boolean srcPts) {\n" +
460 				"		final float error2;\n" +
461 				"		if (!(srcPts && (error2 = maxError()) > 0)) {\n" +
462 				"		} else {\n" +
463 				"			return error2;\n" +
464 				"		}\n" +
465 				"		return 0;\n" +
466 				"	}\n" +
467 				"	float bar1(boolean srcPts) {\n" +
468 				"		final float error3;\n" +
469 				"		if ((srcPts && (error3 = maxError()) > 0)) {\n" +
470 				"			return error3;\n" +
471 				"		}\n" +
472 				"		return 0;\n" +
473 				"	}	\n" +
474 				"	protected void transform2(boolean srcPts) {\n" +
475 				"		final float error4;\n" +
476 				"		assert (srcPts && (error4 = maxError()) > 0) : error4;\n" +
477 				"	}\n" +
478 				"	float foo2(boolean srcPts) {\n" +
479 				"		final float error5;\n" +
480 				"		if (srcPts && (error5 = maxError()) > 0) {\n" +
481 				"		} else {\n" +
482 				"			return error5;\n" +
483 				"		}\n" +
484 				"		return 0;\n" +
485 				"	}\n" +
486 				"	float bar2(boolean srcPts) {\n" +
487 				"		final float error6;\n" +
488 				"		if (!(srcPts && (error6 = maxError()) > 0)) {\n" +
489 				"			return error6;\n" +
490 				"		}\n" +
491 				"		return 0;\n" +
492 				"	}\n" +
493 				"	private float maxError() {\n" +
494 				"		return 0;\n" +
495 				"	}\n" +
496 				"\n" +
497 				"}\n"
498 			},
499 			"----------\n" +
500 			"1. ERROR in X.java (at line 23)\n" +
501 			"	assert (srcPts && (error4 = maxError()) > 0) : error4;\n" +
502 			"	                                               ^^^^^^\n" +
503 			"The local variable error4 may not have been initialized\n" +
504 			"----------\n" +
505 			"2. ERROR in X.java (at line 29)\n" +
506 			"	return error5;\n" +
507 			"	       ^^^^^^\n" +
508 			"The local variable error5 may not have been initialized\n" +
509 			"----------\n" +
510 			"3. ERROR in X.java (at line 36)\n" +
511 			"	return error6;\n" +
512 			"	       ^^^^^^\n" +
513 			"The local variable error6 may not have been initialized\n" +
514 			"----------\n");
515 	}
516 	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=328361
test018()517 	public void test018() {
518 		this.runNegativeTest(new String[] {
519 			"X.java",
520 			"public class X {\n" +
521 			"    static final int i;\n" +
522 			"    static {\n" +
523 			"        assert (i = 0) == 0;\n" +
524 			"        System.out.println(i);\n" +
525 			"    }\n" +
526 			"}"
527 		},
528 		"----------\n" +
529 		"1. ERROR in X.java (at line 2)\n" +
530 		"	static final int i;\n" +
531 		"	                 ^\n" +
532 		"The blank final field i may not have been initialized\n" +
533 		"----------\n" +
534 		"2. ERROR in X.java (at line 5)\n" +
535 		"	System.out.println(i);\n" +
536 		"	                   ^\n" +
537 		"The blank final field i may not have been initialized\n" +
538 		"----------\n");
539 	}
540 	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=328361
test019()541 	public void test019() {
542 		this.runConformTest(new String[] {
543 			"X.java",
544 			"public class X {\n" +
545 			"    static final int i;\n" +
546 			"    static {\n" +
547 			"        i = 0;\n" +
548 			"        assert i == 0;\n" +
549 			"        System.out.println(i);\n" +
550 			"    }\n" +
551 			"}"
552 		},
553 		"");
554 	}
555 	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=328361
test020()556 	public void test020() throws Exception {
557 		this.runNegativeTest(
558 			new String[] {
559 					"X.java",
560 					"public class X {\n" +
561 						"    void method1() {\n" +
562 						"		 int i;" +
563 						"        assert (i = 0) == 0;\n" +
564 						"        System.out.println(i);\n" +
565 						"    }\n" +
566 						"}\n"
567 			},
568 			"----------\n" +
569 			"1. ERROR in X.java (at line 4)\n" +
570 			"	System.out.println(i);\n" +
571 			"	                   ^\n" +
572 			"The local variable i may not have been initialized\n" +
573 			"----------\n");
574 	}
575 	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=328361
test021()576 	public void test021() throws Exception {
577 		this.runNegativeTest(
578 			new String[] {
579 				"X.java",
580 				"public class X {\n" +
581 					"	public int bar() {\n" +
582 					"		return 1;\n" +
583 					"	}\n" +
584 					"    void method1() {\n" +
585 						"		 int i;" +
586 						"        assert (i = this.bar()) == 0;\n" +
587 						"        System.out.println(i);\n" +
588 						"    }\n" +
589 						"}\n"
590 			},
591 			"----------\n" +
592 			"1. ERROR in X.java (at line 7)\n" +
593 			"	System.out.println(i);\n" +
594 			"	                   ^\n" +
595 			"The local variable i may not have been initialized\n" +
596 			"----------\n");
597 	}
598 	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=328361
test022()599 	public void test022() throws Exception {
600 		this.runNegativeTest(
601 			new String[] {
602 				"X.java",
603 				"public class X {\n" +
604 					"	public int bar() {\n" +
605 					"		return 1;\n" +
606 					"	}\n" +
607 					"    void method1() {\n" +
608 						"		 int i;\n" +
609 						"        assert i++ == 0;\n" +
610 						"        System.out.println(i);\n" +
611 						"    }\n" +
612 						"}\n"
613 			},
614 			"----------\n" +
615 			"1. ERROR in X.java (at line 7)\n" +
616 			"	assert i++ == 0;\n" +
617 			"	       ^\n" +
618 			"The local variable i may not have been initialized\n" +
619 			"----------\n" +
620 			"2. ERROR in X.java (at line 8)\n" +
621 			"	System.out.println(i);\n" +
622 			"	                   ^\n" +
623 			"The local variable i may not have been initialized\n" +
624 			"----------\n");
625 	}
test023()626 	public void test023() {
627 		if (this.complianceLevel < ClassFileConstants.JDK1_8)
628 			return;
629 		this.runConformTest(new String[] {"X.java",
630 				"interface Foo {\n" +
631 				"  default Object test(Object a) {\n" +
632 				"    assert a != null; // triggers creation of bogus synthetic field\n" +
633 				"    return a;\n" +
634 				"  }\n" +
635 				"}\n" +
636 				"public class X implements Foo {\n" +
637 				"	public static void main(String[] args) {\n" +
638 				"		new X().test(\"\");\n" +
639 				"		System.out.println(\"Hello\");\n" +
640 				"	}\n" +
641 				"}\n"}, "Hello");
642 	}
643 }
644