From 96da5881a07d89161102a1adb50d2269f5b7eca5 Mon Sep 17 00:00:00 2001 From: James Nord Date: Tue, 9 Jul 2024 16:10:15 +0100 Subject: [PATCH] [JENKINS-73404] unit test --- .../hudson/util/FormFieldValidatorTest.java | 125 +++++++++++++++++- .../ValidatingDescribable/config.jelly | 20 +++ 2 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 test/src/test/resources/hudson/util/FormFieldValidatorTest/ValidatingDescribable/config.jelly diff --git a/test/src/test/java/hudson/util/FormFieldValidatorTest.java b/test/src/test/java/hudson/util/FormFieldValidatorTest.java index bdbb76946288..489c37baf852 100644 --- a/test/src/test/java/hudson/util/FormFieldValidatorTest.java +++ b/test/src/test/java/hudson/util/FormFieldValidatorTest.java @@ -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; @@ -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; @@ -134,7 +141,7 @@ public boolean isApplicable(Class 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()); @@ -152,4 +159,120 @@ public void negative() throws Exception { Publisher.all().remove(d); } } + + @Issue("JENKINS-73404") + @Test + public void testValidationforComponents() throws Exception { + FreeStyleProject p = j.createFreeStyleProject(); + 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 { + // 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; + } + + } + + } + } diff --git a/test/src/test/resources/hudson/util/FormFieldValidatorTest/ValidatingDescribable/config.jelly b/test/src/test/resources/hudson/util/FormFieldValidatorTest/ValidatingDescribable/config.jelly new file mode 100644 index 000000000000..c560072d1ed4 --- /dev/null +++ b/test/src/test/resources/hudson/util/FormFieldValidatorTest/ValidatingDescribable/config.jelly @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + +