Skip to content

Commit 4039677

Browse files
committed
Run the cases of each JDK from a class of its own, JDK 21 too
FormatterCasesJava21Test, Java25Test and Java28Test each run the case folders their JDK parses, named in the class: one, two or three. Their JDK ranges do not overlap, so on any JDK every case runs once. FormatterCasesTest keeps what they share, casesIn, which takes the folders.
1 parent 01fc6dc commit 4039677

4 files changed

Lines changed: 67 additions & 40 deletions

File tree

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 on JDK 21 to 24, which parse the syntax of JDK 21. */
26+
@EnabledForJreRange(max = JRE.JAVA_24)
27+
@Execution(ExecutionMode.CONCURRENT)
28+
final class FormatterCasesJava21Test {
29+
@TestFactory
30+
List<DynamicTest> cases() throws Exception {
31+
return FormatterCasesTest.casesIn("test-cases-java21");
32+
}
33+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import org.junit.jupiter.api.parallel.Execution;
2323
import org.junit.jupiter.api.parallel.ExecutionMode;
2424

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)
25+
/** The cases on JDK 25 to 27: those of JDK 21, and those for the syntax of JDK 25. */
26+
@EnabledForJreRange(min = JRE.JAVA_25, max = JRE.JAVA_27)
2727
@Execution(ExecutionMode.CONCURRENT)
2828
final class FormatterCasesJava25Test {
2929
@TestFactory
3030
List<DynamicTest> cases() throws Exception {
31-
return FormatterCasesTest.casesIn("test-cases-java25");
31+
return FormatterCasesTest.casesIn("test-cases-java21", "test-cases-java25");
3232
}
3333
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import org.junit.jupiter.api.parallel.Execution;
2323
import org.junit.jupiter.api.parallel.ExecutionMode;
2424

25-
/** The cases under {@code test-cases-java28}, as {@link FormatterCasesTest} runs those of JDK 21, for syntax of JDK 28. */
25+
/** The cases on JDK 28 and later: those of JDK 21 and 25, and those for the syntax of JDK 28. */
2626
@EnabledForJreRange(min = JRE.JAVA_28)
2727
@Execution(ExecutionMode.CONCURRENT)
2828
final class FormatterCasesJava28Test {
2929
@TestFactory
3030
List<DynamicTest> cases() throws Exception {
31-
return FormatterCasesTest.casesIn("test-cases-java28");
31+
return FormatterCasesTest.casesIn("test-cases-java21", "test-cases-java25", "test-cases-java28");
3232
}
3333
}

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

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,55 +23,49 @@
2323
import java.util.ArrayList;
2424
import java.util.List;
2525
import org.junit.jupiter.api.DynamicTest;
26-
import org.junit.jupiter.api.TestFactory;
27-
import org.junit.jupiter.api.parallel.Execution;
28-
import org.junit.jupiter.api.parallel.ExecutionMode;
2926

3027
/**
31-
* 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. 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}.
28+
* One test for each {@link CaseFile} in the given folders on the test classpath: formatting the before block gives the
29+
* after block, and formatting that again changes nothing. Adding a case is adding a file. The cases are in a folder of
30+
* the JDK they need, and each JDK has a class that runs the folders it can parse, such as
31+
* {@link FormatterCasesJava21Test}.
3432
*/
35-
@Execution(ExecutionMode.CONCURRENT)
3633
final class FormatterCasesTest {
3734
private static final Formatter FORMATTER = Formatter.createFormatter(
3835
JavaFormatterOptions.builder().style(Style.OJF).build());
3936

40-
@TestFactory
41-
List<DynamicTest> cases() throws Exception {
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());
51-
var paths = CaseFile.pathsIn(root);
37+
/** The tests of the case files under {@code folders} on the test classpath. */
38+
static List<DynamicTest> casesIn(String... folders) throws Exception {
39+
var tests = new ArrayList<DynamicTest>();
5240

53-
// A wrong folder or a resource filter would otherwise pass with no tests at all.
54-
assertThat(paths).as("the case files under %s", root).isNotEmpty();
41+
for (var folder : folders) {
42+
var url = requireNonNull(
43+
FormatterCasesTest.class.getClassLoader().getResource(folder),
44+
"no " + folder + " folder on the test classpath");
45+
var root = Path.of(url.toURI());
46+
var paths = CaseFile.pathsIn(root);
5547

56-
var tests = new ArrayList<DynamicTest>();
48+
// A wrong folder or a resource filter would otherwise pass with no tests at all.
49+
assertThat(paths).as("the case files under %s", root).isNotEmpty();
5750

58-
for (var path : paths) {
59-
var name = path.substring(0, path.length() - CaseFile.EXTENSION.length());
51+
for (var path : paths) {
52+
var name = path.substring(0, path.length() - CaseFile.EXTENSION.length());
6053

61-
tests.add(DynamicTest.dynamicTest(name, () -> {
62-
var testCase = CaseFile.parse(path, Files.readString(root.resolve(path)));
63-
var once = FORMATTER.formatSourceAndFixImports(testCase.before());
54+
tests.add(DynamicTest.dynamicTest(name, () -> {
55+
var testCase = CaseFile.parse(path, Files.readString(root.resolve(path)));
56+
var once = FORMATTER.formatSourceAndFixImports(testCase.before());
6457

65-
assertThat(once)
66-
.as(() -> folder + "/" + path + ", first run, " + testCase.firstDifference(once))
67-
.isEqualTo(testCase.after());
58+
assertThat(once)
59+
.as(() -> folder + "/" + path + ", first run, " + testCase.firstDifference(once))
60+
.isEqualTo(testCase.after());
6861

69-
var twice = FORMATTER.formatSourceAndFixImports(once);
62+
var twice = FORMATTER.formatSourceAndFixImports(once);
7063

71-
assertThat(twice)
72-
.as(() -> folder + "/" + path + ", second run, " + testCase.firstDifference(twice))
73-
.isEqualTo(once);
74-
}));
64+
assertThat(twice)
65+
.as(() -> folder + "/" + path + ", second run, " + testCase.firstDifference(twice))
66+
.isEqualTo(once);
67+
}));
68+
}
7569
}
7670

7771
return tests;

0 commit comments

Comments
 (0)