Skip to content

Commit

Permalink
[JENKINS-73404] unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jtnord committed Jul 10, 2024
1 parent 6357012 commit e166d4a
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
126 changes: 125 additions & 1 deletion test/src/test/java/hudson/util/FormFieldValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@
package hudson.util;


import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

import hudson.model.AbstractProject;
import hudson.model.FreeStyleProject;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;
import hudson.tasks.Publisher;
import hudson.tasks.Recorder;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.htmlunit.ScriptResult;
import org.htmlunit.WebResponseListener;
import org.htmlunit.html.HtmlPage;
Expand All @@ -42,6 +48,7 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -134,7 +141,7 @@ public boolean isApplicable(Class<? extends AbstractProject> jobType) {
@Issue("JENKINS-3382")
public void negative() throws Exception {
BrokenFormValidatorBuilder.DescriptorImpl d = new BrokenFormValidatorBuilder.DescriptorImpl();
Publisher.all().add(d);
Recorder.all().add(d);
try {
FreeStyleProject p = j.createFreeStyleProject();
p.getPublishersList().add(new BrokenFormValidatorBuilder());
Expand All @@ -152,4 +159,121 @@ public void negative() throws Exception {
Publisher.all().remove(d);
}
}

@Issue("JENKINS-73404")
@Test
public void testValidationforComponents() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
ValidatingDescribable vd = new ValidatingDescribable();
p.getBuildersList().add(new ValidatingDescribable());
try (JenkinsRule.WebClient wc = j.createWebClient()) {
HtmlPage page = wc.getPage(p, "configure");
assertThat(page.asNormalizedText(), allOf(
containsString("FormValidation: Password (empty)"),
containsString("FormValidation: Password (populated)"),
containsString("FormValidation: Textarea"),
containsString("FormValidation: SecretTextarea (empty)"),
containsString("FormValidation: SecretTextarea (populated)")));

}

}

public static class ValidatingDescribable extends Builder {

private Secret emptyPassword;
// give the secret some data so that it is hidden and not a regular field!
private Secret populatedPassword = Secret.fromString("secret!");
private String textarea;
private Secret emptySecretTextarea;
private Secret populatedSecretTextarea = Secret.fromString("sensitive!");;

@DataBoundConstructor
public ValidatingDescribable() {
}

public Secret getEmptyPassword() {
return emptyPassword;
}

@DataBoundSetter
public void setEmptyPassword(String emptyPassword) {
this.emptyPassword = Secret.fromString(emptyPassword);
}

public Secret getPopulatedPassword() {
return populatedPassword;
}

@DataBoundSetter
public void setPopulatedPassword(String populatedPassword) {
this.populatedPassword = Secret.fromString(populatedPassword);
}

public String getTextarea() {
return textarea;
}

@DataBoundSetter
public void setTextarea(String textarea) {
this.textarea = textarea;
}

public Secret getEmptySecretTextarea() {
return emptySecretTextarea;
}

@DataBoundSetter
public void setEmptySecretTextarea(String emptySecretTextarea) {
this.emptySecretTextarea = Secret.fromString(emptySecretTextarea);
}

public Secret getPopulatedSecretTextarea() {
return populatedSecretTextarea;
}

@DataBoundSetter
public void setPopulatedSecretTextarea(String populatedSecretTextarea) {
this.populatedSecretTextarea = Secret.fromString(populatedSecretTextarea);
}

@TestExtension
public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
// not used for the test class but useful for interactive debugging to check the validation has been called
AtomicInteger i = new AtomicInteger();

@Override
public String getDisplayName() {
return "Validation Testing";
}

public FormValidation doCheckEmptyPassword(@QueryParameter String value) {
return FormValidation.ok("FormValidation: Password (empty)" + i.getAndIncrement());
}

public FormValidation doCheckPopulatedPassword(@QueryParameter String value) {
return FormValidation.ok("FormValidation: Password (populated)" + i.getAndIncrement());
}

public FormValidation doCheckTextarea(@QueryParameter String value) {
return FormValidation.ok("FormValidation: Textarea" + i.getAndIncrement());
}

public FormValidation doCheckEmptySecretTextarea(@QueryParameter String value) {
return FormValidation.ok("FormValidation: SecretTextarea (empty)" + i.getAndIncrement());
}

public FormValidation doCheckPopulatedSecretTextarea(@QueryParameter String value) {
return FormValidation.ok("FormValidation: SecretTextarea (populated)" + i.getAndIncrement());
}

@Override
public boolean isApplicable(Class jobType) {
return true;
}

}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<f:entry title="Password (empty)" field="emptyPassword">
<f:password/>
</f:entry>
<f:entry title="Password (populated)" field="populatedPassword">
<f:password/>
</f:entry>
<f:entry title="Textarea" field="textarea">
<f:textarea/>
</f:entry>
<f:entry title="Secret Textarea (empty)" field="emptySecretTextarea">
<f:secretTextarea/>
</f:entry>
<f:entry title="Secret Textarea (populated)" field="populatedSecretTextarea">
<f:secretTextarea/>
</f:entry>

</j:jelly>

0 comments on commit e166d4a

Please sign in to comment.