|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2016-2025 DiffPlug | 
|  | 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 | +package com.diffplug.spotless.java; | 
|  | 17 | + | 
|  | 18 | +import static com.diffplug.spotless.java.RemoveUnusedImportsStep.DEFAULT_FORMATTER; | 
|  | 19 | +import static com.diffplug.spotless.java.RemoveUnusedImportsStep.NAME; | 
|  | 20 | +import static com.diffplug.spotless.java.RemoveUnusedImportsStep.defaultFormatter; | 
|  | 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 
|  | 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; | 
|  | 23 | + | 
|  | 24 | +import java.util.Collections; | 
|  | 25 | + | 
|  | 26 | +import org.junit.jupiter.api.Test; | 
|  | 27 | + | 
|  | 28 | +import com.diffplug.spotless.maven.MavenIntegrationHarness; | 
|  | 29 | + | 
|  | 30 | +class RemoveUnusedImportsStepTest extends MavenIntegrationHarness { | 
|  | 31 | + | 
|  | 32 | +	@Test | 
|  | 33 | +	void testRemoveUnusedImports() throws Exception { | 
|  | 34 | +		writePomWithJavaSteps("<removeUnusedImports/>"); | 
|  | 35 | + | 
|  | 36 | +		String path = "src/main/java/test.java"; | 
|  | 37 | +		setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); | 
|  | 38 | +		mavenRunner().withArguments("spotless:apply").runNoError(); | 
|  | 39 | +		assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); | 
|  | 40 | +	} | 
|  | 41 | + | 
|  | 42 | +	@Test | 
|  | 43 | +	void testDefaults() { | 
|  | 44 | +		assertEquals("palantir-java-format", defaultFormatter()); | 
|  | 45 | +		assertEquals("palantir-java-format", DEFAULT_FORMATTER); | 
|  | 46 | +		assertEquals("removeUnusedImports", NAME); | 
|  | 47 | +	} | 
|  | 48 | + | 
|  | 49 | +	@Test | 
|  | 50 | +	void testCreateWithDefaultFormatter() { | 
|  | 51 | +		assertEquals(NAME, RemoveUnusedImportsStep.create((groupArtifact, version) -> Collections.emptySet()).getName()); | 
|  | 52 | +	} | 
|  | 53 | + | 
|  | 54 | +	@Test | 
|  | 55 | +	void testCreateWithPalantirFormatter() { | 
|  | 56 | +		assertEquals(NAME, RemoveUnusedImportsStep.create("palantir-java-format", (groupArtifact, version) -> Collections.emptySet()).getName()); | 
|  | 57 | +	} | 
|  | 58 | + | 
|  | 59 | +	@Test | 
|  | 60 | +	void testCreateWithCleanthatFormatter() { | 
|  | 61 | +		assertEquals(NAME, RemoveUnusedImportsStep.create("cleanthat-javaparser-unnecessaryimport", (groupArtifact, version) -> Collections.emptySet()).getName()); | 
|  | 62 | +	} | 
|  | 63 | + | 
|  | 64 | +	@Test | 
|  | 65 | +	void testCreateWithInvalidFormatter() { | 
|  | 66 | +		assertThrows(IllegalArgumentException.class, () -> RemoveUnusedImportsStep.create("invalid-formatter", (groupArtifact, version) -> Collections.emptySet())); | 
|  | 67 | +	} | 
|  | 68 | + | 
|  | 69 | +	@Test | 
|  | 70 | +	void testCreateWithNullProvisioner() { | 
|  | 71 | +		assertThrows(NullPointerException.class, () -> RemoveUnusedImportsStep.create("palantir-java-format", null)); | 
|  | 72 | +	} | 
|  | 73 | +} | 
0 commit comments