Add test cases for ObjectUtils#unwrapOptional

See gh-33618
This commit is contained in:
Tran Ngoc Nhan 2024-10-01 01:02:21 +07:00 committed by Sébastien Deleuze
parent fc8bd64f34
commit 4d87c77649
1 changed files with 19 additions and 0 deletions

View File

@ -62,6 +62,7 @@ import static org.springframework.util.ObjectUtils.isEmpty;
* @author Rick Evans
* @author Sam Brannen
* @author Hyunjin Choi
* @author Ngoc Nhan
*/
class ObjectUtilsTests {
@ -1114,4 +1115,22 @@ class ObjectUtilsTests {
}
}
@Test
void unwrapOptional() {
assertThat(ObjectUtils.unwrapOptional(null)).isNull();
assertThat(ObjectUtils.unwrapOptional(Optional.empty())).isNull();
assertThat(ObjectUtils.unwrapOptional(Optional.of("some value"))).isEqualTo("some value");
Optional<Optional<Object>> nestedEmptyOptional = Optional.of(Optional.empty());
assertThatIllegalArgumentException()
.isThrownBy(() -> ObjectUtils.unwrapOptional(nestedEmptyOptional))
.withMessage("Multi-level Optional usage not supported");
Optional<Optional<String>> nestedStringOptional = Optional.of(Optional.of("some value"));
assertThatIllegalArgumentException()
.isThrownBy(() -> ObjectUtils.unwrapOptional(nestedStringOptional))
.withMessage("Multi-level Optional usage not supported");
}
}