Skip to main content
summaryrefslogtreecommitdiffstats
blob: a9e9e449872a1315c11f6e8d59d3039eefb4427e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * Here we focus on various aspects of the runtime behavior of the generated
 * code.
 */
package org.eclipse.jdt.core.tests.compiler.regression;

import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;

import junit.framework.Test;

public class RuntimeTests extends AbstractRegressionTest {

public RuntimeTests(String name) {
	super(name);
}

// Static initializer to specify tests subset using TESTS_* static variables
// All specified tests which does not belong to the class are skipped...
// Only the highest compliance level is run; add the VM argument
// -Dcompliance=1.4 (for example) to lower it if needed
static {
//		TESTS_NAMES = new String[] { "test0001" };
//	 	TESTS_NUMBERS = new int[] { 1 };
//		TESTS_RANGE = new int[] { 1, -1 };
}

public static Test suite() {
	return buildAllCompliancesTestSuite(testClass());
}

public static Class testClass() {
	return RuntimeTests.class;
}

// decided not to keep this active because of negative effects on the test
// series (the OOME potentially causing grief to others)
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=217078
// memory exhaustion - try to allocate too big an instance
public void _test0001_memory_exhaustion() {
	runTest(
		new String[] { /* testFiles */
			"X.java",
			"public class X {\n" +
			"  public static void main(String args[]) {\n" +
			"    try {" +
			"      Y y = new Y(Integer.MAX_VALUE);\n" +
			"    }" +
			"    catch (OutOfMemoryError e) {\n" +
			"      System.out.println(\"SUCCESS\");\n" +
			"      return;\n" +
			"    }\n" +
			"    System.out.println(\"FAILURE\");\n" +
			"  }\n" +
			"}\n" +
			"class Y {\n" +
			"  long storage[];\n" +
			"  Y(int itemsNb) {\n" +
			"    storage = new long[itemsNb];\n" +
			"  }\n" +
			"}\n"},
		false /* expectingCompilerErrors */,
		"" /* expectedCompilerLog */,
		"SUCCESS" /* expectedOutputString */,
		null /* expectedErrorString - skip this because some JREs emit additional info to stderr in case of exception */,
		false /* forceExecution */,
		null /* classLib */,
		true /* shouldFlushOutputDirectory */,
		null /* vmArguments */,
		null /* customOptions */,
		null /* clientRequestor */,
		true /* skipJavac */);
}

// synchronization - concurrent access to a resource with explicit and
// implicit locks
public void test0500_synchronization() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main (String args[]) {\n" +
			"  new Lock().implicitLock();\n" +
			"}\n" +
			"}\n" +
			"class Lock extends Thread {\n" +
			"  byte step = 0;\n" +
			"  void logStep(String start) {\n" +
			"    System.out.println(start + \" \" + this.step); //$NON-NLS-1$\n" +
			"  }\n" +
			"  public void run() {\n" +
			"    for (int i = 1; i < 3; i++) {\n" +
			"      logStep(\"explicit lock\"); //$NON-NLS-1$\n" +
			"      synchronized (this) {\n" +
			"        this.step++;\n" +
			"        notify();\n" +
			"        while(this.step < 2 * i) {\n" +
			"          try {\n" +
			"            wait();\n" +
			"          } catch (InterruptedException e) {\n" +
			"            System.out.println(\"EXCEPTION\"); //$NON-NLS-1$\n" +
			"          }\n" +
			"        }\n" +
			"      }\n" +
			"    }\n" +
			"  }\n" +
			"  synchronized void implicitLock() {\n" +
			"      this.start();\n" +
			"      for (int i = 0; i < 2; i++) {\n" +
			"        while (this.step < 1 + i * 2) {\n" +
			"          try {\n" +
			"            wait();\n" +
			"          } catch (InterruptedException e) {\n" +
			"            System.out.println(\"EXCEPTION\"); //$NON-NLS-1$\n" +
			"          }\n" +
			"        }\n" +
			"        logStep(\"implicit lock\"); //$NON-NLS-1$\n" +
			"        this.step++;\n" +
			"        notify();\n" +
			"      }\n" +
			"      return;\n" +
			"  }\n" +
			"}\n"},
		"explicit lock 0\n" +
		"implicit lock 1\n" +
		"explicit lock 2\n" +
		"implicit lock 3"
	);
}

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=126712
// reflection - access to a public method of a package visible
// class through a public extending class
public void test0600_reflection() {
	runConformTest(
		true,
		new String[] {
			"X.java",
			"import java.lang.reflect.*;\n" +
			"import p.*;\n" +
			"public class X {\n" +
			"static public void main (String args[]) {\n" +
			"  Y y = new Y();\n" +
			"  try {\n" +
			"    Method foo = Y.class.getMethod(\"foo\", (Class []) null);\n" +
			"    y.foo();\n" +
			"    foo.invoke(y, (Object []) null);\n" +
			"  } catch (NoSuchMethodException e) {\n" +
			"      //ignore\n" +
			"  } catch (InvocationTargetException e) {\n" +
			"      //ignore\n" +
			"  } catch (IllegalAccessException e) {\n" +
			"    System.out.print(\"FAILURE: IllegalAccessException\");\n" +
			"  }\n" +
			"}\n" +
			"}",
			"p/Y.java",
			"package p;\n" +
			"public class Y extends Z {\n" +
			"  /* empty */\n" +
			"}\n",
			"p/Z.java",
			"package p;\n" +
			"class Z {\n" +
			"  public void foo() {\n" +
			"  System.out.println(\"SUCCESS\"); //$NON-NLS-1$\n" +
			"  }\n" +
			"}\n"},
		"",
		"SUCCESS\n" +
		"FAILURE: IllegalAccessException",
		"",
		JavacTestOptions.EclipseJustification.EclipseBug126712
	);
}

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=126712
// reflection - access to a public field of a package visible
// class through a public extending class
public void test0601_reflection() {
	this.runConformTest(
		new String[] {
			"X.java",
			"import java.lang.reflect.*;\n" +
			"import p.*;\n" +
			"public class X {\n" +
			"static public void main (String args[]) {\n" +
			"  Y y = new Y();\n" +
			"  try {\n" +
			"    Field f = Y.class.getField(\"m\");\n" +
			"    System.out.println(y.m);\n" +
			"    System.out.println(f.get(y));\n" +
			"  } catch (NoSuchFieldException e) {\n" +
			"      //ignore\n" +
			"  } catch (IllegalAccessException e) {\n" +
			"    System.out.print(\"FAILURE: IllegalAccessException\");\n" +
			"  }\n" +
			"}\n" +
			"}",
			"p/Y.java",
			"package p;\n" +
			"public class Y extends Z {\n" +
			"  /* empty */\n" +
			"}\n",
			"p/Z.java",
			"package p;\n" +
			"class Z {\n" +
			"  public String m = \"SUCCESS\";\n" +
			"}\n"},
		"SUCCESS\n" +
		"FAILURE: IllegalAccessException"
	);
}

