Skip to content

Commit

Permalink
Demonstrator test for #1073 java.io.NotSerializableException with upT…
Browse files Browse the repository at this point in the history
…oDateChecking enabled
  • Loading branch information
tisoft committed Jan 7, 2022
1 parent bc9fc74 commit 59051ff
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 13 deletions.
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 59051ff

Please sign in to comment.