Skip to content

Commit ee1f420

Browse files
committed
Polishing.
Original pull request #1097
1 parent af72307 commit ee1f420

File tree

3 files changed

+30
-48
lines changed

3 files changed

+30
-48
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlIdentifierParameterSourceUnitTests.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,10 @@ public void addOtherDatabaseObjectIdentifierParameterSource() {
9797
parameters2.addValue(SqlIdentifier.unquoted("key3"), 222);
9898

9999
parameters.addAll(parameters2);
100-
String[] allKeys = parameters.getParameterNames();
101-
Arrays.sort(allKeys);
100+
102101
assertSoftly(softly -> {
103102

104-
softly.assertThat(allKeys).isEqualTo(new String[] { "key1", "key2", "key3" });
103+
softly.assertThat(parameters.getParameterNames()).containsExactlyInAnyOrder("key1", "key2", "key3");
105104
softly.assertThat(parameters.getValue("key1")).isEqualTo(111);
106105
softly.assertThat(parameters.hasValue("key1")).isTrue();
107106
softly.assertThat(parameters.getSqlType("key1")).isEqualTo(11);

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQueryUnitTests.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -589,24 +589,18 @@ public void createsQueryByEmbeddedObject() throws Exception {
589589
new Object[] { new Address("Hello", "World") });
590590
ParametrizedQuery query = jdbcQuery.createQuery(accessor, returnedType);
591591

592-
String expectedSql = BASE_SELECT + " WHERE (" + TABLE + ".\"USER_STREET\" = :user_street AND " + TABLE
593-
+ ".\"USER_CITY\" = :user_city)";
594592
String actualSql = query.getQuery();
595593

596-
assertThat(compareSqlStr(expectedSql, actualSql)).isTrue();
594+
assertThat(actualSql) //
595+
.startsWith(BASE_SELECT + " WHERE (" + TABLE + ".\"USER_") //
596+
.endsWith(")") //
597+
.contains(TABLE + ".\"USER_STREET\" = :user_street", //
598+
" AND ", //
599+
TABLE + ".\"USER_CITY\" = :user_city");
597600
assertThat(query.getParameterSource().getValue("user_street")).isEqualTo("Hello");
598601
assertThat(query.getParameterSource().getValue("user_city")).isEqualTo("World");
599602
}
600603

601-
private boolean compareSqlStr(String expectedSql, String actualSql) {
602-
String[] expected = expectedSql.split("WHERE");
603-
String[] actual = actualSql.split("WHERE");
604-
if (!expected[0].equals(actual[0])) return false;
605-
expected[1] = expected[1].trim().substring(1, expected[1].length() - 2);
606-
String[] flakyParts = expected[1].split("AND");
607-
return actual[1].contains(flakyParts[0]) && actual[1].contains(flakyParts[1]);
608-
}
609-
610604
@Test // DATAJDBC-318
611605
public void createsQueryByEmbeddedProperty() throws Exception {
612606

spring-data-relational/src/test/java/org/springframework/data/relational/repository/query/RelationalExampleMapperTests.java

+22-33
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,14 @@
2525
import lombok.Data;
2626
import lombok.NoArgsConstructor;
2727

28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.HashSet;
31-
import java.util.List;
3228
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;
3729

3830
import org.junit.jupiter.api.BeforeEach;
3931
import org.junit.jupiter.api.Test;
4032
import org.springframework.data.annotation.Id;
4133
import org.springframework.data.domain.Example;
4234
import org.springframework.data.domain.ExampleMatcher;
4335
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
44-
import org.springframework.data.relational.core.query.CriteriaDefinition;
4536
import org.springframework.data.relational.core.query.Query;
4637

4738
/**
@@ -98,9 +89,10 @@ void queryByExampleWithFirstnameAndLastname() {
9889
Example<Person> example = Example.of(person);
9990

10091
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')");
10496
}
10597

10698
@Test // GH-929
@@ -130,9 +122,10 @@ void queryByExampleWithNullMatchingFirstnameAndLastname() {
130122
Example<Person> example = Example.of(person, matcher);
131123

132124
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')");
136129
}
137130

138131
@Test // GH-929
@@ -373,9 +366,10 @@ void queryByExampleWithFirstnameOrLastname() {
373366
Example<Person> example = Example.of(person, matcher);
374367

375368
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')");
379373
}
380374

381375
@Test // GH-929
@@ -388,9 +382,11 @@ void queryByExampleEvenHandlesInvisibleFields() {
388382
Example<Person> example = Example.of(person);
389383

390384
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!')");
394390
}
395391

396392
@Test // GH-929
@@ -418,19 +414,12 @@ void queryByExampleSupportsPropertyTransforms() {
418414
Example<Person> example = Example.of(person, matcher);
419415

420416
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-
}
425417

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!')");
434423
}
435424

436425
@Data

0 commit comments

Comments
 (0)