15
15
*/
16
16
package org .springframework .data .jdbc .core .convert ;
17
17
18
+ import static java .util .Arrays .*;
19
+ import static java .util .Collections .*;
18
20
import static org .assertj .core .api .Assertions .*;
19
21
import static org .mockito .ArgumentMatchers .*;
20
22
import static org .mockito .Mockito .*;
23
25
import lombok .AllArgsConstructor ;
24
26
import lombok .Data ;
25
27
import lombok .RequiredArgsConstructor ;
28
+ import lombok .Value ;
26
29
27
30
import java .util .ArrayList ;
28
- import java .util .Arrays ;
29
31
import java .util .HashMap ;
30
32
import java .util .List ;
31
33
32
- import lombok . Value ;
34
+ import org . jetbrains . annotations . NotNull ;
33
35
import org .junit .Before ;
34
36
import org .junit .Test ;
35
37
import org .mockito .ArgumentCaptor ;
36
-
37
38
import org .springframework .core .convert .converter .Converter ;
38
39
import org .springframework .data .annotation .Id ;
39
40
import org .springframework .data .convert .ReadingConverter ;
@@ -96,7 +97,7 @@ public void additionalParameterForIdDoesNotLeadToDuplicateParameters() {
96
97
97
98
additionalParameters .put (SqlIdentifier .quoted ("ID" ), ID_FROM_ADDITIONAL_VALUES );
98
99
99
- accessStrategy .insert (new DummyEntity (ORIGINAL_ID ), DummyEntity .class , Identifier .from ( additionalParameters ));
100
+ accessStrategy .insert (new DummyEntity (ORIGINAL_ID ), DummyEntity .class , Identifier .from (additionalParameters ));
100
101
101
102
verify (namedJdbcOperations ).update (eq ("INSERT INTO \" DUMMY_ENTITY\" (\" ID\" ) VALUES (:ID)" ),
102
103
paramSourceCaptor .capture (), any (KeyHolder .class ));
@@ -122,21 +123,8 @@ public void additionalParametersGetAddedToStatement() {
122
123
@ Test // DATAJDBC-235
123
124
public void considersConfiguredWriteConverter () {
124
125
125
- DelegatingDataAccessStrategy relationResolver = new DelegatingDataAccessStrategy ();
126
-
127
- Dialect dialect = HsqlDbDialect .INSTANCE ;
128
-
129
- JdbcConverter converter = new BasicJdbcConverter (context , relationResolver ,
130
- new JdbcCustomConversions (Arrays .asList (BooleanToStringConverter .INSTANCE , StringToBooleanConverter .INSTANCE )),
131
- new DefaultJdbcTypeFactory (jdbcOperations ), dialect .getIdentifierProcessing ());
132
-
133
- DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy ( //
134
- new SqlGeneratorSource (context , converter , dialect ), //
135
- context , //
136
- converter , //
137
- namedJdbcOperations );
138
-
139
- relationResolver .setDelegate (accessStrategy );
126
+ DefaultDataAccessStrategy accessStrategy = createAccessStrategyWithConverter (
127
+ asList (BooleanToStringConverter .INSTANCE , StringToBooleanConverter .INSTANCE ));
140
128
141
129
ArgumentCaptor <String > sqlCaptor = ArgumentCaptor .forClass (String .class );
142
130
@@ -153,21 +141,8 @@ public void considersConfiguredWriteConverter() {
153
141
@ Test // DATAJDBC-412
154
142
public void considersConfiguredWriteConverterForIdValueObjects () {
155
143
156
- DelegatingDataAccessStrategy relationResolver = new DelegatingDataAccessStrategy ();
157
-
158
- Dialect dialect = HsqlDbDialect .INSTANCE ;
159
-
160
- JdbcConverter converter = new BasicJdbcConverter (context , relationResolver ,
161
- new JdbcCustomConversions (Arrays .asList (IdValueToStringConverter .INSTANCE )),
162
- new DefaultJdbcTypeFactory (jdbcOperations ), dialect .getIdentifierProcessing ());
163
-
164
- DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy ( //
165
- new SqlGeneratorSource (context , converter , dialect ), //
166
- context , //
167
- converter , //
168
- namedJdbcOperations );
169
-
170
- relationResolver .setDelegate (accessStrategy );
144
+ DefaultDataAccessStrategy accessStrategy = createAccessStrategyWithConverter (
145
+ singletonList (IdValueToStringConverter .INSTANCE ));
171
146
172
147
String rawId = "batman" ;
173
148
@@ -187,24 +162,11 @@ public void considersConfiguredWriteConverterForIdValueObjects() {
187
162
assertThat (paramSourceCaptor .getValue ().getValue ("id" )).isEqualTo (rawId );
188
163
}
189
164
190
- @ Test // DATAJDBC-587
165
+ @ Test // DATAJDBC-349
191
166
public void considersConfiguredWriteConverterForIdValueObjectsWhichReferencedInOneToManyRelationship () {
192
167
193
- DelegatingDataAccessStrategy relationResolver = new DelegatingDataAccessStrategy ();
194
-
195
- Dialect dialect = HsqlDbDialect .INSTANCE ;
196
-
197
- JdbcConverter converter = new BasicJdbcConverter (context , relationResolver ,
198
- new JdbcCustomConversions (Arrays .asList (IdValueToStringConverter .INSTANCE )),
199
- new DefaultJdbcTypeFactory (jdbcOperations ), dialect .getIdentifierProcessing ());
200
-
201
- DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy ( //
202
- new SqlGeneratorSource (context , converter , dialect ), //
203
- context , //
204
- converter , //
205
- namedJdbcOperations );
206
-
207
- relationResolver .setDelegate (accessStrategy );
168
+ DefaultDataAccessStrategy accessStrategy = createAccessStrategyWithConverter (
169
+ singletonList (IdValueToStringConverter .INSTANCE ));
208
170
209
171
String rawId = "batman" ;
210
172
IdValue rootIdValue = new IdValue (rawId );
@@ -216,29 +178,46 @@ public void considersConfiguredWriteConverterForIdValueObjectsWhichReferencedInO
216
178
additionalParameters .put (SqlIdentifier .quoted ("DUMMYENTITYROOT" ), rootIdValue );
217
179
accessStrategy .insert (root , DummyEntityRoot .class , Identifier .from (additionalParameters ));
218
180
219
- verify (namedJdbcOperations ).update (anyString (), paramSourceCaptor .capture (),
220
- any (KeyHolder .class ));
181
+ verify (namedJdbcOperations ).update (anyString (), paramSourceCaptor .capture (), any (KeyHolder .class ));
221
182
222
183
assertThat (paramSourceCaptor .getValue ().getValue ("id" )).isEqualTo (rawId );
223
184
224
- PersistentPropertyPath <RelationalPersistentProperty > path =
225
- context . getPersistentPropertyPath ( "dummyEntities" , DummyEntityRoot .class );
185
+ PersistentPropertyPath <RelationalPersistentProperty > path = context . getPersistentPropertyPath ( "dummyEntities" ,
186
+ DummyEntityRoot .class );
226
187
227
188
accessStrategy .findAllByPath (Identifier .from (additionalParameters ), path );
228
189
229
- verify (namedJdbcOperations ).query (anyString (), paramSourceCaptor .capture (),
230
- any (RowMapper .class ));
190
+ verify (namedJdbcOperations ).query (anyString (), paramSourceCaptor .capture (), any (RowMapper .class ));
231
191
232
192
assertThat (paramSourceCaptor .getValue ().getValue ("DUMMYENTITYROOT" )).isEqualTo (rawId );
233
193
}
234
194
195
+ @ NotNull
196
+ private DefaultDataAccessStrategy createAccessStrategyWithConverter (List <?> converters ) {
197
+ DelegatingDataAccessStrategy relationResolver = new DelegatingDataAccessStrategy ();
198
+
199
+ Dialect dialect = HsqlDbDialect .INSTANCE ;
200
+
201
+ JdbcConverter converter = new BasicJdbcConverter (context , relationResolver , new JdbcCustomConversions (converters ),
202
+ new DefaultJdbcTypeFactory (jdbcOperations ), dialect .getIdentifierProcessing ());
203
+
204
+ DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy ( //
205
+ new SqlGeneratorSource (context , converter , dialect ), //
206
+ context , //
207
+ converter , //
208
+ namedJdbcOperations );
209
+
210
+ relationResolver .setDelegate (accessStrategy );
211
+ return accessStrategy ;
212
+ }
213
+
235
214
@ RequiredArgsConstructor
236
215
private static class DummyEntity {
237
216
238
217
@ Id private final Long id ;
239
218
}
240
219
241
- @ RequiredArgsConstructor // DATAJDBC-587
220
+ @ RequiredArgsConstructor // DATAJDBC-349
242
221
private static class DummyEntityRoot {
243
222
244
223
@ Id private final IdValue id ;
0 commit comments