16
16
17
17
package org .springframework .data .relational .repository .query ;
18
18
19
+ import static org .assertj .core .api .Assertions .*;
20
+ import static org .springframework .data .domain .ExampleMatcher .*;
21
+ import static org .springframework .data .domain .ExampleMatcher .GenericPropertyMatchers .*;
22
+ import static org .springframework .data .domain .ExampleMatcher .StringMatcher .*;
23
+
24
+ import java .util .List ;
25
+ import java .util .Map ;
26
+ import java .util .Objects ;
27
+
19
28
import org .junit .jupiter .api .BeforeEach ;
20
29
import org .junit .jupiter .api .Test ;
21
30
import org .springframework .data .annotation .Id ;
22
31
import org .springframework .data .domain .Example ;
23
32
import org .springframework .data .domain .ExampleMatcher ;
24
33
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
25
34
import org .springframework .data .relational .core .query .Query ;
26
-
27
- import java .util .Objects ;
28
-
29
- import static org .assertj .core .api .Assertions .*;
30
- import static org .springframework .data .domain .ExampleMatcher .GenericPropertyMatchers .*;
31
- import static org .springframework .data .domain .ExampleMatcher .StringMatcher .*;
32
- import static org .springframework .data .domain .ExampleMatcher .*;
35
+ import org .springframework .lang .Nullable ;
33
36
34
37
/**
35
38
* Verify that the {@link RelationalExampleMapper} properly turns {@link Example}s into {@link Query}'s.
@@ -48,8 +51,7 @@ public void before() {
48
51
@ Test // GH-929
49
52
void queryByExampleWithId () {
50
53
51
- Person person = new Person ();
52
- person .setId ("id1" );
54
+ Person person = new Person ("id1" , null , null , null , null , null );
53
55
54
56
Example <Person > example = Example .of (person );
55
57
@@ -63,8 +65,7 @@ void queryByExampleWithId() {
63
65
@ Test // GH-929
64
66
void queryByExampleWithFirstname () {
65
67
66
- Person person = new Person ();
67
- person .setFirstname ("Frodo" );
68
+ Person person = new Person (null , "Frodo" , null , null , null , null );
68
69
69
70
Example <Person > example = Example .of (person );
70
71
@@ -78,9 +79,7 @@ void queryByExampleWithFirstname() {
78
79
@ Test // GH-929
79
80
void queryByExampleWithFirstnameAndLastname () {
80
81
81
- Person person = new Person ();
82
- person .setFirstname ("Frodo" );
83
- person .setLastname ("Baggins" );
82
+ Person person = new Person (null , "Frodo" , "Baggins" , null , null , null );
84
83
85
84
Example <Person > example = Example .of (person );
86
85
@@ -94,8 +93,7 @@ void queryByExampleWithFirstnameAndLastname() {
94
93
@ Test // GH-929
95
94
void queryByExampleWithNullMatchingLastName () {
96
95
97
- Person person = new Person ();
98
- person .setLastname ("Baggins" );
96
+ Person person = new Person (null , null , "Baggins" , null , null , null );
99
97
100
98
ExampleMatcher matcher = matching ().withIncludeNullValues ();
101
99
Example <Person > example = Example .of (person , matcher );
@@ -110,9 +108,7 @@ void queryByExampleWithNullMatchingLastName() {
110
108
@ Test // GH-929
111
109
void queryByExampleWithNullMatchingFirstnameAndLastname () {
112
110
113
- Person person = new Person ();
114
- person .setFirstname ("Bilbo" );
115
- person .setLastname ("Baggins" );
111
+ Person person = new Person (null , "Bilbo" , "Baggins" , null , null , null );
116
112
117
113
ExampleMatcher matcher = matching ().withIncludeNullValues ();
118
114
Example <Person > example = Example .of (person , matcher );
@@ -127,9 +123,7 @@ void queryByExampleWithNullMatchingFirstnameAndLastname() {
127
123
@ Test // GH-929
128
124
void queryByExampleWithFirstnameAndLastnameIgnoringFirstname () {
129
125
130
- Person person = new Person ();
131
- person .setFirstname ("Frodo" );
132
- person .setLastname ("Baggins" );
126
+ Person person = new Person (null , "Bilbo" , "Baggins" , null , null , null );
133
127
134
128
ExampleMatcher matcher = matching ().withIgnorePaths ("firstname" );
135
129
Example <Person > example = Example .of (person , matcher );
@@ -144,9 +138,7 @@ void queryByExampleWithFirstnameAndLastnameIgnoringFirstname() {
144
138
@ Test // GH-929
145
139
void queryByExampleWithFirstnameAndLastnameWithNullMatchingIgnoringFirstName () {
146
140
147
- Person person = new Person ();
148
- person .setFirstname ("Frodo" );
149
- person .setLastname ("Baggins" );
141
+ Person person = new Person (null , "Bilbo" , "Baggins" , null , null , null );
150
142
151
143
ExampleMatcher matcher = matching ().withIncludeNullValues ().withIgnorePaths ("firstname" );
152
144
Example <Person > example = Example .of (person , matcher );
@@ -161,8 +153,7 @@ void queryByExampleWithFirstnameAndLastnameWithNullMatchingIgnoringFirstName() {
161
153
@ Test // GH-929
162
154
void queryByExampleWithFirstnameWithStringMatchingAtTheBeginning () {
163
155
164
- Person person = new Person ();
165
- person .setFirstname ("Fro" );
156
+ Person person = new Person (null , "Fro" , null , null , null , null );
166
157
167
158
ExampleMatcher matcher = matching ().withStringMatcher (STARTING );
168
159
Example <Person > example = Example .of (person , matcher );
@@ -177,8 +168,7 @@ void queryByExampleWithFirstnameWithStringMatchingAtTheBeginning() {
177
168
@ Test // GH-929
178
169
void queryByExampleWithFirstnameWithStringMatchingOnTheEnding () {
179
170
180
- Person person = new Person ();
181
- person .setFirstname ("do" );
171
+ Person person = new Person (null , "do" , null , null , null , null );
182
172
183
173
ExampleMatcher matcher = matching ().withStringMatcher (ENDING );
184
174
Example <Person > example = Example .of (person , matcher );
@@ -193,8 +183,7 @@ void queryByExampleWithFirstnameWithStringMatchingOnTheEnding() {
193
183
@ Test // GH-929
194
184
void queryByExampleWithFirstnameWithStringMatchingContaining () {
195
185
196
- Person person = new Person ();
197
- person .setFirstname ("do" );
186
+ Person person = new Person (null , "do" , null , null , null , null );
198
187
199
188
ExampleMatcher matcher = matching ().withStringMatcher (CONTAINING );
200
189
Example <Person > example = Example .of (person , matcher );
@@ -209,8 +198,7 @@ void queryByExampleWithFirstnameWithStringMatchingContaining() {
209
198
@ Test // GH-929
210
199
void queryByExampleWithFirstnameWithStringMatchingRegEx () {
211
200
212
- Person person = new Person ();
213
- person .setFirstname ("do" );
201
+ Person person = new Person (null , "do" , null , null , null , null );
214
202
215
203
ExampleMatcher matcher = matching ().withStringMatcher (ExampleMatcher .StringMatcher .REGEX );
216
204
Example <Person > example = Example .of (person , matcher );
@@ -222,8 +210,7 @@ void queryByExampleWithFirstnameWithStringMatchingRegEx() {
222
210
@ Test // GH-929
223
211
void queryByExampleWithFirstnameWithFieldSpecificStringMatcherEndsWith () {
224
212
225
- Person person = new Person ();
226
- person .setFirstname ("do" );
213
+ Person person = new Person (null , "do" , null , null , null , null );
227
214
228
215
ExampleMatcher matcher = matching ().withMatcher ("firstname" , endsWith ());
229
216
Example <Person > example = Example .of (person , matcher );
@@ -238,8 +225,7 @@ void queryByExampleWithFirstnameWithFieldSpecificStringMatcherEndsWith() {
238
225
@ Test // GH-929
239
226
void queryByExampleWithFirstnameWithFieldSpecificStringMatcherStartsWith () {
240
227
241
- Person person = new Person ();
242
- person .setFirstname ("Fro" );
228
+ Person person = new Person (null , "Fro" , null , null , null , null );
243
229
244
230
ExampleMatcher matcher = matching ().withMatcher ("firstname" , startsWith ());
245
231
Example <Person > example = Example .of (person , matcher );
@@ -254,8 +240,7 @@ void queryByExampleWithFirstnameWithFieldSpecificStringMatcherStartsWith() {
254
240
@ Test // GH-929
255
241
void queryByExampleWithFirstnameWithFieldSpecificStringMatcherContains () {
256
242
257
- Person person = new Person ();
258
- person .setFirstname ("do" );
243
+ Person person = new Person (null , "do" , null , null , null , null );
259
244
260
245
ExampleMatcher matcher = matching ().withMatcher ("firstname" , contains ());
261
246
Example <Person > example = Example .of (person , matcher );
@@ -270,8 +255,7 @@ void queryByExampleWithFirstnameWithFieldSpecificStringMatcherContains() {
270
255
@ Test // GH-929
271
256
void queryByExampleWithFirstnameWithStringMatchingAtTheBeginningIncludingNull () {
272
257
273
- Person person = new Person ();
274
- person .setFirstname ("Fro" );
258
+ Person person = new Person (null , "Fro" , null , null , null , null );
275
259
276
260
ExampleMatcher matcher = matching ().withStringMatcher (STARTING ).withIncludeNullValues ();
277
261
Example <Person > example = Example .of (person , matcher );
@@ -286,8 +270,7 @@ void queryByExampleWithFirstnameWithStringMatchingAtTheBeginningIncludingNull()
286
270
@ Test // GH-929
287
271
void queryByExampleWithFirstnameWithStringMatchingOnTheEndingIncludingNull () {
288
272
289
- Person person = new Person ();
290
- person .setFirstname ("do" );
273
+ Person person = new Person (null , "do" , null , null , null , null );
291
274
292
275
ExampleMatcher matcher = matching ().withStringMatcher (ENDING ).withIncludeNullValues ();
293
276
Example <Person > example = Example .of (person , matcher );
@@ -302,8 +285,7 @@ void queryByExampleWithFirstnameWithStringMatchingOnTheEndingIncludingNull() {
302
285
@ Test // GH-929
303
286
void queryByExampleWithFirstnameIgnoreCaseFieldLevel () {
304
287
305
- Person person = new Person ();
306
- person .setFirstname ("fro" );
288
+ Person person = new Person (null , "fro" , null , null , null , null );
307
289
308
290
ExampleMatcher matcher = matching ().withMatcher ("firstname" , startsWith ().ignoreCase ());
309
291
Example <Person > example = Example .of (person , matcher );
@@ -320,8 +302,7 @@ void queryByExampleWithFirstnameIgnoreCaseFieldLevel() {
320
302
@ Test // GH-929
321
303
void queryByExampleWithFirstnameWithStringMatchingContainingIncludingNull () {
322
304
323
- Person person = new Person ();
324
- person .setFirstname ("do" );
305
+ Person person = new Person (null , "do" , null , null , null , null );
325
306
326
307
ExampleMatcher matcher = matching ().withStringMatcher (CONTAINING ).withIncludeNullValues ();
327
308
Example <Person > example = Example .of (person , matcher );
@@ -336,8 +317,7 @@ void queryByExampleWithFirstnameWithStringMatchingContainingIncludingNull() {
336
317
@ Test // GH-929
337
318
void queryByExampleWithFirstnameIgnoreCase () {
338
319
339
- Person person = new Person ();
340
- person .setFirstname ("Frodo" );
320
+ Person person = new Person (null , "Frodo" , null , null , null , null );
341
321
342
322
ExampleMatcher matcher = matching ().withIgnoreCase (true );
343
323
Example <Person > example = Example .of (person , matcher );
@@ -354,9 +334,7 @@ void queryByExampleWithFirstnameIgnoreCase() {
354
334
@ Test // GH-929
355
335
void queryByExampleWithFirstnameOrLastname () {
356
336
357
- Person person = new Person ();
358
- person .setFirstname ("Frodo" );
359
- person .setLastname ("Baggins" );
337
+ Person person = new Person (null , "Frodo" , "Baggins" , null , null , null );
360
338
361
339
ExampleMatcher matcher = matchingAny ();
362
340
Example <Person > example = Example .of (person , matcher );
@@ -371,9 +349,7 @@ void queryByExampleWithFirstnameOrLastname() {
371
349
@ Test // GH-929
372
350
void queryByExampleEvenHandlesInvisibleFields () {
373
351
374
- Person person = new Person ();
375
- person .setFirstname ("Frodo" );
376
- person .setSecret ("I have the ring!" );
352
+ Person person = new Person (null , "Frodo" , null , "I have the ring!" , null , null );
377
353
378
354
Example <Person > example = Example .of (person );
379
355
@@ -388,10 +364,7 @@ void queryByExampleEvenHandlesInvisibleFields() {
388
364
@ Test // GH-929
389
365
void queryByExampleSupportsPropertyTransforms () {
390
366
391
- Person person = new Person ();
392
- person .setFirstname ("Frodo" );
393
- person .setLastname ("Baggins" );
394
- person .setSecret ("I have the ring!" );
367
+ Person person = new Person (null , "Frodo" , "Baggins" , "I have the ring!" , null , null );
395
368
396
369
ExampleMatcher matcher = matching () //
397
370
.withTransformer ("firstname" , o -> {
@@ -418,55 +391,33 @@ void queryByExampleSupportsPropertyTransforms() {
418
391
"(secret = 'I have the ring!')" );
419
392
}
420
393
421
- static class Person {
394
+ @ Test // GH-1969
395
+ void collectionLikeAttributesGetIgnored () {
422
396
423
- @ Id
424
- String id ;
425
- String firstname ;
426
- String lastname ;
427
- String secret ;
397
+ Example <Person > example = Example .of (new Person (null , "Frodo" , null , null , List .of (new Possession ("Ring" )), null ));
428
398
429
- public Person (String id , String firstname , String lastname , String secret ) {
430
- this .id = id ;
431
- this .firstname = firstname ;
432
- this .lastname = lastname ;
433
- this .secret = secret ;
434
- }
435
-
436
- public Person () {
437
- }
399
+ Query query = exampleMapper .getMappedExample (example );
438
400
439
- // Override default visibility of getting the secret.
440
- private String getSecret () {
441
- return this .secret ;
442
- }
401
+ assertThat (query .getCriteria ().orElseThrow ().toString ()).doesNotContainIgnoringCase ("possession" );
402
+ }
443
403
444
- public String getId () {
445
- return this .id ;
446
- }
404
+ @ Test // GH-1969
405
+ void mapAttributesGetIgnored () {
447
406
448
- public String getFirstname () {
449
- return this .firstname ;
450
- }
407
+ Example <Person > example = Example .of (new Person (null , "Frodo" , null , null , null , Map .of ("Home" , new Address ("Bag End" ))));
451
408
452
- public String getLastname () {
453
- return this .lastname ;
454
- }
409
+ Query query = exampleMapper .getMappedExample (example );
455
410
456
- public void setId (String id ) {
457
- this .id = id ;
458
- }
411
+ assertThat (query .getCriteria ().orElseThrow ().toString ()).doesNotContainIgnoringCase ("address" );
412
+ }
459
413
460
- public void setFirstname ( String firstname ) {
461
- this . firstname = firstname ;
462
- }
414
+ record Person ( @ Id @ Nullable String id , @ Nullable String firstname , @ Nullable String lastname , @ Nullable String secret ,
415
+ @ Nullable List < Possession > possessions , @ Nullable Map < String , Address > addresses ) {
416
+ }
463
417
464
- public void setLastname (String lastname ) {
465
- this .lastname = lastname ;
466
- }
418
+ record Possession (String name ) {
419
+ }
467
420
468
- public void setSecret (String secret ) {
469
- this .secret = secret ;
470
- }
421
+ record Address (String description ) {
471
422
}
472
423
}
0 commit comments