Merge branch '6.2.x'

This commit is contained in:
Sam Brannen
2025-07-09 12:56:33 +02:00
6 changed files with 208 additions and 9 deletions
@@ -17,6 +17,7 @@
package org.springframework.aot.hint.support;
import java.net.URI;
import java.time.Instant;
import java.time.LocalDate;
import org.junit.jupiter.api.BeforeEach;
@@ -24,38 +25,45 @@ import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
/**
* Tests for {@link ObjectToObjectConverterRuntimeHints}.
*
* @author Sebastien Deleuze
* @author Sam Brannen
*/
class ObjectToObjectConverterRuntimeHintsTests {
private RuntimeHints hints;
private final RuntimeHints hints = new RuntimeHints();
@BeforeEach
void setup() {
this.hints = new RuntimeHints();
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
.load(RuntimeHintsRegistrar.class).forEach(registrar -> registrar
.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
.load(RuntimeHintsRegistrar.class)
.forEach(registrar -> registrar.registerHints(this.hints, classLoader));
}
@Test
void javaSqlDateHasHints() throws NoSuchMethodException {
assertThat(RuntimeHintsPredicates.reflection().onMethodInvocation(java.sql.Date.class, "toLocalDate")).accepts(this.hints);
assertThat(RuntimeHintsPredicates.reflection().onMethodInvocation(java.sql.Date.class.getMethod("valueOf", LocalDate.class))).accepts(this.hints);
assertThat(reflection().onMethodInvocation(java.sql.Date.class, "toLocalDate")).accepts(this.hints);
assertThat(reflection().onMethodInvocation(java.sql.Date.class.getMethod("valueOf", LocalDate.class))).accepts(this.hints);
}
@Test // gh-35156
void javaSqlTimestampHasHints() throws NoSuchMethodException {
assertThat(reflection().onMethodInvocation(java.sql.Timestamp.class.getMethod("from", Instant.class))).accepts(this.hints);
}
@Test
void uriHasHints() throws NoSuchMethodException {
assertThat(RuntimeHintsPredicates.reflection().onType(URI.class)).accepts(this.hints);
void uriHasHints() {
assertThat(reflection().onType(URI.class)).accepts(this.hints);
}
}
@@ -22,12 +22,16 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneId;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Currency;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -1066,6 +1070,88 @@ class DefaultConversionServiceTests {
}
}
@Test // gh-35175
void convertDateToInstant() {
TypeDescriptor dateDescriptor = TypeDescriptor.valueOf(Date.class);
TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class);
Date date = new Date();
// Conversion performed by DateToInstantConverter.
assertThat(conversionService.convert(date, dateDescriptor, instantDescriptor))
.isEqualTo(date.toInstant());
}
@Test // gh-35175
void convertSqlDateToInstant() {
TypeDescriptor sqlDateDescriptor = TypeDescriptor.valueOf(java.sql.Date.class);
TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class);
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
// DateToInstantConverter blindly invokes toInstant() on any java.util.Date
// subtype, which results in an UnsupportedOperationException since
// java.sql.Date does not have a time component. However, even if
// DateToInstantConverter were not registered, ObjectToObjectConverter
// would still attempt to invoke toInstant() on a java.sql.Date by convention,
// which results in the same UnsupportedOperationException.
assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> conversionService.convert(sqlDate, sqlDateDescriptor, instantDescriptor))
.withCauseExactlyInstanceOf(UnsupportedOperationException.class);
}
@Test // gh-35175
void convertSqlTimeToInstant() {
TypeDescriptor timeDescriptor = TypeDescriptor.valueOf(Time.class);
TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class);
Time time = new Time(System.currentTimeMillis());
// DateToInstantConverter blindly invokes toInstant() on any java.util.Date
// subtype, which results in an UnsupportedOperationException since
// java.sql.Date does not have a time component. However, even if
// DateToInstantConverter were not registered, ObjectToObjectConverter
// would still attempt to invoke toInstant() on a java.sql.Date by convention,
// which results in the same UnsupportedOperationException.
assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> conversionService.convert(time, timeDescriptor, instantDescriptor))
.withCauseExactlyInstanceOf(UnsupportedOperationException.class);
}
@Test // gh-35175
void convertSqlTimestampToInstant() {
TypeDescriptor timestampDescriptor = TypeDescriptor.valueOf(Timestamp.class);
TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// Conversion performed by DateToInstantConverter.
assertThat(conversionService.convert(timestamp, timestampDescriptor, instantDescriptor))
.isEqualTo(timestamp.toInstant());
}
@Test // gh-35175
void convertInstantToDate() {
TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class);
TypeDescriptor dateDescriptor = TypeDescriptor.valueOf(Date.class);
Date date = new Date();
Instant instant = date.toInstant();
// Conversion performed by InstantToDateConverter.
assertThat(conversionService.convert(instant, instantDescriptor, dateDescriptor))
.isExactlyInstanceOf(Date.class)
.isEqualTo(date);
}
@Test
void convertInstantToSqlTimestamp() {
TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class);
TypeDescriptor timestampDescriptor = TypeDescriptor.valueOf(Timestamp.class);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Instant instant = timestamp.toInstant();
// Conversion performed by ObjectToObjectConverter.
assertThat(conversionService.convert(instant, instantDescriptor, timestampDescriptor))
.isExactlyInstanceOf(Timestamp.class)
.isEqualTo(timestamp);
}
// test fields and helpers