DATAJPA-389 - Introduce NativeSqlQuery type for native sql queries.
Extracted the functionality specific to the handling of native queries out of SimpleJpaQuery and moved that to the new NativeJpaQuery type. Moved common functionality to AbstractStringBasedJpaQuery and used that as a new base class for SimpleJpaQuery and NativeJpaQuery. Introduced JpqQueryFactory to centralize construction of JpaQuery objects. Renamed getQuery() method in StringQuery to getQueryString(). Original pull request: #36.
This commit is contained in:
committed by
Oliver Gierke
parent
0a11d30aac
commit
3735464ec2
@@ -46,6 +46,6 @@ public class ExpressionBasedStringQueryUnitTests {
|
||||
|
||||
String source = "select from #{#entityName} u where u.firstname like :firstname";
|
||||
StringQuery query = new ExpressionBasedStringQuery(source, metadata);
|
||||
assertThat(query.getQuery(), is("select from User u where u.firstname like :firstname"));
|
||||
assertThat(query.getQueryString(), is("select from User u where u.firstname like :firstname"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,11 +45,13 @@ import org.springframework.data.jpa.repository.sample.UserRepository;
|
||||
import org.springframework.data.jpa.repository.support.DefaultJpaEntityMetadata;
|
||||
import org.springframework.data.jpa.repository.support.JpaEntityMetadata;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
|
||||
/**
|
||||
* Unit test for {@link SimpleJpaQuery}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SimpleJpaQueryUnitTests {
|
||||
@@ -122,7 +124,9 @@ public class SimpleJpaQueryUnitTests {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class);
|
||||
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
|
||||
SimpleJpaQuery jpaQuery = new SimpleJpaQuery(queryMethod, em);
|
||||
AbstractJpaQuery jpaQuery = JpaQueryFactory.INSTANCE.fromQueryAnnotation(queryMethod, em);
|
||||
|
||||
assertThat(jpaQuery instanceof NativeJpaQuery, is(true));
|
||||
|
||||
Class<?> type = Mockito.any();
|
||||
when(em.createNativeQuery(Mockito.anyString(), type)).thenReturn(query);
|
||||
@@ -137,14 +141,14 @@ public class SimpleJpaQueryUnitTests {
|
||||
public void rejectsNativeQueryWithDynamicSort() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class, Sort.class);
|
||||
createSimpleJpaQuery(method);
|
||||
createJpaQuery(method);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsNativeQueryWithPageable() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findNativeByLastname", String.class, Pageable.class);
|
||||
createSimpleJpaQuery(method);
|
||||
createJpaQuery(method);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,7 +162,7 @@ public class SimpleJpaQueryUnitTests {
|
||||
Method method = SampleRepository.class.getMethod("findByAnnotatedQuery");
|
||||
when(em.createQuery(contains("count"))).thenThrow(IllegalArgumentException.class);
|
||||
|
||||
createSimpleJpaQuery(method);
|
||||
createJpaQuery(method);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,13 +179,27 @@ public class SimpleJpaQueryUnitTests {
|
||||
exception.expectMessage("Count");
|
||||
exception.expectMessage(method.getName());
|
||||
|
||||
createSimpleJpaQuery(method);
|
||||
createJpaQuery(method);
|
||||
}
|
||||
|
||||
private void createSimpleJpaQuery(Method method) {
|
||||
@Test
|
||||
public void createsASimpleJpaQueryFromAnnotation() throws Exception {
|
||||
|
||||
RepositoryQuery query = createJpaQuery(SampleRepository.class.getMethod("findByAnnotatedQuery"));
|
||||
assertThat(query instanceof SimpleJpaQuery, is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsANativeJpaQueryFromAnnotation() throws Exception {
|
||||
|
||||
RepositoryQuery query = createJpaQuery(SampleRepository.class.getMethod("findNativeByLastname", String.class));
|
||||
assertThat(query instanceof NativeJpaQuery, is(true));
|
||||
}
|
||||
|
||||
private RepositoryQuery createJpaQuery(Method method) {
|
||||
|
||||
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
|
||||
new SimpleJpaQuery(queryMethod, em);
|
||||
return JpaQueryFactory.INSTANCE.fromQueryAnnotation(queryMethod, em);
|
||||
}
|
||||
|
||||
interface SampleRepository {
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.data.repository.query.parser.Part.Type;
|
||||
* Unit tests for {@link StringQuery}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public class StringQueryUnitTests {
|
||||
|
||||
@@ -41,7 +42,7 @@ public class StringQueryUnitTests {
|
||||
StringQuery query = new StringQuery(source);
|
||||
|
||||
assertThat(query.hasLikeBindings(), is(true));
|
||||
assertThat(query.getQuery(), is(source));
|
||||
assertThat(query.getQueryString(), is(source));
|
||||
|
||||
List<LikeBinding> bindings = query.getLikeBindings();
|
||||
assertThat(bindings, hasSize(1));
|
||||
@@ -57,7 +58,7 @@ public class StringQueryUnitTests {
|
||||
StringQuery query = new StringQuery("select u from User u where u.firstname like %?1% or u.lastname like %?2");
|
||||
|
||||
assertThat(query.hasLikeBindings(), is(true));
|
||||
assertThat(query.getQuery(), is("select u from User u where u.firstname like ?1 or u.lastname like ?2"));
|
||||
assertThat(query.getQueryString(), is("select u from User u where u.firstname like ?1 or u.lastname like ?2"));
|
||||
|
||||
List<LikeBinding> bindings = query.getLikeBindings();
|
||||
assertThat(bindings, hasSize(2));
|
||||
@@ -79,7 +80,7 @@ public class StringQueryUnitTests {
|
||||
StringQuery query = new StringQuery("select u from User u where u.firstname like %:firstname");
|
||||
|
||||
assertThat(query.hasLikeBindings(), is(true));
|
||||
assertThat(query.getQuery(), is("select u from User u where u.firstname like :firstname"));
|
||||
assertThat(query.getQueryString(), is("select u from User u where u.firstname like :firstname"));
|
||||
|
||||
List<LikeBinding> bindings = query.getLikeBindings();
|
||||
assertThat(bindings, hasSize(1));
|
||||
|
||||
Reference in New Issue
Block a user