Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #968 from akiellor/JENKINS-18434
  • Loading branch information
olivergondza committed Oct 8, 2013
2 parents 8a55679 + c4e2e59 commit bd2a4c8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
30 changes: 22 additions & 8 deletions 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;
Expand All @@ -15,25 +17,26 @@
* @author huybrechts
*/
public class ChoiceParameterDefinition extends SimpleParameterDefinition {
public static final String CHOICES_DELIMETER = "\\r?\\n";

private final List<String> 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<String>(Arrays.asList(choices));
if (this.choices.isEmpty()) {
throw new IllegalArgumentException("No choices found");
}
defaultValue = null;
}

Expand All @@ -52,7 +55,7 @@ public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
return this;
}
}

@Exported
public List<String> getChoices() {
return choices;
Expand Down Expand Up @@ -95,6 +98,17 @@ public String getDisplayName() {
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());
}
}
}

}
Expand Up @@ -30,7 +30,7 @@ THE SOFTWARE.
<f:textbox name="parameter.name" value="${instance.name}" />
</f:entry>
<f:entry title="${%Choices}" help="/help/parameter/choice-choices.html">
<f:textarea name="parameter.choices" value="${instance.choicesText}" />
<f:textarea checkUrl="'${rootURL}/descriptorByName/hudson.model.ChoiceParameterDefinition/checkChoices?value='+encodeURIComponent(this.value)" name="parameter.choices" value="${instance.choicesText}" />
</f:entry>
<f:entry title="${%Description}" help="/help/parameter/description.html">
<f:textarea name="parameter.description" value="${instance.description}" />
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/hudson/model/Messages.properties
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions 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);
}
}

0 comments on commit bd2a4c8

Please sign in to comment.