diff --git a/CHANGES.md b/CHANGES.md index 04f139fd85..6852314801 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)). ## [2.21.2] - 2022-01-07 ### Fixed diff --git a/README.md b/README.md index 779e3bf9c7..6ace09631d 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ extra('cpp.EclipseFormatterStep') +'{{yes}} | {{yes}} extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', +lib('java.PalantirJavaFormatStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |', lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', extra('java.EclipseJdtFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', lib('kotlin.KtLintStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |', @@ -98,6 +99,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}} | [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :+1: | :white_large_square: | +| [`java.PalantirJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: | | [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`java.EclipseJdtFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: | | [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :+1: | :+1: | :white_large_square: | diff --git a/gradle.properties b/gradle.properties index 2f4f7952a4..a4f33272ac 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,6 @@ +# To fix metaspace errors +org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 + name=spotless description=Spotless - keep your code spotless with Gradle org=diffplug diff --git a/lib/build.gradle b/lib/build.gradle index e49d13f5d5..90d985ad69 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -8,6 +8,7 @@ apply from: rootProject.file('gradle/java-publish.gradle') def NEEDS_GLUE = [ 'sortPom', + 'palantirJavaFormat', 'ktlint', 'flexmark' ] @@ -29,6 +30,8 @@ dependencies { // used for pom sorting sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0' + palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0' + String VER_KTLINT='0.43.2' ktlintCompileOnly "com.pinterest:ktlint:$VER_KTLINT" ktlintCompileOnly "com.pinterest.ktlint:ktlint-core:$VER_KTLINT" diff --git a/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java new file mode 100644 index 0000000000..a5474d549b --- /dev/null +++ b/lib/src/main/java/com/diffplug/spotless/java/PalantirJavaFormatStep.java @@ -0,0 +1,74 @@ +/* + * Copyright 2016-2022 DiffPlug + * + * 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.diffplug.spotless.java; + +import java.io.Serializable; +import java.lang.reflect.Constructor; +import java.util.Objects; + +import com.diffplug.spotless.*; + +/** Wraps up palantir-java-format fork of + * google-java-format as a FormatterStep. */ +public class PalantirJavaFormatStep { + // prevent direct instantiation + private PalantirJavaFormatStep() {} + + private static final String NAME = "palantir-java-format"; + private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:"; + private static final Jvm.Support JVM_SUPPORT = Jvm. support(NAME).add(8, "1.1.0").add(11, "2.10.0"); + + /** Creates a step which formats everything - code, import order, and unused imports. */ + public static FormatterStep create(Provisioner provisioner) { + return create(defaultVersion(), provisioner); + } + + /** Creates a step which formats everything - code, import order, and unused imports. */ + public static FormatterStep create(String version, Provisioner provisioner) { + Objects.requireNonNull(version, "version"); + Objects.requireNonNull(provisioner, "provisioner"); + + return FormatterStep.createLazy(NAME, + () -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version), + State::createFormat); + } + + /** Get default formatter version */ + public static String defaultVersion() { + return JVM_SUPPORT.getRecommendedFormatterVersion(); + } + + private static final class State implements Serializable { + private static final long serialVersionUID = 1L; + + /** The jar that contains the formatter. */ + private final JarState jarState; + /** Version of the formatter jar. */ + private final String formatterVersion; + + State(JarState jarState, String formatterVersion) { + this.jarState = jarState; + this.formatterVersion = formatterVersion; + } + + FormatterFunc createFormat() throws Exception { + final ClassLoader classLoader = jarState.getClassLoader(); + final Class formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc"); + final Constructor constructor = formatterFunc.getConstructor(); + return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance()); + } + } +} diff --git a/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java b/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java new file mode 100644 index 0000000000..6824cdbc48 --- /dev/null +++ b/lib/src/palantirJavaFormat/java/com/diffplug/spotless/glue/pjf/PalantirJavaFormatFormatterFunc.java @@ -0,0 +1,47 @@ +/* + * Copyright 2022 DiffPlug + * + * 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.diffplug.spotless.glue.pjf; + +import com.palantir.javaformat.java.Formatter; +import com.palantir.javaformat.java.ImportOrderer; +import com.palantir.javaformat.java.JavaFormatterOptions; +import com.palantir.javaformat.java.RemoveUnusedImports; + +import com.diffplug.spotless.FormatterFunc; + +public class PalantirJavaFormatFormatterFunc implements FormatterFunc { + + private final Formatter formatter; + + public PalantirJavaFormatFormatterFunc() { + formatter = Formatter.createFormatter(JavaFormatterOptions.builder() + .style(JavaFormatterOptions.Style.PALANTIR) + .build()); + } + + @Override + public String apply(String input) throws Exception { + String source = input; + source = ImportOrderer.reorderImports(source, JavaFormatterOptions.Style.PALANTIR); + source = RemoveUnusedImports.removeUnusedImports(source); + return formatter.formatSource(source); + } + + @Override + public String toString() { + return "PalantirJavaFormatFormatterFunc{formatter=" + formatter + '}'; + } +} diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index d96cc67b7d..170f412af1 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Added * 🎉🎉🎉 [**IntelliJ plugin**](https://plugins.jetbrains.com/plugin/18321-spotless-gradle) thanks to [@ragurney](https://github.com/ragurney) 🎉🎉🎉 +* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)). ## [6.1.2] - 2022-01-07 ### Fixed diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index c63dcada42..7013dfee0c 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -60,7 +60,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui - [**Quickstart**](#quickstart) - [Requirements](#requirements) - **Languages** - - [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [clang-format](#clang-format), [prettier](#prettier)) + - [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [clang-format](#clang-format), [prettier](#prettier), [palantir-java-format](#palantir-java-format)) - [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy)) - [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier)) - [Scala](#scala) ([scalafmt](#scalafmt)) @@ -205,6 +205,32 @@ org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAME ``` This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834). +### palantir-java-format + +[homepage](https://github.com/palantir/palantir-java-format). [changelog](https://github.com/palantir/palantir-java-format/releases). +```gradle +spotless { + java { + palantirJavaFormat() + // optional: you can specify a specific version + palantirJavaFormat('2.9.0') +``` + +**⚠️ Note on using Palantir Java Format with Java 16+** + +Using Java 16+ with Palantir Java Format [requires additional flags](https://github.com/google/google-java-format/releases/tag/v1.10.0) on the running JDK. +These Flags can be provided using the `gradle.properties` file (See [documentation](https://docs.gradle.org/current/userguide/build_environment.html)). + +For example the following file under `gradle.properties` will run maven with the required flags: +``` +org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \ + --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \ + --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \ + --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \ + --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED +``` +This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834). + ### eclipse jdt [homepage](https://www.eclipse.org/downloads/packages/). [compatible versions](https://github.com/diffplug/spotless/tree/main/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_jdt_formatter). See [here](../ECLIPSE_SCREENSHOTS.md) for screenshots that demonstrate how to get and install the config file mentioned below. diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java index 086c521737..479d2ffd12 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import com.diffplug.spotless.generic.LicenseHeaderStep; import com.diffplug.spotless.java.GoogleJavaFormatStep; import com.diffplug.spotless.java.ImportOrderStep; +import com.diffplug.spotless.java.PalantirJavaFormatStep; import com.diffplug.spotless.java.RemoveUnusedImportsStep; public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense { @@ -175,6 +176,35 @@ private FormatterStep createStep() { } } + /** Uses the palantir-java-format jar to format source code. */ + public PalantirJavaFormatConfig palantirJavaFormat() { + return palantirJavaFormat(PalantirJavaFormatStep.defaultVersion()); + } + + /** + * Uses the given version of palantir-java-format to format source code. + * + * Limited to published versions. See issue #33 + * for an workaround for using snapshot versions. + */ + public PalantirJavaFormatConfig palantirJavaFormat(String version) { + Objects.requireNonNull(version); + return new PalantirJavaFormatConfig(version); + } + + public class PalantirJavaFormatConfig { + final String version; + + PalantirJavaFormatConfig(String version) { + this.version = Objects.requireNonNull(version); + addStep(createStep()); + } + + private FormatterStep createStep() { + return PalantirJavaFormatStep.create(version, provisioner()); + } + } + public EclipseConfig eclipse() { return new EclipseConfig(EclipseJdtFormatterStep.defaultVersion()); } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java new file mode 100644 index 0000000000..2283ce34bf --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PalantirJavaFormatIntegrationTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2022 DiffPlug + * + * 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.diffplug.gradle.spotless; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +class PalantirJavaFormatIntegrationTest extends GradleIntegrationHarness { + @Test + void integration() throws IOException { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "", + "spotless {", + " java {", + " target file('test.java')", + " palantirJavaFormat('1.1.0')", + " }", + "}"); + + setFile("test.java").toResource("java/palantirjavaformat/JavaCodeUnformatted.test"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("test.java").sameAsResource("java/palantirjavaformat/JavaCodeFormatted.test"); + + checkRunsThenUpToDate(); + replace("build.gradle", + "palantirJavaFormat('1.1.0')", + "palantirJavaFormat('1.0.1')"); + checkRunsThenUpToDate(); + } +} diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 05e4ad0a00..559958993d 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +* Added support for the [palantir-java-format](https://github.com/palantir/palantir-java-format) Java formatter ([#1083](https://github.com/diffplug/spotless/pull/1083)). ## [2.19.2] - 2022-01-10 ### Fixed diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 84806e7104..887522e60e 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -47,7 +47,7 @@ user@machine repo % mvn spotless:check - [Requirements](#requirements) - [Binding to maven phase](#binding-to-maven-phase) - **Languages** - - [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier)) + - [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier), [palantir-java-format](#palantir-java-format)) - [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy)) - [Kotlin](#kotlin) ([ktfmt](#ktfmt), [ktlint](#ktlint), [diktat](#diktat), [prettier](#prettier)) - [Scala](#scala) ([scalafmt](#scalafmt)) @@ -223,6 +223,27 @@ For example the following file under `.mvn/jvm.config` will run maven with the r ``` This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834). +### palantir-java-format + +[homepage](https://github.com/palantir/palantir-java-format). [changelog](https://github.com/palantir/palantir-java-format/releases). [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java). + +```xml + + 2.10.0 + +``` + +**⚠️ Note on using Palantir Java Format with Java 16+** + +Using Java 16+ with Palantir Java Format [requires additional flags](https://github.com/google/google-java-format/releases/tag/v1.10.0) on the running JDK. +These Flags can be provided using `MAVEN_OPTS` environment variable or using the `./mvn/jvm.config` file (See [documentation](https://maven.apache.org/configure.html#mvn-jvm-config-file)). + +For example the following file under `.mvn/jvm.config` will run maven with the required flags: +``` +--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED +``` +This is a workaround to a [pending issue](https://github.com/diffplug/spotless/issues/834). + ### eclipse jdt [homepage](https://www.eclipse.org/downloads/packages/). [compatible versions](https://github.com/diffplug/spotless/tree/main/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_jdt_formatter). [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Eclipse.java). See [here](../ECLIPSE_SCREENSHOTS.md) for screenshots that demonstrate how to get and install the config file mentioned below. diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java index f48f2b742c..819179ebba 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 DiffPlug + * Copyright 2016-2022 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,6 +54,10 @@ public void addImportOrder(ImportOrder importOrder) { addStepFactory(importOrder); } + public void addPalantirJavaFormat(PalantirJavaFormat palantirJavaFormat) { + addStepFactory(palantirJavaFormat); + } + public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) { addStepFactory(removeUnusedImports); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java new file mode 100644 index 0000000000..df58764c1d --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/PalantirJavaFormat.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016-2022 DiffPlug + * + * 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.diffplug.spotless.maven.java; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.java.PalantirJavaFormatStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class PalantirJavaFormat implements FormatterStepFactory { + + @Parameter + private String version; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + String version = this.version != null ? this.version : PalantirJavaFormatStep.defaultVersion(); + return PalantirJavaFormatStep.create(version, config.getProvisioner()); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java new file mode 100644 index 0000000000..33ec8ea84f --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/PalantirJavaFormatTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2022 DiffPlug + * + * 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.diffplug.spotless.maven.java; + +import static org.junit.jupiter.api.condition.JRE.JAVA_11; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; + +import com.diffplug.spotless.maven.MavenIntegrationHarness; + +class PalantirJavaFormatTest extends MavenIntegrationHarness { + @Test + void specificVersionDefaultStyle() throws Exception { + writePomWithJavaSteps( + "", + " 1.1.0", + ""); + + runTest("java/palantirjavaformat/JavaCodeFormatted.test"); + } + + @Test + @EnabledForJreRange(min = JAVA_11) + void specificJava11Version2() throws Exception { + writePomWithJavaSteps( + "", + " 2.10.0", + ""); + + runTest("java/palantirjavaformat/JavaCodeFormatted.test"); + } + + private void runTest(String targetResource) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toResource("java/palantirjavaformat/JavaCodeUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource(targetResource); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormatted.test new file mode 100644 index 0000000000..25ccbc25f5 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeFormatted.test @@ -0,0 +1,11 @@ +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeUnformatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeUnformatted.test new file mode 100644 index 0000000000..18f2aca0e2 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeUnformatted.test @@ -0,0 +1,12 @@ + +import mylib.Unused; +import mylib.UsedB; +import mylib.UsedA; + +public class Java { +public static void main(String[] args) { +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); +UsedB.someMethod(); +UsedA.someMethod(); +} +} \ No newline at end of file diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormatted.test new file mode 100644 index 0000000000..ab0bbbf033 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseFormatted.test @@ -0,0 +1,16 @@ +/* + * Some license stuff. + * Very official. + */ + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test new file mode 100644 index 0000000000..f43756b938 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test @@ -0,0 +1,16 @@ +/* + * Some license stuff. + * Very official. + */ + +import mylib.Unused; +import mylib.UsedB; +import mylib.UsedA; + +public class Java { +public static void main(String[] args) { +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); +UsedB.someMethod(); +UsedA.someMethod(); +} +} \ No newline at end of file diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormatted.test new file mode 100644 index 0000000000..5c2c225e68 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageFormatted.test @@ -0,0 +1,13 @@ +package hello.world; + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + System.out.println( + "A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageUnformatted.test b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageUnformatted.test new file mode 100644 index 0000000000..61b1b8fce4 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/JavaCodeWithPackageUnformatted.test @@ -0,0 +1,13 @@ +package hello.world; + +import mylib.Unused; +import mylib.UsedB; +import mylib.UsedA; + +public class Java { +public static void main(String[] args) { +System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length."); +UsedB.someMethod(); +UsedA.someMethod(); +} +} \ No newline at end of file diff --git a/testlib/src/main/resources/java/palantirjavaformat/TextBlock.clean b/testlib/src/main/resources/java/palantirjavaformat/TextBlock.clean new file mode 100644 index 0000000000..7f60a80cf3 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/TextBlock.clean @@ -0,0 +1,14 @@ +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + var a = """ + Howdy + Partner! + """; + System.out.println(a); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/main/resources/java/palantirjavaformat/TextBlock.dirty b/testlib/src/main/resources/java/palantirjavaformat/TextBlock.dirty new file mode 100644 index 0000000000..0f5846d711 --- /dev/null +++ b/testlib/src/main/resources/java/palantirjavaformat/TextBlock.dirty @@ -0,0 +1,15 @@ + +import mylib.UsedA; +import mylib.UsedB; + +public class Java { + public static void main(String[] args) { + var a = """ + Howdy + Partner! + """; + System.out.println(a); + UsedB.someMethod(); + UsedA.someMethod(); + } +} diff --git a/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java b/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java new file mode 100644 index 0000000000..a589992567 --- /dev/null +++ b/testlib/src/test/java/com/diffplug/spotless/java/PalantirJavaFormatStepTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2022 DiffPlug + * + * 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.diffplug.spotless.java; + +import static org.junit.jupiter.api.condition.JRE.JAVA_11; +import static org.junit.jupiter.api.condition.JRE.JAVA_13; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledForJreRange; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.ResourceHarness; +import com.diffplug.spotless.SerializableEqualityTester; +import com.diffplug.spotless.StepHarness; +import com.diffplug.spotless.TestProvisioner; + +class PalantirJavaFormatStepTest extends ResourceHarness { + + @Test + @EnabledForJreRange(min = JAVA_13) + void jvm13Features() throws Exception { + try (StepHarness step = StepHarness.forStep(PalantirJavaFormatStep.create(TestProvisioner.mavenCentral()))) { + step.testResource("java/palantirjavaformat/TextBlock.dirty", "java/palantirjavaformat/TextBlock.clean"); + } + } + + @Test + @EnabledForJreRange(min = JAVA_11) + void behavior2() throws Exception { + FormatterStep step = PalantirJavaFormatStep.create("2.10.0", TestProvisioner.mavenCentral()); + StepHarness.forStep(step) + .testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test"); + } + + @Test + void behavior() throws Exception { + FormatterStep step = PalantirJavaFormatStep.create("1.1.0", TestProvisioner.mavenCentral()); + StepHarness.forStep(step) + .testResource("java/palantirjavaformat/JavaCodeUnformatted.test", "java/palantirjavaformat/JavaCodeFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithLicenseUnformatted.test", "java/palantirjavaformat/JavaCodeWithLicenseFormatted.test") + .testResource("java/palantirjavaformat/JavaCodeWithPackageUnformatted.test", "java/palantirjavaformat/JavaCodeWithPackageFormatted.test"); + } + + @Test + void equality() { + new SerializableEqualityTester() { + String version = "1.1.0"; + + @Override + protected void setupTest(API api) { + // same version == same + api.areDifferentThan(); + // change the version, and it's different + version = "1.0.0"; + api.areDifferentThan(); + } + + @Override + protected FormatterStep create() { + String finalVersion = this.version; + return PalantirJavaFormatStep.create(finalVersion, TestProvisioner.mavenCentral()); + } + }.testEquals(); + } +}