25
25
import lombok .Data ;
26
26
import lombok .NoArgsConstructor ;
27
27
28
- import java .util .ArrayList ;
29
- import java .util .Arrays ;
30
- import java .util .HashSet ;
31
- import java .util .List ;
32
28
import java .util .Objects ;
33
- import java .util .Optional ;
34
- import java .util .Set ;
35
- import java .util .stream .Collector ;
36
- import java .util .stream .Collectors ;
37
29
38
30
import org .junit .jupiter .api .BeforeEach ;
39
31
import org .junit .jupiter .api .Test ;
40
32
import org .springframework .data .annotation .Id ;
41
33
import org .springframework .data .domain .Example ;
42
34
import org .springframework .data .domain .ExampleMatcher ;
43
35
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
44
- import org .springframework .data .relational .core .query .CriteriaDefinition ;
45
36
import org .springframework .data .relational .core .query .Query ;
46
37
47
38
/**
@@ -98,9 +89,10 @@ void queryByExampleWithFirstnameAndLastname() {
98
89
Example <Person > example = Example .of (person );
99
90
100
91
Query query = exampleMapper .getMappedExample (example );
101
- String actual = query .getCriteria ().map (Object ::toString ).get ();
102
- String expected = "(firstname = 'Frodo') AND (lastname = 'Baggins')" ;
103
- assertThat (compareStrWithFlakiness (expected , actual , "AND" )).isTrue ();
92
+ assertThat (query .getCriteria ().map (Object ::toString ).get ()) //
93
+ .contains ("(firstname = 'Frodo')" , //
94
+ " AND " , //
95
+ "(lastname = 'Baggins')" );
104
96
}
105
97
106
98
@ Test // GH-929
@@ -130,9 +122,10 @@ void queryByExampleWithNullMatchingFirstnameAndLastname() {
130
122
Example <Person > example = Example .of (person , matcher );
131
123
132
124
Query query = exampleMapper .getMappedExample (example );
133
- String actual = query .getCriteria ().map (Object ::toString ).get ();
134
- String expected = "(firstname IS NULL OR firstname = 'Bilbo') AND (lastname IS NULL OR lastname = 'Baggins')" ;
135
- assertThat (compareStrWithFlakiness (expected , actual , "AND" )).isTrue ();
125
+ assertThat (query .getCriteria ().map (Object ::toString ).get ()) //
126
+ .contains ("(firstname IS NULL OR firstname = 'Bilbo')" , //
127
+ " AND " , //
128
+ "(lastname IS NULL OR lastname = 'Baggins')" );
136
129
}
137
130
138
131
@ Test // GH-929
@@ -373,9 +366,10 @@ void queryByExampleWithFirstnameOrLastname() {
373
366
Example <Person > example = Example .of (person , matcher );
374
367
375
368
Query query = exampleMapper .getMappedExample (example );
376
- String actual = query .getCriteria ().map (Object ::toString ).get ();
377
- String expected = "(firstname = 'Frodo') OR (lastname = 'Baggins')" ;
378
- assertThat (compareStrWithFlakiness (expected , actual , "OR" )).isTrue ();
369
+ assertThat (query .getCriteria ().map (Object ::toString ).get ()) //
370
+ .contains ("(firstname = 'Frodo')" , //
371
+ " OR " , //
372
+ "(lastname = 'Baggins')" );
379
373
}
380
374
381
375
@ Test // GH-929
@@ -388,9 +382,11 @@ void queryByExampleEvenHandlesInvisibleFields() {
388
382
Example <Person > example = Example .of (person );
389
383
390
384
Query query = exampleMapper .getMappedExample (example );
391
- String actual = query .getCriteria ().map (Object ::toString ).get ();
392
- String expected = "(firstname = 'Frodo') AND (secret = 'I have the ring!')" ;
393
- assertThat (compareStrWithFlakiness (expected , actual , "AND" )).isTrue ();
385
+
386
+ assertThat (query .getCriteria ().map (Object ::toString ).get ()) //
387
+ .contains ("(firstname = 'Frodo')" , //
388
+ " AND " , //
389
+ "(secret = 'I have the ring!')" );
394
390
}
395
391
396
392
@ Test // GH-929
@@ -418,19 +414,12 @@ void queryByExampleSupportsPropertyTransforms() {
418
414
Example <Person > example = Example .of (person , matcher );
419
415
420
416
Query query = exampleMapper .getMappedExample (example );
421
- String actual = query .getCriteria ().map (Object ::toString ).get ();
422
- String expected = "(firstname = 'FRODO') AND (lastname = 'baggins') AND (secret = 'I have the ring!')" ;
423
- assertThat (compareStrWithFlakiness (expected , actual , "AND" )).isTrue ();
424
- }
425
417
426
- private boolean compareStrWithFlakiness (String expected , String actual , String regex ) {
427
- String [] flakyParts = expected .split (regex );
428
- for (String part : flakyParts ) {
429
- if (!actual .contains (part )) {
430
- return false ;
431
- }
432
- }
433
- return true ;
418
+ assertThat (query .getCriteria ().map (Object ::toString ).get ()) //
419
+ .contains ("(firstname = 'FRODO')" , //
420
+ " AND " , //
421
+ "(lastname = 'baggins')" , //
422
+ "(secret = 'I have the ring!')" );
434
423
}
435
424
436
425
@ Data
0 commit comments