Skip to content

Commit

Permalink
[JENKINS-62889] ParameterDefinition check for validity (#4826)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffret-b committed Jul 6, 2020
1 parent a1b5ec1 commit 55d1b9e
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 126 deletions.
20 changes: 14 additions & 6 deletions core/src/main/java/hudson/model/ChoiceParameterDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,29 @@ public StringParameterValue getDefaultParameterValue() {
return new StringParameterValue(getName(), defaultValue, getDescription());
}

private StringParameterValue checkValue(StringParameterValue value) {
if (!choices.contains(value.value))
throw new IllegalArgumentException("Illegal choice for parameter " + getName() + ": " + value.value);
return value;
@Override
public boolean isValid(ParameterValue value) {
return choices.contains(((StringParameterValue) value).getValue());
}

@Override
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
StringParameterValue value = req.bindJSON(StringParameterValue.class, jo);
value.setDescription(getDescription());
return checkValue(value);
checkValue(value, value.getValue());
return value;
}

private void checkValue(StringParameterValue value, String value2) {
if (!isValid(value)) {
throw new IllegalArgumentException("Illegal choice for parameter " + getName() + ": " + value2);
}
}

public StringParameterValue createValue(String value) {
return checkValue(new StringParameterValue(getName(), value, getDescription()));
StringParameterValue parameterValue = new StringParameterValue(getName(), value, getDescription());
checkValue(parameterValue, value);
return parameterValue;
}

@Extension @Symbol({"choice","choiceParam"})
Expand Down
61 changes: 0 additions & 61 deletions core/src/main/java/hudson/model/JobParameterDefinition.java

This file was deleted.

58 changes: 0 additions & 58 deletions core/src/main/java/hudson/model/JobParameterValue.java

This file was deleted.

11 changes: 11 additions & 0 deletions core/src/main/java/hudson/model/ParameterDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ public ParameterValue getDefaultParameterValue() {
return null;
}

/**
* Checks whether a given value is valid for this definition.
* @since TODO
* @param value The value to validate.
* @return True if the value is valid for this definition. False if it is invalid.
*/
public boolean isValid(ParameterValue value) {
// The base implementation just accepts the value.
return true;
}

/**
* Returns all the registered {@link ParameterDefinition} descriptors.
*/
Expand Down
110 changes: 109 additions & 1 deletion core/src/test/java/hudson/model/ChoiceParameterDefinitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
Expand All @@ -20,7 +22,7 @@ public void shouldValidateChoices(){
}

@Test
public void testCheckChoices() throws Exception {
public void testCheckChoices() {
ChoiceParameterDefinition.DescriptorImpl descriptorImpl = new ChoiceParameterDefinition.DescriptorImpl();

assertEquals(FormValidation.Kind.OK, descriptorImpl.doCheckChoices("abc\ndef").kind);
Expand All @@ -33,4 +35,110 @@ public void testNullDefaultParameter() {
ChoiceParameterDefinition param = new ChoiceParameterDefinition("name", new String[0], null);
assertNull(param.getDefaultParameterValue());
}

@Test
public void createNullChoice() {
String stringValue = null;
String[] choices = new String[]{stringValue};
assertCreation(stringValue, choices, true);
}

@Test
@Issue("JENKINS-62889")
public void checkValue_Null() {
String stringValue = null;
String[] choices = new String[]{stringValue};
assertCheckValue(stringValue, choices, true);
}

@Test
public void createBlankChoice() {
String stringValue = "";
String[] choices = new String[]{stringValue};
assertCreation(stringValue, choices, true);
}

@Test
@Issue("JENKINS-62889")
public void checkValue_Blank() {
String stringValue = "";
String[] choices = new String[]{stringValue};
assertCheckValue(stringValue, choices, true);
}

@Test
public void createEmptyChoice() {
String stringValue = " ";
String[] choices = new String[]{stringValue};
assertCreation(stringValue, choices, true);
}

@Test
@Issue("JENKINS-62889")
public void checkValue_Empty() {
String stringValue = "";
String[] choices = new String[]{stringValue};
assertCheckValue(stringValue, choices, true);
}

@Test
@Issue("JENKINS-62889")
public void checkValue_Single() {
String stringValue = "single";
String[] choices = new String[]{stringValue};
assertCheckValue(stringValue, choices, true);
}

@Test
@Issue("JENKINS-62889")
public void checkValue_Multiple() {
String[] choices = new String[]{"one", "two", "three"};
ChoiceParameterDefinition parameterDefinition = new ChoiceParameterDefinition("name", choices, "description");
for (String choiceValue : choices) {
StringParameterValue parameterValue = parameterDefinition.createValue(choiceValue);
assertTrue(parameterDefinition.isValid(parameterValue));
}
}

@Test
@Issue("JENKINS-62889")
public void checkValue_Invalid() {
String stringValue = "single";
String[] choices = new String[]{stringValue};
ChoiceParameterDefinition parameterDefinition = new ChoiceParameterDefinition("name", choices, "description");
StringParameterValue parameterValue = new StringParameterValue("choice", "invalid");
assertFalse(parameterDefinition.isValid(parameterValue));
}

@Test(expected = ClassCastException.class)
@Issue("JENKINS-62889")
public void checkValue_WrongValueType() {
String stringValue = "single";
String[] choices = new String[]{stringValue};
ChoiceParameterDefinition parameterDefinition = new ChoiceParameterDefinition("name", choices, "description");
BooleanParameterValue parameterValue = new BooleanParameterValue("choice", false);
parameterDefinition.isValid(parameterValue);
}

@Test(expected = IllegalArgumentException.class)
@Issue("JENKINS-62889")
public void createValue_Invalid() {
String stringValue = "single";
String[] choices = new String[]{stringValue};
ChoiceParameterDefinition parameterDefinition = new ChoiceParameterDefinition("name", choices, "description");
parameterDefinition.createValue("invalid");
}

private void assertCreation(String stringValue, String[] choices, boolean valid) {
ChoiceParameterDefinition parameterDefinition = new ChoiceParameterDefinition("name", choices, "description");
StringParameterValue parameterValue = new StringParameterValue("choice", stringValue);
assertThat(parameterDefinition.isValid(parameterValue), is(valid));
}

private void assertCheckValue(String stringValue, String[] choices, boolean valid) {
ChoiceParameterDefinition parameterDefinition = new ChoiceParameterDefinition("name", choices, "description");
StringParameterValue parameterValue = new StringParameterValue("choice", stringValue);
assertThat(parameterDefinition.isValid(parameterValue), is(valid));
}

}

0 comments on commit 55d1b9e

Please sign in to comment.