Skip to content

blindspot on RemoveUnusedImportsStep #2533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 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.
Expand All @@ -22,12 +22,22 @@
class RemoveUnusedImportsStepTest extends MavenIntegrationHarness {

@Test
void testRemoveUnusedInports() throws Exception {
void testRemoveUnusedImports() throws Exception {
writePomWithJavaSteps("<removeUnusedImports/>");

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 testIssue2532() throws Exception {
writePomWithJavaSteps("<removeUnusedImports/>");

String path = "src/main/java/test.java";
setFile(path).toResource("java/removeunusedimports/Issue2532Pre.test");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile(path).sameAsResource("java/removeunusedimports/Issue2532Post.test");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.quarkus.test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

final class ExportUtil {

static final String APPLICATION_PROPERTIES = "application.properties";

private ExportUtil() {
}

static void exportToQuarkusDeploymentPath(JavaArchive archive) throws IOException {
String exportPath = System.getProperty("quarkus.deploymentExportPath");
if (exportPath == null) {
return;
}
File exportDir = new File(exportPath);
if (exportDir.exists()) {
if (!exportDir.isDirectory()) {
throw new IllegalStateException("Export path is not a directory: " + exportPath);
}
try (Stream<Path> stream = Files.walk(exportDir.toPath())) {
stream.sorted(Comparator.reverseOrder()).map(Path::toFile)
.forEach(File::delete);
}
} else if (!exportDir.mkdirs()) {
throw new IllegalStateException("Export path could not be created: " + exportPath);
}
File exportFile = new File(exportDir, archive.getName());
archive.as(ZipExporter.class).exportTo(exportFile);
}

static void mergeCustomApplicationProperties(JavaArchive archive, Properties customApplicationProperties)
throws IOException {
Node applicationProperties = archive.get(APPLICATION_PROPERTIES);
if (applicationProperties != null) {
// Merge the existing "application.properties" asset and overriden config properties
// Overriden properties take precedence
Properties mergedProperties = new Properties();
Asset asset = applicationProperties.getAsset();
if (asset instanceof StringAsset strAsset) {
mergedProperties.load(new StringReader(strAsset.getSource()));
} else {
try (InputStream in = asset.openStream()) {
mergedProperties.load(in);
}
}
customApplicationProperties.forEach(mergedProperties::put);

if (Boolean.parseBoolean(System.getProperty("quarkus.test.log-merged-properties"))) {
System.out.println("Merged config properties:\n"
+ mergedProperties.keySet().stream().map(Object::toString).collect(Collectors.joining("\n")));
} else {
System.out.println(
"NOTE: overrideConfigKey() and application.properties were merged; use quarkus.test.log-merged-properties=true to list the specific values");
}
deleteApplicationProperties(archive);
archive.add(new PropertiesAsset(mergedProperties), APPLICATION_PROPERTIES);
} else {
archive.add(new PropertiesAsset(customApplicationProperties), APPLICATION_PROPERTIES);
}
}

static void deleteApplicationProperties(JavaArchive archive) {
// MemoryMapArchiveBase#addAsset(ArchivePath,Asset) does not overwrite the existing node correctly
// https://github.com/shrinkwrap/shrinkwrap/issues/179
archive.delete(APPLICATION_PROPERTIES);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.quarkus.test;

import static io.quarkus.test.ExportUtil.APPLICATION_PROPERTIES;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

final class ExportUtil {

static final String APPLICATION_PROPERTIES = "application.properties";

private ExportUtil() {
}

static void exportToQuarkusDeploymentPath(JavaArchive archive) throws IOException {
String exportPath = System.getProperty("quarkus.deploymentExportPath");
if (exportPath == null) {
return;
}
File exportDir = new File(exportPath);
if (exportDir.exists()) {
if (!exportDir.isDirectory()) {
throw new IllegalStateException("Export path is not a directory: " + exportPath);
}
try (Stream<Path> stream = Files.walk(exportDir.toPath())) {
stream.sorted(Comparator.reverseOrder()).map(Path::toFile)
.forEach(File::delete);
}
} else if (!exportDir.mkdirs()) {
throw new IllegalStateException("Export path could not be created: " + exportPath);
}
File exportFile = new File(exportDir, archive.getName());
archive.as(ZipExporter.class).exportTo(exportFile);
}

static void mergeCustomApplicationProperties(JavaArchive archive, Properties customApplicationProperties)
throws IOException {
Node applicationProperties = archive.get(APPLICATION_PROPERTIES);
if (applicationProperties != null) {
// Merge the existing "application.properties" asset and overriden config properties
// Overriden properties take precedence
Properties mergedProperties = new Properties();
Asset asset = applicationProperties.getAsset();
if (asset instanceof StringAsset strAsset) {
mergedProperties.load(new StringReader(strAsset.getSource()));
} else {
try (InputStream in = asset.openStream()) {
mergedProperties.load(in);
}
}
customApplicationProperties.forEach(mergedProperties::put);

if (Boolean.parseBoolean(System.getProperty("quarkus.test.log-merged-properties"))) {
System.out.println("Merged config properties:\n"
+ mergedProperties.keySet().stream().map(Object::toString).collect(Collectors.joining("\n")));
} else {
System.out.println(
"NOTE: overrideConfigKey() and application.properties were merged; use quarkus.test.log-merged-properties=true to list the specific values");
}
deleteApplicationProperties(archive);
archive.add(new PropertiesAsset(mergedProperties), APPLICATION_PROPERTIES);
} else {
archive.add(new PropertiesAsset(customApplicationProperties), APPLICATION_PROPERTIES);
}
}

static void deleteApplicationProperties(JavaArchive archive) {
// MemoryMapArchiveBase#addAsset(ArchivePath,Asset) does not overwrite the existing node correctly
// https://github.com/shrinkwrap/shrinkwrap/issues/179
archive.delete(APPLICATION_PROPERTIES);
}
}
Loading