DATAJPA-1171 - Polishing.

Added unit test to verify new behavior. Slightly simplified Stream creation using Streamable.

Original pull request: #212.
This commit is contained in:
Oliver Gierke
2017-09-04 15:07:40 +02:00
parent 18c4f32f9d
commit 2c54644847
2 changed files with 14 additions and 7 deletions

View File

@@ -58,7 +58,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.JpaSort.JpaOrder;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.util.StreamUtils;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -185,13 +185,11 @@ public abstract class QueryUtils {
public static String getExistsQueryString(String entityName, String countQueryPlaceHolder,
Iterable<String> idAttributes) {
String baseQuery = String.format(COUNT_QUERY_STRING, countQueryPlaceHolder, entityName);
String whereClause = StreamUtils.createStreamFromIterator(idAttributes.iterator())
.map(idAttribute -> String.format(EQUALS_CONDITION_STRING, "x", idAttribute, idAttribute))
String whereClause = Streamable.of(idAttributes).stream() //
.map(idAttribute -> String.format(EQUALS_CONDITION_STRING, "x", idAttribute, idAttribute)) //
.collect(Collectors.joining(" AND ", " WHERE ", ""));
return baseQuery + whereClause;
return String.format(COUNT_QUERY_STRING, countQueryPlaceHolder, entityName) + whereClause;
}
/**

View File

@@ -15,10 +15,12 @@
*/
package org.springframework.data.jpa.repository.query;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
import java.util.Collections;
import java.util.Set;
import org.hamcrest.Matcher;
@@ -385,6 +387,13 @@ public class QueryUtilsUnitTests {
assertThat(aliases, contains("authority"));
}
@Test // DATAJPA-1171
public void doesNotContainStaticClauseInExistsQuery() {
assertThat(QueryUtils.getExistsQueryString("entity", "x", Collections.singleton("id"))) //
.endsWith("WHERE x.id = :id");
}
private static void assertCountQuery(String originalQuery, String countQuery) {
assertThat(createCountQueryFor(originalQuery), is(countQuery));
}