|
| 1 | +/* |
| 2 | + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.palantir.javaformat.gradle; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 24 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 25 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 26 | +import com.palantir.gradle.testing.execution.GradleInvoker; |
| 27 | +import com.palantir.gradle.testing.files.arbitrary.ArbitraryFile; |
| 28 | +import com.palantir.gradle.testing.junit.DisabledConfigurationCache; |
| 29 | +import com.palantir.gradle.testing.junit.GradlePluginTests; |
| 30 | +import com.palantir.gradle.testing.project.RootProject; |
| 31 | +import java.io.File; |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Optional; |
| 35 | +import org.junit.jupiter.params.ParameterizedTest; |
| 36 | +import org.junit.jupiter.params.provider.ValueSource; |
| 37 | + |
| 38 | +@GradlePluginTests |
| 39 | +@DisabledConfigurationCache |
| 40 | +class PalantirJavaFormatIdeaPluginTest { |
| 41 | + |
| 42 | + private static final String NATIVE_IMAGE_FILE = new File("build/nativeImage.path").getAbsolutePath(); |
| 43 | + |
| 44 | + private static final String NATIVE_CONFIG = |
| 45 | + "palantirJavaFormatNative files(file(\"" + NATIVE_IMAGE_FILE + "\").text)"; |
| 46 | + |
| 47 | + private static final ObjectMapper XML_MAPPER = new XmlMapper(); |
| 48 | + |
| 49 | + @ParameterizedTest(name = "extraGradleProperties={0}") |
| 50 | + @ValueSource(strings = {"", "palantir.native.formatter=true"}) |
| 51 | + void idea_configures_xml_files(String extraGradleProperties, GradleInvoker gradle, RootProject rootProject) |
| 52 | + throws IOException { |
| 53 | + |
| 54 | + rootProject.gradlePropertiesFile().appendLine(extraGradleProperties); |
| 55 | + |
| 56 | + rootProject.buildGradle().plugins().add("com.palantir.java-format-idea").add("idea"); |
| 57 | + |
| 58 | + rootProject.buildGradle().append(""" |
| 59 | + dependencies { |
| 60 | + palantirJavaFormat project.files() // no need to store the real thing in here |
| 61 | + %s |
| 62 | + } |
| 63 | + """, extraGradleProperties.isBlank() ? "" : NATIVE_CONFIG); |
| 64 | + |
| 65 | + gradle.withArgs("idea").buildsSuccessfully(); |
| 66 | + |
| 67 | + ArbitraryFile pjfXmlFile = rootProject.file(".idea/palantir-java-format.xml"); |
| 68 | + pjfXmlFile.assertThat().exists(); |
| 69 | + |
| 70 | + Project xmlContent = XML_MAPPER.readValue(pjfXmlFile.path().toFile(), Project.class); |
| 71 | + |
| 72 | + assertThat(xmlContent.components()).anyMatch(c -> "PalantirJavaFormatSettings".equals(c.name())); |
| 73 | + |
| 74 | + List<Option> allOptions = xmlContent.components().stream() |
| 75 | + .flatMap(c -> Optional.ofNullable(c.options()).stream().flatMap(List::stream)) |
| 76 | + .toList(); |
| 77 | + |
| 78 | + assertThat(allOptions).anyMatch(o -> "implementationClassPath".equals(o.name())); |
| 79 | + |
| 80 | + if (extraGradleProperties.contains("palantir.native.formatter=true")) { |
| 81 | + assertThat(allOptions).anyMatch(o -> "nativeImageClassPath".equals(o.name())); |
| 82 | + } |
| 83 | + |
| 84 | + ArbitraryFile workspaceXmlFile = rootProject.file(".idea/workspace.xml"); |
| 85 | + workspaceXmlFile.assertThat().exists(); |
| 86 | + |
| 87 | + Project workspaceContent = XML_MAPPER.readValue(workspaceXmlFile.path().toFile(), Project.class); |
| 88 | + |
| 89 | + assertThat(workspaceContent.components()).anyMatch(c -> "FormatOnSaveOptions".equals(c.name())); |
| 90 | + assertThat(workspaceContent.components()).anyMatch(c -> "OptimizeOnSaveOptions".equals(c.name())); |
| 91 | + } |
| 92 | + |
| 93 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 94 | + private record Project( |
| 95 | + @JacksonXmlProperty(localName = "component") @JacksonXmlElementWrapper(useWrapping = false) |
| 96 | + List<Component> components) {} |
| 97 | + |
| 98 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 99 | + private record Component( |
| 100 | + @JacksonXmlProperty(isAttribute = true) String name, |
| 101 | + |
| 102 | + @JacksonXmlProperty(localName = "option") @JacksonXmlElementWrapper(useWrapping = false) |
| 103 | + List<Option> options) {} |
| 104 | + |
| 105 | + @JsonIgnoreProperties(ignoreUnknown = true) |
| 106 | + private record Option( |
| 107 | + @JacksonXmlProperty(isAttribute = true) String name) {} |
| 108 | +} |
0 commit comments