|
22 | 22 | import java.math.BigInteger;
|
23 | 23 | import java.nio.charset.Charset;
|
24 | 24 | import java.nio.charset.StandardCharsets;
|
| 25 | +import java.sql.Time; |
| 26 | +import java.sql.Timestamp; |
| 27 | +import java.time.Instant; |
25 | 28 | import java.time.ZoneId;
|
26 | 29 | import java.util.AbstractList;
|
27 | 30 | import java.util.ArrayList;
|
28 | 31 | import java.util.Collection;
|
29 | 32 | import java.util.Collections;
|
30 | 33 | import java.util.Currency;
|
| 34 | +import java.util.Date; |
31 | 35 | import java.util.EnumSet;
|
32 | 36 | import java.util.HashMap;
|
33 | 37 | import java.util.LinkedHashMap;
|
@@ -973,6 +977,88 @@ void convertExistingOptional() {
|
973 | 977 | assertThat((Object) conversionService.convert(Optional.empty(), Optional.class)).isSameAs(Optional.empty());
|
974 | 978 | }
|
975 | 979 |
|
| 980 | + @Test // gh-35175 |
| 981 | + void convertDateToInstant() { |
| 982 | + TypeDescriptor dateDescriptor = TypeDescriptor.valueOf(Date.class); |
| 983 | + TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class); |
| 984 | + Date date = new Date(); |
| 985 | + |
| 986 | + // Conversion performed by DateToInstantConverter. |
| 987 | + assertThat(conversionService.convert(date, dateDescriptor, instantDescriptor)) |
| 988 | + .isEqualTo(date.toInstant()); |
| 989 | + } |
| 990 | + |
| 991 | + @Test // gh-35175 |
| 992 | + void convertSqlDateToInstant() { |
| 993 | + TypeDescriptor sqlDateDescriptor = TypeDescriptor.valueOf(java.sql.Date.class); |
| 994 | + TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class); |
| 995 | + java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis()); |
| 996 | + |
| 997 | + // DateToInstantConverter blindly invokes toInstant() on any java.util.Date |
| 998 | + // subtype, which results in an UnsupportedOperationException since |
| 999 | + // java.sql.Date does not have a time component. However, even if |
| 1000 | + // DateToInstantConverter were not registered, ObjectToObjectConverter |
| 1001 | + // would still attempt to invoke toInstant() on a java.sql.Date by convention, |
| 1002 | + // which results in the same UnsupportedOperationException. |
| 1003 | + assertThatExceptionOfType(ConversionFailedException.class) |
| 1004 | + .isThrownBy(() -> conversionService.convert(sqlDate, sqlDateDescriptor, instantDescriptor)) |
| 1005 | + .withCauseExactlyInstanceOf(UnsupportedOperationException.class); |
| 1006 | + } |
| 1007 | + |
| 1008 | + @Test // gh-35175 |
| 1009 | + void convertSqlTimeToInstant() { |
| 1010 | + TypeDescriptor timeDescriptor = TypeDescriptor.valueOf(Time.class); |
| 1011 | + TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class); |
| 1012 | + Time time = new Time(System.currentTimeMillis()); |
| 1013 | + |
| 1014 | + // DateToInstantConverter blindly invokes toInstant() on any java.util.Date |
| 1015 | + // subtype, which results in an UnsupportedOperationException since |
| 1016 | + // java.sql.Date does not have a time component. However, even if |
| 1017 | + // DateToInstantConverter were not registered, ObjectToObjectConverter |
| 1018 | + // would still attempt to invoke toInstant() on a java.sql.Date by convention, |
| 1019 | + // which results in the same UnsupportedOperationException. |
| 1020 | + assertThatExceptionOfType(ConversionFailedException.class) |
| 1021 | + .isThrownBy(() -> conversionService.convert(time, timeDescriptor, instantDescriptor)) |
| 1022 | + .withCauseExactlyInstanceOf(UnsupportedOperationException.class); |
| 1023 | + } |
| 1024 | + |
| 1025 | + @Test // gh-35175 |
| 1026 | + void convertSqlTimestampToInstant() { |
| 1027 | + TypeDescriptor timestampDescriptor = TypeDescriptor.valueOf(Timestamp.class); |
| 1028 | + TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class); |
| 1029 | + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); |
| 1030 | + |
| 1031 | + // Conversion performed by DateToInstantConverter. |
| 1032 | + assertThat(conversionService.convert(timestamp, timestampDescriptor, instantDescriptor)) |
| 1033 | + .isEqualTo(timestamp.toInstant()); |
| 1034 | + } |
| 1035 | + |
| 1036 | + @Test // gh-35175 |
| 1037 | + void convertInstantToDate() { |
| 1038 | + TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class); |
| 1039 | + TypeDescriptor dateDescriptor = TypeDescriptor.valueOf(Date.class); |
| 1040 | + Date date = new Date(); |
| 1041 | + Instant instant = date.toInstant(); |
| 1042 | + |
| 1043 | + // Conversion performed by InstantToDateConverter. |
| 1044 | + assertThat(conversionService.convert(instant, instantDescriptor, dateDescriptor)) |
| 1045 | + .isExactlyInstanceOf(Date.class) |
| 1046 | + .isEqualTo(date); |
| 1047 | + } |
| 1048 | + |
| 1049 | + @Test |
| 1050 | + void convertInstantToSqlTimestamp() { |
| 1051 | + TypeDescriptor instantDescriptor = TypeDescriptor.valueOf(Instant.class); |
| 1052 | + TypeDescriptor timestampDescriptor = TypeDescriptor.valueOf(Timestamp.class); |
| 1053 | + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); |
| 1054 | + Instant instant = timestamp.toInstant(); |
| 1055 | + |
| 1056 | + // Conversion performed by ObjectToObjectConverter. |
| 1057 | + assertThat(conversionService.convert(instant, instantDescriptor, timestampDescriptor)) |
| 1058 | + .isExactlyInstanceOf(Timestamp.class) |
| 1059 | + .isEqualTo(timestamp); |
| 1060 | + } |
| 1061 | + |
976 | 1062 |
|
977 | 1063 | // test fields and helpers
|
978 | 1064 |
|
|
0 commit comments