|
| 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.intellij; |
| 16 | + |
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | + |
| 21 | +import com.palantir.javaformat.java.FormatterService; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.attribute.FileTime; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.jar.Attributes; |
| 29 | +import java.util.jar.JarEntry; |
| 30 | +import java.util.jar.JarOutputStream; |
| 31 | +import java.util.jar.Manifest; |
| 32 | +import org.jspecify.annotations.Nullable; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.condition.EnabledOnOs; |
| 35 | +import org.junit.jupiter.api.condition.OS; |
| 36 | +import org.junit.jupiter.api.io.TempDir; |
| 37 | + |
| 38 | +final class FormatterVersionsTest { |
| 39 | + |
| 40 | + @TempDir |
| 41 | + private Path dir; |
| 42 | + |
| 43 | + private final FormatterVersions versions = new FormatterVersions(); |
| 44 | + |
| 45 | + @Test |
| 46 | + void readsTheVersionOfTheJarThatProvidesTheFormatter() throws Exception { |
| 47 | + // Every jar of a build's class path has a version of its own. |
| 48 | + var classPath = List.of(jar("guava.jar", "33.4.0", false), jar("open-java-format.jar", "21.1.0", true)); |
| 49 | + |
| 50 | + assertThat(versions.of(classPath)).hasValue("21.1.0"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void readsTheJarsOnceForAsLongAsTheyStayTheSame() throws Exception { |
| 55 | + var formatter = jar("open-java-format.jar", "21.1.0", true); |
| 56 | + assertThat(versions.of(List.of(formatter))).hasValue("21.1.0"); |
| 57 | + |
| 58 | + // No jar at all now, of the same size and time: not opened again, it still has the version read before. |
| 59 | + var lastModified = Files.getLastModifiedTime(formatter); |
| 60 | + Files.write(formatter, new byte[(int) Files.size(formatter)]); |
| 61 | + Files.setLastModifiedTime(formatter, lastModified); |
| 62 | + |
| 63 | + assertThat(versions.of(List.of(formatter))).hasValue("21.1.0"); |
| 64 | + |
| 65 | + // A jar rebuilt in place. |
| 66 | + jar("open-java-format.jar", "21.2.0", true); |
| 67 | + Files.setLastModifiedTime( |
| 68 | + formatter, FileTime.from(lastModified.toInstant().plusSeconds(60))); |
| 69 | + |
| 70 | + assertThat(versions.of(List.of(formatter))).hasValue("21.2.0"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @EnabledOnOs(value = OS.LINUX, disabledReason = "Only Linux names the files a process holds open, in /proc/self/fd") |
| 75 | + void leavesNoJarOpen() throws Exception { |
| 76 | + // On Windows a jar the IDE holds open is one that nobody can replace or delete, a Gradle build included. |
| 77 | + var dependency = jar("guava.jar", "33.4.0", false); |
| 78 | + var formatter = jar("open-java-format.jar", "21.1.0", true); |
| 79 | + |
| 80 | + versions.of(List.of(dependency, formatter)); |
| 81 | + |
| 82 | + assertThat(openFiles()).doesNotContain(dependency.toRealPath(), formatter.toRealPath()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void takesAFormatterWithoutAVersionForNone() throws Exception { |
| 87 | + // As a version it cannot parse: the plugin formats with the formatter bundled with it. |
| 88 | + assertThat(versions.of(List.of(jar("open-java-format.jar", null, true)))) |
| 89 | + .isEmpty(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void saysSoWhenNoJarProvidesTheFormatter() throws Exception { |
| 94 | + var classPath = List.of(jar("guava.jar", "33.4.0", false)); |
| 95 | + |
| 96 | + assertThatThrownBy(() -> versions.of(classPath)) |
| 97 | + .isInstanceOf(IllegalStateException.class) |
| 98 | + .hasMessage("No jar provides the formatter in %s", classPath); |
| 99 | + } |
| 100 | + |
| 101 | + /** A jar in the test's directory, written over the one there, with the formatter in it if it provides it. */ |
| 102 | + private Path jar(String name, @Nullable String version, boolean providesTheFormatter) throws IOException { |
| 103 | + var manifest = new Manifest(); |
| 104 | + manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
| 105 | + if (version != null) { |
| 106 | + manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VERSION, version); |
| 107 | + } |
| 108 | + var path = dir.resolve(name); |
| 109 | + try (var jar = new JarOutputStream(Files.newOutputStream(path), manifest)) { |
| 110 | + if (providesTheFormatter) { |
| 111 | + jar.putNextEntry(new JarEntry("META-INF/services/" + FormatterService.class.getName())); |
| 112 | + jar.write("com.palantir.javaformat.java.FormatterServiceImpl\n".getBytes(UTF_8)); |
| 113 | + jar.closeEntry(); |
| 114 | + } |
| 115 | + } |
| 116 | + return path; |
| 117 | + } |
| 118 | + |
| 119 | + private static List<Path> openFiles() throws IOException { |
| 120 | + var open = new ArrayList<Path>(); |
| 121 | + try (var descriptors = Files.list(Path.of("/proc/self/fd"))) { |
| 122 | + for (var descriptor : descriptors.toList()) { |
| 123 | + try { |
| 124 | + open.add(Files.readSymbolicLink(descriptor)); |
| 125 | + } catch (IOException e) { |
| 126 | + // Closed since it was listed, as the listing's own is. |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return open; |
| 131 | + } |
| 132 | +} |
0 commit comments