Skip to content

Commit 1196b72

Browse files
author
Vincent Potucek
committed
blindspot on RemoveUnusedImportsStep
1 parent 2d32ccd commit 1196b72

File tree

3 files changed

+181
-1
lines changed

3 files changed

+181
-1
lines changed

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@
2222
class RemoveUnusedImportsStepTest extends MavenIntegrationHarness {
2323

2424
@Test
25-
void testRemoveUnusedInports() throws Exception {
25+
void testRemoveUnusedImports() throws Exception {
2626
writePomWithJavaSteps("<removeUnusedImports/>");
2727

2828
String path = "src/main/java/test.java";
2929
setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test");
3030
mavenRunner().withArguments("spotless:apply").runNoError();
3131
assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test");
3232
}
33+
34+
@Test
35+
void testIssue2532() throws Exception {
36+
writePomWithJavaSteps("<removeUnusedImports/>");
37+
38+
String path = "src/main/java/test.java";
39+
setFile(path).toResource("java/removeunusedimports/Issue2532Pre.test");
40+
mavenRunner().withArguments("spotless:apply").runNoError();
41+
assertFile(path).sameAsResource("java/removeunusedimports/Issue2532Post.test");
42+
}
3343
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.quarkus.test;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.StringReader;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.util.Comparator;
10+
import java.util.Properties;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
13+
14+
import org.jboss.shrinkwrap.api.Node;
15+
import org.jboss.shrinkwrap.api.asset.Asset;
16+
import org.jboss.shrinkwrap.api.asset.StringAsset;
17+
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
18+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
19+
20+
final class ExportUtil {
21+
22+
static final String APPLICATION_PROPERTIES = "application.properties";
23+
24+
private ExportUtil() {
25+
}
26+
27+
static void exportToQuarkusDeploymentPath(JavaArchive archive) throws IOException {
28+
String exportPath = System.getProperty("quarkus.deploymentExportPath");
29+
if (exportPath == null) {
30+
return;
31+
}
32+
File exportDir = new File(exportPath);
33+
if (exportDir.exists()) {
34+
if (!exportDir.isDirectory()) {
35+
throw new IllegalStateException("Export path is not a directory: " + exportPath);
36+
}
37+
try (Stream<Path> stream = Files.walk(exportDir.toPath())) {
38+
stream.sorted(Comparator.reverseOrder()).map(Path::toFile)
39+
.forEach(File::delete);
40+
}
41+
} else if (!exportDir.mkdirs()) {
42+
throw new IllegalStateException("Export path could not be created: " + exportPath);
43+
}
44+
File exportFile = new File(exportDir, archive.getName());
45+
archive.as(ZipExporter.class).exportTo(exportFile);
46+
}
47+
48+
static void mergeCustomApplicationProperties(JavaArchive archive, Properties customApplicationProperties)
49+
throws IOException {
50+
Node applicationProperties = archive.get(APPLICATION_PROPERTIES);
51+
if (applicationProperties != null) {
52+
// Merge the existing "application.properties" asset and overriden config properties
53+
// Overriden properties take precedence
54+
Properties mergedProperties = new Properties();
55+
Asset asset = applicationProperties.getAsset();
56+
if (asset instanceof StringAsset strAsset) {
57+
mergedProperties.load(new StringReader(strAsset.getSource()));
58+
} else {
59+
try (InputStream in = asset.openStream()) {
60+
mergedProperties.load(in);
61+
}
62+
}
63+
customApplicationProperties.forEach(mergedProperties::put);
64+
65+
if (Boolean.parseBoolean(System.getProperty("quarkus.test.log-merged-properties"))) {
66+
System.out.println("Merged config properties:\n"
67+
+ mergedProperties.keySet().stream().map(Object::toString).collect(Collectors.joining("\n")));
68+
} else {
69+
System.out.println(
70+
"NOTE: overrideConfigKey() and application.properties were merged; use quarkus.test.log-merged-properties=true to list the specific values");
71+
}
72+
deleteApplicationProperties(archive);
73+
archive.add(new PropertiesAsset(mergedProperties), APPLICATION_PROPERTIES);
74+
} else {
75+
archive.add(new PropertiesAsset(customApplicationProperties), APPLICATION_PROPERTIES);
76+
}
77+
}
78+
79+
static void deleteApplicationProperties(JavaArchive archive) {
80+
// MemoryMapArchiveBase#addAsset(ArchivePath,Asset) does not overwrite the existing node correctly
81+
// https://github.com/shrinkwrap/shrinkwrap/issues/179
82+
archive.delete(APPLICATION_PROPERTIES);
83+
}
84+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package io.quarkus.test;
2+
3+
import static io.quarkus.test.ExportUtil.APPLICATION_PROPERTIES;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.StringReader;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.util.Comparator;
12+
import java.util.Properties;
13+
import java.util.stream.Collectors;
14+
import java.util.stream.Stream;
15+
16+
import org.jboss.shrinkwrap.api.Node;
17+
import org.jboss.shrinkwrap.api.asset.Asset;
18+
import org.jboss.shrinkwrap.api.asset.StringAsset;
19+
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
20+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
21+
22+
final class ExportUtil {
23+
24+
static final String APPLICATION_PROPERTIES = "application.properties";
25+
26+
private ExportUtil() {
27+
}
28+
29+
static void exportToQuarkusDeploymentPath(JavaArchive archive) throws IOException {
30+
String exportPath = System.getProperty("quarkus.deploymentExportPath");
31+
if (exportPath == null) {
32+
return;
33+
}
34+
File exportDir = new File(exportPath);
35+
if (exportDir.exists()) {
36+
if (!exportDir.isDirectory()) {
37+
throw new IllegalStateException("Export path is not a directory: " + exportPath);
38+
}
39+
try (Stream<Path> stream = Files.walk(exportDir.toPath())) {
40+
stream.sorted(Comparator.reverseOrder()).map(Path::toFile)
41+
.forEach(File::delete);
42+
}
43+
} else if (!exportDir.mkdirs()) {
44+
throw new IllegalStateException("Export path could not be created: " + exportPath);
45+
}
46+
File exportFile = new File(exportDir, archive.getName());
47+
archive.as(ZipExporter.class).exportTo(exportFile);
48+
}
49+
50+
static void mergeCustomApplicationProperties(JavaArchive archive, Properties customApplicationProperties)
51+
throws IOException {
52+
Node applicationProperties = archive.get(APPLICATION_PROPERTIES);
53+
if (applicationProperties != null) {
54+
// Merge the existing "application.properties" asset and overriden config properties
55+
// Overriden properties take precedence
56+
Properties mergedProperties = new Properties();
57+
Asset asset = applicationProperties.getAsset();
58+
if (asset instanceof StringAsset strAsset) {
59+
mergedProperties.load(new StringReader(strAsset.getSource()));
60+
} else {
61+
try (InputStream in = asset.openStream()) {
62+
mergedProperties.load(in);
63+
}
64+
}
65+
customApplicationProperties.forEach(mergedProperties::put);
66+
67+
if (Boolean.parseBoolean(System.getProperty("quarkus.test.log-merged-properties"))) {
68+
System.out.println("Merged config properties:\n"
69+
+ mergedProperties.keySet().stream().map(Object::toString).collect(Collectors.joining("\n")));
70+
} else {
71+
System.out.println(
72+
"NOTE: overrideConfigKey() and application.properties were merged; use quarkus.test.log-merged-properties=true to list the specific values");
73+
}
74+
deleteApplicationProperties(archive);
75+
archive.add(new PropertiesAsset(mergedProperties), APPLICATION_PROPERTIES);
76+
} else {
77+
archive.add(new PropertiesAsset(customApplicationProperties), APPLICATION_PROPERTIES);
78+
}
79+
}
80+
81+
static void deleteApplicationProperties(JavaArchive archive) {
82+
// MemoryMapArchiveBase#addAsset(ArchivePath,Asset) does not overwrite the existing node correctly
83+
// https://github.com/shrinkwrap/shrinkwrap/issues/179
84+
archive.delete(APPLICATION_PROPERTIES);
85+
}
86+
}

0 commit comments

Comments
 (0)