Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.SqlIdentifier;

import java.util.Arrays;

/**
* Tests for {@link SqlIdentifierParameterSource}.
*
Expand Down Expand Up @@ -95,10 +97,11 @@ public void addOtherDatabaseObjectIdentifierParameterSource() {
parameters2.addValue(SqlIdentifier.unquoted("key3"), 222);

parameters.addAll(parameters2);

String[] allKeys = parameters.getParameterNames();
Arrays.sort(allKeys);
assertSoftly(softly -> {

softly.assertThat(parameters.getParameterNames()).isEqualTo(new String[] { "key1", "key2", "key3" });
softly.assertThat(allKeys).isEqualTo(new String[] { "key1", "key2", "key3" });
softly.assertThat(parameters.getValue("key1")).isEqualTo(111);
softly.assertThat(parameters.hasValue("key1")).isTrue();
softly.assertThat(parameters.getSqlType("key1")).isEqualTo(11);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,12 +591,22 @@ public void createsQueryByEmbeddedObject() throws Exception {

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

assertThat(query.getQuery()).isEqualTo(expectedSql);
assertThat(compareSqlStr(expectedSql, actualSql)).isTrue();
assertThat(query.getParameterSource().getValue("user_street")).isEqualTo("Hello");
assertThat(query.getParameterSource().getValue("user_city")).isEqualTo("World");
}

private boolean compareSqlStr(String expectedSql, String actualSql) {
String[] expected = expectedSql.split("WHERE");
String[] actual = actualSql.split("WHERE");
if (!expected[0].equals(actual[0])) return false;
expected[1] = expected[1].trim().substring(1, expected[1].length() - 2);
String[] flakyParts = expected[1].split("AND");
return actual[1].contains(flakyParts[0]) && actual[1].contains(flakyParts[1]);
}

@Test // DATAJDBC-318
public void createsQueryByEmbeddedProperty() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,23 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.query.CriteriaDefinition;
import org.springframework.data.relational.core.query.Query;

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

Query query = exampleMapper.getMappedExample(example);

assertThat(query.getCriteria()) //
.map(Object::toString) //
.hasValue("(firstname = 'Frodo') AND (lastname = 'Baggins')");
String actual = query.getCriteria().map(Object::toString).get();
String expected = "(firstname = 'Frodo') AND (lastname = 'Baggins')";
assertThat(compareStrWithFlakiness(expected, actual, "AND")).isTrue();
}

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

Query query = exampleMapper.getMappedExample(example);

assertThat(query.getCriteria()) //
.map(Object::toString) //
.hasValue("(firstname IS NULL OR firstname = 'Bilbo') AND (lastname IS NULL OR lastname = 'Baggins')");
String actual = query.getCriteria().map(Object::toString).get();
String expected = "(firstname IS NULL OR firstname = 'Bilbo') AND (lastname IS NULL OR lastname = 'Baggins')";
assertThat(compareStrWithFlakiness(expected, actual, "AND")).isTrue();
}

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

Query query = exampleMapper.getMappedExample(example);

assertThat(query.getCriteria()) //
.map(Object::toString) //
.hasValue("(firstname = 'Frodo') OR (lastname = 'Baggins')");
String actual = query.getCriteria().map(Object::toString).get();
String expected = "(firstname = 'Frodo') OR (lastname = 'Baggins')";
assertThat(compareStrWithFlakiness(expected, actual, "OR")).isTrue();
}

@Test // GH-929
Expand All @@ -382,10 +388,9 @@ void queryByExampleEvenHandlesInvisibleFields() {
Example<Person> example = Example.of(person);

Query query = exampleMapper.getMappedExample(example);

assertThat(query.getCriteria()) //
.map(Object::toString) //
.hasValue("(firstname = 'Frodo') AND (secret = 'I have the ring!')");
String actual = query.getCriteria().map(Object::toString).get();
String expected = "(firstname = 'Frodo') AND (secret = 'I have the ring!')";
assertThat(compareStrWithFlakiness(expected, actual, "AND")).isTrue();
}

@Test // GH-929
Expand Down Expand Up @@ -413,11 +418,19 @@ void queryByExampleSupportsPropertyTransforms() {
Example<Person> example = Example.of(person, matcher);

Query query = exampleMapper.getMappedExample(example);
String actual = query.getCriteria().map(Object::toString).get();
String expected = "(firstname = 'FRODO') AND (lastname = 'baggins') AND (secret = 'I have the ring!')";
assertThat(compareStrWithFlakiness(expected, actual, "AND")).isTrue();
}

assertThat(query.getCriteria()) //
.map(Object::toString) //
.hasValue("(firstname = 'FRODO') AND (lastname = 'baggins') AND (secret = 'I have the ring!')");

private boolean compareStrWithFlakiness(String expected, String actual, String regex) {
String[] flakyParts = expected.split(regex);
for (String part : flakyParts) {
if (!actual.contains(part)) {
return false;
}
}
return true;
}

@Data
Expand Down