|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
44 | 44 | import java.util.stream.Stream;
|
45 | 45 |
|
46 | 46 | import org.jspecify.annotations.Nullable;
|
| 47 | +import org.junit.jupiter.api.Nested; |
47 | 48 | import org.junit.jupiter.api.Test;
|
48 | 49 |
|
49 | 50 | import org.springframework.core.MethodParameter;
|
@@ -948,29 +949,44 @@ void convertCannotOptimizeArray() {
|
948 | 949 | assertThat(converted).containsExactly(2, 3, 4);
|
949 | 950 | }
|
950 | 951 |
|
951 |
| - @Test |
952 |
| - @SuppressWarnings("unchecked") |
953 |
| - void convertObjectToOptional() { |
954 |
| - Method method = ClassUtils.getMethod(TestEntity.class, "handleOptionalValue", Optional.class); |
955 |
| - MethodParameter parameter = new MethodParameter(method, 0); |
956 |
| - TypeDescriptor descriptor = new TypeDescriptor(parameter); |
957 |
| - Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor); |
958 |
| - assertThat(actual.getClass()).isEqualTo(Optional.class); |
959 |
| - assertThat(((Optional<List<Integer>>) actual)).contains(List.of(1, 2, 3)); |
960 |
| - } |
961 | 952 |
|
962 |
| - @Test |
963 |
| - void convertObjectToOptionalNull() { |
964 |
| - assertThat(conversionService.convert(null, TypeDescriptor.valueOf(Object.class), |
965 |
| - TypeDescriptor.valueOf(Optional.class))).isSameAs(Optional.empty()); |
966 |
| - assertThat((Object) conversionService.convert(null, Optional.class)).isSameAs(Optional.empty()); |
967 |
| - } |
| 953 | + @Nested |
| 954 | + class OptionalConversionTests { |
968 | 955 |
|
969 |
| - @Test |
970 |
| - void convertExistingOptional() { |
971 |
| - assertThat(conversionService.convert(Optional.empty(), TypeDescriptor.valueOf(Object.class), |
972 |
| - TypeDescriptor.valueOf(Optional.class))).isSameAs(Optional.empty()); |
973 |
| - assertThat((Object) conversionService.convert(Optional.empty(), Optional.class)).isSameAs(Optional.empty()); |
| 956 | + private static final TypeDescriptor rawOptionalType = TypeDescriptor.valueOf(Optional.class); |
| 957 | + |
| 958 | + |
| 959 | + @Test |
| 960 | + @SuppressWarnings("unchecked") |
| 961 | + void convertObjectToOptional() { |
| 962 | + Method method = ClassUtils.getMethod(getClass(), "handleOptionalList", Optional.class); |
| 963 | + MethodParameter parameter = new MethodParameter(method, 0); |
| 964 | + TypeDescriptor descriptor = new TypeDescriptor(parameter); |
| 965 | + Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor); |
| 966 | + assertThat(((Optional<List<Integer>>) actual)).contains(List.of(1, 2, 3)); |
| 967 | + } |
| 968 | + |
| 969 | + @Test |
| 970 | + void convertNullToOptional() { |
| 971 | + assertThat((Object) conversionService.convert(null, Optional.class)).isSameAs(Optional.empty()); |
| 972 | + assertThat(conversionService.convert(null, TypeDescriptor.valueOf(Object.class), rawOptionalType)) |
| 973 | + .isSameAs(Optional.empty()); |
| 974 | + } |
| 975 | + |
| 976 | + @Test |
| 977 | + void convertNullOptionalToNull() { |
| 978 | + assertThat(conversionService.convert(null, rawOptionalType, TypeDescriptor.valueOf(Object.class))).isNull(); |
| 979 | + } |
| 980 | + |
| 981 | + @Test |
| 982 | + void convertEmptyOptionalToOptional() { |
| 983 | + assertThat((Object) conversionService.convert(Optional.empty(), Optional.class)).isSameAs(Optional.empty()); |
| 984 | + assertThat(conversionService.convert(Optional.empty(), TypeDescriptor.valueOf(Object.class), rawOptionalType)) |
| 985 | + .isSameAs(Optional.empty()); |
| 986 | + } |
| 987 | + |
| 988 | + public void handleOptionalList(Optional<List<Integer>> value) { |
| 989 | + } |
974 | 990 | }
|
975 | 991 |
|
976 | 992 |
|
@@ -1068,9 +1084,6 @@ public Long getId() {
|
1068 | 1084 | public static TestEntity findTestEntity(Long id) {
|
1069 | 1085 | return new TestEntity(id);
|
1070 | 1086 | }
|
1071 |
| - |
1072 |
| - public void handleOptionalValue(Optional<List<Integer>> value) { |
1073 |
| - } |
1074 | 1087 | }
|
1075 | 1088 |
|
1076 | 1089 |
|
|
0 commit comments