|
16 | 16 | package org.springframework.data.jdbc.core.convert; |
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
19 | 20 |
|
20 | 21 | import lombok.Data; |
21 | 22 |
|
| 23 | +import java.sql.Array; |
22 | 24 | import java.sql.Timestamp; |
23 | 25 | import java.time.Instant; |
24 | 26 | import java.time.LocalDate; |
|
35 | 37 | import org.springframework.data.annotation.Id; |
36 | 38 | import org.springframework.data.jdbc.core.mapping.AggregateReference; |
37 | 39 | import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; |
| 40 | +import org.springframework.data.jdbc.support.JdbcUtil; |
38 | 41 | import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; |
39 | 42 | import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; |
| 43 | +import org.springframework.data.relational.core.sql.IdentifierProcessing; |
40 | 44 | import org.springframework.data.util.ClassTypeInformation; |
41 | 45 |
|
42 | 46 | /** |
|
47 | 51 | public class BasicJdbcConverterUnitTests { |
48 | 52 |
|
49 | 53 | JdbcMappingContext context = new JdbcMappingContext(); |
50 | | - BasicJdbcConverter converter = new BasicJdbcConverter(context, (identifier, path) -> { |
51 | | - throw new UnsupportedOperationException(); |
52 | | - }); |
| 54 | + StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory(); |
| 55 | + BasicJdbcConverter converter = new BasicJdbcConverter( // |
| 56 | + context, // |
| 57 | + (identifier, path) -> { |
| 58 | + throw new UnsupportedOperationException(); |
| 59 | + }, // |
| 60 | + new JdbcCustomConversions(), // |
| 61 | + typeFactory, IdentifierProcessing.ANSI // |
| 62 | + ); |
53 | 63 |
|
54 | 64 | @Test // DATAJDBC-104, DATAJDBC-1384 |
55 | 65 | public void testTargetTypesForPropertyType() { |
@@ -110,14 +120,25 @@ public void conversionOfDateLikeValueAndBackYieldsOriginalValue() { |
110 | 120 | LocalDateTime testLocalDateTime = LocalDateTime.of(2001, 2, 3, 4, 5, 6, 123456789); |
111 | 121 | checkConversionToTimestampAndBack(softly, persistentEntity, "localDateTime", testLocalDateTime); |
112 | 122 | checkConversionToTimestampAndBack(softly, persistentEntity, "localDate", LocalDate.of(2001, 2, 3)); |
113 | | - checkConversionToTimestampAndBack(softly, persistentEntity, "localTime", LocalTime.of(1, 2, 3,123456789)); |
114 | | - checkConversionToTimestampAndBack(softly, persistentEntity, "instant", testLocalDateTime.toInstant(ZoneOffset.UTC)); |
| 123 | + checkConversionToTimestampAndBack(softly, persistentEntity, "localTime", LocalTime.of(1, 2, 3, 123456789)); |
| 124 | + checkConversionToTimestampAndBack(softly, persistentEntity, "instant", |
| 125 | + testLocalDateTime.toInstant(ZoneOffset.UTC)); |
115 | 126 | }); |
116 | 127 |
|
117 | 128 | } |
118 | 129 |
|
119 | | - private void checkConversionToTimestampAndBack(SoftAssertions softly, RelationalPersistentEntity<?> persistentEntity, String propertyName, |
120 | | - Object value) { |
| 130 | + @Test // #945 |
| 131 | + public void conversionOfPrimitiveArrays() { |
| 132 | + |
| 133 | + int[] ints = { 1, 2, 3, 4, 5 }; |
| 134 | + JdbcValue converted = converter.writeJdbcValue(ints, ints.getClass(), JdbcUtil.sqlTypeFor(ints.getClass())); |
| 135 | + |
| 136 | + assertThat(converted.getValue()).isInstanceOf(Array.class); |
| 137 | + assertThat(typeFactory.arraySource).containsExactly(1, 2, 3, 4, 5); |
| 138 | + } |
| 139 | + |
| 140 | + private void checkConversionToTimestampAndBack(SoftAssertions softly, RelationalPersistentEntity<?> persistentEntity, |
| 141 | + String propertyName, Object value) { |
121 | 142 |
|
122 | 143 | RelationalPersistentProperty property = persistentEntity.getRequiredPersistentProperty(propertyName); |
123 | 144 |
|
@@ -165,4 +186,14 @@ private enum SomeEnum { |
165 | 186 |
|
166 | 187 | @SuppressWarnings("unused") |
167 | 188 | private static class OtherEntity {} |
| 189 | + |
| 190 | + private static class StubbedJdbcTypeFactory implements JdbcTypeFactory { |
| 191 | + public Object[] arraySource; |
| 192 | + |
| 193 | + @Override |
| 194 | + public Array createArray(Object[] value) { |
| 195 | + arraySource = value; |
| 196 | + return mock(Array.class); |
| 197 | + } |
| 198 | + } |
168 | 199 | } |
0 commit comments