Skip to content

Commit 2119e2f

Browse files
committed
Run tests that need a newer JDK in classes of their own
Tests that need a newer JDK skipped themselves from inside, in three ways, and counted as skipped on JDK 21 among the tests that run (ADR-002). Each now says what it needs on its class: - ModuleImportTest, and the module-import test of RemoveUnusedImportsTest moved to RemoveUnusedImportsJava25Test, carry @EnabledForJreRange(min = JAVA_25), where module imports are final; they started at JDK 23 before, which CI does not run. - The ModuleImport pair of FormatterIntegrationTest is a case in test-cases-java25/, run by FormatterCasesJava25Test, so VERSIONED_TESTS and assumeJavaVersionForTest go. - The value-class tests of FormatterTest and ModifierOrdererTest move to ValueClassTest, enabled where javac has the value modifier: they need a JDK with Valhalla, not a version.
1 parent 970c348 commit 2119e2f

13 files changed

Lines changed: 196 additions & 131 deletions

File tree

‎open-java-format/src/test/java/com/palantir/javaformat/java/FileBasedTests.java‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@
4343

4444
@Execution(ExecutionMode.CONCURRENT)
4545
public final class FileBasedTests {
46-
// Test files that are only used when run with a minimum Java version
47-
private static final ImmutableMultimap<Integer, String> VERSIONED_TESTS =
48-
ImmutableMultimap.<Integer, String>builder()
49-
.putAll(23, "ModuleImport")
50-
.build();
51-
5246
private final Class<?> testClass;
5347
/** The path prefix for all tests if loaded as resources. */
5448
private final Path resourcePrefix;
@@ -66,13 +60,6 @@ public FileBasedTests(Class<?> testClass, String testDirName) {
6660
this.fullTestPath = Paths.get("src/test/resources").resolve(resourcePrefix);
6761
}
6862

69-
public static void assumeJavaVersionForTest(String testName) {
70-
Optional<Integer> maybeJavaVersion =
71-
VERSIONED_TESTS.inverse().get(testName).stream().collect(toOptional());
72-
maybeJavaVersion.ifPresent(version -> Assumptions.assumeTrue(
73-
Formatter.getRuntimeVersion() >= version, String.format("Not running on jdk %d or later", version)));
74-
}
75-
7663
/** The names of the resources under {@link #resourcePrefix}, in every directory of the class path that has it. */
7764
private List<String> resourceNames(ClassLoader classLoader) throws IOException {
7865
var names = new ArrayList<String>();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.palantir.javaformat.java;
16+
17+
import java.util.List;
18+
import org.junit.jupiter.api.DynamicTest;
19+
import org.junit.jupiter.api.TestFactory;
20+
import org.junit.jupiter.api.condition.EnabledForJreRange;
21+
import org.junit.jupiter.api.condition.JRE;
22+
import org.junit.jupiter.api.parallel.Execution;
23+
import org.junit.jupiter.api.parallel.ExecutionMode;
24+
25+
/** The cases under {@code test-cases-java25}, as {@link FormatterCasesTest} runs those of JDK 21, for syntax of JDK 25. */
26+
@EnabledForJreRange(min = JRE.JAVA_25)
27+
@Execution(ExecutionMode.CONCURRENT)
28+
final class FormatterCasesJava25Test {
29+
@TestFactory
30+
List<DynamicTest> cases() throws Exception {
31+
return FormatterCasesTest.casesIn("test-cases-java25");
32+
}
33+
}

‎open-java-format/src/test/java/com/palantir/javaformat/java/FormatterCasesTest.java‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@
2929

3030
/**
3131
* One test for each {@link CaseFile} under {@code test-cases-java21} on the test classpath: formatting the before block
32-
* gives the after block, and formatting that again changes nothing. Adding a case is adding a file.
32+
* gives the after block, and formatting that again changes nothing. Adding a case is adding a file. Cases that need a
33+
* newer JDK are in a folder of that JDK, run by a class of their own, such as {@link FormatterCasesJava25Test}.
3334
*/
3435
@Execution(ExecutionMode.CONCURRENT)
3536
final class FormatterCasesTest {
36-
private static final String CASES = "test-cases-java21";
37-
3837
private static final Formatter FORMATTER = Formatter.createFormatter(
3938
JavaFormatterOptions.builder().style(Style.OJF).build());
4039

4140
@TestFactory
4241
List<DynamicTest> cases() throws Exception {
43-
var folder = requireNonNull(
44-
FormatterCasesTest.class.getClassLoader().getResource(CASES),
45-
"no " + CASES + " folder on the test classpath");
46-
var root = Path.of(folder.toURI());
42+
return casesIn("test-cases-java21");
43+
}
44+
45+
/** The tests of the case files under {@code folder} on the test classpath. */
46+
static List<DynamicTest> casesIn(String folder) throws Exception {
47+
var url = requireNonNull(
48+
FormatterCasesTest.class.getClassLoader().getResource(folder),
49+
"no " + folder + " folder on the test classpath");
50+
var root = Path.of(url.toURI());
4751
var paths = CaseFile.pathsIn(root);
4852

4953
// A wrong folder or a resource filter would otherwise pass with no tests at all.
@@ -59,13 +63,13 @@ List<DynamicTest> cases() throws Exception {
5963
var once = FORMATTER.formatSourceAndFixImports(testCase.before());
6064

6165
assertThat(once)
62-
.as(() -> CASES + "/" + path + ", first run, " + testCase.firstDifference(once))
66+
.as(() -> folder + "/" + path + ", first run, " + testCase.firstDifference(once))
6367
.isEqualTo(testCase.after());
6468

6569
var twice = FORMATTER.formatSourceAndFixImports(once);
6670

6771
assertThat(twice)
68-
.as(() -> CASES + "/" + path + ", second run, " + testCase.firstDifference(twice))
72+
.as(() -> folder + "/" + path + ", second run, " + testCase.firstDifference(twice))
6973
.isEqualTo(once);
7074
}));
7175
}

‎open-java-format/src/test/java/com/palantir/javaformat/java/FormatterIntegrationTest.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.palantir.javaformat.java;
1616

1717
import static com.google.common.base.Preconditions.checkNotNull;
18-
import static com.palantir.javaformat.java.FileBasedTests.assumeJavaVersionForTest;
1918
import static com.palantir.javaformat.java.FileBasedTests.isRecreate;
2019
import static org.assertj.core.api.Assertions.assertThat;
2120

@@ -74,7 +73,6 @@ private static boolean isDebugMode() {
7473

7574
@TestTemplate
7675
public void format() {
77-
assumeJavaVersionForTest(name);
7876
try {
7977
Formatter formatter = createFormatter();
8078
String output = formatter.formatSource(input);
@@ -99,7 +97,6 @@ private static Formatter createFormatter() {
9997

10098
@TestTemplate
10199
public void idempotentLF() {
102-
assumeJavaVersionForTest(name);
103100
Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs");
104101
try {
105102
String mangled = expected.replace(separator(), "\n");
@@ -112,7 +109,6 @@ public void idempotentLF() {
112109

113110
@TestTemplate
114111
public void idempotentCR() {
115-
assumeJavaVersionForTest(name);
116112
Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs");
117113
try {
118114
String mangled = expected.replace(separator(), "\r");
@@ -125,7 +121,6 @@ public void idempotentCR() {
125121

126122
@TestTemplate
127123
public void idempotentCRLF() {
128-
assumeJavaVersionForTest(name);
129124
Assumptions.assumeFalse(isRecreate(), "Not running when recreating test outputs");
130125
try {
131126
String mangled = expected.replace(separator(), "\r\n");

‎open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@
3333
import java.nio.file.Files;
3434
import java.nio.file.Path;
3535
import java.time.Duration;
36-
import java.util.Arrays;
3736
import java.util.List;
38-
import javax.lang.model.element.Modifier;
3937
import org.jspecify.annotations.Nullable;
40-
import org.junit.jupiter.api.Assumptions;
4138
import org.junit.jupiter.api.Test;
4239
import org.junit.jupiter.api.io.TempDir;
4340
import org.junit.jupiter.api.parallel.Execution;
@@ -352,44 +349,6 @@ void indentsCommentsThatShareALineLinearly() throws FormatterException {
352349
assertThat(formatter.formatSource(input.toString()).length()).isLessThan(10_000);
353350
}
354351

355-
@Test
356-
void valueClassAndRecord() throws FormatterException {
357-
Assumptions.assumeTrue(
358-
Arrays.stream(Modifier.values())
359-
.anyMatch(modifier -> modifier.name().equals("VALUE")),
360-
"`value` is a modifier only on a JDK with Project Valhalla's value classes");
361-
String input = """
362-
value final public class PointValueClass {
363-
final int x;
364-
final int y;
365-
366-
PointValueClass(int x, int y) {
367-
this.x = x;
368-
this.y = y;
369-
}
370-
}
371-
372-
value public record PointValueRecord(int x, int y) {}
373-
""";
374-
String expected = """
375-
public final value class PointValueClass {
376-
final int x;
377-
final int y;
378-
379-
PointValueClass(int x, int y) {
380-
this.x = x;
381-
this.y = y;
382-
}
383-
}
384-
385-
public value record PointValueRecord(int x, int y) {}
386-
""";
387-
Formatter formatter = Formatter.createFormatter(
388-
JavaFormatterOptions.builder().style(Style.OJF).build());
389-
390-
assertThat(formatter.formatSource(input)).isEqualTo(expected);
391-
}
392-
393352
@Test
394353
@SuppressWarnings("deprecation")
395354
void palantirStyleAliasMatchesOjfStyle() throws FormatterException {

‎open-java-format/src/test/java/com/palantir/javaformat/java/ModifierOrdererTest.java‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import com.google.common.base.Joiner;
2222
import com.google.common.collect.Range;
2323
import java.util.Arrays;
24-
import javax.lang.model.element.Modifier;
25-
import org.junit.jupiter.api.Assumptions;
2624
import org.junit.jupiter.api.Test;
2725
import org.junit.jupiter.api.parallel.Execution;
2826
import org.junit.jupiter.api.parallel.ExecutionMode;
@@ -115,16 +113,6 @@ void nonSealedBeforeAccessModifier() throws FormatterException {
115113
.isEqualTo("private non-sealed interface B extends I {}");
116114
}
117115

118-
@Test
119-
void valueClass() throws FormatterException {
120-
Assumptions.assumeTrue(
121-
Arrays.stream(Modifier.values())
122-
.anyMatch(modifier -> modifier.name().equals("VALUE")),
123-
"`value` is a modifier only on a JDK with Project Valhalla's value classes");
124-
assertThat(ModifierOrderer.reorderModifiers("value final public").getText())
125-
.isEqualTo("public final value");
126-
}
127-
128116
@Test
129117
void nonMinusValueIsAnExpression() throws FormatterException {
130118
// `non - value` lexes like `non-sealed`, and there is no NON_VALUE modifier to look up for it

‎open-java-format/src/test/java/com/palantir/javaformat/java/ModuleImportTest.java‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,17 @@
2222
import java.io.ByteArrayInputStream;
2323
import java.io.PrintWriter;
2424
import java.io.StringWriter;
25-
import org.junit.jupiter.api.Assumptions;
26-
import org.junit.jupiter.api.BeforeAll;
2725
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.condition.EnabledForJreRange;
27+
import org.junit.jupiter.api.condition.JRE;
2828

2929
/**
3030
* End-to-end tests for module import declarations (JEP 511) through the pipeline the Gradle plugin and Spotless use,
31-
* which reorders imports before formatting. The {@code ModuleImport} golden only exercises {@code formatSource}.
31+
* which reorders imports before formatting. Module imports are final from JDK 25 on, and the tests run there.
3232
*/
33+
@EnabledForJreRange(min = JRE.JAVA_25)
3334
final class ModuleImportTest {
3435

35-
@BeforeAll
36-
static void requiresAParserThatProducesModuleImports() {
37-
// Module imports parse from JDK 23 on, as a preview feature there; the formatter enables preview.
38-
Assumptions.assumeTrue(
39-
Formatter.getRuntimeVersion() >= 23, "import module requires running on JDK 23 or later");
40-
}
41-
4236
/** Asserts both entry points that reorder imports, and that their output survives a second pass. */
4337
private static void assertFormats(String input, String expected) throws FormatterException {
4438
assertThat(Formatter.create().formatSourceAndFixImports(input)).isEqualTo(expected);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.palantir.javaformat.java;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
import static com.palantir.javaformat.java.RemoveUnusedImports.removeUnusedImports;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.condition.EnabledForJreRange;
22+
import org.junit.jupiter.api.condition.JRE;
23+
import org.junit.jupiter.api.parallel.Execution;
24+
import org.junit.jupiter.api.parallel.ExecutionMode;
25+
26+
/** {@link RemoveUnusedImports} on module imports, which are final from JDK 25 on. */
27+
@EnabledForJreRange(min = JRE.JAVA_25)
28+
@Execution(ExecutionMode.CONCURRENT)
29+
final class RemoveUnusedImportsJava25Test {
30+
// Module imports (JEP 511, `import module foo.bar;`) parse to JCModuleImport, a sibling of JCImport rather than a
31+
// subtype. They must never be reported as unused (see isUnused), and must not crash buildReplacements when mixed
32+
// with ordinary imports, some used and some not.
33+
@Test
34+
void moduleImportStaysAmongOrdinaryImports() throws FormatterException {
35+
var input = """
36+
import module java.base;
37+
import java.util.List;
38+
import java.util.Map;
39+
class T { List<String> xs; }
40+
""";
41+
42+
assertThat(removeUnusedImports(input)).isEqualTo("""
43+
import module java.base;
44+
import java.util.List;
45+
class T { List<String> xs; }
46+
""");
47+
}
48+
}

‎open-java-format/src/test/java/com/palantir/javaformat/java/RemoveUnusedImportsTest.java‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
import static com.google.common.truth.Truth.assertThat;
1818
import static com.palantir.javaformat.java.RemoveUnusedImports.removeUnusedImports;
1919

20-
import org.junit.jupiter.api.Assumptions;
2120
import org.junit.jupiter.api.Test;
2221
import org.junit.jupiter.api.parallel.Execution;
2322
import org.junit.jupiter.api.parallel.ExecutionMode;
2423

2524
/**
2625
* {@link RemoveUnusedImports} on its own, where a case under {@code test-cases-java21/imports} cannot show it. A case
2726
* fixes the imports before it formats, and formatting takes a doubled blank line away by itself, while the command line
28-
* formats first. And the cases run on JDK 21, where a module import does not parse.
27+
* formats first.
2928
*/
3029
@Execution(ExecutionMode.CONCURRENT)
3130
final class RemoveUnusedImportsTest {
@@ -84,27 +83,4 @@ public class TestBug {}
8483
public class TestBug {}
8584
""");
8685
}
87-
88-
// Module imports (JEP 511, `import module foo.bar;`) parse to JCModuleImport, a sibling of JCImport rather than a
89-
// subtype. They must never be reported as unused (see isUnused), and must not crash buildReplacements when mixed
90-
// with ordinary imports, some used and some not.
91-
@Test
92-
void moduleImportStaysAmongOrdinaryImports() throws FormatterException {
93-
// Module imports parse from JDK 23 on, as preview there; the formatter enables preview.
94-
Assumptions.assumeTrue(
95-
Formatter.getRuntimeVersion() >= 23, "import module requires running on JDK 23 or later");
96-
97-
var input = """
98-
import module java.base;
99-
import java.util.List;
100-
import java.util.Map;
101-
class T { List<String> xs; }
102-
""";
103-
104-
assertThat(removeUnusedImports(input)).isEqualTo("""
105-
import module java.base;
106-
import java.util.List;
107-
class T { List<String> xs; }
108-
""");
109-
}
11086
}

0 commit comments

Comments
 (0)