Skip to content

Commit 51c084f

Browse files
committed
Polishing
1 parent 5be83e9 commit 51c084f

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

Diff for: spring-core/src/main/java/org/springframework/core/convert/support/ObjectToOptionalConverter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@
2929
import org.springframework.util.CollectionUtils;
3030

3131
/**
32-
* Convert an Object to {@code java.util.Optional<T>} if necessary using the
32+
* Convert an Object to a {@code java.util.Optional<T>}, if necessary using the
3333
* {@code ConversionService} to convert the source Object to the generic type
3434
* of Optional when known.
3535
*

Diff for: spring-core/src/test/java/org/springframework/core/convert/converter/DefaultConversionServiceTests.java

+38-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
import java.util.stream.Stream;
4545

4646
import org.jspecify.annotations.Nullable;
47+
import org.junit.jupiter.api.Nested;
4748
import org.junit.jupiter.api.Test;
4849

4950
import org.springframework.core.MethodParameter;
@@ -948,29 +949,44 @@ void convertCannotOptimizeArray() {
948949
assertThat(converted).containsExactly(2, 3, 4);
949950
}
950951

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-
}
961952

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 {
968955

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+
}
974990
}
975991

976992

@@ -1068,9 +1084,6 @@ public Long getId() {
10681084
public static TestEntity findTestEntity(Long id) {
10691085
return new TestEntity(id);
10701086
}
1071-
1072-
public void handleOptionalValue(Optional<List<Integer>> value) {
1073-
}
10741087
}
10751088

10761089

0 commit comments

Comments
 (0)