From c110f26f16ac654522817bee16f63bdd61110370 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Wed, 8 Jan 2025 14:42:38 +0100 Subject: [PATCH 1/2] feat: Added a test to specify licenseSet includes from CLI This one fails --- .config/checkstyle-suppressions.xml | 2 +- .../license-plugin-report.xml | 2 +- .../invoker.properties | 1 + .../mock-license-1.txt | 2 + .../mock-license-2.txt | 2 + .../mock-license-3.txt | 2 + .../it/tri-license-set-with-includes/pom.xml | 59 +++++++++++++++++++ .../setup.groovy | 23 ++++++++ .../main/java/com/mycila/it/Unformatted1.java | 4 ++ .../main/java/com/mycila/it/Unformatted2.java | 4 ++ .../main/java/com/mycila/it/Unformatted3.java | 4 ++ .../verify.groovy | 54 +++++++++++++++++ .../plugin/license/AbstractLicenseMojo.java | 2 +- .../maven/plugin/license/LicenseSet.java | 4 ++ pom.xml | 1 + 15 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/invoker.properties create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-1.txt create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-2.txt create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-3.txt create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/pom.xml create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/setup.groovy create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted1.java create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted2.java create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted3.java create mode 100644 license-maven-plugin/src/it/tri-license-set-with-includes/verify.groovy diff --git a/.config/checkstyle-suppressions.xml b/.config/checkstyle-suppressions.xml index 4204b8117..c52b7d97a 100644 --- a/.config/checkstyle-suppressions.xml +++ b/.config/checkstyle-suppressions.xml @@ -13,4 +13,4 @@ - \ No newline at end of file + diff --git a/docs/reports/5.0.0-SNAPSHOT/license-maven-plugin/license-plugin-report.xml b/docs/reports/5.0.0-SNAPSHOT/license-maven-plugin/license-plugin-report.xml index c10ec864f..218deefdc 100644 --- a/docs/reports/5.0.0-SNAPSHOT/license-maven-plugin/license-plugin-report.xml +++ b/docs/reports/5.0.0-SNAPSHOT/license-maven-plugin/license-plugin-report.xml @@ -1,5 +1,5 @@ - + diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/invoker.properties b/license-maven-plugin/src/it/tri-license-set-with-includes/invoker.properties new file mode 100644 index 000000000..e5573eedb --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/invoker.properties @@ -0,0 +1 @@ +invoker.goals=license:format -Dlicense.licenseSets.licenseSet.includes=src/main/java/com/mycila/it/Unformatted2.java diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-1.txt b/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-1.txt new file mode 100644 index 000000000..cd4a9d5f0 --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-1.txt @@ -0,0 +1,2 @@ +This is the 1st +mock license \ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-2.txt b/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-2.txt new file mode 100644 index 000000000..891e9697e --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-2.txt @@ -0,0 +1,2 @@ +This is the 2nd +mock license \ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-3.txt b/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-3.txt new file mode 100644 index 000000000..4a01d0771 --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/mock-license-3.txt @@ -0,0 +1,2 @@ +This is the 3rd +mock license \ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/pom.xml b/license-maven-plugin/src/it/tri-license-set-with-includes/pom.xml new file mode 100644 index 000000000..6a3f5e9df --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/pom.xml @@ -0,0 +1,59 @@ + + 4.0.0 + + com.mycila.license-maven-plugin.it + tri-license-set + 1.0.0 + + Check a Triple License Set + Integration Test for checking a triple license set + + + + + com.mycila + license-maven-plugin + @project.version@ + + + +
mock-license-1.txt
+ + invoker.properties + pom.xml + *.groovy + **/*.bak + *.log + mock-license-* + **/Unformatted2.java + **/Unformatted3.java + +
+ +
mock-license-2.txt
+ + invoker.properties + pom.xml + *.groovy + **/*.bak + *.log + mock-license-* + **/Unformatted1.java + **/Unformatted3.java + +
+ +
mock-license-3.txt
+ + **/Unformatted3.java + +
+
+
+
+
+
+ +
\ No newline at end of file diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/setup.groovy b/license-maven-plugin/src/it/tri-license-set-with-includes/setup.groovy new file mode 100644 index 000000000..43586ed3f --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/setup.groovy @@ -0,0 +1,23 @@ +import java.nio.file.Files +import java.nio.file.Path + +final Path base = basedir.toPath() + +if (!Files.exists(base) || !Files.isDirectory(base)) { + System.err.println("base directory is missing.") + return false +} + +final List ALL_FILES = Arrays.asList("Unformatted1.java", "Unformatted2.java", "Unformatted3.java") + +for (final String filename : ALL_FILES) { + final Path unformattedJavaFile = base.resolve("src/main/java/com/mycila/it/" + filename) + if (!Files.exists(unformattedJavaFile)) { + System.err.println(filename + " file is missing.") + return false + } + + Files.copy(unformattedJavaFile, unformattedJavaFile.resolveSibling(filename + ".bak")) +} + +return true; diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted1.java b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted1.java new file mode 100644 index 000000000..53c628eba --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted1.java @@ -0,0 +1,4 @@ +package com.mycila.it; + +public class Unformatted1 { +} diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted2.java b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted2.java new file mode 100644 index 000000000..2c4311d3c --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted2.java @@ -0,0 +1,4 @@ +package com.mycila.it; + +public class Unformatted2 { +} diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted3.java b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted3.java new file mode 100644 index 000000000..d24e2823e --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted3.java @@ -0,0 +1,4 @@ +package com.mycila.it; + +public class Unformatted3 { +} diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/verify.groovy b/license-maven-plugin/src/it/tri-license-set-with-includes/verify.groovy new file mode 100644 index 000000000..4e4904938 --- /dev/null +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/verify.groovy @@ -0,0 +1,54 @@ +import java.nio.file.Files +import java.nio.file.Path + +import static org.junit.jupiter.api.Assertions.assertEquals + + +final Path base = basedir.toPath() + +if (!Files.exists(base) || !Files.isDirectory(base)) { + System.err.println("base directory is missing.") + return false +} + +for (int i = 1; i <= 3; i++) { + + final Path license = base.resolve("mock-license-" + i + ".txt") + if (!Files.exists(license)) { + System.err.println(license.getFileName() + " file is missing.") + return false + } + + final String filename = "Unformatted" + i + ".java" + + final Path unformattedJavaFile = base.resolve("src/main/java/com/mycila/it/" + filename + ".bak") + if (!Files.exists(unformattedJavaFile)) { + System.err.println(unformattedJavaFile.getFileName() + " file is missing (should have been created by setup.groovy).") + return false + } + + final Path formattedJavaFile = base.resolve("src/main/java/com/mycila/it/" + filename) + if (!Files.exists(formattedJavaFile)) { + System.err.println(formattedJavaFile.getFileName() + " file is missing.") + return false + } + + final StringBuilder expected = new StringBuilder(); + if (i == 2) { + expected.append("/*\n"); + license.withReader { reader -> + while ((line = reader.readLine()) != null) { + expected.append(" * ").append(line).append('\n') + } + } + expected.append(" */\n") + } + + expected.append(new String(Files.readAllBytes(unformattedJavaFile))) + + final String actual = new String(Files.readAllBytes(formattedJavaFile)) + + assertEquals(expected.toString(), actual) +} + +return true diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java index f21825c2a..e6d6bf4ad 100644 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/AbstractLicenseMojo.java @@ -84,7 +84,7 @@ public abstract class AbstractLicenseMojo extends AbstractMojo { private static final String[] DEFAULT_KEYWORDS = {"copyright"}; - @Parameter + @Parameter(property = "license.licenseSets", alias = "licenseSets") public LicenseSet[] licenseSets; /** diff --git a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/LicenseSet.java b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/LicenseSet.java index 94c097be2..6c3fe12af 100644 --- a/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/LicenseSet.java +++ b/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/LicenseSet.java @@ -126,4 +126,8 @@ public class LicenseSet { @Parameter(property = "license.useDefaultExcludes") public Boolean useDefaultExcludes; + @Override + public String toString() { + return header; + } } diff --git a/pom.xml b/pom.xml index c6edaf271..d0e177e87 100644 --- a/pom.xml +++ b/pom.xml @@ -421,6 +421,7 @@ limitations under the License. .config/** **/*.checkstyle + .vscode/** From c9a00210ba28b5ac11b2c5e7f8a7a9b18b0ba754 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Sat, 11 Jan 2025 17:48:02 +0100 Subject: [PATCH 2/2] Fix initial states and invoker command --- .../src/it/tri-license-set-with-includes/invoker.properties | 2 +- .../src/main/java/com/mycila/it/Unformatted1.java | 4 ++++ .../src/main/java/com/mycila/it/Unformatted3.java | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/invoker.properties b/license-maven-plugin/src/it/tri-license-set-with-includes/invoker.properties index e5573eedb..16a894bde 100644 --- a/license-maven-plugin/src/it/tri-license-set-with-includes/invoker.properties +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/invoker.properties @@ -1 +1 @@ -invoker.goals=license:format -Dlicense.licenseSets.licenseSet.includes=src/main/java/com/mycila/it/Unformatted2.java +invoker.goals=license:format -Dlicense.licenseSets[0].includes=src/main/java/com/mycila/it/Unformatted2.java -Dlicense.licenseSets[1].includes=src/main/java/com/mycila/it/Unformatted2.java -Dlicense.licenseSets[2].includes=src/main/java/com/mycila/it/Unformatted2.java diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted1.java b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted1.java index 53c628eba..6081962c4 100644 --- a/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted1.java +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted1.java @@ -1,3 +1,7 @@ +/* + * This is the 1st + * mock license + */ package com.mycila.it; public class Unformatted1 { diff --git a/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted3.java b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted3.java index d24e2823e..87e5101b3 100644 --- a/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted3.java +++ b/license-maven-plugin/src/it/tri-license-set-with-includes/src/main/java/com/mycila/it/Unformatted3.java @@ -1,3 +1,7 @@ +/* + * This is the 3rd + * mock license + */ package com.mycila.it; public class Unformatted3 {