This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-project-info-reports-plugin.git
commit 1aa7d4e0295c98d0c8d885a46025a1e760f04425 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Thu Nov 26 07:30:32 2026 +0000 Test report rendering with fixture jars and models Covers what the MPIR-242, -300, -407, -431, -437, -455, -460 and dependencies-java-1.7-and-1.8 integration tests assert, without a Maven build. --- .../report/projectinfo/DependenciesReportTest.java | 15 + .../DependencyInformationReportTest.java | 85 +++++ .../report/projectinfo/LicensesReportTest.java | 24 ++ .../report/projectinfo/PluginsReportTest.java | 135 +++++++ .../projectinfo/dependencies/DependencyNodes.java | 44 +++ .../renderer/DependenciesRendererTest.java | 393 +++++++++++++++++++++ .../dependencies/renderer/JarFixtures.java | 170 +++++++++ ...dependencies-plugin-config-license-mappings.xml | 47 +++ .../dependency-info-jar-plugin-config.xml | 41 +++ .../dependency-info-maven-plugin-plugin-config.xml | 41 +++ .../plugin-configs/licenses-encoding-LICENSE.txt | 18 + .../licenses-plugin-config-encoding.xml | 47 +++ .../plugin-configs/plugins-plugin-config.xml | 102 ++++++ 13 files changed, 1162 insertions(+) diff --git a/src/test/java/org/apache/maven/report/projectinfo/DependenciesReportTest.java b/src/test/java/org/apache/maven/report/projectinfo/DependenciesReportTest.java index 6590ec1..f7feec8 100644 --- a/src/test/java/org/apache/maven/report/projectinfo/DependenciesReportTest.java +++ b/src/test/java/org/apache/maven/report/projectinfo/DependenciesReportTest.java @@ -21,6 +21,8 @@ package org.apache.maven.report.projectinfo; import javax.inject.Inject; import java.net.URL; +import java.util.HashMap; +import java.util.Map; import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.TextBlock; @@ -118,4 +120,17 @@ class DependenciesReportTest extends AbstractProjectInfoTest { assertEquals(getString("report.dependencies.graph.tree.title"), textBlocks[9].getText()); assertEquals(getString("report.dependencies.graph.tables.licenses"), textBlocks[10].getText()); } + + /** + * MPIR-407: the licenseMappings configured in the POM reach the report as a map from each name to its target. + */ + @Test + @InjectMojo(goal = "dependencies", pom = "dependencies-plugin-config-license-mappings.xml") + void testLicenseMappingsAreReadFromTheConfiguration(DependenciesReport mojo) { + Map<String, String> expected = new HashMap<>(); + expected.put("The Apache Software License, Version 2.0", "Apache License, Version 2.0"); + expected.put("Apache Public License 2.0", "Apache License, Version 2.0"); + + assertEquals(expected, mojo.getLicenseMappings()); + } } diff --git a/src/test/java/org/apache/maven/report/projectinfo/DependencyInformationReportTest.java b/src/test/java/org/apache/maven/report/projectinfo/DependencyInformationReportTest.java new file mode 100644 index 0000000..0e7d7ae --- /dev/null +++ b/src/test/java/org/apache/maven/report/projectinfo/DependencyInformationReportTest.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.report.projectinfo; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +import org.apache.maven.api.plugin.testing.Basedir; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.junit.jupiter.api.Test; + +import static org.apache.maven.api.plugin.testing.MojoExtension.getTestFile; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Checks which coordinate snippets the dependency-info report offers for each packaging (MPIR-460). + */ +@MojoTest(realRepositorySession = true) +@Basedir("/plugin-configs") +class DependencyInformationReportTest extends AbstractProjectInfoTest { + + private static final String[] DEPENDENCY_TOOLS = { + "Apache Ivy", "Groovy Grape", "Gradle/Grails", "Scala SBT", "Leiningen" + }; + + @Test + @InjectMojo(goal = "dependency-info", pom = "dependency-info-jar-plugin-config.xml") + void jarPackagingOffersDependencySnippetsForEveryTool(DependencyInformationReport mojo) throws Exception { + readMavenProjectModel(mavenProject, "dependency-info-jar-plugin-config.xml"); + mojo.execute(); + + String report = read("target/dependency-info-jar/dependency-info.html"); + + assertFalse(report.contains("<pre><plugin>")); + assertFalse(report.contains("</plugin></pre>")); + + assertTrue(report.contains("<pre><dependency>")); + assertTrue(report.contains("</dependency></pre>")); + assertTrue(report.contains("<artifactId>dependency-info-jar</artifactId>")); + for (String tool : DEPENDENCY_TOOLS) { + assertTrue(report.contains(tool), tool); + } + } + + @Test + @InjectMojo(goal = "dependency-info", pom = "dependency-info-maven-plugin-plugin-config.xml") + void mavenPluginPackagingOffersOnlyThePluginSnippet(DependencyInformationReport mojo) throws Exception { + readMavenProjectModel(mavenProject, "dependency-info-maven-plugin-plugin-config.xml"); + mojo.execute(); + + String report = read("target/dependency-info-maven-plugin/dependency-info.html"); + + assertTrue(report.contains("<pre><plugin>")); + assertTrue(report.contains("</plugin></pre>")); + assertTrue(report.contains("<artifactId>dependency-info-maven-plugin</artifactId>")); + + assertFalse(report.contains("<pre><dependency>")); + assertFalse(report.contains("</dependency></pre>")); + for (String tool : DEPENDENCY_TOOLS) { + assertFalse(report.contains(tool), tool); + } + } + + private static String read(String path) throws Exception { + return new String(Files.readAllBytes(getTestFile(path).toPath()), StandardCharsets.UTF_8); + } +} diff --git a/src/test/java/org/apache/maven/report/projectinfo/LicensesReportTest.java b/src/test/java/org/apache/maven/report/projectinfo/LicensesReportTest.java index 0f9e69f..cfb6aa4 100644 --- a/src/test/java/org/apache/maven/report/projectinfo/LicensesReportTest.java +++ b/src/test/java/org/apache/maven/report/projectinfo/LicensesReportTest.java @@ -22,6 +22,8 @@ import javax.inject.Inject; import java.io.File; import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -202,4 +204,26 @@ class LicensesReportTest extends AbstractProjectInfoTest { ((String) cache.get(expectedKey)).contains("Local license text for MPIR-586"), "cached text should be the license file content"); } + + /** + * MPIR-242: a license file in a non-UTF-8 encoding is decoded with licenseFileEncoding. Doxia writes the Cyrillic + * text as character references, so the report has the same characters whatever the platform encoding is. + */ + @Test + @InjectMojo(goal = "licenses", pom = "licenses-plugin-config-encoding.xml") + void testLicenseFileEncodingIsHonoured(LicensesReport mojo) throws Exception { + readMavenProjectModel(mavenProject, "licenses-plugin-config-encoding.xml"); + + mojo.execute(); + + String html = new String( + Files.readAllBytes( + getTestFile("target/licenses-encoding/licenses.html").toPath()), + StandardCharsets.UTF_8); + // "Свобода всему коду!" + assertTrue( + html.contains("Свобода " + + "всему коду!"), + html); + } } diff --git a/src/test/java/org/apache/maven/report/projectinfo/PluginsReportTest.java b/src/test/java/org/apache/maven/report/projectinfo/PluginsReportTest.java new file mode 100644 index 0000000..c563bb4 --- /dev/null +++ b/src/test/java/org/apache/maven/report/projectinfo/PluginsReportTest.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.report.projectinfo; + +import javax.inject.Inject; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +import org.apache.maven.api.di.Provides; +import org.apache.maven.api.plugin.testing.Basedir; +import org.apache.maven.api.plugin.testing.InjectMojo; +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.apache.maven.project.DefaultProjectBuildingRequest; +import org.apache.maven.project.ProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.project.ProjectBuildingResult; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.stubbing.Answer; + +import static org.apache.maven.api.plugin.testing.MojoExtension.getTestFile; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.isA; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +/** + * Checks the plugin versions listed by the plugins report. + */ +@MojoTest(realRepositorySession = true) +@Basedir("/plugin-configs") +class PluginsReportTest extends AbstractProjectInfoTest { + + @Inject + private MavenSession mavenSession; + + /** + * Echoes the requested coordinates back as the plugin's own project, so the version the report renders is the + * one it resolved for the plugin. + */ + @Provides + @SuppressWarnings("unused") + private ProjectBuilder projectBuilderProvider() throws ProjectBuildingException { + ProjectBuilder builder = mock(ProjectBuilder.class); + when(builder.build(isA(Artifact.class), isA(ProjectBuildingRequest.class))) + .thenAnswer((Answer<ProjectBuildingResult>) + invocation -> createProjectBuildingResult(invocation.getArgument(0))); + return builder; + } + + @BeforeEach + void setup() { + DefaultProjectBuildingRequest pbr = spy(new DefaultProjectBuildingRequest()); + doAnswer(__ -> mavenSession.getRepositorySession()).when(pbr).getRepositorySession(); + when(mavenSession.getProjectBuildingRequest()).thenReturn(pbr); + } + + /** + * MPIR-300: a report plugin without a version takes it from build/plugins, then from build/pluginManagement; a + * version in the reporting section wins over both. + */ + @Test + @InjectMojo(goal = "plugins", pom = "plugins-plugin-config.xml") + void reportPluginVersionComesFromReportingThenPluginsThenPluginManagement(PluginsReport mojo) throws Exception { + readMavenProjectModel(mavenProject, "plugins-plugin-config.xml"); + mojo.execute(); + + String html = new String( + Files.readAllBytes(getTestFile("target/plugins/plugins.html").toPath()), StandardCharsets.UTF_8); + + int reportsStart = html.indexOf(getString("report.plugins.report.title")); + assertTrue(reportsStart > html.indexOf(getString("report.plugins.build.title")), "report section comes last"); + String reports = html.substring(reportsStart); + + // pluginManagement only + assertRow(reports, "maven-invoker-plugin", "1.invoker-pluginManagement"); + // build/plugins overrides pluginManagement + assertRow(reports, "maven-javadoc-plugin", "2.javadoc-plugin"); + // build/plugins only + assertRow(reports, "maven-checkstyle-plugin", "2.checkstyle-plugin"); + // reporting overrides build/plugins and pluginManagement + assertRow(reports, "maven-changes-plugin", "3.changes-reporting"); + // defined nowhere + assertRow(reports, "maven-jxr-plugin", Artifact.RELEASE_VERSION); + + assertFalse(reports.contains("1.javadoc-pluginManagement")); + assertFalse(reports.contains("2.changes-plugin")); + } + + private static void assertRow(String html, String artifactId, String version) { + String row = "<td>" + artifactId + "</td>"; + int start = html.indexOf(row); + assertTrue(start >= 0, "no row for " + artifactId); + int end = html.indexOf("</tr>", start); + assertTrue( + html.substring(start, end).contains("<td>" + version + "</td>"), + artifactId + " should be listed with version " + version + " but the row is " + + html.substring(start, end)); + } + + private static ProjectBuildingResult createProjectBuildingResult(Artifact artifact) { + ProjectBuildingResult result = mock(ProjectBuildingResult.class); + MavenProjectStub stub = new MavenProjectStub(); + stub.setGroupId(artifact.getGroupId()); + stub.setArtifactId(artifact.getArtifactId()); + stub.setVersion(artifact.getVersion()); + + when(result.getProject()).thenReturn(stub); + return result; + } +} diff --git a/src/test/java/org/apache/maven/report/projectinfo/dependencies/DependencyNodes.java b/src/test/java/org/apache/maven/report/projectinfo/dependencies/DependencyNodes.java new file mode 100644 index 0000000..f266379 --- /dev/null +++ b/src/test/java/org/apache/maven/report/projectinfo/dependencies/DependencyNodes.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.report.projectinfo.dependencies; + +import org.apache.maven.artifact.Artifact; + +/** + * Builds {@link DependencyNode} trees for tests; the node constructor is package-private. + */ +public final class DependencyNodes { + + private DependencyNodes() {} + + /** + * @param project the artifact of the project itself, the root of the tree + * @param children the direct dependencies of the project, each a leaf + * @return the root node + */ + public static DependencyNode tree(Artifact project, Artifact... children) { + DependencyNode root = new DependencyNode(null, project, false); + java.util.List<DependencyNode> nodes = new java.util.ArrayList<>(); + for (Artifact child : children) { + nodes.add(new DependencyNode(root, child, child.isOptional())); + } + root.setChildren(nodes); + return root; + } +} diff --git a/src/test/java/org/apache/maven/report/projectinfo/dependencies/renderer/DependenciesRendererTest.java b/src/test/java/org/apache/maven/report/projectinfo/dependencies/renderer/DependenciesRendererTest.java new file mode 100644 index 0000000..ef953e7 --- /dev/null +++ b/src/test/java/org/apache/maven/report/projectinfo/dependencies/renderer/DependenciesRendererTest.java @@ -0,0 +1,393 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.report.projectinfo.dependencies.renderer; + +import javax.inject.Inject; + +import java.io.File; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.regex.Pattern; + +import org.apache.maven.api.plugin.testing.MojoTest; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.apache.maven.doxia.sink.impl.Xhtml5BaseSink; +import org.apache.maven.model.License; +import org.apache.maven.plugin.logging.SystemStreamLog; +import org.apache.maven.project.MavenProject; +import org.apache.maven.report.projectinfo.dependencies.Dependencies; +import org.apache.maven.report.projectinfo.dependencies.DependenciesReportConfiguration; +import org.apache.maven.report.projectinfo.dependencies.DependencyNode; +import org.apache.maven.report.projectinfo.dependencies.DependencyNodes; +import org.apache.maven.report.projectinfo.dependencies.RepositoryUtils; +import org.apache.maven.report.projectinfo.dependencies.renderer.JarFixtures.Entry; +import org.apache.maven.shared.jar.classes.JarClassesAnalysis; +import org.codehaus.plexus.i18n.I18N; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Renders the dependencies report from fixture jars and checks the file details table and the license columns. The + * jars are real zip files with real class files, so the jar analysis runs too; nothing is resolved from a repository. + */ +@MojoTest +class DependenciesRendererTest { + + @Inject + private I18N i18n; + + @TempDir + private File tmp; + + private final Map<String, License> licenses = new HashMap<>(); + + private final Map<String, String> licenseMappings = new HashMap<>(); + + /** + * MPIR-431: the summary rows of the file details table. A row of totals, then one row per scope that has + * dependencies with each cell prefixed by the scope, and one Java version cell spanning the compile, runtime and + * provided rows. + */ + @Test + void fileDetailsSummaryHasOneRowPerScopeAndASharedJavaVersionCell() throws Exception { + Artifact compile = artifact("compile", "compile-dep", jar("compile.jar", 45_500, clazz("p/A", 47))); + Artifact runtime = artifact("runtime", "runtime-dep", jar("runtime.jar", 284_200, clazz("p/B", 50))); + Artifact provided = artifact("provided", "provided-dep", jar("provided.jar", 85_700, clazz("p/C", 49))); + Artifact test1 = artifact("test", "test-dep-1", jar("test1.jar", 1_000_100, clazz("p/D", 52))); + Artifact test2 = artifact("test", "test-dep-2", jar("test2.jar", 1_199_900, clazz("q/E", 51))); + + String html = render(true, compile, runtime, provided, test1, test2); + + assertRows( + html, + row(right("5"), right("2.6 MB"), right("10"), right("5"), right("5"), center("1.8"), right("-")), + row( + right("compile: 1"), + right("compile: 45.5 kB"), + right("compile: 2"), + right("compile: 1"), + right("compile: 1"), + "<td rowspan=\"3\" style=\"text-align: center; vertical-align: middle;\">1.6</td>", + right("-")), + row( + right("runtime: 1"), + right("runtime: 284.2 kB"), + right("runtime: 2"), + right("runtime: 1"), + right("runtime: 1"), + right("-")), + row( + right("provided: 1"), + right("provided: 85.7 kB"), + right("provided: 2"), + right("provided: 1"), + right("provided: 1"), + right("-")), + row( + right("test: 2"), + right("test: 2.2 MB"), + right("test: 4"), + right("test: 2"), + right("test: 2"), + center("1.8"), + right("-"))); + } + + /** + * MPIR-437: a jar without any class has no Java version; the cell, like the debug information cell, shows a dash + * rather than staying empty. The totals show a dash where there is nothing to count. + */ + @Test + void jarWithoutClassesShowsDashesForJavaVersion() throws Exception { + Artifact resources = artifact( + "compile", + "resources-only", + jar( + "resources-only.jar", + 78_800, + Entry.resource("messages_de.properties"), + Entry.resource("messages_fr.properties"), + Entry.resource("messages_it.properties"))); + + String html = render(true, resources); + + // file row: no Java version and no debug information to report + assertRows( + html, + row( + left("resources-only.jar"), + right("78.8 kB"), + right("4"), + right("0"), + right("0"), + center("-"), + center("-"))); + // totals row: nothing to total for classes and packages + assertRows( + html, row(right("1"), right("78.8 kB"), right("4"), right("-"), right("-"), center("-"), right("-"))); + } + + /** + * MPIR-455: a multi-release jar is one row for the jar, then one for the root content and one per versioned + * runtime, each with its own entry and class counts and Java version. A jar that has only a module descriptor in + * a versioned directory has no classes at the root. + */ + @Test + void multiReleaseJarListsRootAndVersionedRuntimes() throws Exception { + String bullet = "   • "; + Artifact moduleInfoOnly = artifact( + "runtime", + "module-info-only", + jar( + "module-info-only.jar", + 3_300, + Entry.manifest("Multi-Release: true"), + Entry.resource("README.txt"), + Entry.moduleInfo("META-INF/versions/11/module-info.class", 55))); + Artifact multiRelease = artifact( + "test", + "multi-release", + jar( + "multi-release.jar", + 10_200, + Entry.manifest("Multi-Release: true"), + clazz("p/R", 52), + Entry.classFile("META-INF/versions/9/p/R.class", "p/R", 53), + Entry.classFile("META-INF/versions/11/p/R.class", "p/R", 55))); + + String html = render(true, moduleInfoOnly, multiRelease); + + assertRows( + html, + row( + left("module-info-only.jar"), + right("3.3 kB"), + right("4"), + right("-"), + right("-"), + center("-"), + center("-")), + row(left(bullet + "Root"), right("-"), right("3"), right("0"), right("0"), center("-"), center("-")), + row( + left(bullet + "Versioned"), + right("-"), + right("1"), + right("1"), + right("1"), + center("11"), + center("No")), + row( + left("multi-release.jar"), + right("10.2 kB"), + right("5"), + right("-"), + right("-"), + center("-"), + center("-")), + row(left(bullet + "Root"), right("-"), right("3"), right("1"), right("1"), center("1.8"), center("No")), + row( + left(bullet + "Versioned"), + right("-"), + right("1"), + right("1"), + right("1"), + center("9"), + center("No")), + row( + left(bullet + "Versioned"), + right("-"), + right("1"), + right("1"), + right("1"), + center("11"), + center("No"))); + } + + /** The Java version column is the highest class file version of the jar: 1.7 for major 51, 1.8 for 52. */ + @Test + void javaVersionIsReadFromTheClassFiles() throws Exception { + Map<Integer, String> expected = new LinkedHashMap<>(); + expected.put(48, "1.4"); + expected.put(49, "1.5"); + expected.put(50, "1.6"); + expected.put(51, "1.7"); + expected.put(52, "1.8"); + expected.put(53, "9"); + expected.put(55, "11"); + + List<Artifact> artifacts = new ArrayList<>(); + for (int major : expected.keySet()) { + artifacts.add(artifact( + "compile", "dep" + major, jar("dep" + major + ".jar", 2_000, clazz("p/C" + major, major)))); + } + + String html = render(true, artifacts.toArray(new Artifact[0])); + + for (Map.Entry<Integer, String> e : expected.entrySet()) { + assertRows( + html, + row( + left("dep" + e.getKey() + ".jar"), + right("2 kB"), + right("2"), + right("1"), + right("1"), + center(e.getValue()), + center("No"))); + } + } + + /** + * MPIR-407: a license name that has a mapping is shown under the mapped name in the dependency tables, the + * dependency details and the license listing; one without a mapping is left alone. + */ + @Test + void licenseMappingsRenameLicensesEverywhere() throws Exception { + licenseMappings.put("The Apache Software License, Version 2.0", "Apache License, Version 2.0"); + licenseMappings.put("Apache Public License 2.0", "Apache License, Version 2.0"); + licenses.put("old-name", license("The Apache Software License, Version 2.0")); + licenses.put("other-old-name", license("Apache Public License 2.0")); + licenses.put("unmapped", license("MIT License")); + + String html = render( + false, + artifact("compile", "old-name", null), + artifact("compile", "other-old-name", null), + artifact("compile", "unmapped", null)); + + assertFalse(html.contains("The Apache Software License, Version 2.0"), "mapped name is gone"); + assertFalse(html.contains("Apache Public License 2.0"), "mapped name is gone"); + assertTrue(html.contains(">Apache License, Version 2.0</a>"), "dependency table and details use the new name"); + assertTrue(html.contains(">MIT License</a>"), "an unmapped license is kept"); + // the listing groups the three artifacts by license: both mapped ones under one name + assertTrue( + Pattern.compile("Apache License, Version 2.0: </b>other-old-name, old-name|" + + "Apache License, Version 2.0: </b>old-name, other-old-name") + .matcher(html.replaceAll("\\s+", " ")) + .find(), + "both artifacts are listed under the mapped license"); + } + + private static License license(String name) { + License license = new License(); + license.setName(name); + license.setUrl("https://example.org/" + name.replace(' ', '-')); + return license; + } + + private File jar(String name, long length, Entry... entries) throws Exception { + return JarFixtures.jar(tmp, name, length, entries); + } + + private Artifact artifact(String scope, String artifactId, File file) { + Artifact artifact = new DefaultArtifact( + "org.example", artifactId, "1.0", scope, "jar", null, new DefaultArtifactHandler("jar")); + artifact.setFile(file); + return artifact; + } + + private static Entry clazz(String internalName, int major) throws Exception { + return Entry.classFile(internalName + ".class", internalName, major); + } + + private String render(boolean fileDetails, Artifact... dependencies) throws Exception { + Artifact projectArtifact = new DefaultArtifact( + "org.example", "project", "1.0", null, "pom", null, new DefaultArtifactHandler("pom")); + MavenProject project = new MavenProject(); + project.setGroupId("org.example"); + project.setArtifactId("project"); + project.setVersion("1.0"); + + DependencyNode tree = DependencyNodes.tree(projectArtifact, dependencies); + Dependencies deps = new Dependencies(project, tree, new JarClassesAnalysis()); + + RepositoryUtils repoUtils = mock(RepositoryUtils.class); + when(repoUtils.getMavenProjectFromRepository(any(Artifact.class))).thenAnswer(invocation -> { + Artifact artifact = invocation.getArgument(0); + MavenProject artifactProject = new MavenProject(); + artifactProject.setName(artifact.getArtifactId()); + License license = licenses.get(artifact.getArtifactId()); + if (license != null) { + artifactProject.setLicenses(Collections.singletonList(license)); + } + return artifactProject; + }); + + StringWriter out = new StringWriter(); + DependenciesRenderer renderer = new DependenciesRenderer( + new Xhtml5BaseSink(out), + Locale.ENGLISH, + i18n, + new SystemStreamLog(), + deps, + tree, + new DependenciesReportConfiguration(fileDetails), + repoUtils, + licenseMappings); + renderer.render(); + return out.toString().replace("\r\n", "\n"); + } + + private static String cell(String align, String text) { + return "<td style=\"text-align: " + align + ";\">" + text + "</td>"; + } + + private static String left(String text) { + return cell("left", text); + } + + private static String right(String text) { + return cell("right", text); + } + + private static String center(String text) { + return cell("center", text); + } + + private static String row(String... cells) { + return String.join("\n", cells) + "</tr>"; + } + + /** Asserts that the rows occur one after the other in the report. */ + private static void assertRows(String html, String... rows) { + StringBuilder regex = new StringBuilder(); + for (String row : rows) { + if (regex.length() > 0) { + regex.append("\n"); + } + regex.append("<tr class=\"[ab]\">\n").append(Pattern.quote(row)); + } + assertTrue( + Pattern.compile(regex.toString()).matcher(html).find(), "rows not found:\n" + String.join("\n", rows)); + } +} diff --git a/src/test/java/org/apache/maven/report/projectinfo/dependencies/renderer/JarFixtures.java b/src/test/java/org/apache/maven/report/projectinfo/dependencies/renderer/JarFixtures.java new file mode 100644 index 0000000..4b1a700 --- /dev/null +++ b/src/test/java/org/apache/maven/report/projectinfo/dependencies/renderer/JarFixtures.java @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.report.projectinfo.dependencies.renderer; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.CRC32; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * Writes small jars with real class files, so the jar analysis behind the dependencies report runs on them. + */ +final class JarFixtures { + + private JarFixtures() {} + + /** A jar entry: a class file of the given class file major version, or plain bytes. */ + static final class Entry { + final String name; + final byte[] content; + + private Entry(String name, byte[] content) { + this.name = name; + this.content = content; + } + + /** A class file; {@code internalName} is e.g. {@code p/Foo}, {@code major} 52 is Java 8. */ + static Entry classFile(String path, String internalName, int major) throws IOException { + return new Entry(path, classBytes(internalName, major, false)); + } + + /** A {@code module-info.class}. */ + static Entry moduleInfo(String path, int major) throws IOException { + return new Entry(path, classBytes("module-info", major, true)); + } + + static Entry manifest(String... lines) { + StringBuilder sb = new StringBuilder("Manifest-Version: 1.0\r\n"); + for (String line : lines) { + sb.append(line).append("\r\n"); + } + return new Entry("META-INF/MANIFEST.MF", sb.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8)); + } + + static Entry resource(String path) { + return new Entry(path, new byte[] {1, 2, 3}); + } + } + + /** + * Writes a jar of exactly {@code length} bytes: the entries plus a stored padding entry sized to fit. + */ + static File jar(File dir, String fileName, long length, Entry... entries) throws IOException { + File file = new File(dir, fileName); + write(file, 0, entries); + long base = file.length(); + if (base > length) { + throw new IllegalArgumentException("a jar of the entries alone is " + base + " bytes, more than " + length); + } + write(file, (int) (length - base), entries); + if (file.length() != length) { + throw new IllegalStateException("padding did not give " + length + " bytes but " + file.length()); + } + return file; + } + + private static void write(File file, int padding, Entry... entries) throws IOException { + try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file))) { + for (Entry entry : entries) { + ZipEntry zipEntry = new ZipEntry(entry.name); + zipEntry.setTime(0); + zip.putNextEntry(zipEntry); + zip.write(entry.content); + zip.closeEntry(); + } + byte[] pad = new byte[padding]; + CRC32 crc = new CRC32(); + crc.update(pad); + ZipEntry stored = new ZipEntry("pad.bin"); + stored.setTime(0); + stored.setMethod(ZipEntry.STORED); + stored.setSize(pad.length); + stored.setCompressedSize(pad.length); + stored.setCrc(crc.getValue()); + zip.putNextEntry(stored); + zip.write(pad); + zip.closeEntry(); + } + } + + /** The smallest valid class file: no fields, no methods, only a name and {@code java/lang/Object}. */ + private static byte[] classBytes(String internalName, int major, boolean module) throws IOException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(bytes); + out.writeInt(0xCAFEBABE); + out.writeShort(0); + out.writeShort(major); + if (module) { + // 1 Utf8 module-info, 2 Class, 3 Utf8 Module, 4 Utf8 m, 5 Module + out.writeShort(6); + out.writeByte(1); + out.writeUTF("module-info"); + out.writeByte(7); + out.writeShort(1); + out.writeByte(1); + out.writeUTF("Module"); + out.writeByte(1); + out.writeUTF("m"); + out.writeByte(19); + out.writeShort(4); + out.writeShort(0x8000); // ACC_MODULE + out.writeShort(2); // this_class + out.writeShort(0); // super_class + out.writeShort(0); // interfaces + out.writeShort(0); // fields + out.writeShort(0); // methods + out.writeShort(1); // attributes: Module + out.writeShort(3); + out.writeInt(16); + out.writeShort(5); // module_name_index + out.writeShort(0); // flags + out.writeShort(0); // version + out.writeShort(0); // requires + out.writeShort(0); // exports + out.writeShort(0); // opens + out.writeShort(0); // uses + out.writeShort(0); // provides + } else { + // 1 Utf8 name, 2 Class, 3 Utf8 java/lang/Object, 4 Class + out.writeShort(5); + out.writeByte(1); + out.writeUTF(internalName); + out.writeByte(7); + out.writeShort(1); + out.writeByte(1); + out.writeUTF("java/lang/Object"); + out.writeByte(7); + out.writeShort(3); + out.writeShort(0x0021); // public super + out.writeShort(2); // this_class + out.writeShort(4); // super_class + out.writeShort(0); // interfaces + out.writeShort(0); // fields + out.writeShort(0); // methods + out.writeShort(0); // attributes + } + out.flush(); + return bytes.toByteArray(); + } +} diff --git a/src/test/resources/plugin-configs/dependencies-plugin-config-license-mappings.xml b/src/test/resources/plugin-configs/dependencies-plugin-config-license-mappings.xml new file mode 100644 index 0000000..7458ffe --- /dev/null +++ b/src/test/resources/plugin-configs/dependencies-plugin-config-license-mappings.xml @@ -0,0 +1,47 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<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>org.apache.maven.plugin.projectinfo.tests</groupId> + <artifactId>dependencies-license-mappings</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + <name>dependencies project info</name> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-project-info-reports-plugin</artifactId> + <configuration> + <outputDirectory>target/dependencies-license-mappings</outputDirectory> + <licenseMappings> + <licenseMapping> + <froms> + <from>The Apache Software License, Version 2.0</from> + <from>Apache Public License 2.0</from> + </froms> + <to>Apache License, Version 2.0</to> + </licenseMapping> + </licenseMappings> + </configuration> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file diff --git a/src/test/resources/plugin-configs/dependency-info-jar-plugin-config.xml b/src/test/resources/plugin-configs/dependency-info-jar-plugin-config.xml new file mode 100644 index 0000000..42092ca --- /dev/null +++ b/src/test/resources/plugin-configs/dependency-info-jar-plugin-config.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<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>org.apache.maven.plugin.projectinfo.tests</groupId> + <artifactId>dependency-info-jar</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + <name>dependency-info jar project info</name> + <build> + <plugins> + <plugin> + <artifactId>maven-project-info-reports-plugin</artifactId> + <configuration> + <outputDirectory>target/dependency-info-jar</outputDirectory> + <groupId>org.apache.maven.plugin.projectinfo.tests</groupId> + <artifactId>dependency-info-jar</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/src/test/resources/plugin-configs/dependency-info-maven-plugin-plugin-config.xml b/src/test/resources/plugin-configs/dependency-info-maven-plugin-plugin-config.xml new file mode 100644 index 0000000..c61b77f --- /dev/null +++ b/src/test/resources/plugin-configs/dependency-info-maven-plugin-plugin-config.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<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>org.apache.maven.plugin.projectinfo.tests</groupId> + <artifactId>dependency-info-maven-plugin</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + <name>dependency-info maven-plugin project info</name> + <build> + <plugins> + <plugin> + <artifactId>maven-project-info-reports-plugin</artifactId> + <configuration> + <outputDirectory>target/dependency-info-maven-plugin</outputDirectory> + <groupId>org.apache.maven.plugin.projectinfo.tests</groupId> + <artifactId>dependency-info-maven-plugin</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>maven-plugin</packaging> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/src/test/resources/plugin-configs/licenses-encoding-LICENSE.txt b/src/test/resources/plugin-configs/licenses-encoding-LICENSE.txt new file mode 100644 index 0000000..d8ed8c9 --- /dev/null +++ b/src/test/resources/plugin-configs/licenses-encoding-LICENSE.txt @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +������� ����� ����! diff --git a/src/test/resources/plugin-configs/licenses-plugin-config-encoding.xml b/src/test/resources/plugin-configs/licenses-plugin-config-encoding.xml new file mode 100644 index 0000000..b1a45a5 --- /dev/null +++ b/src/test/resources/plugin-configs/licenses-plugin-config-encoding.xml @@ -0,0 +1,47 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +<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>org.apache.maven.plugin.projectinfo.tests</groupId> + <artifactId>licenses-encoding</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + <name>licenses project info</name> + <url>http://maven.apache.org</url> + <licenses> + <license> + <name>The Apache Software License, Version 2.0</name> + <url>licenses-encoding-LICENSE.txt</url> + <distribution>repo</distribution> + </license> + </licenses> + <build> + <plugins> + <plugin> + <artifactId>maven-project-info-reports-plugin</artifactId> + <configuration> + <outputDirectory>target/licenses-encoding</outputDirectory> + <offline>false</offline> + <licenseFileEncoding>ISO-8859-5</licenseFileEncoding> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/src/test/resources/plugin-configs/plugins-plugin-config.xml b/src/test/resources/plugin-configs/plugins-plugin-config.xml new file mode 100644 index 0000000..f4ea0e1 --- /dev/null +++ b/src/test/resources/plugin-configs/plugins-plugin-config.xml @@ -0,0 +1,102 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> +<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>org.apache.maven.plugin.projectinfo.tests</groupId> + <artifactId>plugins-report-versions</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>pom</packaging> + <name>plugins project info</name> + <description>MPIR-300: the version of a report plugin without one comes from build/plugins, then from + build/pluginManagement; a version in reporting wins over both.</description> + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-invoker-plugin</artifactId> + <version>1.invoker-pluginManagement</version> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>1.javadoc-pluginManagement</version> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-changes-plugin</artifactId> + <version>1.changes-pluginManagement</version> + </plugin> + </plugins> + </pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-project-info-reports-plugin</artifactId> + <configuration> + <outputDirectory>target/plugins</outputDirectory> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.javadoc-plugin</version> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + <version>2.checkstyle-plugin</version> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-changes-plugin</artifactId> + <version>2.changes-plugin</version> + </plugin> + </plugins> + </build> + <reporting> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-invoker-plugin</artifactId> + <!-- version comes from pluginManagement --> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <!-- version comes from build/plugins, overriding pluginManagement --> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-checkstyle-plugin</artifactId> + <!-- version comes from build/plugins --> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-changes-plugin</artifactId> + <version>3.changes-reporting</version> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jxr-plugin</artifactId> + <!-- no version defined anywhere --> + </plugin> + </plugins> + </reporting> +</project>
