Skip to content

Commit 98db5e5

Browse files
committed
[MPIR-401] Mailing list subscribe and unsubscribe links
1 parent 2e187d0 commit 98db5e5

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

src/main/java/org/apache/maven/report/projectinfo/MailingListsReport.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.codehaus.plexus.i18n.I18N;
2727
import org.codehaus.plexus.util.StringUtils;
2828

29+
import java.net.URI;
2930
import java.util.ArrayList;
3031
import java.util.Iterator;
3132
import java.util.List;
@@ -169,7 +170,7 @@ public void renderBody()
169170

170171
if ( StringUtils.isNotEmpty( mailingList.getSubscribe() ) )
171172
{
172-
textRow.add( createEmailLinkPatternedText( subscribe, mailingList.getSubscribe(), null ) );
173+
textRow.add( createURILinkPatternedText( subscribe, mailingList.getSubscribe(), null ) );
173174
}
174175
else
175176
{
@@ -178,7 +179,7 @@ public void renderBody()
178179

179180
if ( StringUtils.isNotEmpty( mailingList.getUnsubscribe() ) )
180181
{
181-
textRow.add( createEmailLinkPatternedText( unsubscribe, mailingList.getUnsubscribe(), null ) );
182+
textRow.add( createURILinkPatternedText( unsubscribe, mailingList.getUnsubscribe(), null ) );
182183
}
183184
else
184185
{
@@ -187,7 +188,7 @@ public void renderBody()
187188

188189
if ( StringUtils.isNotEmpty( mailingList.getPost() ) )
189190
{
190-
textRow.add( createEmailLinkPatternedText( post, mailingList.getPost(), null ) );
191+
textRow.add( createURILinkPatternedText( post, mailingList.getPost(), null ) );
191192
}
192193
else
193194
{
@@ -262,23 +263,31 @@ public void renderBody()
262263
}
263264

264265
/**
265-
* Create a link pattern text for email addresses defined by <code>{text, mailto:href}</code>. If href is null,
266-
* then <code>defaultHref</code> is used instead.
266+
* Create a URI link pattern text for a mailing list. If no scheme is provided {@code mailto:}
267+
* will be prepended by default. If href is null, then <code>defaultHref</code> is used instead.
267268
*
268269
* @param text a text.
269-
* @param href the email address to use.
270+
* @param href the potential URI to use.
270271
* @param defaultHref the String to use in case href is null.
271-
* @return an email link pattern.
272+
* @return a link pattern.
272273
* @see #createLinkPatternedText(String,String)
273274
*/
274-
private String createEmailLinkPatternedText( String text, String href, String defaultHref )
275+
private String createURILinkPatternedText( String text, String href, String defaultHref )
275276
{
276277
if ( href == null || href.isEmpty() )
277278
{
278279
return createLinkPatternedText( text, defaultHref );
279280
}
280-
return createLinkPatternedText( text,
281-
href.toLowerCase( Locale.ENGLISH ).startsWith( "mailto:" ) ? href : "mailto:" + href );
281+
282+
URI hrefUri = URI.create( href );
283+
if ( StringUtils.isNotEmpty( hrefUri.getScheme() ) )
284+
{
285+
return createLinkPatternedText( text, href );
286+
}
287+
else
288+
{
289+
return createLinkPatternedText( text, "mailto:" + href );
290+
}
282291
}
283292
}
284293
}

src/test/java/org/apache/maven/report/projectinfo/MailingListsReportTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.meterware.httpunit.GetMethodWebRequest;
2626
import com.meterware.httpunit.TextBlock;
2727
import com.meterware.httpunit.WebConversation;
28+
import com.meterware.httpunit.WebLink;
2829
import com.meterware.httpunit.WebRequest;
2930
import com.meterware.httpunit.WebResponse;
3031

@@ -73,13 +74,19 @@ public void testReport()
7374
assertEquals( getString( "report.mailing-lists.title" ), textBlocks[0].getText() );
7475
assertEquals( getString( "report.mailing-lists.intro" ), textBlocks[1].getText() );
7576

76-
// MPIR-385: Test emails starts with 'mailto:'
77+
// MPIR-385 + MPIR-401: Test links are URIs otherwise assume a plain email address
7778
String post = getString("report.mailing-lists.column.post");
78-
assertEquals( "mailto:[email protected]", response.getLinkWith( post ).getAttribute( "href" ) );
79+
WebLink[] postLinks = response.getMatchingLinks( WebLink.MATCH_CONTAINED_TEXT, post );
80+
assertEquals( "mailto:[email protected]", postLinks[0].getAttribute( "href" ) );
81+
assertEquals( "mailto:[email protected]", postLinks[1].getAttribute( "href" ) );
7982
String subscribe = getString("report.mailing-lists.column.subscribe");
80-
assertEquals( "MAILTO:[email protected]", response.getLinkWith( subscribe ).getAttribute( "href" ) );
83+
WebLink[] subscribeLinks = response.getMatchingLinks( WebLink.MATCH_CONTAINED_TEXT, subscribe );
84+
assertEquals( "MAILTO:[email protected]", subscribeLinks[0].getAttribute( "href" ) );
85+
assertEquals( "MAILTO:[email protected]", subscribeLinks[1].getAttribute( "href" ) );
8186
String unsubscribe = getString("report.mailing-lists.column.unsubscribe");
82-
assertNull( response.getLinkWith( unsubscribe ) );
87+
WebLink[] unsubscribeLinks = response.getMatchingLinks( WebLink.MATCH_CONTAINED_TEXT, unsubscribe );
88+
assertTrue( unsubscribeLinks.length == 1 );
89+
assertEquals( "https://example.com/unsubscribe", unsubscribeLinks[0].getAttribute( "href" ) );
8390
}
8491

8592
/**

src/test/resources/plugin-configs/mailing-lists-plugin-config.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ under the License.
3838
<post>[email protected]</post>
3939
<subscribe>MAILTO:[email protected]</subscribe>
4040
</mailingList>
41+
<mailingList>
42+
<name>Test List 2</name>
43+
<post>[email protected]</post>
44+
<subscribe>MAILTO:[email protected]</subscribe>
45+
<unsubscribe>https://example.com/unsubscribe</unsubscribe>
46+
</mailingList>
4147
</mailingLists>
4248
<build>
4349
<plugins>
@@ -51,4 +57,4 @@ under the License.
5157
</plugin>
5258
</plugins>
5359
</build>
54-
</project>
60+
</project>

0 commit comments

Comments
 (0)