15
15
*/
16
16
package org .springframework .data .jdbc .core ;
17
17
18
+ import static java .util .Arrays .*;
19
+ import static org .assertj .core .api .Assertions .*;
20
+ import static org .mockito .Mockito .*;
21
+
18
22
import lombok .RequiredArgsConstructor ;
23
+
24
+ import java .sql .ResultSet ;
25
+ import java .sql .SQLException ;
26
+ import java .util .AbstractMap .SimpleEntry ;
27
+ import java .util .ArrayList ;
28
+ import java .util .HashMap ;
29
+ import java .util .HashSet ;
30
+ import java .util .List ;
31
+ import java .util .Map ;
32
+ import java .util .Set ;
33
+
34
+ import javax .naming .OperationNotSupportedException ;
35
+
19
36
import org .junit .Test ;
20
37
import org .mockito .invocation .InvocationOnMock ;
21
38
import org .mockito .stubbing .Answer ;
22
39
import org .springframework .core .convert .support .DefaultConversionService ;
23
40
import org .springframework .core .convert .support .GenericConversionService ;
24
41
import org .springframework .data .annotation .Id ;
25
42
import org .springframework .data .convert .Jsr310Converters ;
26
- import org .springframework .data .jdbc .mapping .model .DefaultNamingStrategy ;
27
43
import org .springframework .data .jdbc .mapping .model .JdbcMappingContext ;
28
44
import org .springframework .data .jdbc .mapping .model .JdbcPersistentEntity ;
29
45
import org .springframework .data .jdbc .mapping .model .JdbcPersistentProperty ;
30
46
import org .springframework .data .jdbc .mapping .model .NamingStrategy ;
31
47
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
32
48
import org .springframework .util .Assert ;
33
49
34
- import javax .naming .OperationNotSupportedException ;
35
- import java .sql .ResultSet ;
36
- import java .sql .SQLException ;
37
- import java .util .AbstractMap .SimpleEntry ;
38
- import java .util .ArrayList ;
39
- import java .util .HashMap ;
40
- import java .util .HashSet ;
41
- import java .util .List ;
42
- import java .util .Map ;
43
- import java .util .Set ;
44
-
45
- import static java .util .Arrays .*;
46
- import static org .assertj .core .api .Assertions .*;
47
- import static org .mockito .Mockito .*;
48
-
49
50
/**
50
51
* Tests the extraction of entities from a {@link ResultSet} by the {@link EntityRowMapper}.
51
52
*
@@ -56,10 +57,10 @@ public class EntityRowMapperUnitTests {
56
57
public static final long ID_FOR_ENTITY_REFERENCING_MAP = 42L ;
57
58
public static final long ID_FOR_ENTITY_REFERENCING_LIST = 4711L ;
58
59
public static final long ID_FOR_ENTITY_NOT_REFERENCING_MAP = 23L ;
59
- public static final DefaultNamingStrategy X_APPENDING_NAMINGSTRATEGY = new DefaultNamingStrategy () {
60
+ public static final NamingStrategy X_APPENDING_NAMINGSTRATEGY = new NamingStrategy () {
60
61
@ Override
61
62
public String getColumnName (JdbcPersistentProperty property ) {
62
- return super .getColumnName (property ) + "x" ;
63
+ return NamingStrategy . super .getColumnName (property ) + "x" ;
63
64
}
64
65
};
65
66
@@ -169,23 +170,22 @@ public void listReferenceGetsLoadedWithAdditionalSelect() throws SQLException {
169
170
}
170
171
171
172
private <T > EntityRowMapper <T > createRowMapper (Class <T > type ) {
172
- return createRowMapper (type , new DefaultNamingStrategy () );
173
+ return createRowMapper (type , NamingStrategy . INSTANCE );
173
174
}
174
175
175
176
private <T > EntityRowMapper <T > createRowMapper (Class <T > type , NamingStrategy namingStrategy ) {
176
177
177
178
JdbcMappingContext context = new JdbcMappingContext ( //
178
179
namingStrategy , //
179
180
mock (NamedParameterJdbcOperations .class ), //
180
- __ -> {
181
- } //
181
+ __ -> {} //
182
182
);
183
183
184
184
DataAccessStrategy accessStrategy = mock (DataAccessStrategy .class );
185
185
186
186
// the ID of the entity is used to determine what kind of ResultSet is needed for subsequent selects.
187
- doReturn (new HashSet <>(asList (new Trivial (), new Trivial ()))).when (accessStrategy ). findAllByProperty ( eq ( ID_FOR_ENTITY_NOT_REFERENCING_MAP ),
188
- any (JdbcPersistentProperty .class ));
187
+ doReturn (new HashSet <>(asList (new Trivial (), new Trivial ()))).when (accessStrategy )
188
+ . findAllByProperty ( eq ( ID_FOR_ENTITY_NOT_REFERENCING_MAP ), any (JdbcPersistentProperty .class ));
189
189
190
190
doReturn (new HashSet <>(asList ( //
191
191
new SimpleEntry ("one" , new Trivial ()), //
@@ -202,8 +202,8 @@ private <T> EntityRowMapper<T> createRowMapper(Class<T> type, NamingStrategy nam
202
202
DefaultConversionService .addDefaultConverters (conversionService );
203
203
Jsr310Converters .getConvertersToRegister ().forEach (conversionService ::addConverter );
204
204
205
- return new EntityRowMapper <>((JdbcPersistentEntity <T >) context .getRequiredPersistentEntity (type ),
206
- context , accessStrategy );
205
+ return new EntityRowMapper <>((JdbcPersistentEntity <T >) context .getRequiredPersistentEntity (type ), context ,
206
+ accessStrategy );
207
207
}
208
208
209
209
private static ResultSet mockResultSet (List <String > columns , Object ... values ) {
@@ -215,7 +215,7 @@ private static ResultSet mockResultSet(List<String> columns, Object... values) {
215
215
"Number of values [%d] must be a multiple of the number of columns [%d]" , //
216
216
values .length , //
217
217
columns .size () //
218
- ) //
218
+ ) //
219
219
);
220
220
221
221
List <Map <String , Object >> result = convertValues (columns , values );
@@ -291,7 +291,8 @@ private Object getObject(String column) {
291
291
292
292
Map <String , Object > rowMap = values .get (index );
293
293
294
- Assert .isTrue (rowMap .containsKey (column ), String .format ("Trying to access a column (%s) that does not exist" , column ));
294
+ Assert .isTrue (rowMap .containsKey (column ),
295
+ String .format ("Trying to access a column (%s) that does not exist" , column ));
295
296
296
297
return rowMap .get (column );
297
298
}
@@ -306,46 +307,40 @@ private boolean next() {
306
307
@ RequiredArgsConstructor
307
308
static class TrivialImmutable {
308
309
309
- @ Id
310
- private final Long id ;
310
+ @ Id private final Long id ;
311
311
private final String name ;
312
312
}
313
313
314
314
static class Trivial {
315
315
316
- @ Id
317
- Long id ;
316
+ @ Id Long id ;
318
317
String name ;
319
318
}
320
319
321
320
static class OneToOne {
322
321
323
- @ Id
324
- Long id ;
322
+ @ Id Long id ;
325
323
String name ;
326
324
Trivial child ;
327
325
}
328
326
329
327
static class OneToSet {
330
328
331
- @ Id
332
- Long id ;
329
+ @ Id Long id ;
333
330
String name ;
334
331
Set <Trivial > children ;
335
332
}
336
333
337
334
static class OneToMap {
338
335
339
- @ Id
340
- Long id ;
336
+ @ Id Long id ;
341
337
String name ;
342
338
Map <String , Trivial > children ;
343
339
}
344
340
345
341
static class OneToList {
346
342
347
- @ Id
348
- Long id ;
343
+ @ Id Long id ;
349
344
String name ;
350
345
List <Trivial > children ;
351
346
}
0 commit comments