|
| 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.palantir.gradle.testing.execution.GradleInvoker; |
| 22 | +import com.palantir.gradle.testing.junit.GradlePluginTests; |
| 23 | +import com.palantir.gradle.testing.project.RootProject; |
| 24 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.UncheckedIOException; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import javax.xml.parsers.DocumentBuilder; |
| 30 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.w3c.dom.Document; |
| 33 | +import org.w3c.dom.Element; |
| 34 | +import org.w3c.dom.NodeList; |
| 35 | + |
| 36 | +@GradlePluginTests |
| 37 | +class PalantirJavaFormatIdeaPluginTest { |
| 38 | + |
| 39 | + private static final File NATIVE_IMAGE_FILE = new File("build/nativeImage.path"); |
| 40 | + private static final String NATIVE_CONFIG = |
| 41 | + String.format("palantirJavaFormatNative files(\"%s\")", readNativeImageFile()); |
| 42 | + |
| 43 | + private static String readNativeImageFile() { |
| 44 | + try { |
| 45 | + return Files.readString(NATIVE_IMAGE_FILE.toPath()).trim(); |
| 46 | + } catch (IOException e) { |
| 47 | + throw new UncheckedIOException("Failed to read native image file", e); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void idea_configures_xml_files(GradleInvoker gradle, RootProject rootProject) throws Exception { |
| 53 | + rootProject.buildGradle().plugins().add("com.palantir.java-format-idea").add("idea"); |
| 54 | + |
| 55 | + rootProject.buildGradle().append(""" |
| 56 | + dependencies { |
| 57 | + palantirJavaFormat project.files() // no need to store the real thing in here |
| 58 | + } |
| 59 | + """); |
| 60 | + |
| 61 | + gradle.withArgs("idea").buildsSuccessfully(); |
| 62 | + |
| 63 | + assertXmlFilesConfiguredCorrectly(rootProject, false); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void idea_configures_xml_files_with_native_formatter(GradleInvoker gradle, RootProject rootProject) |
| 68 | + throws Exception { |
| 69 | + rootProject.gradlePropertiesFile().appendProperty("palantir.native.formatter", "true"); |
| 70 | + |
| 71 | + rootProject.buildGradle().plugins().add("com.palantir.java-format-idea").add("idea"); |
| 72 | + |
| 73 | + rootProject.buildGradle().append(""" |
| 74 | + dependencies { |
| 75 | + palantirJavaFormat project.files() // no need to store the real thing in here |
| 76 | + %s |
| 77 | + } |
| 78 | + """, NATIVE_CONFIG); |
| 79 | + |
| 80 | + gradle.withArgs("idea").buildsSuccessfully(); |
| 81 | + |
| 82 | + assertXmlFilesConfiguredCorrectly(rootProject, true); |
| 83 | + } |
| 84 | + |
| 85 | + private void assertXmlFilesConfiguredCorrectly(RootProject rootProject, boolean expectNativeImage) |
| 86 | + throws Exception { |
| 87 | + Path pjfXmlFile = |
| 88 | + rootProject.directory(".idea").file("palantir-java-format.xml").path(); |
| 89 | + Document xmlContent = parseXmlFile(pjfXmlFile); |
| 90 | + |
| 91 | + NodeList components = xmlContent.getElementsByTagName("component"); |
| 92 | + boolean foundPalantirJavaFormatSettings = false; |
| 93 | + boolean foundImplementationClassPath = false; |
| 94 | + boolean foundNativeImageClassPath = false; |
| 95 | + |
| 96 | + for (int i = 0; i < components.getLength(); i++) { |
| 97 | + Element component = (Element) components.item(i); |
| 98 | + String componentName = component.getAttribute("name"); |
| 99 | + |
| 100 | + if ("PalantirJavaFormatSettings".equals(componentName)) { |
| 101 | + foundPalantirJavaFormatSettings = true; |
| 102 | + } |
| 103 | + |
| 104 | + NodeList options = component.getElementsByTagName("option"); |
| 105 | + for (int j = 0; j < options.getLength(); j++) { |
| 106 | + Element option = (Element) options.item(j); |
| 107 | + String optionName = option.getAttribute("name"); |
| 108 | + |
| 109 | + if ("implementationClassPath".equals(optionName)) { |
| 110 | + foundImplementationClassPath = true; |
| 111 | + } |
| 112 | + if ("nativeImageClassPath".equals(optionName)) { |
| 113 | + foundNativeImageClassPath = true; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + assertThat(foundPalantirJavaFormatSettings) |
| 119 | + .as("PalantirJavaFormatSettings component should be present") |
| 120 | + .isTrue(); |
| 121 | + assertThat(foundImplementationClassPath) |
| 122 | + .as("implementationClassPath option should be present") |
| 123 | + .isTrue(); |
| 124 | + |
| 125 | + if (expectNativeImage) { |
| 126 | + assertThat(foundNativeImageClassPath) |
| 127 | + .as("nativeImageClassPath option should be present when native formatter is enabled") |
| 128 | + .isTrue(); |
| 129 | + } |
| 130 | + |
| 131 | + Path workspaceXmlFile = |
| 132 | + rootProject.directory(".idea").file("workspace.xml").path(); |
| 133 | + Document workspaceContent = parseXmlFile(workspaceXmlFile); |
| 134 | + |
| 135 | + NodeList workspaceComponents = workspaceContent.getElementsByTagName("component"); |
| 136 | + boolean foundFormatOnSave = false; |
| 137 | + boolean foundOptimizeOnSave = false; |
| 138 | + |
| 139 | + for (int i = 0; i < workspaceComponents.getLength(); i++) { |
| 140 | + Element component = (Element) workspaceComponents.item(i); |
| 141 | + String componentName = component.getAttribute("name"); |
| 142 | + |
| 143 | + if ("FormatOnSaveOptions".equals(componentName)) { |
| 144 | + foundFormatOnSave = true; |
| 145 | + } |
| 146 | + if ("OptimizeOnSaveOptions".equals(componentName)) { |
| 147 | + foundOptimizeOnSave = true; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + assertThat(foundFormatOnSave) |
| 152 | + .as("FormatOnSaveOptions component should be present") |
| 153 | + .isTrue(); |
| 154 | + assertThat(foundOptimizeOnSave) |
| 155 | + .as("OptimizeOnSaveOptions component should be present") |
| 156 | + .isTrue(); |
| 157 | + } |
| 158 | + |
| 159 | + private Document parseXmlFile(Path xmlFile) throws Exception { |
| 160 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 161 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 162 | + return builder.parse(xmlFile.toFile()); |
| 163 | + } |
| 164 | +} |
0 commit comments