This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch MPIR-401
in repository 
https://gitbox.apache.org/repos/asf/maven-project-info-reports-plugin.git

commit 98db5e5289ca43147a8b4e37d8acffa8ef74e1c6
Author: Michael Osipov <micha...@apache.org>
AuthorDate: Sun Apr 25 18:23:42 2021 +0200

    [MPIR-401] Mailing list subscribe and unsubscribe links
---
 .../report/projectinfo/MailingListsReport.java     | 29 ++++++++++++++--------
 .../report/projectinfo/MailingListsReportTest.java | 15 ++++++++---
 .../plugin-configs/mailing-lists-plugin-config.xml |  8 +++++-
 3 files changed, 37 insertions(+), 15 deletions(-)

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 6c18f73..f0fef47 100644
--- a/src/main/java/org/apache/maven/report/projectinfo/MailingListsReport.java
+++ b/src/main/java/org/apache/maven/report/projectinfo/MailingListsReport.java
@@ -26,6 +26,7 @@ import org.apache.maven.plugins.annotations.Mojo;
 import org.codehaus.plexus.i18n.I18N;
 import org.codehaus.plexus.util.StringUtils;
 
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
@@ -169,7 +170,7 @@ public class MailingListsReport
 
                 if ( StringUtils.isNotEmpty( mailingList.getSubscribe() ) )
                 {
-                    textRow.add( createEmailLinkPatternedText( subscribe, 
mailingList.getSubscribe(), null ) );
+                    textRow.add( createURILinkPatternedText( subscribe, 
mailingList.getSubscribe(), null ) );
                 }
                 else
                 {
@@ -178,7 +179,7 @@ public class MailingListsReport
 
                 if ( StringUtils.isNotEmpty( mailingList.getUnsubscribe() ) )
                 {
-                    textRow.add( createEmailLinkPatternedText( unsubscribe, 
mailingList.getUnsubscribe(), null ) );
+                    textRow.add( createURILinkPatternedText( unsubscribe, 
mailingList.getUnsubscribe(), null ) );
                 }
                 else
                 {
@@ -187,7 +188,7 @@ public class MailingListsReport
 
                 if ( StringUtils.isNotEmpty( mailingList.getPost() ) )
                 {
-                    textRow.add( createEmailLinkPatternedText( post, 
mailingList.getPost(), null ) );
+                    textRow.add( createURILinkPatternedText( post, 
mailingList.getPost(), null ) );
                 }
                 else
                 {
@@ -262,23 +263,31 @@ public class MailingListsReport
         }
 
         /**
-         * Create a link pattern text for email addresses defined by 
<code>{text, mailto:href}</code>. If href is null,
-         * then <code>defaultHref</code> is used instead.
+         * Create a URI link pattern text for a mailing list. If no scheme is 
provided {@code mailto:}
+         * will be prepended by default. If href is null, then 
<code>defaultHref</code> is used instead.
          *
          * @param text a text.
-         * @param href the email address to use.
+         * @param href the potential URI to use.
          * @param defaultHref the String to use in case href is null.
-         * @return an email link pattern.
+         * @return a link pattern.
          * @see #createLinkPatternedText(String,String)
          */
-        private String createEmailLinkPatternedText( String text, String href, 
String defaultHref )
+        private String createURILinkPatternedText( String text, String href, 
String defaultHref )
         {
             if ( href == null || href.isEmpty() )
             {
                 return createLinkPatternedText( text, defaultHref );
             }
-            return createLinkPatternedText( text,
-                    href.toLowerCase( Locale.ENGLISH ).startsWith( "mailto:"; ) 
? href : "mailto:"; + href );
+
+            URI hrefUri = URI.create( href );
+            if ( StringUtils.isNotEmpty( hrefUri.getScheme() ) )
+            {
+                return createLinkPatternedText( text, href );
+            }
+            else
+            {
+                return createLinkPatternedText( text, "mailto:"; + 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 f8237f9..3437f88 100644
--- 
a/src/test/java/org/apache/maven/report/projectinfo/MailingListsReportTest.java
+++ 
b/src/test/java/org/apache/maven/report/projectinfo/MailingListsReportTest.java
@@ -25,6 +25,7 @@ import java.util.Locale;
 import com.meterware.httpunit.GetMethodWebRequest;
 import com.meterware.httpunit.TextBlock;
 import com.meterware.httpunit.WebConversation;
+import com.meterware.httpunit.WebLink;
 import com.meterware.httpunit.WebRequest;
 import com.meterware.httpunit.WebResponse;
 
@@ -73,13 +74,19 @@ public class MailingListsReportTest
         assertEquals( getString( "report.mailing-lists.title" ), 
textBlocks[0].getText() );
         assertEquals( getString( "report.mailing-lists.intro" ), 
textBlocks[1].getText() );
 
-        // MPIR-385: Test emails starts with 'mailto:'
+        // MPIR-385 + MPIR-401: Test links are URIs otherwise assume a plain 
email address
         String post = getString("report.mailing-lists.column.post");
-        assertEquals( "mailto:t...@maven.apache.org";, response.getLinkWith( 
post ).getAttribute( "href" ) );
+        WebLink[] postLinks = response.getMatchingLinks( 
WebLink.MATCH_CONTAINED_TEXT, post );
+        assertEquals( "mailto:t...@maven.apache.org";, 
postLinks[0].getAttribute( "href" ) );
+        assertEquals( "mailto:te...@maven.apache.org";, 
postLinks[1].getAttribute( "href" ) );
         String subscribe = getString("report.mailing-lists.column.subscribe");
-        assertEquals( "MAILTO:test-subscr...@maven.apache.org", 
response.getLinkWith( subscribe ).getAttribute( "href" ) );
+        WebLink[] subscribeLinks = response.getMatchingLinks( 
WebLink.MATCH_CONTAINED_TEXT, subscribe );
+        assertEquals( "MAILTO:test-subscr...@maven.apache.org", 
subscribeLinks[0].getAttribute( "href" ) );
+        assertEquals( "MAILTO:test-subscri...@maven.apache.org", 
subscribeLinks[1].getAttribute( "href" ) );
         String unsubscribe = 
getString("report.mailing-lists.column.unsubscribe");
-        assertNull( response.getLinkWith( unsubscribe ) );
+        WebLink[] unsubscribeLinks = response.getMatchingLinks( 
WebLink.MATCH_CONTAINED_TEXT, unsubscribe );
+        assertTrue( unsubscribeLinks.length == 1 );
+        assertEquals( "https://example.com/unsubscribe";, 
unsubscribeLinks[0].getAttribute( "href" ) );
     }
 
     /**
diff --git a/src/test/resources/plugin-configs/mailing-lists-plugin-config.xml 
b/src/test/resources/plugin-configs/mailing-lists-plugin-config.xml
index 036a99a..2cd6e45 100644
--- a/src/test/resources/plugin-configs/mailing-lists-plugin-config.xml
+++ b/src/test/resources/plugin-configs/mailing-lists-plugin-config.xml
@@ -38,6 +38,12 @@ under the License.
       <post>t...@maven.apache.org</post>
       <subscribe>MAILTO:test-subscr...@maven.apache.org</subscribe>
     </mailingList>
+    <mailingList>
+      <name>Test List 2</name>
+      <post>te...@maven.apache.org</post>
+      <subscribe>MAILTO:test-subscri...@maven.apache.org</subscribe>
+      <unsubscribe>https://example.com/unsubscribe</unsubscribe>
+    </mailingList>
   </mailingLists>
   <build>
     <plugins>
@@ -51,4 +57,4 @@ under the License.
       </plugin>
     </plugins>
   </build>
-</project>
\ No newline at end of file
+</project>

Reply via email to