/
FormatterIntegrationTest.java
174 lines (160 loc) · 6.44 KB
/
FormatterIntegrationTest.java
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
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.googlejavaformat.java;
import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_VERSION;
import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION;
import static com.google.common.io.Files.getFileExtension;
import static com.google.common.io.Files.getNameWithoutExtension;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.CharStreams;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ResourceInfo;
import com.google.googlejavaformat.Newlines;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/** Integration test for google-java-format. */
@RunWith(Parameterized.class)
public class FormatterIntegrationTest {
private static final ImmutableSet<String> JAVA14_TESTS =
ImmutableSet.of("I477", "Records", "RSLs", "Var", "ExpressionSwitch");
private static final ImmutableSet<String> JAVA16_TESTS = ImmutableSet.of("I588");
@Parameters(name = "{index}: {0}")
public static Iterable<Object[]> data() throws IOException {
Path testDataPath = Paths.get("com/google/googlejavaformat/java/testdata");
ClassLoader classLoader = FormatterIntegrationTest.class.getClassLoader();
Map<String, String> inputs = new TreeMap<>();
Map<String, String> outputs = new TreeMap<>();
for (ResourceInfo resourceInfo : ClassPath.from(classLoader).getResources()) {
String resourceName = resourceInfo.getResourceName();
Path resourceNamePath = Paths.get(resourceName);
if (resourceNamePath.startsWith(testDataPath)) {
Path subPath = testDataPath.relativize(resourceNamePath);
assertEquals("bad testdata file names", 1, subPath.getNameCount());
String baseName = getNameWithoutExtension(subPath.getFileName().toString());
String extension = getFileExtension(subPath.getFileName().toString());
String contents;
try (InputStream stream =
FormatterIntegrationTest.class.getClassLoader().getResourceAsStream(resourceName)) {
contents = CharStreams.toString(new InputStreamReader(stream, UTF_8));
}
switch (extension) {
case "input":
inputs.put(baseName, contents);
break;
case "output":
outputs.put(baseName, contents);
break;
default: // fall out
}
}
}
List<Object[]> testInputs = new ArrayList<>();
assertEquals("unmatched inputs and outputs", inputs.size(), outputs.size());
for (Map.Entry<String, String> entry : inputs.entrySet()) {
String fileName = entry.getKey();
String input = inputs.get(fileName);
assertTrue("unmatched input", outputs.containsKey(fileName));
String expectedOutput = outputs.get(fileName);
if (JAVA14_TESTS.contains(fileName) && getMajor() < 14) {
continue;
}
if (JAVA16_TESTS.contains(fileName) && getMajor() < 16) {
continue;
}
testInputs.add(new Object[] {fileName, input, expectedOutput});
}
return testInputs;
}
private static int getMajor() {
try {
Method versionMethod = Runtime.class.getMethod("version");
Object version = versionMethod.invoke(null);
return (int) version.getClass().getMethod("major").invoke(version);
} catch (Exception e) {
// continue below
}
int version = (int) Double.parseDouble(JAVA_CLASS_VERSION.value());
if (49 <= version && version <= 52) {
return version - (49 - 5);
}
throw new IllegalStateException("Unknown Java version: " + JAVA_SPECIFICATION_VERSION.value());
}
private final String name;
private final String input;
private final String expected;
private final String separator;
public FormatterIntegrationTest(String name, String input, String expected) {
this.name = name;
this.input = input;
this.expected = expected;
this.separator = Newlines.getLineEnding(expected);
}
@Test
public void format() {
try {
Formatter formatter = new Formatter();
String output = formatter.formatSource(input);
output = StringWrapper.wrap(output, formatter);
assertEquals("bad output for " + name, expected, output);
} catch (FormatterException e) {
fail(String.format("Formatter crashed on %s: %s", name, e.getMessage()));
}
}
@Test
public void idempotentLF() {
try {
String mangled = expected.replace(separator, "\n");
String output = new Formatter().formatSource(mangled);
assertEquals("bad output for " + name, mangled, output);
} catch (FormatterException e) {
fail(String.format("Formatter crashed on %s: %s", name, e.getMessage()));
}
}
@Test
public void idempotentCR() throws IOException {
try {
String mangled = expected.replace(separator, "\r");
String output = new Formatter().formatSource(mangled);
assertEquals("bad output for " + name, mangled, output);
} catch (FormatterException e) {
fail(String.format("Formatter crashed on %s: %s", name, e.getMessage()));
}
}
@Test
public void idempotentCRLF() {
try {
String mangled = expected.replace(separator, "\r\n");
String output = new Formatter().formatSource(mangled);
assertEquals("bad output for " + name, mangled, output);
} catch (FormatterException e) {
fail(String.format("Formatter crashed on %s: %s", name, e.getMessage()));
}
}
}