Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make String to Boolean parameter conversion more strict #3178

Merged
merged 6 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ private static class StringToBooleanAndCharPrimitiveConverter implements StringT
private static final Map<Class<?>, Function<String, ?>> CONVERTERS;
static {
Map<Class<?>, Function<String, ?>> converters = new HashMap<>();
converters.put(Boolean.class, Boolean::valueOf);
converters.put(Boolean.class, source -> {
Preconditions.condition("true".equalsIgnoreCase(source) || "false".equalsIgnoreCase(source),
() -> "String must be (ignoring case) 'true' or 'false': " + source);
return Boolean.valueOf(source);
});
s-rwe marked this conversation as resolved.
Show resolved Hide resolved
converters.put(Character.class, source -> {
Preconditions.condition(source.length() == 1, () -> "String must have length of 1: " + source);
return source.charAt(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.junit.jupiter.params.converter;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.lang.Thread.State;
Expand Down Expand Up @@ -99,6 +100,19 @@ void convertsStringsToPrimitiveTypes() {
assertConverts("42.2_3", double.class, 42.23);
}

@Test
void throwsExceptionOnInvalidStringForPrimitiveTypes() {
var invalidCharException = assertThrows(ArgumentConversionException.class, () -> convert("ab", char.class));
assertThat(invalidCharException).hasMessage("Failed to convert String \"ab\" to type java.lang.Character");
assertThat(invalidCharException.getCause()).hasMessage("String must have length of 1: ab");
s-rwe marked this conversation as resolved.
Show resolved Hide resolved

var invalidBooleanException = assertThrows(ArgumentConversionException.class,
() -> convert("tru", boolean.class));
assertThat(invalidBooleanException).hasMessage("Failed to convert String \"tru\" to type java.lang.Boolean");
assertThat(invalidBooleanException.getCause()).hasMessage(
"String must be (ignoring case) 'true' or 'false': tru");
}
s-rwe marked this conversation as resolved.
Show resolved Hide resolved

/**
* @since 5.4
*/
Expand Down Expand Up @@ -232,11 +246,14 @@ void convertsStringToUUID() {
// -------------------------------------------------------------------------

private void assertConverts(Object input, Class<?> targetClass, Object expectedOutput) {
var result = DefaultArgumentConverter.INSTANCE.convert(input, targetClass);
var result = convert(input, targetClass);

assertThat(result) //
.describedAs(input + " --(" + targetClass.getName() + ")--> " + expectedOutput) //
.isEqualTo(expectedOutput);
}

private Object convert(Object input, Class<?> targetClass) {
return DefaultArgumentConverter.INSTANCE.convert(input, targetClass);
}
}