// partial rebuild - method signature changed (return type)
public void test1000_partial_rebuild() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main(String args[]) {\n" +
			"  Z.go();\n" +
			"}\n" +
			"}\n",
			"Z.java",
			"public class Z {\n" +
			"static public void go() {\n" +
			"  int flag = 0;\n" +
			"  try {\n" +
			"    new Y().random();\n" +
			"    flag = 1;\n" +
			"  }\n" +
			"  catch (NoSuchMethodError e) {\n" +
			"    flag = 2;\n" +
			"  }\n" +
			"  catch (Throwable t) {\n" +
			"    flag = 3;\n" +
			"  }\n" +
			"  System.out.println(flag);\n" +
			"}\n" +
			"}\n",
			"Y.java",
			"public class Y {\n" +
			"java.util.Random generator = new java.util.Random();" +
			"public byte random() {\n" +
			"  return (byte) (generator.nextInt() % Byte.MAX_VALUE);\n" +
			"}\n" +
			"}\n",
			},
		"1");
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main(String args[]) {\n" +
			"  Z.go();\n" +
			"}\n" +
			"}\n",
			"Y.java",
			"public class Y {\n" +
			"java.util.Random generator = new java.util.Random();" +
			"public int random() {\n" + // random now returns an int
			"  return generator.nextInt();\n" +
			"}\n" +
			"}\n",
			},
		"2",
		null,
		false, // do not purge output directory - pick old version of Z.class
		null);
}

// partial rebuild - method signature changed (parameter type)
public void test1001_partial_rebuild() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main(String args[]) {\n" +
			"  Z.go();\n" +
			"}\n" +
			"}\n",
			"Z.java",
			"public class Z {\n" +
			"static public void go() {\n" +
			"  byte flag = 0;\n" +
			"  try {\n" +
			"    new Y().random(flag);\n" +
			"    flag = 1;\n" +
			"  }\n" +
			"  catch (NoSuchMethodError e) {\n" +
			"    flag = 2;\n" +
			"  }\n" +
			"  catch (Throwable t) {\n" +
			"    flag = 3;\n" +
			"  }\n" +
			"  System.out.println(flag);\n" +
			"}\n" +
			"}\n",
			"Y.java",
			"public class Y {\n" +
			"public int random(byte seed) {\n" +
			"  return seed++;\n" +
			"}\n" +
			"}\n",
			},
		"1");
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main(String args[]) {\n" +
			"  Z.go();\n" +
			"}\n" +
			"}\n",
			"Y.java",
			"public class Y {\n" +
			"public int random(int seed) {\n" + // seed now of type int
			"  return seed++;\n" +
			"}\n" +
			"}\n",
			},
		"2",
		null,
		false, // do not purge output directory - pick old version of Z.class
		null);
}

