Skip to content

Commit

Permalink
Merge pull request #988 from daspilker/JENKINS-41270
Browse files Browse the repository at this point in the history
[JENKINS-41270] improved error message for invalid enum values
  • Loading branch information
daspilker committed Jan 22, 2017
2 parents c12c0c4 + b837bb3 commit 877e860
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/Home.md
Expand Up @@ -29,6 +29,8 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
* 1.58 (unreleased)
* Fixed a problem with the plugin's dependencies
([JENKINS-41001](https://issues.jenkins-ci.org/browse/JENKINS-41001))
* Improved error message for invalid enum values
([JENKINS-41270](https://issues.jenkins-ci.org/browse/JENKINS-41270))
* 1.57 (January 15 2017)
* Updated optional
[Config File Provider Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Config+File+Provider+Plugin) dependency to
Expand Down
Expand Up @@ -80,11 +80,13 @@ class DescribableContext implements Context {
return value.every { isAssignable(it, (AtomicType) arrayType.elementType) }
} else if (arrayType.elementType instanceof EnumType) {
EnumType enumType = (EnumType) arrayType.elementType
return value.every { isValidEnumValue(enumType, it) }
value.each { checkValidEnumValue(enumType, it) }
return true
}
}
} else if (parameterType instanceof EnumType) {
return isValidEnumValue((EnumType) parameterType, value)
checkValidEnumValue((EnumType) parameterType, value)
return true
} else if (parameterType instanceof AtomicType) {
return (value != null && isAssignable(value, parameterType)) ||
(value == null && !((AtomicType) parameterType).type.primitive)
Expand Down Expand Up @@ -138,7 +140,11 @@ class DescribableContext implements Context {
ClassUtils.isAssignable(normalizedType, parameterType.type, true)
}

private static boolean isValidEnumValue(EnumType enumType, Object value) {
enumType.type.isInstance(value) || enumType.values.contains(value)
private static void checkValidEnumValue(EnumType enumType, Object value) {
if (!enumType.type.isInstance(value) && !enumType.values.contains(value)) {
throw new DslScriptException(
"invalid enum value '${value}', must be one of '${enumType.values.join("', '")}'"
)
}
}
}
Expand Up @@ -221,11 +221,8 @@ class DescribableContextSpec extends Specification {
context.anEnum('true')

then:
MissingMethodException e = thrown(MissingMethodException)
e.type == DescribableContext
e.method == 'anEnum'
e.arguments == ['true']
!e.static
Exception e = thrown(DslScriptException)
e.message =~ "invalid enum value 'true', must be one of 'NEW', 'RUNNABLE', 'BLOCKED', 'WAITING', 'TIMED_WAITING"
}

def 'enum property with string value'() {
Expand Down Expand Up @@ -280,11 +277,8 @@ class DescribableContextSpec extends Specification {
context.anEnum(null)

then:
MissingMethodException e = thrown(MissingMethodException)
e.type == DescribableContext
e.method == 'anEnum'
e.arguments == [null]
!e.static
Exception e = thrown(DslScriptException)
e.message =~ "invalid enum value 'null', must be one of 'NEW', 'RUNNABLE', 'BLOCKED', 'WAITING', 'TIMED_WAITING"
}

def 'heterogeneous property with invalid type'() {
Expand Down

0 comments on commit 877e860

Please sign in to comment.