diff --git a/core/src/main/java/hudson/model/ChoiceParameterDefinition.java b/core/src/main/java/hudson/model/ChoiceParameterDefinition.java index 3b983ed0d931f13aeb45172966e23040159b8108..4da0c2998510ac9ffc96c09953a2ad45daf4ff0c 100755 --- a/core/src/main/java/hudson/model/ChoiceParameterDefinition.java +++ b/core/src/main/java/hudson/model/ChoiceParameterDefinition.java @@ -1,5 +1,7 @@ package hudson.model; +import hudson.util.FormValidation; +import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.export.Exported; @@ -15,25 +17,26 @@ import java.util.Arrays; * @author huybrechts */ public class ChoiceParameterDefinition extends SimpleParameterDefinition { + public static final String CHOICES_DELIMETER = "\\r?\\n"; + private final List choices; private final String defaultValue; + public static boolean areValidChoices(String choices) { + String strippedChoices = choices.trim(); + return !StringUtils.isEmpty(strippedChoices) && strippedChoices.split(CHOICES_DELIMETER).length > 0; + } + @DataBoundConstructor public ChoiceParameterDefinition(String name, String choices, String description) { super(name, description); - this.choices = Arrays.asList(choices.split("\\r?\\n")); - if (choices.length()==0) { - throw new IllegalArgumentException("No choices found"); - } + this.choices = Arrays.asList(choices.split(CHOICES_DELIMETER)); defaultValue = null; } public ChoiceParameterDefinition(String name, String[] choices, String description) { super(name, description); this.choices = new ArrayList(Arrays.asList(choices)); - if (this.choices.isEmpty()) { - throw new IllegalArgumentException("No choices found"); - } defaultValue = null; } @@ -52,7 +55,7 @@ public class ChoiceParameterDefinition extends SimpleParameterDefinition { return this; } } - + @Exported public List getChoices() { return choices; @@ -95,6 +98,17 @@ public class ChoiceParameterDefinition extends SimpleParameterDefinition { public String getHelpFile() { return "/help/parameter/choice.html"; } + + /** + * Checks if parameterised build choices are valid. + */ + public FormValidation doCheckChoices(@QueryParameter String value) { + if (ChoiceParameterDefinition.areValidChoices(value)) { + return FormValidation.ok(); + } else { + return FormValidation.error(Messages.ChoiceParameterDefinition_MissingChoices()); + } + } } } \ No newline at end of file diff --git a/core/src/main/resources/hudson/model/ChoiceParameterDefinition/config.jelly b/core/src/main/resources/hudson/model/ChoiceParameterDefinition/config.jelly index da360341ceb0ca7b9cd5aed3bc72e897b7bc5b12..fb0aea9b55e1b976ab524e9ca91252af2c133aa4 100755 --- a/core/src/main/resources/hudson/model/ChoiceParameterDefinition/config.jelly +++ b/core/src/main/resources/hudson/model/ChoiceParameterDefinition/config.jelly @@ -30,7 +30,7 @@ THE SOFTWARE. - + diff --git a/core/src/main/resources/hudson/model/Messages.properties b/core/src/main/resources/hudson/model/Messages.properties index 97b545a6408f87e310b386f406ca906078eeb92a..917e8cb123bdcd136e3935e503218941cc164a68 100644 --- a/core/src/main/resources/hudson/model/Messages.properties +++ b/core/src/main/resources/hudson/model/Messages.properties @@ -308,6 +308,7 @@ TextParameterDefinition.DisplayName=Text Parameter FileParameterDefinition.DisplayName=File Parameter BooleanParameterDefinition.DisplayName=Boolean Value ChoiceParameterDefinition.DisplayName=Choice +ChoiceParameterDefinition.MissingChoices=Requires Choices. RunParameterDefinition.DisplayName=Run Parameter PasswordParameterDefinition.DisplayName=Password Parameter diff --git a/core/src/test/java/hudson/model/ChoiceParameterDefinitionTest.java b/core/src/test/java/hudson/model/ChoiceParameterDefinitionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..74ac53e23826dc61b36757c35f6d33757cee57f9 --- /dev/null +++ b/core/src/test/java/hudson/model/ChoiceParameterDefinitionTest.java @@ -0,0 +1,28 @@ +package hudson.model; + +import hudson.util.FormValidation; +import jenkins.model.Jenkins; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ChoiceParameterDefinitionTest { + @Test + public void shouldValidateChoices(){ + assertFalse(ChoiceParameterDefinition.areValidChoices("")); + assertFalse(ChoiceParameterDefinition.areValidChoices(" ")); + assertTrue(ChoiceParameterDefinition.areValidChoices("abc")); + assertTrue(ChoiceParameterDefinition.areValidChoices("abc\ndef")); + assertTrue(ChoiceParameterDefinition.areValidChoices("abc\r\ndef")); + } + + @Test + public void testCheckChoices() throws Exception { + ChoiceParameterDefinition.DescriptorImpl descriptorImpl = new ChoiceParameterDefinition.DescriptorImpl(); + + assertEquals(FormValidation.Kind.OK, descriptorImpl.doCheckChoices("abc\ndef").kind); + assertEquals(FormValidation.Kind.ERROR, descriptorImpl.doCheckChoices("").kind); + } +}