DATACMNS-669 - Final polishing.
Mostly Javadoc. Removed all API that took String paths so far. Changed return type of bean definition for QuerydslPredicateArgumentResolver to HandlerMethodArgumentResolver so that Querydsl stays optional during class loading. Original pull request: #132.
This commit is contained in:
@@ -23,6 +23,7 @@ import org.junit.Test;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.data.querydsl.QUser;
|
||||
import org.springframework.data.querydsl.SimpleEntityPathResolver;
|
||||
import org.springframework.data.querydsl.User;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
@@ -52,7 +53,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
this.builder = new QuerydslPredicateBuilder(new DefaultConversionService());
|
||||
this.builder = new QuerydslPredicateBuilder(new DefaultConversionService(), SimpleEntityPathResolver.INSTANCE);
|
||||
this.bindings = new QuerydslBindings();
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void returnsRegisteredBindingForSimplePath() {
|
||||
|
||||
bindings.bind("firstname").using(CONTAINS_BINDING);
|
||||
bindings.bind(QUser.user.firstname).first(CONTAINS_BINDING);
|
||||
|
||||
assertAdapterWithTargetBinding(bindings.getBindingForPath(PropertyPath.from("firstname", User.class)),
|
||||
CONTAINS_BINDING);
|
||||
@@ -90,7 +91,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void getBindingForPathShouldReturnSpeficicBindingForNestedElementsWhenAvailable() {
|
||||
|
||||
bindings.bind("address.street").using(CONTAINS_BINDING);
|
||||
bindings.bind(QUser.user.address.street).first(CONTAINS_BINDING);
|
||||
|
||||
assertAdapterWithTargetBinding(bindings.getBindingForPath(PropertyPath.from("address.street", User.class)),
|
||||
CONTAINS_BINDING);
|
||||
@@ -102,7 +103,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void getBindingForPathShouldReturnSpeficicBindingForTypes() {
|
||||
|
||||
bindings.bind(String.class).single(CONTAINS_BINDING);
|
||||
bindings.bind(String.class).first(CONTAINS_BINDING);
|
||||
|
||||
assertAdapterWithTargetBinding(bindings.getBindingForPath(PropertyPath.from("address.street", User.class)),
|
||||
CONTAINS_BINDING);
|
||||
@@ -114,7 +115,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void propertyNotExplicitlyIncludedAndWithoutTypeBindingIsInvisible() {
|
||||
|
||||
bindings.bind(String.class).single(CONTAINS_BINDING);
|
||||
bindings.bind(String.class).first(CONTAINS_BINDING);
|
||||
|
||||
assertThat(bindings.getBindingForPath(PropertyPath.from("inceptionYear", User.class)), nullValue());
|
||||
}
|
||||
@@ -125,7 +126,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void pathIsVisibleIfTypeBasedBindingWasRegistered() {
|
||||
|
||||
bindings.bind(String.class).single(CONTAINS_BINDING);
|
||||
bindings.bind(String.class).first(CONTAINS_BINDING);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("inceptionYear", User.class)), is(true));
|
||||
}
|
||||
@@ -136,7 +137,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void explicitlyIncludedPathIsVisible() {
|
||||
|
||||
bindings.including("inceptionYear");
|
||||
bindings.including(QUser.user.inceptionYear);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("inceptionYear", User.class)), is(true));
|
||||
}
|
||||
@@ -147,7 +148,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void notExplicitlyIncludedPathIsInvisible() {
|
||||
|
||||
bindings.including("inceptionYear");
|
||||
bindings.including(QUser.user.inceptionYear);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("firstname", User.class)), is(false));
|
||||
}
|
||||
@@ -158,7 +159,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void excludedPathIsInvisible() {
|
||||
|
||||
bindings.excluding("inceptionYear");
|
||||
bindings.excluding(QUser.user.inceptionYear);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("inceptionYear", User.class)), is(false));
|
||||
}
|
||||
@@ -169,7 +170,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void pathIsVisibleIfNotExplicitlyExcluded() {
|
||||
|
||||
bindings.excluding("inceptionYear");
|
||||
bindings.excluding(QUser.user.inceptionYear);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("firstname", User.class)), is(true));
|
||||
}
|
||||
@@ -180,8 +181,8 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void pathIsVisibleIfItsBothBlackAndWhitelisted() {
|
||||
|
||||
bindings.excluding("firstname");
|
||||
bindings.including("firstname");
|
||||
bindings.excluding(QUser.user.firstname);
|
||||
bindings.including(QUser.user.firstname);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("firstname", User.class)), is(true));
|
||||
}
|
||||
@@ -192,7 +193,7 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void nestedPathIsInvisibleIfAParanetPathWasExcluded() {
|
||||
|
||||
bindings.excluding("address");
|
||||
bindings.excluding(QUser.user.address);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("address.city", User.class)), is(false));
|
||||
}
|
||||
@@ -203,8 +204,8 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void pathIsVisibleIfConcretePathIsVisibleButParentExcluded() {
|
||||
|
||||
bindings.excluding("address");
|
||||
bindings.including("address.city");
|
||||
bindings.excluding(QUser.user.address);
|
||||
bindings.including(QUser.user.address.city);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("address.city", User.class)), is(true));
|
||||
}
|
||||
@@ -215,12 +216,15 @@ public class QuerydslBindingsUnitTests {
|
||||
@Test
|
||||
public void isPathVisibleShouldReturnFalseWhenPartialPathContainedInExcludingAndConcretePathToDifferentPropertyIsIncluded() {
|
||||
|
||||
bindings.excluding("address");
|
||||
bindings.including("address.city");
|
||||
bindings.excluding(QUser.user.address);
|
||||
bindings.including(QUser.user.address.city);
|
||||
|
||||
assertThat(bindings.isPathVisible(PropertyPath.from("address.street", User.class)), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-669
|
||||
*/
|
||||
@Test
|
||||
public void testname() {
|
||||
|
||||
|
||||
@@ -18,11 +18,14 @@ package org.springframework.data.web.querydsl;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver.*;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -30,13 +33,16 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.mapping.PropertyPath;
|
||||
import org.springframework.data.querydsl.QUser;
|
||||
import org.springframework.data.querydsl.User;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.support.Repositories;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.hateoas.Resource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.Predicate;
|
||||
@@ -51,6 +57,10 @@ import com.mysema.query.types.path.StringPath;
|
||||
*/
|
||||
public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
|
||||
static final TypeInformation<?> USER_TYPE = ClassTypeInformation.from(User.class);
|
||||
|
||||
public @Rule ExpectedException exception = ExpectedException.none();
|
||||
|
||||
QuerydslPredicateArgumentResolver resolver;
|
||||
MockHttpServletRequest request;
|
||||
|
||||
@@ -202,7 +212,7 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
* @see DATACMNS-669
|
||||
*/
|
||||
@Test
|
||||
public void shouldExcludePorpertiesCorrectly() throws Exception {
|
||||
public void shouldExcludePropertiesCorrectly() throws Exception {
|
||||
|
||||
request.addParameter("address.street", "downhill");
|
||||
request.addParameter("inceptionYear", "973");
|
||||
@@ -234,7 +244,6 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
public void createBindingsShouldHonorQuerydslBinderCustomizerHookWhenPresent() {
|
||||
|
||||
Repositories repositories = mock(Repositories.class);
|
||||
RepositoryInformation repoInfo = mock(RepositoryInformation.class);
|
||||
|
||||
when(repositories.hasRepositoryFor(User.class)).thenReturn(true);
|
||||
when(repositories.getRepositoryFor(User.class)).thenReturn(new SampleRepo());
|
||||
@@ -242,9 +251,9 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
resolver = new QuerydslPredicateArgumentResolver(null);
|
||||
ReflectionTestUtils.setField(resolver, "repositories", repositories);
|
||||
|
||||
QuerydslBindings bindings = resolver.createBindings(null, User.class);
|
||||
MultiValueBinding<Path<Object>, Object> binding = bindings.getBindingForPath(PropertyPath.from("firstname",
|
||||
User.class));
|
||||
QuerydslBindings bindings = resolver.createBindings(null, USER_TYPE);
|
||||
MultiValueBinding<Path<Object>, Object> binding = bindings
|
||||
.getBindingForPath(PropertyPath.from("firstname", User.class));
|
||||
|
||||
assertThat(binding.bind((Path) QUser.user.firstname, Collections.singleton("rand")),
|
||||
is((Predicate) QUser.user.firstname.contains("rand")));
|
||||
@@ -259,20 +268,48 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
|
||||
AutowireCapableBeanFactory beanFactory = mock(AutowireCapableBeanFactory.class);
|
||||
when(beanFactory.getBean(SpecificBinding.class)).thenReturn(new SpecificBinding());
|
||||
QuerydslPredicate annotation = getMethodParameterFor("specificFind", Predicate.class).getParameterAnnotation(
|
||||
QuerydslPredicate.class);
|
||||
QuerydslPredicate annotation = getMethodParameterFor("specificFind", Predicate.class)
|
||||
.getParameterAnnotation(QuerydslPredicate.class);
|
||||
|
||||
resolver = new QuerydslPredicateArgumentResolver(null);
|
||||
ReflectionTestUtils.setField(resolver, "beanFactory", beanFactory);
|
||||
|
||||
QuerydslBindings bindings = resolver.createBindings(annotation, User.class);
|
||||
MultiValueBinding<Path<Object>, Object> binding = bindings.getBindingForPath(PropertyPath.from("firstname",
|
||||
User.class));
|
||||
QuerydslBindings bindings = resolver.createBindings(annotation, USER_TYPE);
|
||||
MultiValueBinding<Path<Object>, Object> binding = bindings
|
||||
.getBindingForPath(PropertyPath.from("firstname", User.class));
|
||||
|
||||
assertThat(binding.bind((Path) QUser.user.firstname, Collections.singleton("rand")),
|
||||
is((Predicate) QUser.user.firstname.eq("RAND")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-669
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void detectsDomainTypesCorrectly() {
|
||||
|
||||
TypeInformation USER_TYPE = ClassTypeInformation.from(User.class);
|
||||
TypeInformation MODELA_AND_VIEW_TYPE = ClassTypeInformation.from(ModelAndView.class);
|
||||
|
||||
assertThat(extractTypeInfo(getMethodParameterFor("forEntity")), is(USER_TYPE));
|
||||
assertThat(extractTypeInfo(getMethodParameterFor("forResourceOfUser")), is(USER_TYPE));
|
||||
assertThat(extractTypeInfo(getMethodParameterFor("forModelAndView")), is(MODELA_AND_VIEW_TYPE));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-669
|
||||
*/
|
||||
@Test
|
||||
public void rejectsPredicateResolutionIfDomainTypeCantBeAutoDetected() {
|
||||
|
||||
exception.expect(IllegalStateException.class);
|
||||
exception.expectMessage(QuerydslPredicate.class.getSimpleName());
|
||||
exception.expectMessage("root");
|
||||
|
||||
resolver.createBindings(null, ClassTypeInformation.from(ModelAndView.class));
|
||||
}
|
||||
|
||||
private static MethodParameter getMethodParameterFor(String methodName, Class<?>... args) throws RuntimeException {
|
||||
|
||||
try {
|
||||
@@ -286,7 +323,7 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
|
||||
public void customize(QuerydslBindings bindings, QUser user) {
|
||||
|
||||
bindings.bind("firstname").using(new SingleValueBinding<StringPath, String>() {
|
||||
bindings.bind(user.firstname).first(new SingleValueBinding<StringPath, String>() {
|
||||
|
||||
@Override
|
||||
public Predicate bind(StringPath path, String value) {
|
||||
@@ -294,7 +331,7 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
}
|
||||
});
|
||||
|
||||
bindings.bind(user.lastname).single(new SingleValueBinding<StringPath, String>() {
|
||||
bindings.bind(user.lastname).first(new SingleValueBinding<StringPath, String>() {
|
||||
|
||||
@Override
|
||||
public Predicate bind(StringPath path, String value) {
|
||||
@@ -302,7 +339,7 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
}
|
||||
});
|
||||
|
||||
bindings.excluding("address");
|
||||
bindings.excluding(user.address);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,6 +356,12 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
Page<User> pagedFind(@QuerydslPredicate Predicate predicate, Pageable page);
|
||||
|
||||
User specificFind(@QuerydslPredicate(bindings = SpecificBinding.class) Predicate predicate);
|
||||
|
||||
HttpEntity<User> forEntity();
|
||||
|
||||
ModelAndView forModelAndView();
|
||||
|
||||
ResponseEntity<Resource<User>> forResourceOfUser();
|
||||
}
|
||||
|
||||
public static class SampleRepo implements QuerydslBinderCustomizer<QUser> {
|
||||
@@ -326,7 +369,7 @@ public class QuerydslPredicateArgumentResolverUnitTests {
|
||||
@Override
|
||||
public void customize(QuerydslBindings bindings, QUser user) {
|
||||
|
||||
bindings.bind(QUser.user.firstname).single(new SingleValueBinding<StringPath, String>() {
|
||||
bindings.bind(QUser.user.firstname).first(new SingleValueBinding<StringPath, String>() {
|
||||
|
||||
@Override
|
||||
public Predicate bind(StringPath path, String value) {
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.data.querydsl.QUser;
|
||||
import org.springframework.data.querydsl.SimpleEntityPathResolver;
|
||||
import org.springframework.data.querydsl.User;
|
||||
import org.springframework.data.querydsl.Users;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
@@ -51,7 +52,7 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.builder = new QuerydslPredicateBuilder(new DefaultConversionService());
|
||||
this.builder = new QuerydslPredicateBuilder(new DefaultConversionService(), SimpleEntityPathResolver.INSTANCE);
|
||||
this.values = new LinkedMultiValueMap<String, String>();
|
||||
}
|
||||
|
||||
@@ -60,7 +61,7 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullConversionService() {
|
||||
new QuerydslPredicateBuilder(null);
|
||||
new QuerydslPredicateBuilder(null, SimpleEntityPathResolver.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +69,7 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getPredicateShouldThrowErrorWhenBindingContextIsNull() {
|
||||
builder.getPredicate(values, null, null);
|
||||
builder.getPredicate(null, values, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +78,7 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
@Test
|
||||
public void getPredicateShouldReturnEmptyPredicateWhenPropertiesAreEmpty() {
|
||||
|
||||
assertThat(builder.getPredicate(values, DEFAULT_BINDINGS, ClassTypeInformation.OBJECT), is(nullValue()));
|
||||
assertThat(builder.getPredicate(ClassTypeInformation.OBJECT, values, DEFAULT_BINDINGS), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +89,7 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
|
||||
values.add("firstname", "Oliver");
|
||||
|
||||
Predicate predicate = builder.getPredicate(values, DEFAULT_BINDINGS, USER_TYPE);
|
||||
Predicate predicate = builder.getPredicate(USER_TYPE, values, DEFAULT_BINDINGS);
|
||||
|
||||
assertThat(predicate, is((Predicate) QUser.user.firstname.eq("Oliver")));
|
||||
|
||||
@@ -106,7 +107,7 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
|
||||
values.add("address.city", "Linz");
|
||||
|
||||
Predicate predicate = builder.getPredicate(values, DEFAULT_BINDINGS, USER_TYPE);
|
||||
Predicate predicate = builder.getPredicate(USER_TYPE, values, DEFAULT_BINDINGS);
|
||||
|
||||
assertThat(predicate, is((Predicate) QUser.user.address.city.eq("Linz")));
|
||||
|
||||
@@ -125,7 +126,7 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
values.add("firstname", "rand");
|
||||
values.add("lastname".toUpperCase(), "al'thor");
|
||||
|
||||
Predicate predicate = builder.getPredicate(values, new QuerydslBindings(), USER_TYPE);
|
||||
Predicate predicate = builder.getPredicate(USER_TYPE, values, DEFAULT_BINDINGS);
|
||||
|
||||
assertThat(predicate, is((Predicate) QUser.user.firstname.eq("rand")));
|
||||
}
|
||||
@@ -139,7 +140,7 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
values.add("lastname", null);
|
||||
|
||||
QuerydslBindings bindings = new QuerydslBindings();
|
||||
bindings.bind(QUser.user.lastname).single(new SingleValueBinding<StringPath, String>() {
|
||||
bindings.bind(QUser.user.lastname).first(new SingleValueBinding<StringPath, String>() {
|
||||
|
||||
@Override
|
||||
public Predicate bind(StringPath path, String value) {
|
||||
@@ -147,6 +148,6 @@ public class QuerydslPredicateBuilderUnitTests {
|
||||
}
|
||||
});
|
||||
|
||||
builder.getPredicate(values, bindings, USER_TYPE);
|
||||
builder.getPredicate(USER_TYPE, values, bindings);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user