|
14 | 14 |
|
15 | 15 | package com.google.googlejavaformat.java; |
16 | 16 |
|
17 | | -import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_VERSION; |
18 | | -import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION; |
| 17 | +import static com.google.common.collect.MoreCollectors.toOptional; |
19 | 18 | import static com.google.common.io.Files.getFileExtension; |
20 | 19 | import static com.google.common.io.Files.getNameWithoutExtension; |
21 | 20 | import static java.nio.charset.StandardCharsets.UTF_8; |
22 | 21 | import static org.junit.Assert.assertEquals; |
23 | 22 | import static org.junit.Assert.assertTrue; |
24 | 23 | import static org.junit.Assert.fail; |
25 | 24 |
|
26 | | -import com.google.common.collect.ImmutableSet; |
| 25 | +import com.google.common.collect.ImmutableMultimap; |
27 | 26 | import com.google.common.io.CharStreams; |
28 | 27 | import com.google.common.reflect.ClassPath; |
29 | 28 | import com.google.common.reflect.ClassPath.ResourceInfo; |
30 | 29 | import com.google.googlejavaformat.Newlines; |
31 | 30 | import java.io.IOException; |
32 | 31 | import java.io.InputStream; |
33 | 32 | import java.io.InputStreamReader; |
34 | | -import java.lang.reflect.Method; |
35 | 33 | import java.nio.file.Path; |
36 | 34 | import java.nio.file.Paths; |
37 | 35 | import java.util.ArrayList; |
38 | 36 | import java.util.List; |
39 | 37 | import java.util.Map; |
| 38 | +import java.util.Optional; |
40 | 39 | import java.util.TreeMap; |
41 | 40 | import org.junit.Test; |
42 | 41 | import org.junit.runner.RunWith; |
|
47 | 46 | @RunWith(Parameterized.class) |
48 | 47 | public class FormatterIntegrationTest { |
49 | 48 |
|
50 | | - private static final ImmutableSet<String> JAVA14_TESTS = |
51 | | - ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574", "I594"); |
52 | | - |
53 | | - private static final ImmutableSet<String> JAVA15_TESTS = ImmutableSet.of("I603"); |
54 | | - |
55 | | - private static final ImmutableSet<String> JAVA16_TESTS = ImmutableSet.of("I588"); |
| 49 | + private static final ImmutableMultimap<Integer, String> VERSIONED_TESTS = |
| 50 | + ImmutableMultimap.<Integer, String>builder() |
| 51 | + .putAll(14, "I477", "Records", "RSLs", "Var", "ExpressionSwitch", "I574", "I594") |
| 52 | + .putAll(15, "I603") |
| 53 | + .putAll(16, "I588") |
| 54 | + .putAll(17, "I683", "I684") |
| 55 | + .build(); |
56 | 56 |
|
57 | 57 | @Parameters(name = "{index}: {0}") |
58 | 58 | public static Iterable<Object[]> data() throws IOException { |
@@ -91,35 +91,16 @@ public static Iterable<Object[]> data() throws IOException { |
91 | 91 | String input = inputs.get(fileName); |
92 | 92 | assertTrue("unmatched input", outputs.containsKey(fileName)); |
93 | 93 | String expectedOutput = outputs.get(fileName); |
94 | | - if (JAVA14_TESTS.contains(fileName) && getMajor() < 14) { |
95 | | - continue; |
96 | | - } |
97 | | - if (JAVA15_TESTS.contains(fileName) && getMajor() < 15) { |
98 | | - continue; |
99 | | - } |
100 | | - if (JAVA16_TESTS.contains(fileName) && getMajor() < 16) { |
| 94 | + Optional<Integer> version = |
| 95 | + VERSIONED_TESTS.inverse().get(fileName).stream().collect(toOptional()); |
| 96 | + if (version.isPresent() && Runtime.version().feature() < version.get()) { |
101 | 97 | continue; |
102 | 98 | } |
103 | 99 | testInputs.add(new Object[] {fileName, input, expectedOutput}); |
104 | 100 | } |
105 | 101 | return testInputs; |
106 | 102 | } |
107 | 103 |
|
108 | | - private static int getMajor() { |
109 | | - try { |
110 | | - Method versionMethod = Runtime.class.getMethod("version"); |
111 | | - Object version = versionMethod.invoke(null); |
112 | | - return (int) version.getClass().getMethod("major").invoke(version); |
113 | | - } catch (Exception e) { |
114 | | - // continue below |
115 | | - } |
116 | | - int version = (int) Double.parseDouble(JAVA_CLASS_VERSION.value()); |
117 | | - if (49 <= version && version <= 52) { |
118 | | - return version - (49 - 5); |
119 | | - } |
120 | | - throw new IllegalStateException("Unknown Java version: " + JAVA_SPECIFICATION_VERSION.value()); |
121 | | - } |
122 | | - |
123 | 104 | private final String name; |
124 | 105 | private final String input; |
125 | 106 | private final String expected; |
|
0 commit comments