Skip to content

Commit

Permalink
[JENKINS-20226] Restored hudsonUrl, which is referred by JenkinsLocat…
Browse files Browse the repository at this point in the history
…ionConfiguration. Added a test to verify JenkinsLocationConfiguration can refer it.
  • Loading branch information
ikedam committed Oct 29, 2013
1 parent 557b3ec commit 93ad8a7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/hudson/tasks/Mailer.java
Expand Up @@ -40,6 +40,7 @@
import hudson.tasks.i18n.Messages;
import hudson.util.FormValidation;
import hudson.util.Secret;
import hudson.util.XStream2;

import jenkins.model.JenkinsLocationConfiguration;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -204,6 +205,17 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Publisher>
*/
private String defaultSuffix;

/**
* Hudson's own URL, to put into the e-mail.
*
* @deprecated as of 1.4
* Maintained in {@link JenkinsLocationConfiguration} but left here
* for compatibility just in case, so as not to lose this information.
* This is loaded to {@link JenkinsLocationConfiguration} via the XML file
* marshaled with {@link XStream2}.
*/
private String hudsonUrl;

/**
* If non-null, use SMTP-AUTH with these information.
*/
Expand Down Expand Up @@ -447,6 +459,19 @@ public void setSmtpAuth(String userName, String password) {
this.smtpAuthPassword = Secret.fromString(password);
}

@Override
public Publisher newInstance(StaplerRequest req, JSONObject formData) throws FormException {
Mailer m = (Mailer)super.newInstance(req, formData);

if(hudsonUrl==null) {
// if Hudson URL is not configured yet, infer some default
hudsonUrl = Functions.inferHudsonURL(req);
save();
}

return m;
}

public FormValidation doAddressCheck(@QueryParameter String value) {
try {
new InternetAddress(value);
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/hudson/tasks/MailerTest.java
Expand Up @@ -34,6 +34,8 @@
import javax.mail.Address;
import javax.mail.internet.InternetAddress;

import jenkins.model.JenkinsLocationConfiguration;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;

Expand Down Expand Up @@ -110,4 +112,32 @@ public void testGlobalConfigRoundtrip() throws Exception {
assertNull("expected null, got: " + d.getSmtpAuthUserName(), d.getSmtpAuthUserName());
assertNull("expected null, got: " + d.getSmtpAuthPassword(), d.getSmtpAuthPassword());
}

/**
* Simulates {@link JenkinsLocationConfiguration} is not configured.
*/
private static class CleanJenkinsLocationConfiguration extends JenkinsLocationConfiguration {
@Override
public synchronized void load() {
getConfigFile().delete();
super.load();
}
};

/**
* Test {@link JenkinsLocationConfiguration} can load hudsonUrl.
*/
public void testHudsonUrlCompatibility() throws Exception {
// not configured.
assertNull(new CleanJenkinsLocationConfiguration().getUrl());

Mailer m = new Mailer("", true, false);
FreeStyleProject p = createFreeStyleProject();
p.getPublishersList().add(m);
WebClient wc = new WebClient();
submit(wc.getPage(p,"configure").getFormByName("config"));

// configured via the marshaled XML file of Mailer
assertEquals(wc.getContextPath(), new CleanJenkinsLocationConfiguration().getUrl());
}
}

0 comments on commit 93ad8a7

Please sign in to comment.