|
| 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 java.nio.charset.StandardCharsets.UTF_8; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 23 | +import com.fasterxml.jackson.datatype.guava.GuavaModule; |
| 24 | +import com.google.common.collect.Range; |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.PrintWriter; |
| 27 | +import java.io.StringWriter; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import org.junit.jupiter.api.DynamicTest; |
| 32 | + |
| 33 | +/** |
| 34 | + * The service that formats in the build's own JVM, as {@code formatDiff} does without the native binary, against the |
| 35 | + * command line, which the bootstrapping and native services run: for each case, the lines that formatting changes, |
| 36 | + * taken as a range, get the same replacements from both. Each JDK has a class that runs the case folders it can parse, |
| 37 | + * such as {@link FormatterServiceImplJava21Test}. |
| 38 | + */ |
| 39 | +final class FormatterServiceImplTest { |
| 40 | + private static final FormatterService SERVICE = new FormatterServiceImpl(); |
| 41 | + private static final ObjectMapper MAPPER = |
| 42 | + JsonMapper.builder().addModule(new GuavaModule()).build(); |
| 43 | + |
| 44 | + /** The tests of the case files under {@code folders} on the test classpath. */ |
| 45 | + static List<DynamicTest> changedLinesIn(String... folders) throws Exception { |
| 46 | + var tests = new ArrayList<DynamicTest>(); |
| 47 | + |
| 48 | + for (var folder : folders) { |
| 49 | + var root = CaseFile.folder(folder); |
| 50 | + |
| 51 | + for (var path : CaseFile.pathsIn(root)) { |
| 52 | + tests.add(DynamicTest.dynamicTest(path, () -> { |
| 53 | + var testCase = CaseFile.parse(path, Files.readString(root.resolve(path))); |
| 54 | + var lines = testCase.changedLines(); |
| 55 | + |
| 56 | + assertThat(SERVICE.getFormatReplacements(testCase.before(), List.of(lines))) |
| 57 | + .as(() -> folder + "/" + path + ", characters " + lines) |
| 58 | + .isEqualTo(commandLine(testCase.before(), lines)); |
| 59 | + })); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return tests; |
| 64 | + } |
| 65 | + |
| 66 | + /** What {@code open-java-format --character-ranges … --output-replacements -} prints for the source. */ |
| 67 | + private static List<Replacement> commandLine(String source, Range<Integer> range) throws Exception { |
| 68 | + var out = new StringWriter(); |
| 69 | + var err = new StringWriter(); |
| 70 | + var main = new Main( |
| 71 | + new PrintWriter(out, true), |
| 72 | + new PrintWriter(err, true), |
| 73 | + new ByteArrayInputStream(source.getBytes(UTF_8))); |
| 74 | + |
| 75 | + assertThat(main.format( |
| 76 | + "--character-ranges", |
| 77 | + range.lowerEndpoint() + ":" + (range.upperEndpoint() - 1), |
| 78 | + "--output-replacements", |
| 79 | + "-")) |
| 80 | + .as(err::toString) |
| 81 | + .isZero(); |
| 82 | + |
| 83 | + return MAPPER.readValue(out.toString(), new TypeReference<>() {}); |
| 84 | + } |
| 85 | +} |
0 commit comments