Skip to content

Commit

Permalink
Merge pull request #1074 from tisoft/fix_fingerprint_with_deps
Browse files Browse the repository at this point in the history
Fix fingerprint with deps
  • Loading branch information
lutovich authored Jan 10, 2022
2 parents 01e4d9e + 4d60d62 commit eba1c59
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ indent_size = 2
# Doc: https://youtrack.jetbrains.com/issue/IDEA-170643#focus=streamItem-27-3708697.0-0
ij_java_imports_layout = java.**,|,javax.**,|,org.**,|,com.**,|,com.diffplug.**,|,*
ij_java_use_single_class_imports = true

[*.xml.mustache]
indent_style = space
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Fixed
* Enabling the upToDateChecking with the plugin configured inside pluginManagement, with an additional dependency and running under Maven 3.6.3 leads to a java.io.NotSerializableException. ([#1074](https://github.com/diffplug/spotless/pull/1074)).

## [2.19.1] - 2022-01-07
### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2022 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 @@ -17,9 +17,12 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Objects;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;

Expand Down Expand Up @@ -77,6 +80,13 @@ public String toString() {
}

private static byte[] digest(Plugin plugin, Iterable<Formatter> formatters) {
// dependencies can be an unserializable org.apache.maven.model.merge.ModelMerger$MergingList
// replace it with a serializable ArrayList
List<Dependency> dependencies = null;
if (plugin != null) {
dependencies = plugin.getDependencies();
plugin.setDependencies(new ArrayList<>(dependencies));
}
try (ObjectDigestOutputStream out = ObjectDigestOutputStream.create()) {
out.writeObject(plugin);
for (Formatter formatter : formatters) {
Expand All @@ -86,6 +96,11 @@ private static byte[] digest(Plugin plugin, Iterable<Formatter> formatters) {
return out.digest();
} catch (IOException e) {
throw new UncheckedIOException("Unable to serialize plugin " + plugin, e);
} finally {
// reset the original list
if (plugin != null) {
plugin.setDependencies(dependencies);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -57,6 +57,7 @@ public class MavenIntegrationHarness extends ResourceHarness {
private static final String CONFIGURATION = "configuration";
private static final String EXECUTIONS = "executions";
private static final String MODULES = "modules";
private static final String DEPENDENCIES = "dependencies";
private static final String MODULE_NAME = "name";
private static final String CHILD_ID = "childId";
private static final int REMOTE_DEBUG_PORT = 5005;
Expand Down Expand Up @@ -144,11 +145,11 @@ protected void writePomWithMarkdownSteps(String... steps) throws IOException {
}

protected void writePom(String... configuration) throws IOException {
writePom(null, configuration);
writePom(null, configuration, null);
}

protected void writePom(String[] executions, String[] configuration) throws IOException {
String pomXmlContent = createPomXmlContent(executions, configuration);
protected void writePom(String[] executions, String[] configuration, String[] dependencies) throws IOException {
String pomXmlContent = createPomXmlContent(null, executions, configuration, dependencies);
setFile("pom.xml").toContent(pomXmlContent);
}

Expand All @@ -171,13 +172,17 @@ protected MultiModuleProjectCreator multiModuleProject() {
return new MultiModuleProjectCreator();
}

protected String createPomXmlContent(String[] executions, String[] configuration) throws IOException {
return createPomXmlContent(null, executions, configuration);
protected String createPomXmlContent(String pluginVersion, String[] executions, String[] configuration, String[] dependencies) throws IOException {
return createPomXmlContent("/pom-test.xml.mustache", pluginVersion, executions, configuration, dependencies);
}

protected String createPomXmlContent(String pomTemplate, String pluginVersion, String[] executions, String[] configuration, String[] dependencies) throws IOException {
Map<String, Object> params = buildPomXmlParams(pluginVersion, executions, configuration, null, dependencies);
return createPomXmlContent(pomTemplate, params);
}

protected String createPomXmlContent(String pluginVersion, String[] executions, String[] configuration) throws IOException {
Map<String, Object> params = buildPomXmlParams(pluginVersion, executions, configuration, null);
return createPomXmlContent("/pom-test.xml.mustache", params);
return createPomXmlContent(pluginVersion, executions, configuration, null);
}

private String createPomXmlContent(String pomTemplate, Map<String, Object> params) throws IOException {
Expand All @@ -190,7 +195,7 @@ private String createPomXmlContent(String pomTemplate, Map<String, Object> param
}
}

private static Map<String, Object> buildPomXmlParams(String pluginVersion, String[] executions, String[] configuration, String[] modules) {
private static Map<String, Object> buildPomXmlParams(String pluginVersion, String[] executions, String[] configuration, String[] modules, String[] dependencies) {
Map<String, Object> params = new HashMap<>();
params.put(SPOTLESS_MAVEN_PLUGIN_VERSION, pluginVersion == null ? getSystemProperty(SPOTLESS_MAVEN_PLUGIN_VERSION) : pluginVersion);

Expand All @@ -207,6 +212,10 @@ private static Map<String, Object> buildPomXmlParams(String pluginVersion, Strin
params.put(MODULES, moduleNames);
}

if (dependencies != null) {
params.put(DEPENDENCIES, String.join("\n", dependencies));
}

return params;
}

Expand Down Expand Up @@ -283,7 +292,7 @@ private void createRootPom() throws IOException {
modulesList.addAll(subProjects.keySet());
String[] modules = modulesList.toArray(new String[0]);

Map<String, Object> rootPomParams = buildPomXmlParams(null, null, configuration, modules);
Map<String, Object> rootPomParams = buildPomXmlParams(null, null, configuration, modules, null);
setFile("pom.xml").toContent(createPomXmlContent("/multi-module/pom-parent.xml.mustache", rootPomParams));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -59,7 +59,8 @@ void testSpotlessCheckBindingToVerifyPhase() throws Exception {
" <licenseHeader>",
" <file>${basedir}/license.txt</file>",
" </licenseHeader>",
"</java>"});
"</java>"},
null);

testSpotlessCheck(UNFORMATTED_FILE, "verify", true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,6 +64,25 @@ class PluginFingerprintTest extends MavenIntegrationHarness {
"</googleJavaFormat>"
};

private static final String[] DEPENDENCIES_1 = {
"<dependencies>",
" <dependency>",
" <groupId>unknown</groupId>",
" <artifactId>unknown</artifactId>",
" <version>1.0</version>",
" </dependency>",
"</dependencies>"
};
private static final String[] DEPENDENCIES_2 = {
"<dependencies>",
" <dependency>",
" <groupId>unknown</groupId>",
" <artifactId>unknown</artifactId>",
" <version>2.0</version>",
" </dependency>",
"</dependencies>"
};

private static final List<Formatter> FORMATTERS = singletonList(formatter(formatterStep("default")));

@Test
Expand All @@ -80,6 +99,34 @@ void sameFingerprint() throws Exception {
assertThat(fingerprint1).isEqualTo(fingerprint2);
}

@Test
void sameFingerprintWithDependencies() throws Exception {
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1);
String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1);

MavenProject project1 = mavenProject(xml1);
MavenProject project2 = mavenProject(xml2);

PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS);
PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS);

assertThat(fingerprint1).isEqualTo(fingerprint2);
}

@Test
void differentFingerprintForDifferentDependencies() throws Exception {
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_1);
String xml2 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1, DEPENDENCIES_2);

MavenProject project1 = mavenProject(xml1);
MavenProject project2 = mavenProject(xml2);

PluginFingerprint fingerprint1 = PluginFingerprint.from(project1, FORMATTERS);
PluginFingerprint fingerprint2 = PluginFingerprint.from(project2, FORMATTERS);

assertThat(fingerprint1).isNotEqualTo(fingerprint2);
}

@Test
void differentFingerprintForDifferentPluginVersion() throws Exception {
String xml1 = createPomXmlContent(VERSION_1, EXECUTION_1, CONFIGURATION_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,51 @@ void enableUpToDateChecking() throws Exception {
assertFormatted(files);
}

@Test
void enableUpToDateCheckingWithPluginDependencies() throws Exception {
writePomWithPluginManagementAndDependency();

List<File> files = writeUnformattedFiles(1);
String output = runSpotlessApply();

assertThat(output).contains("Up-to-date checking enabled");
assertFormatted(files);
}

@Test
void enableUpToDateCheckingWithPluginDependenciesMaven3_6_3() throws Exception {
writePomWithPluginManagementAndDependency();

setFile(".mvn/wrapper/maven-wrapper.properties").toContent("distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip\n");

List<File> files = writeUnformattedFiles(1);
String output = runSpotlessApply();

assertThat(output).contains("Up-to-date checking enabled");
assertFormatted(files);
}

private void writePomWithPluginManagementAndDependency() throws IOException {
setFile("pom.xml").toContent(createPomXmlContent("/pom-test-management.xml.mustache",
null,
null,
new String[]{
"<java>",
" <googleJavaFormat/>",
"</java>",
"<upToDateChecking>",
" <enabled>true</enabled>",
"</upToDateChecking>"},
new String[]{
"<dependencies>",
" <dependency>",
" <groupId>javax.inject</groupId>",
" <artifactId>javax.inject</artifactId>",
" <version>1</version>",
" </dependency>",
"</dependencies>"}));
}

@Test
void disableUpToDateChecking() throws Exception {
writePomWithUpToDateCheckingEnabled(false);
Expand Down
47 changes: 47 additions & 0 deletions plugin-maven/src/test/resources/pom-test-management.xml.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin-tests</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>Spotless Maven Plugin Tests</name>

<!-- Require plugin to be tested with Maven 3.1.0+ -->
<prerequisites>
<maven>3.1.0</maven>
</prerequisites>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>{{spotlessMavenPluginVersion}}</version>
<configuration>
{{{configuration}}}
</configuration>
{{{dependencies}}}
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<executions>
{{{executions}}}
</executions>
</plugin>
</plugins>
</build>

</project>
1 change: 1 addition & 0 deletions plugin-maven/src/test/resources/pom-test.xml.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<executions>
{{{executions}}}
</executions>
{{{dependencies}}}
</plugin>
</plugins>
</build>
Expand Down

0 comments on commit eba1c59

Please sign in to comment.