Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved.
*
* 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.palantir.javaformat.gradle;

import static org.assertj.core.api.Assertions.assertThat;

import com.palantir.gradle.testing.execution.GradleInvoker;
import com.palantir.gradle.testing.junit.GradlePluginTests;
import com.palantir.gradle.testing.project.RootProject;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

@GradlePluginTests
class PalantirJavaFormatIdeaPluginTest {

private static final File NATIVE_IMAGE_FILE = new File("build/nativeImage.path");
private static final String NATIVE_CONFIG =
String.format("palantirJavaFormatNative files(\"%s\")", readNativeImageFile());

private static String readNativeImageFile() {
try {
return Files.readString(NATIVE_IMAGE_FILE.toPath()).trim();
} catch (IOException e) {
throw new UncheckedIOException("Failed to read native image file", e);
}
}

@Test
void idea_configures_xml_files(GradleInvoker gradle, RootProject rootProject) throws Exception {
rootProject.buildGradle().plugins().add("com.palantir.java-format-idea").add("idea");

rootProject.buildGradle().append("""
dependencies {
palantirJavaFormat project.files() // no need to store the real thing in here
}
""");

gradle.withArgs("idea").buildsSuccessfully();

assertXmlFilesConfiguredCorrectly(rootProject, false);
}

@Test
void idea_configures_xml_files_with_native_formatter(GradleInvoker gradle, RootProject rootProject)
throws Exception {
rootProject.gradlePropertiesFile().appendProperty("palantir.native.formatter", "true");

rootProject.buildGradle().plugins().add("com.palantir.java-format-idea").add("idea");

rootProject.buildGradle().append("""
dependencies {
palantirJavaFormat project.files() // no need to store the real thing in here
%s
}
""", NATIVE_CONFIG);

gradle.withArgs("idea").buildsSuccessfully();

assertXmlFilesConfiguredCorrectly(rootProject, true);
}

private void assertXmlFilesConfiguredCorrectly(RootProject rootProject, boolean expectNativeImage)
throws Exception {
Path pjfXmlFile =
rootProject.directory(".idea").file("palantir-java-format.xml").path();
Document xmlContent = parseXmlFile(pjfXmlFile);

NodeList components = xmlContent.getElementsByTagName("component");
boolean foundPalantirJavaFormatSettings = false;
boolean foundImplementationClassPath = false;
boolean foundNativeImageClassPath = false;

for (int i = 0; i < components.getLength(); i++) {
Element component = (Element) components.item(i);
String componentName = component.getAttribute("name");

if ("PalantirJavaFormatSettings".equals(componentName)) {
foundPalantirJavaFormatSettings = true;
}

NodeList options = component.getElementsByTagName("option");
for (int j = 0; j < options.getLength(); j++) {
Element option = (Element) options.item(j);
String optionName = option.getAttribute("name");

if ("implementationClassPath".equals(optionName)) {
foundImplementationClassPath = true;
}
if ("nativeImageClassPath".equals(optionName)) {
foundNativeImageClassPath = true;
}
}
}

assertThat(foundPalantirJavaFormatSettings)
.as("PalantirJavaFormatSettings component should be present")
.isTrue();
assertThat(foundImplementationClassPath)
.as("implementationClassPath option should be present")
.isTrue();

if (expectNativeImage) {
assertThat(foundNativeImageClassPath)
.as("nativeImageClassPath option should be present when native formatter is enabled")
.isTrue();
}

Path workspaceXmlFile =
rootProject.directory(".idea").file("workspace.xml").path();
Document workspaceContent = parseXmlFile(workspaceXmlFile);

NodeList workspaceComponents = workspaceContent.getElementsByTagName("component");
boolean foundFormatOnSave = false;
boolean foundOptimizeOnSave = false;

for (int i = 0; i < workspaceComponents.getLength(); i++) {
Element component = (Element) workspaceComponents.item(i);
String componentName = component.getAttribute("name");

if ("FormatOnSaveOptions".equals(componentName)) {
foundFormatOnSave = true;
}
if ("OptimizeOnSaveOptions".equals(componentName)) {
foundOptimizeOnSave = true;
}
}

assertThat(foundFormatOnSave)
.as("FormatOnSaveOptions component should be present")
.isTrue();
assertThat(foundOptimizeOnSave)
.as("OptimizeOnSaveOptions component should be present")
.isTrue();
}

private Document parseXmlFile(Path xmlFile) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(xmlFile.toFile());
}
}
Loading