Skip to content
Permalink
Browse files

Merge pull request #988 from daspilker/JENKINS-41270

[JENKINS-41270] improved error message for invalid enum values
  • Loading branch information
daspilker committed Jan 22, 2017
2 parents c12c0c4 + b837bb3 commit 877e860ea3d8a455a820d723d4ebd0e6ff331269
@@ -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
@@ -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)
@@ -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("', '")}'"
)
}
}
}
@@ -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'() {
@@ -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'() {

0 comments on commit 877e860

Please sign in to comment.
You can’t perform that action at this time.