Skip to content

Commit

Permalink
[MPIR-401] Mailing list subscribe and unsubscribe links
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Apr 25, 2021
1 parent 2e187d0 commit 98db5e5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -169,7 +170,7 @@ public void renderBody()

if ( StringUtils.isNotEmpty( mailingList.getSubscribe() ) )
{
textRow.add( createEmailLinkPatternedText( subscribe, mailingList.getSubscribe(), null ) );
textRow.add( createURILinkPatternedText( subscribe, mailingList.getSubscribe(), null ) );
}
else
{
Expand All @@ -178,7 +179,7 @@ public void renderBody()

if ( StringUtils.isNotEmpty( mailingList.getUnsubscribe() ) )
{
textRow.add( createEmailLinkPatternedText( unsubscribe, mailingList.getUnsubscribe(), null ) );
textRow.add( createURILinkPatternedText( unsubscribe, mailingList.getUnsubscribe(), null ) );
}
else
{
Expand All @@ -187,7 +188,7 @@ public void renderBody()

if ( StringUtils.isNotEmpty( mailingList.getPost() ) )
{
textRow.add( createEmailLinkPatternedText( post, mailingList.getPost(), null ) );
textRow.add( createURILinkPatternedText( post, mailingList.getPost(), null ) );
}
else
{
Expand Down Expand Up @@ -262,23 +263,31 @@ public void renderBody()
}

/**
* 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 );
}
}
}
}
Expand Up @@ -25,6 +25,7 @@
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;

Expand Down Expand Up @@ -73,13 +74,19 @@ public void testReport()
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:test@maven.apache.org", response.getLinkWith( post ).getAttribute( "href" ) );
WebLink[] postLinks = response.getMatchingLinks( WebLink.MATCH_CONTAINED_TEXT, post );
assertEquals( "mailto:test@maven.apache.org", postLinks[0].getAttribute( "href" ) );
assertEquals( "mailto:test2@maven.apache.org", postLinks[1].getAttribute( "href" ) );
String subscribe = getString("report.mailing-lists.column.subscribe");
assertEquals( "MAILTO:test-subscribe@maven.apache.org", response.getLinkWith( subscribe ).getAttribute( "href" ) );
WebLink[] subscribeLinks = response.getMatchingLinks( WebLink.MATCH_CONTAINED_TEXT, subscribe );
assertEquals( "MAILTO:test-subscribe@maven.apache.org", subscribeLinks[0].getAttribute( "href" ) );
assertEquals( "MAILTO:test-subscribe2@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" ) );
}

/**
Expand Down
Expand Up @@ -38,6 +38,12 @@ under the License.
<post>test@maven.apache.org</post>
<subscribe>MAILTO:test-subscribe@maven.apache.org</subscribe>
</mailingList>
<mailingList>
<name>Test List 2</name>
<post>test2@maven.apache.org</post>
<subscribe>MAILTO:test-subscribe2@maven.apache.org</subscribe>
<unsubscribe>https://example.com/unsubscribe</unsubscribe>
</mailingList>
</mailingLists>
<build>
<plugins>
Expand All @@ -51,4 +57,4 @@ under the License.
</plugin>
</plugins>
</build>
</project>
</project>

0 comments on commit 98db5e5

Please sign in to comment.