From 2ebe138c9646c0d3f438bd5a1cc69ed0b0f47876 Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Thu, 3 Jul 2025 10:16:24 +0200 Subject: [PATCH] Use palantir-java-format as default formatter in RemoveUnusedImportsStep --- CHANGES.md | 1 + .../java/RemoveUnusedImportsStep.java | 13 ++-- .../java/RemoveUnusedImportsStepTest.java | 73 +++++++++++++++++++ .../java/RemoveUnusedImportsStepTest.java | 33 --------- ...dImportsStep_withGoogleJavaFormatTest.java | 6 +- 5 files changed, 83 insertions(+), 43 deletions(-) create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStepTest.java delete mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java diff --git a/CHANGES.md b/CHANGES.md index ba1e66f1f7..8e855c5cbe 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Added * Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517)) +* Use `palantir-java-format` as default formatter in `RemoveUnusedImportsStep`. ([#2541](https://github.com/diffplug/spotless/pull/2541)) ## [3.1.2] - 2025-05-27 ### Fixed diff --git a/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java b/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java index 753678a86b..fcdb52503b 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2024 DiffPlug + * Copyright 2016-2025 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,12 +22,12 @@ import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.Provisioner; -/** Uses google-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */ +/** Uses palantir-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */ public class RemoveUnusedImportsStep implements Serializable { private static final long serialVersionUID = 1L; static final String NAME = "removeUnusedImports"; - static final String GJF = "google-java-format"; + static final String DEFAULT_FORMATTER = "palantir-java-format"; static final String CLEANTHAT = "cleanthat-javaparser-unnecessaryimport"; // https://github.com/solven-eu/cleanthat/blob/master/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryImport.java @@ -37,18 +37,17 @@ public class RemoveUnusedImportsStep implements Serializable { private RemoveUnusedImportsStep() {} public static String defaultFormatter() { - return GJF; + return DEFAULT_FORMATTER; } public static FormatterStep create(Provisioner provisioner) { - // The default importRemover is GJF - return create(GJF, provisioner); + return create(DEFAULT_FORMATTER, provisioner); } public static FormatterStep create(String unusedImportRemover, Provisioner provisioner) { Objects.requireNonNull(provisioner, "provisioner"); switch (unusedImportRemover) { - case GJF: + case DEFAULT_FORMATTER: return GoogleJavaFormatStep.createRemoveUnusedImportsOnly(provisioner); case CLEANTHAT: return CleanthatJavaStep.createWithStepName(NAME, CleanthatJavaStep.defaultGroupArtifact(), CleanthatJavaStep.defaultVersion(), "99.9", List.of(CLEANTHAT_MUTATOR), List.of(), false, provisioner); diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStepTest.java new file mode 100644 index 0000000000..85713423cc --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStepTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2016-2025 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 com.diffplug.spotless.java.RemoveUnusedImportsStep.DEFAULT_FORMATTER; +import static com.diffplug.spotless.java.RemoveUnusedImportsStep.NAME; +import static com.diffplug.spotless.java.RemoveUnusedImportsStep.defaultFormatter; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.Collections; + +import org.junit.jupiter.api.Test; + +import com.diffplug.spotless.maven.MavenIntegrationHarness; + +class RemoveUnusedImportsStepTest extends MavenIntegrationHarness { + + @Test + void testRemoveUnusedImports() throws Exception { + writePomWithJavaSteps(""); + + String path = "src/main/java/test.java"; + setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); + } + + @Test + void testDefaults() { + assertEquals("palantir-java-format", defaultFormatter()); + assertEquals("palantir-java-format", DEFAULT_FORMATTER); + assertEquals("removeUnusedImports", NAME); + } + + @Test + void testCreateWithDefaultFormatter() { + assertEquals(NAME, RemoveUnusedImportsStep.create((groupArtifact, version) -> Collections.emptySet()).getName()); + } + + @Test + void testCreateWithPalantirFormatter() { + assertEquals(NAME, RemoveUnusedImportsStep.create("palantir-java-format", (groupArtifact, version) -> Collections.emptySet()).getName()); + } + + @Test + void testCreateWithCleanthatFormatter() { + assertEquals(NAME, RemoveUnusedImportsStep.create("cleanthat-javaparser-unnecessaryimport", (groupArtifact, version) -> Collections.emptySet()).getName()); + } + + @Test + void testCreateWithInvalidFormatter() { + assertThrows(IllegalArgumentException.class, () -> RemoveUnusedImportsStep.create("invalid-formatter", (groupArtifact, version) -> Collections.emptySet())); + } + + @Test + void testCreateWithNullProvisioner() { + assertThrows(NullPointerException.class, () -> RemoveUnusedImportsStep.create("palantir-java-format", null)); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java deleted file mode 100644 index 50853ba914..0000000000 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016-2021 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.junit.jupiter.api.Test; - -import com.diffplug.spotless.maven.MavenIntegrationHarness; - -class RemoveUnusedImportsStepTest extends MavenIntegrationHarness { - - @Test - void testRemoveUnusedInports() throws Exception { - writePomWithJavaSteps(""); - - String path = "src/main/java/test.java"; - setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); - } -} diff --git a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java index 44699a8215..766816a659 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2025 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ class RemoveUnusedImportsStep_withGoogleJavaFormatTest { @Test void behavior() throws Exception { - FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral()); + FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral()); StepHarness.forStep(step) .testResource("java/removeunusedimports/JavaCodeUnformatted.test", "java/removeunusedimports/JavaCodeFormatted.test") .testResource("java/removeunusedimports/JavaCodeWithLicenseUnformatted.test", "java/removeunusedimports/JavaCodeWithLicenseFormatted.test") @@ -48,7 +48,7 @@ protected void setupTest(API api) { @Override protected FormatterStep create() { - return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral()); + return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral()); } }.testEquals(); }