From 82ea24458afcab14f2cbbbc165d64114b7454640 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Tue, 10 Aug 2021 12:16:44 +0200 Subject: [PATCH] [MPIR-404] Warn and accept invalid mailing list links This closes #28 --- .../projectinfo/MailingListsReport.java | 27 ++++++--- .../projectinfo/MailingListsReportTest.java | 12 ++++ .../stubs/MailingListsInvalidLinkStub.java | 30 ++++++++++ ...ailing-lists-plugin-config-invalidlink.xml | 55 +++++++++++++++++++ 4 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/apache/maven/report/projectinfo/stubs/MailingListsInvalidLinkStub.java create mode 100644 src/test/resources/plugin-configs/mailing-lists-plugin-config-invalidlink.xml diff --git a/src/main/java/org/apache/maven/report/projectinfo/MailingListsReport.java b/src/main/java/org/apache/maven/report/projectinfo/MailingListsReport.java index f0fef47f..6e138d9f 100644 --- a/src/main/java/org/apache/maven/report/projectinfo/MailingListsReport.java +++ b/src/main/java/org/apache/maven/report/projectinfo/MailingListsReport.java @@ -22,6 +22,7 @@ import org.apache.maven.doxia.sink.Sink; import org.apache.maven.model.MailingList; import org.apache.maven.model.Model; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.codehaus.plexus.i18n.I18N; import org.codehaus.plexus.util.StringUtils; @@ -63,7 +64,7 @@ public boolean canGenerateReport() public void executeReport( Locale locale ) { MailingListsRenderer r = - new MailingListsRenderer( getSink(), getProject().getModel(), getI18N( locale ), locale ); + new MailingListsRenderer( getLog(), getSink(), getProject().getModel(), getI18N( locale ), locale ); r.render(); } @@ -92,13 +93,15 @@ protected String getI18Nsection() protected static class MailingListsRenderer extends AbstractProjectInfoRenderer { + + private final Log log; private final Model model; - MailingListsRenderer( Sink sink, Model model, I18N i18n, Locale locale ) + MailingListsRenderer( Log log, Sink sink, Model model, I18N i18n, Locale locale ) { super( sink, i18n, locale ); this.model = model; - + this.log = log; } @Override @@ -279,14 +282,22 @@ private String createURILinkPatternedText( String text, String href, String defa return createLinkPatternedText( text, defaultHref ); } - URI hrefUri = URI.create( href ); - if ( StringUtils.isNotEmpty( hrefUri.getScheme() ) ) + try { - return createLinkPatternedText( text, href ); + URI hrefUri = URI.create( href ); + if ( StringUtils.isNotEmpty( hrefUri.getScheme() ) ) + { + return createLinkPatternedText( text, href ); + } + else + { + return createLinkPatternedText( text, "mailto:" + href ); + } } - else + catch ( IllegalArgumentException e ) { - return createLinkPatternedText( text, "mailto:" + href ); + log.warn( "Invalid mailing list link provided '" + href + "': " + e.getMessage() ); + return href; } } } diff --git a/src/test/java/org/apache/maven/report/projectinfo/MailingListsReportTest.java b/src/test/java/org/apache/maven/report/projectinfo/MailingListsReportTest.java index 3437f88f..aba5387f 100644 --- a/src/test/java/org/apache/maven/report/projectinfo/MailingListsReportTest.java +++ b/src/test/java/org/apache/maven/report/projectinfo/MailingListsReportTest.java @@ -111,4 +111,16 @@ public void testFrenchReport() Locale.setDefault( oldLocale ); } } + + /** + * Test invalid links (MPIR-404) + * Those should only lead to a WARN but not an exception + * @throws Exception if any + */ + public void testInvalidLink() + throws Exception + { + generateReport( "mailing-lists", "mailing-lists-plugin-config-invalidlink.xml" ); + assertTrue( "Test html generated", getGeneratedReport( "mailing-lists.html" ).exists() ); + } } diff --git a/src/test/java/org/apache/maven/report/projectinfo/stubs/MailingListsInvalidLinkStub.java b/src/test/java/org/apache/maven/report/projectinfo/stubs/MailingListsInvalidLinkStub.java new file mode 100644 index 00000000..565edb1b --- /dev/null +++ b/src/test/java/org/apache/maven/report/projectinfo/stubs/MailingListsInvalidLinkStub.java @@ -0,0 +1,30 @@ +package org.apache.maven.report.projectinfo.stubs; + +/* + * 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. + */ + +public class MailingListsInvalidLinkStub + extends ProjectInfoProjectStub +{ + @Override + protected String getPOM() + { + return "mailing-lists-plugin-config-invalidlink.xml"; + } +} diff --git a/src/test/resources/plugin-configs/mailing-lists-plugin-config-invalidlink.xml b/src/test/resources/plugin-configs/mailing-lists-plugin-config-invalidlink.xml new file mode 100644 index 00000000..6f4cde9a --- /dev/null +++ b/src/test/resources/plugin-configs/mailing-lists-plugin-config-invalidlink.xml @@ -0,0 +1,55 @@ + + + + 4.0.0 + org.apache.maven.plugin.projectinfo.tests + mailing-lists + 1.0-SNAPSHOT + jar + mailing lists project info + + + junit + junit + 3.8.1 + test + + + + + Test List + + test at maven.apache.org + MAILTO:test-subscribe@maven.apache.org + + + + + + maven-project-info-reports-plugin + + target/test-harness/mailing-lists + ${localRepository} + + + + + +