Skip to content

Commit

Permalink
Merge pull request #96 from darinpope/trim-smtphost
Browse files Browse the repository at this point in the history
Hacktoberfest: Trim white space
  • Loading branch information
alecharp committed Mar 8, 2021
2 parents 2fb5973 + b1ea75a commit c060f77
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/main/java/hudson/tasks/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import static hudson.Util.fixEmptyAndTrim;
import hudson.Util;

import hudson.BulkChange;
import hudson.EnvVars;
Expand Down Expand Up @@ -355,7 +355,7 @@ public String getReplyToAddress() {

@DataBoundSetter
public void setReplyToAddress(String address) {
this.replyToAddress = Util.fixEmpty(address);
this.replyToAddress = Util.fixEmptyAndTrim(address);
save();
}

Expand All @@ -370,12 +370,14 @@ private static Session createSession(String smtpHost, String smtpPort, boolean u
final String SMTP_PORT_PROPERTY = "mail.smtp.port";
final String SMTP_SOCKETFACTORY_PORT_PROPERTY = "mail.smtp.socketFactory.port";

smtpPort = fixEmptyAndTrim(smtpPort);
smtpAuthUserName = fixEmptyAndTrim(smtpAuthUserName);
smtpHost = Util.fixEmptyAndTrim(smtpHost);
smtpPort = Util.fixEmptyAndTrim(smtpPort);
smtpAuthUserName = Util.fixEmptyAndTrim(smtpAuthUserName);

Properties props = new Properties(System.getProperties());
if(fixEmptyAndTrim(smtpHost)!=null)
props.put("mail.smtp.host",smtpHost);
if(smtpHost!=null) {
props.put("mail.smtp.host", smtpHost);
}
if (smtpPort!=null) {
props.put(SMTP_PORT_PROPERTY, smtpPort);
}
Expand Down Expand Up @@ -582,7 +584,7 @@ public void setAdminAddress(String adminAddress) {

@DataBoundSetter
public void setSmtpHost(String smtpHost) {
this.smtpHost = nullify(smtpHost);
this.smtpHost = Util.fixEmptyAndTrim(smtpHost);
save();
}

Expand All @@ -600,7 +602,7 @@ public void setUseTls(boolean useTls) {

@DataBoundSetter
public void setSmtpPort(String smtpPort) {
this.smtpPort = smtpPort;
this.smtpPort = Util.fixEmptyAndTrim(smtpPort);
save();
}

Expand All @@ -609,7 +611,7 @@ public void setCharset(String charset) {
if (charset == null || charset.length() == 0) {
charset = "UTF-8";
}
this.charset = charset;
this.charset = Util.fixEmptyAndTrim(charset);
save();
}

Expand Down Expand Up @@ -668,7 +670,7 @@ public FormValidation doAddressCheck(@QueryParameter String value) {

public FormValidation doCheckSmtpServer(@QueryParameter String value) {
try {
if (fixEmptyAndTrim(value)!=null)
if (Util.fixEmptyAndTrim(value)!=null)
InetAddress.getByName(value);
return FormValidation.ok();
} catch (UnknownHostException e) {
Expand All @@ -677,7 +679,7 @@ public FormValidation doCheckSmtpServer(@QueryParameter String value) {
}

public FormValidation doCheckDefaultSuffix(@QueryParameter String value) {
if (value.matches("@[A-Za-z0-9.\\-]+") || fixEmptyAndTrim(value)==null)
if (value.matches("@[A-Za-z0-9.\\-]+") || Util.fixEmptyAndTrim(value)==null)
return FormValidation.ok();
else
return FormValidation.error(Messages.Mailer_Suffix_Error());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/hudson/tasks/SMTPAuthentication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hudson.model.Descriptor;
import hudson.util.Secret;
import org.kohsuke.stapler.DataBoundConstructor;
import hudson.Util;

/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
Expand All @@ -17,7 +18,7 @@ public class SMTPAuthentication extends AbstractDescribableImpl<SMTPAuthenticati

@DataBoundConstructor
public SMTPAuthentication(String username, Secret password) {
this.username = username;
this.username = Util.fixEmptyAndTrim(username);
this.password = password;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/hudson/tasks/Mailer/global.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ THE SOFTWARE.
<f:checkbox />
</f:entry>
<f:entry title="${%SMTP Port}" field="smtpPort">
<f:textbox />
<f:number />
</f:entry>
<f:entry title="${%Reply-To Address}" field="replyToAddress">
<f:textbox />
Expand Down
80 changes: 80 additions & 0 deletions src/test/java/hudson/tasks/MailerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,86 @@ private TestProject create(boolean notifyEveryUnstableBuild, boolean sendToIndiv
return new TestProject(m);
}

@Test
public void testFixEmptyAndTrimOne() throws Exception {
DescriptorImpl d = Mailer.descriptor();
d.setSmtpHost("smtp.host");
d.setAuthentication(new SMTPAuthentication("user", Secret.fromString("pass")));
d.setSmtpPort("1025");
d.setReplyToAddress("foo@bar.com");
d.setCharset("UTF-8");

rule.submit(rule.createWebClient().goTo("configure").getFormByName("config"));

assertEquals("smtp.host",d.getSmtpHost());
SMTPAuthentication authentication = d.getAuthentication();
assertEquals("user",authentication.getUsername());
assertEquals("pass",authentication.getPassword().getPlainText());
assertEquals("1025",d.getSmtpPort());
assertEquals("foo@bar.com",d.getReplyToAddress());
assertEquals("UTF-8",d.getCharset());
}

@Test
public void testFixEmptyAndTrimTwo() throws Exception {
DescriptorImpl d = Mailer.descriptor();
d.setSmtpHost(" smtp.host ");
d.setAuthentication(new SMTPAuthentication(" user ", Secret.fromString("pass")));
d.setSmtpPort(" 1025 ");
d.setReplyToAddress(" foo@bar.com ");
d.setCharset(" UTF-8 ");

rule.submit(rule.createWebClient().goTo("configure").getFormByName("config"));

assertEquals("smtp.host",d.getSmtpHost());
SMTPAuthentication authentication = d.getAuthentication();
assertEquals("user",authentication.getUsername());
assertEquals("pass",authentication.getPassword().getPlainText());
assertEquals("1025",d.getSmtpPort());
assertEquals("foo@bar.com",d.getReplyToAddress());
assertEquals("UTF-8",d.getCharset());
}

@Test
public void testFixEmptyAndTrimThree() throws Exception {
DescriptorImpl d = Mailer.descriptor();
d.setSmtpHost("");
d.setAuthentication(new SMTPAuthentication("", Secret.fromString("pass")));
d.setSmtpPort("");
d.setReplyToAddress("");
d.setCharset("");

rule.submit(rule.createWebClient().goTo("configure").getFormByName("config"));

assertNull(d.getSmtpHost());
SMTPAuthentication authentication = d.getAuthentication();
assertNull(authentication.getUsername());
assertEquals("pass",authentication.getPassword().getPlainText());
assertNull(d.getSmtpPort());
assertNull(d.getReplyToAddress());
assertEquals("UTF-8",d.getCharset());
}

@Test
public void testFixEmptyAndTrimFour() throws Exception {
DescriptorImpl d = Mailer.descriptor();
d.setSmtpHost(null);
d.setAuthentication(new SMTPAuthentication(null, Secret.fromString("pass")));
d.setSmtpPort(null);
d.setReplyToAddress(null);
d.setCharset(null);

rule.submit(rule.createWebClient().goTo("configure").getFormByName("config"));

assertNull(d.getSmtpHost());
SMTPAuthentication authentication = d.getAuthentication();
assertNull(authentication.getUsername());
assertEquals("pass",authentication.getPassword().getPlainText());
assertNull(d.getSmtpPort());
assertNull(d.getReplyToAddress());
assertEquals("UTF-8",d.getCharset());
}

@Bug(1566)
@Test
public void testSenderAddress() throws Exception {
Expand Down

0 comments on commit c060f77

Please sign in to comment.