Skip to content

Commit 1d78e47

Browse files
authored
Skip preview feature tests against future JRE versions (eclipse-jdt#4329)
Most preview related tests fail when the JRE version is higher than the highest compliance level supported by ECJ. Guard such tests from being run in such cases. eclipse-jdt#4329
1 parent 3e46a17 commit 1d78e47

7 files changed

Lines changed: 37 additions & 25 deletions

File tree

org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/RecordElementsTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ protected void internalTestWithPreview(JavaCompiler compiler, String processor,
189189
options.add("-source");
190190
options.add(compliance);
191191
}
192-
// if (preview)
193-
// options.add("--enable-preview");
194192
BatchTestUtils.compileInModuleMode(compiler, options, processor, targetFolder, null, true, processBinaries);
195193
// If it succeeded, the processor will have set this property to "succeeded";
196194
// if not, it will set it to an error value.

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ImplicitlyDeclaredClassesTest.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.Locale;
1616
import java.util.Map;
1717
import java.util.stream.Stream;
18-
import javax.lang.model.SourceVersion;
18+
import org.eclipse.jdt.core.tests.util.PreviewTest;
1919
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
2020
import org.eclipse.jdt.core.util.ClassFormatException;
2121
import org.eclipse.jdt.internal.compiler.CompilationResult;
@@ -32,6 +32,7 @@
3232
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
3333
import org.junit.Test;
3434

35+
@PreviewTest
3536
public class ImplicitlyDeclaredClassesTest extends AbstractRegressionTest9 {
3637
public static boolean optimizeStringLiterals = false;
3738
private static final JavacTestOptions JAVAC_OPTIONS = new JavacTestOptions("--enable-preview -source 24");
@@ -49,7 +50,6 @@ public ImplicitlyDeclaredClassesTest(String testName){
4950
protected void setUp() throws Exception {
5051
this.runJavacOptIn = true;
5152
super.setUp();
52-
this.isJRE25Plus = isRunningIn25Jre();
5353
}
5454
@Override
5555
protected void tearDown() throws Exception {
@@ -69,15 +69,6 @@ public static junit.framework.Test suite() {
6969
protected Map<String, String> getCompilerOptions() {
7070
return getCompilerOptions(true);
7171
}
72-
private boolean isJRE25Plus = false;
73-
public boolean isRunningIn25Jre() {
74-
try {
75-
SourceVersion.valueOf("RELEASE_25");
76-
} catch(IllegalArgumentException iae) {
77-
return false;
78-
}
79-
return true;
80-
}
8172
// Enables the tests to run individually
8273
protected Map<String, String> getCompilerOptions(boolean previewFlag) {
8374
Map<String, String> defaultOptions = super.getCompilerOptions();
@@ -90,14 +81,10 @@ protected Map<String, String> getCompilerOptions(boolean previewFlag) {
9081
}
9182
@Override
9283
protected void runConformTest(String[] testFiles, String expectedOutput) {
93-
if(this.isJRE25Plus)
94-
return;
9584
runConformTest(testFiles, expectedOutput, null, VMARGS, new JavacTestOptions("-source 24 --enable-preview"));
9685
}
9786
@Override
9887
protected void runConformTest(String[] testFiles, String expectedOutput, Map<String, String> customOptions) {
99-
if(this.isJRE25Plus)
100-
return;
10188
runConformTest(testFiles, expectedOutput, customOptions, VMARGS, JAVAC_OPTIONS);
10289
}
10390
@Override
@@ -274,8 +261,6 @@ class XYZ {
274261
// Test implicit type with a valid candidate main method (public but no static, and String[] argument)
275262
@Test
276263
public void testImplicitType006() throws IOException, ClassFormatException {
277-
if (this.isJRE25Plus)
278-
return;
279264
try {
280265
runConformTest(
281266
new String[] {"X.java",
@@ -383,8 +368,6 @@ public static void main(String[] args) {
383368
"Hello2");
384369
}
385370
public void testGH3137b() {
386-
if (this.isJRE25Plus)
387-
return;
388371
runConformTest(new String[] {
389372
"X.java",
390373
"""
@@ -400,8 +383,6 @@ public static void main(String[] args) {
400383
JavacTestOptions.SKIP);
401384
}
402385
public void testGH3714() {
403-
if (this.isJRE25Plus)
404-
return;
405386
runConformTest(new String[] {
406387
"Main.java",
407388
"""

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PreviewFlagTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
import java.io.IOException;
1616
import java.util.Map;
1717
import junit.framework.Test;
18+
import org.eclipse.jdt.core.tests.util.PreviewTest;
1819
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
1920
import org.eclipse.jdt.core.util.ClassFormatException;
2021
import org.eclipse.jdt.internal.compiler.batch.FileSystem;
2122
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
2223
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
2324

25+
@PreviewTest
2426
public class PreviewFlagTest extends AbstractRegressionTest9 {
2527

2628
private static final JavacTestOptions JAVAC_OPTIONS = new JavacTestOptions("--enable-preview -source 24");

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
import java.util.Map;
1616
import junit.framework.Test;
17+
import org.eclipse.jdt.core.tests.util.PreviewTest;
1718
import org.eclipse.jdt.internal.compiler.batch.FileSystem;
1819
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
1920
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
2021

22+
@PreviewTest
2123
public class PrimitiveInPatternsTest extends AbstractRegressionTest9 {
2224

2325
private static final JavacTestOptions JAVAC_OPTIONS = new JavacTestOptions("--enable-preview -source 24");

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SuperAfterStatementsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
import java.io.IOException;
1616
import java.util.Map;
1717
import junit.framework.Test;
18+
import org.eclipse.jdt.core.tests.util.PreviewTest;
1819
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
1920
import org.eclipse.jdt.core.util.ClassFormatException;
2021
import org.eclipse.jdt.internal.compiler.batch.FileSystem;
2122
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
2223
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
23-
24+
@PreviewTest
2425
public class SuperAfterStatementsTest extends AbstractRegressionTest9 {
2526

2627
private static final JavacTestOptions JAVAC_OPTIONS = new JavacTestOptions("--enable-preview -source 24");

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/AbstractCompilerTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.jdt.core.tests.util;
1515

1616
import java.io.File;
17+
import java.lang.annotation.Annotation;
1718
import java.lang.reflect.Constructor;
1819
import java.lang.reflect.Field;
1920
import java.lang.reflect.InvocationTargetException;
@@ -194,10 +195,18 @@ private static Test buildComplianceTestSuite(List testClasses, Class setupClass,
194195
}
195196
if (complianceSuite == null)
196197
return null;
197-
198+
String specVersion = System.getProperty("java.specification.version");
199+
int spec = Integer.parseInt(specVersion);
200+
boolean futureJREUsed = spec > Integer.parseInt(CompilerOptions.getLatestVersion());
198201
// add tests
199202
for (int i=0, m=testClasses.size(); i<m ; i++) {
200203
Class testClass = (Class)testClasses.get(i);
204+
if (futureJREUsed) {
205+
Annotation annotation = testClass.getAnnotation(PreviewTest.class);
206+
if (annotation != null) {
207+
continue;
208+
}
209+
}
201210
TestSuite suite = new TestSuite(testClass.getName());
202211
int inheritedDepth = 0;
203212
try {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.eclipse.jdt.core.tests.util;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Indicates that the target test suite tests preview Java features. In certain cases
10+
* a test is not meant to be run with a future JRE. For e.g., if the highest supported
11+
* version by ECJ is 24, running certain tests, especially those related to preview
12+
* features, may produce unexpected results when run with a JRE 25. Authors of test
13+
* can mark such tests with this annotation and allow them to be skipped under
14+
* conditions such as above.
15+
*
16+
*/
17+
@Target(ElementType.TYPE)
18+
@Retention(RetentionPolicy.RUNTIME)
19+
public @interface PreviewTest {}

0 commit comments

Comments
 (0)