// partial rebuild - method signature changed (visibility)
public void test1002_partial_rebuild() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main(String args[]) {\n" +
			"  new Z().go();\n" +
			"}\n" +
			"}\n",
			"Z.java",
			"public class Z extends p.Y {\n" +
			"  class ZInner extends YInner {\n" +
			"    // empty\n" +
			"  }\n" +
			"public void go() {\n" +
			"  byte flag = 0;\n" +
			"  try {\n" +
			"    new ZInner().foo();\n" +
			"    flag = 1;\n" +
			"  }\n" +
			"  catch (IllegalAccessError e) {\n" +
			"    flag = 2;\n" +
			"  }\n" +
			"  catch (Throwable t) {\n" +
			"    flag = 3;\n" +
			"  }\n" +
			"  System.out.println(flag);\n" +
			"}\n" +
			"}\n",
			"p/Y.java",
			"package p;\n" +
			"public class Y {\n" +
			"  public class YInner {\n" +
			"    public void foo() {\n" +
			"      return;\n" +
			"    }\n" +
			"  }\n" +
			"}\n",
			},
		"1");
	this.runConformTest(
		false,
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main(String args[]) {\n" +
			"  new Z().go();\n" +
			"}\n" +
			"}\n",
			"p/Y.java",
			"package p;\n" +
			"public class Y {\n" +
			"  public class YInner {\n" +
			"    void foo() {\n" + // now foo no more visible (package only)
			"      return;\n" +
			"    }\n" +
			"  }\n" +
			"}\n",
			},
		"",
		"2",
		"",
		JavacTestOptions.JavacHasABug.JavacBugFixed_6_10);
}

// partial rebuild - method signature changed (visibility)
public void test1003_partial_rebuild() {
	this.runConformTest(
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main(String args[]) {\n" +
			"  new Z().go();\n" +
			"}\n" +
			"}\n",
			"Z.java",
			"public class Z extends p.Y {\n" +
			"  class ZInner extends YInner {\n" +
			"    // empty\n" +
			"  }\n" +
			"public void go() {\n" +
			"  byte flag = 0;\n" +
			"  try {\n" +
			"    new ZInner().foo();\n" +
			"    flag = 1;\n" +
			"  }\n" +
			"  catch (IllegalAccessError e) {\n" +
			"    flag = 2;\n" +
			"  }\n" +
			"  catch (Throwable t) {\n" +
			"    flag = 3;\n" +
			"  }\n" +
			"  System.out.println(flag);\n" +
			"}\n" +
			"}\n",
			"p/Y.java",
			"package p;\n" +
			"public class Y {\n" +
			"  public class YInner {\n" +
			"    public void foo() {\n" +
			"      return;\n" +
			"    }\n" +
			"  }\n" +
			"}\n",
			},
		"1");
	this.runConformTest(
		false, // do not purge output directory - pick old version of Z.class
		new String[] {
			"X.java",
			"public class X {\n" +
			"static public void main(String args[]) {\n" +
			"  new Z().go();\n" +
			"}\n" +
			"}\n",
			"p/Y.java",
			"package p;\n" +
			"public class Y {\n" +
			"  public class YInner {\n" +
			"    protected void foo() {\n" +
			  // now foo no more visible (package + inheriting classes only)
			"      return;\n" +
			"    }\n" +
			"  }\n" +
			"}\n",
			},
		"",
		"2",
		"",
		JavacTestOptions.JavacHasABug.JavacBugFixed_6_10);
}

// partial rebuild - extending class now redefines extended class fields and
//                   methods
// was Compliance_1_x#test009
public void test1004_partial_rebuild() {
	this.runConformTest(
		new String[] {
			"p1/Z.java",
			"package p1; \n"+
			"public class Z {	\n" +
			"	public static void main(String[] arguments) { \n"+
			"		Y y = new Y();	\n" +
			"		System.out.print(y.field);	\n"	+
			"		System.out.print(y.staticField);	\n"	+
			"		System.out.print(y.method());	\n"	+
			"		System.out.println(y.staticMethod());	\n"	+
			"	} \n"+
			"} \n",
			"p1/X.java",
			"package p1; \n"+
			"public class X { \n"+
			"	public String field = \"X.field-\";	\n" +
			"	public static String staticField = \"X.staticField-\";	\n" +
			"	public String method(){ return \"X.method()-\";	}	\n" +
			"	public static String staticMethod(){ return \"X.staticMethod()-\";	}	\n" +
			"} \n",
			"p1/Y.java",
			"package p1; \n"+
			"public class Y extends X { \n"+
			"} \n"
		},
		"X.field-X.staticField-X.method()-X.staticMethod()-");
	String expectedOutput =
		this.complianceLevel == ClassFileConstants.JDK1_3 ?
			"X.field-X.staticField-Y.method()-X.staticMethod()-" :
			"Y.field-Y.staticField-Y.method()-Y.staticMethod()-";
	this.runConformTest(
		new String[] {
			"p1/Y.java",
			"package p1; \n"+
			"public class Y extends X { \n"+
			"	public static void main(String[] arguments) { \n"+
			"		Z.main(arguments);	\n" +
			"	}	\n" +
			"	public String field = \"Y.field-\";	\n" +
			"	public static String staticField = \"Y.staticField-\";	\n" +
			"	public String method(){ return \"Y.method()-\";	}	\n" +
			"	public static String staticMethod(){ return \"Y.staticMethod()-\";	}	\n" +
			"} \n"
		},
		expectedOutput, // expected output
		null, // use default class-path
		false, // do not flush previous output dir content
		null); // no special vm args
}

}

Back to the top