Adopt JpaParameters to reflect the actual parameter type when using generics.
Closes #3254
This commit is contained in:
@@ -20,12 +20,15 @@ import jakarta.persistence.TemporalType;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.data.jpa.repository.Temporal;
|
||||
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
|
||||
import org.springframework.data.repository.query.Parameter;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.ParametersSource;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
@@ -41,9 +44,34 @@ public class JpaParameters extends Parameters<JpaParameters, JpaParameter> {
|
||||
* Creates a new {@link JpaParameters} instance from the given {@link Method}.
|
||||
*
|
||||
* @param method must not be {@literal null}.
|
||||
* @deprecated since 3.2.1, use {@link #JpaParameters(ParametersSource)} instead.
|
||||
*/
|
||||
@Deprecated(since = "3.2.1", forRemoval = true)
|
||||
public JpaParameters(Method method) {
|
||||
super(method);
|
||||
this(ParametersSource.of(method), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link JpaParameters} instance from the given {@link ParametersSource}.
|
||||
*
|
||||
* @param parametersSource must not be {@literal null}.
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public JpaParameters(ParametersSource parametersSource) {
|
||||
super(parametersSource,
|
||||
methodParameter -> new JpaParameter(methodParameter, parametersSource.getDomainTypeInformation()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link JpaParameters} instance from the given {@link Method}.
|
||||
*
|
||||
* @param parametersSource must not be {@literal null}.
|
||||
* @param parameterFactory must not be {@literal null}.
|
||||
* @since 3.2.1
|
||||
*/
|
||||
protected JpaParameters(ParametersSource parametersSource,
|
||||
Function<MethodParameter, JpaParameter> parameterFactory) {
|
||||
super(parametersSource, parameterFactory);
|
||||
}
|
||||
|
||||
private JpaParameters(List<JpaParameter> parameters) {
|
||||
@@ -51,6 +79,7 @@ public class JpaParameters extends Parameters<JpaParameters, JpaParameter> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated(forRemoval = true)
|
||||
protected JpaParameter createParameter(MethodParameter parameter) {
|
||||
return new JpaParameter(parameter);
|
||||
}
|
||||
@@ -82,14 +111,31 @@ public class JpaParameters extends Parameters<JpaParameters, JpaParameter> {
|
||||
* Creates a new {@link JpaParameter}.
|
||||
*
|
||||
* @param parameter must not be {@literal null}.
|
||||
* @deprecated since 3.2.1
|
||||
*/
|
||||
@Deprecated(since = "3.2.1", forRemoval = true)
|
||||
protected JpaParameter(MethodParameter parameter) {
|
||||
|
||||
super(parameter);
|
||||
|
||||
this.annotation = parameter.getParameterAnnotation(Temporal.class);
|
||||
this.temporalType = null;
|
||||
if (!isDateParameter() && hasTemporalParamAnnotation()) {
|
||||
throw new IllegalArgumentException(
|
||||
Temporal.class.getSimpleName() + " annotation is only allowed on Date parameter");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link JpaParameter}.
|
||||
*
|
||||
* @param parameter must not be {@literal null}.
|
||||
*/
|
||||
protected JpaParameter(MethodParameter parameter, TypeInformation<?> domainType) {
|
||||
|
||||
super(parameter, domainType);
|
||||
this.annotation = parameter.getParameterAnnotation(Temporal.class);
|
||||
this.temporalType = null;
|
||||
if (!isDateParameter() && hasTemporalParamAnnotation()) {
|
||||
throw new IllegalArgumentException(
|
||||
Temporal.class.getSimpleName() + " annotation is only allowed on Date parameter");
|
||||
|
||||
@@ -42,6 +42,7 @@ import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.query.Parameter;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.ParametersSource;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.util.QueryExecutionConverters;
|
||||
import org.springframework.data.util.Lazy;
|
||||
@@ -447,8 +448,8 @@ public class JpaQueryMethod extends QueryMethod {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JpaParameters createParameters(Method method) {
|
||||
return new JpaParameters(method);
|
||||
protected Parameters<?, ?> createParameters(ParametersSource parametersSource) {
|
||||
return new JpaParameters(parametersSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,9 +7,9 @@ import java.lang.reflect.Method;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.repository.query.HibernateJpaParametersParameterAccessor;
|
||||
import org.springframework.data.jpa.repository.query.JpaParameters;
|
||||
import org.springframework.data.repository.query.ParametersSource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
@@ -48,7 +48,7 @@ class HibernateJpaParametersParameterAccessorUnitTests {
|
||||
private void parametersCanGetAccessesOutsideTransaction() throws NoSuchMethodException {
|
||||
|
||||
Method method = EntityManager.class.getMethod("flush");
|
||||
JpaParameters parameters = new JpaParameters(method);
|
||||
JpaParameters parameters = new JpaParameters(ParametersSource.of(method));
|
||||
HibernateJpaParametersParameterAccessor accessor = new HibernateJpaParametersParameterAccessor(parameters,
|
||||
new Object[] {}, em);
|
||||
Assertions.assertEquals(0, accessor.getValues().length);
|
||||
|
||||
@@ -15,8 +15,9 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.provider.PersistenceProvider;
|
||||
import org.springframework.data.repository.query.ParametersSource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@@ -42,7 +43,7 @@ class JpaParametersParameterAccessorTests {
|
||||
|
||||
Method withNativeQuery = SampleRepository.class.getMethod("withNativeQuery", Integer.class);
|
||||
Object[] values = { null };
|
||||
JpaParameters parameters = new JpaParameters(withNativeQuery);
|
||||
JpaParameters parameters = new JpaParameters(ParametersSource.of(withNativeQuery));
|
||||
JpaParametersParameterAccessor accessor = new JpaParametersParameterAccessor(parameters, values);
|
||||
|
||||
bind(parameters, accessor);
|
||||
@@ -55,7 +56,7 @@ class JpaParametersParameterAccessorTests {
|
||||
|
||||
Method withNativeQuery = SampleRepository.class.getMethod("withNativeQuery", Integer.class);
|
||||
Object[] values = { null };
|
||||
JpaParameters parameters = new JpaParameters(withNativeQuery);
|
||||
JpaParameters parameters = new JpaParameters(ParametersSource.of(withNativeQuery));
|
||||
JpaParametersParameterAccessor accessor = new HibernateJpaParametersParameterAccessor(parameters, values, em);
|
||||
|
||||
bind(parameters, accessor);
|
||||
|
||||
@@ -18,20 +18,24 @@ package org.springframework.data.jpa.repository.query;
|
||||
import static jakarta.persistence.TemporalType.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import jakarta.persistence.TemporalType;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
import jakarta.persistence.TemporalType;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.data.jpa.repository.Temporal;
|
||||
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.query.ParametersSource;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link JpaParameters}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Jens Schauder
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class JpaParametersUnitTests {
|
||||
|
||||
@@ -40,7 +44,7 @@ class JpaParametersUnitTests {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("foo", Date.class, String.class);
|
||||
|
||||
JpaParameters parameters = new JpaParameters(method);
|
||||
JpaParameters parameters = new JpaParameters(ParametersSource.of(method));
|
||||
|
||||
JpaParameter parameter = parameters.getBindableParameter(0);
|
||||
assertThat(parameter.isSpecialParameter()).isFalse();
|
||||
@@ -51,7 +55,7 @@ class JpaParametersUnitTests {
|
||||
assertThat(parameter.isTemporalParameter()).isFalse();
|
||||
}
|
||||
|
||||
interface SampleRepository {
|
||||
interface SampleRepository extends Repository<String, String> {
|
||||
|
||||
void foo(@Temporal(TIMESTAMP) Date date, String firstname);
|
||||
}
|
||||
|
||||
@@ -20,16 +20,15 @@ import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import io.vavr.control.Try;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Query;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Query;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -48,6 +47,7 @@ import org.springframework.data.jpa.repository.query.JpaQueryExecution.PagedExec
|
||||
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
import org.springframework.data.repository.query.ParametersSource;
|
||||
|
||||
/**
|
||||
* Unit test for {@link JpaQueryExecution}.
|
||||
@@ -177,7 +177,8 @@ class JpaQueryExecutionUnitTests {
|
||||
@Test // DATAJPA-124, DATAJPA-912
|
||||
void pagedExecutionRetrievesObjectsForPageableOutOfRange() throws Exception {
|
||||
|
||||
JpaParameters parameters = new JpaParameters(getClass().getMethod("sampleMethod", Pageable.class));
|
||||
JpaParameters parameters = new JpaParameters(
|
||||
ParametersSource.of(getClass().getMethod("sampleMethod", Pageable.class)));
|
||||
when(jpaQuery.createCountQuery(Mockito.any())).thenReturn(countQuery);
|
||||
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
|
||||
when(countQuery.getResultList()).thenReturn(Arrays.asList(20L));
|
||||
@@ -193,7 +194,8 @@ class JpaQueryExecutionUnitTests {
|
||||
@Test // DATAJPA-477, DATAJPA-912
|
||||
void pagedExecutionShouldNotGenerateCountQueryIfQueryReportedNoResults() throws Exception {
|
||||
|
||||
JpaParameters parameters = new JpaParameters(getClass().getMethod("sampleMethod", Pageable.class));
|
||||
JpaParameters parameters = new JpaParameters(
|
||||
ParametersSource.of(getClass().getMethod("sampleMethod", Pageable.class)));
|
||||
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
|
||||
when(query.getResultList()).thenReturn(Arrays.asList(0L));
|
||||
|
||||
@@ -208,7 +210,8 @@ class JpaQueryExecutionUnitTests {
|
||||
@Test // DATAJPA-912
|
||||
void pagedExecutionShouldUseCountFromResultIfOffsetIsZeroAndResultsWithinPageSize() throws Exception {
|
||||
|
||||
JpaParameters parameters = new JpaParameters(getClass().getMethod("sampleMethod", Pageable.class));
|
||||
JpaParameters parameters = new JpaParameters(
|
||||
ParametersSource.of(getClass().getMethod("sampleMethod", Pageable.class)));
|
||||
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
|
||||
when(query.getResultList()).thenReturn(Arrays.asList(new Object(), new Object(), new Object(), new Object()));
|
||||
|
||||
@@ -222,7 +225,8 @@ class JpaQueryExecutionUnitTests {
|
||||
@Test // DATAJPA-912
|
||||
void pagedExecutionShouldUseCountFromResultWithOffsetAndResultsWithinPageSize() throws Exception {
|
||||
|
||||
JpaParameters parameters = new JpaParameters(getClass().getMethod("sampleMethod", Pageable.class));
|
||||
JpaParameters parameters = new JpaParameters(
|
||||
ParametersSource.of(getClass().getMethod("sampleMethod", Pageable.class)));
|
||||
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
|
||||
when(query.getResultList()).thenReturn(Arrays.asList(new Object(), new Object(), new Object(), new Object()));
|
||||
|
||||
@@ -234,10 +238,10 @@ class JpaQueryExecutionUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATAJPA-912
|
||||
void pagedExecutionShouldUseRequestCountFromResultWithOffsetAndResultsHitLowerPageSizeBounds()
|
||||
throws Exception {
|
||||
void pagedExecutionShouldUseRequestCountFromResultWithOffsetAndResultsHitLowerPageSizeBounds() throws Exception {
|
||||
|
||||
JpaParameters parameters = new JpaParameters(getClass().getMethod("sampleMethod", Pageable.class));
|
||||
JpaParameters parameters = new JpaParameters(
|
||||
ParametersSource.of(getClass().getMethod("sampleMethod", Pageable.class)));
|
||||
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
|
||||
when(query.getResultList()).thenReturn(Collections.emptyList());
|
||||
when(jpaQuery.createCountQuery(Mockito.any())).thenReturn(query);
|
||||
@@ -251,10 +255,10 @@ class JpaQueryExecutionUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATAJPA-912
|
||||
void pagedExecutionShouldUseRequestCountFromResultWithOffsetAndResultsHitUpperPageSizeBounds()
|
||||
throws Exception {
|
||||
void pagedExecutionShouldUseRequestCountFromResultWithOffsetAndResultsHitUpperPageSizeBounds() throws Exception {
|
||||
|
||||
JpaParameters parameters = new JpaParameters(getClass().getMethod("sampleMethod", Pageable.class));
|
||||
JpaParameters parameters = new JpaParameters(
|
||||
ParametersSource.of(getClass().getMethod("sampleMethod", Pageable.class)));
|
||||
when(jpaQuery.createQuery(Mockito.any())).thenReturn(query);
|
||||
when(query.getResultList()).thenReturn(Arrays.asList(new Object(), new Object(), new Object(), new Object()));
|
||||
when(jpaQuery.createCountQuery(Mockito.any())).thenReturn(query);
|
||||
|
||||
@@ -68,6 +68,7 @@ import org.springframework.data.util.TypeInformation;
|
||||
* @author Mark Paluch
|
||||
* @author Erik Pellizzon
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class JpaQueryMethodUnitTests {
|
||||
@@ -156,6 +157,8 @@ class JpaQueryMethodUnitTests {
|
||||
void rejectsInvalidReturntypeOnPagebleFinder() {
|
||||
|
||||
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
|
||||
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
|
||||
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);
|
||||
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> new JpaQueryMethod(invalidReturnType, metadata, factory, extractor));
|
||||
@@ -165,6 +168,8 @@ class JpaQueryMethodUnitTests {
|
||||
void rejectsPageableAndSortInFinderMethod() {
|
||||
|
||||
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
|
||||
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
|
||||
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);
|
||||
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(() -> new JpaQueryMethod(pageableAndSort, metadata, factory, extractor));
|
||||
@@ -318,8 +323,10 @@ class JpaQueryMethodUnitTests {
|
||||
@Test // DATAJPA-466
|
||||
void shouldStoreJpa21FetchGraphInformationAsHint() {
|
||||
|
||||
doReturn(User.class).when(metadata).getDomainType();
|
||||
doReturn(User.class).when(metadata).getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph);
|
||||
when(metadata.getDomainType()).thenReturn((Class) User.class);
|
||||
when(metadata.getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph)).thenReturn((Class) User.class);
|
||||
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
|
||||
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);
|
||||
|
||||
JpaQueryMethod method = new JpaQueryMethod(queryMethodWithCustomEntityFetchGraph, metadata, factory, extractor);
|
||||
|
||||
@@ -331,8 +338,10 @@ class JpaQueryMethodUnitTests {
|
||||
@Test // DATAJPA-612
|
||||
void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethod() throws Exception {
|
||||
|
||||
doReturn(User.class).when(metadata).getDomainType();
|
||||
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
|
||||
when(metadata.getDomainType()).thenReturn((Class) User.class);
|
||||
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
|
||||
when(metadata.getReturnedDomainClass(queryMethodWithCustomEntityFetchGraph)).thenReturn((Class) User.class);
|
||||
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);
|
||||
|
||||
JpaQueryMethod method = new JpaQueryMethod(JpaRepositoryOverride.class.getMethod("findAll"), metadata, factory,
|
||||
extractor);
|
||||
@@ -345,8 +354,10 @@ class JpaQueryMethodUnitTests {
|
||||
@Test // DATAJPA-689
|
||||
void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethodFindOne() throws Exception {
|
||||
|
||||
doReturn(User.class).when(metadata).getDomainType();
|
||||
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
|
||||
when(metadata.getDomainType()).thenReturn((Class) User.class);
|
||||
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
|
||||
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
|
||||
when(metadata.getRepositoryInterface()).thenReturn((Class) InvalidRepository.class);
|
||||
|
||||
JpaQueryMethod method = new JpaQueryMethod(JpaRepositoryOverride.class.getMethod("findOne", Integer.class),
|
||||
metadata, factory, extractor);
|
||||
@@ -362,8 +373,10 @@ class JpaQueryMethodUnitTests {
|
||||
@Test
|
||||
void shouldFindEntityGraphAnnotationOnQueryMethodGetOneByWithDerivedName() throws Exception {
|
||||
|
||||
doReturn(User.class).when(metadata).getDomainType();
|
||||
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
|
||||
when(metadata.getDomainType()).thenReturn((Class) User.class);
|
||||
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
|
||||
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
|
||||
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);
|
||||
|
||||
JpaQueryMethod method = new JpaQueryMethod(JpaRepositoryOverride.class.getMethod("getOneById", Integer.class),
|
||||
metadata, factory, extractor);
|
||||
@@ -473,8 +486,10 @@ class JpaQueryMethodUnitTests {
|
||||
@Test // DATAJPA-871
|
||||
void usesAliasedValueForEntityGraph() throws Exception {
|
||||
|
||||
doReturn(User.class).when(metadata).getDomainType();
|
||||
doReturn(User.class).when(metadata).getReturnedDomainClass((Method) any());
|
||||
when(metadata.getDomainType()).thenReturn((Class) User.class);
|
||||
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
|
||||
when(metadata.getReturnedDomainClass(any())).thenReturn((Class) User.class);
|
||||
when(metadata.getRepositoryInterface()).thenReturn((Class) JpaRepositoryOverride.class);
|
||||
|
||||
JpaQueryMethod method = new JpaQueryMethod(
|
||||
JpaRepositoryOverride.class.getMethod("getOneWithCustomEntityGraphAnnotation"), metadata, factory, extractor);
|
||||
|
||||
@@ -15,14 +15,9 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
@@ -38,6 +33,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.provider.QueryExtractor;
|
||||
@@ -75,6 +71,7 @@ class NamedQueryUnitTests {
|
||||
|
||||
method = SampleRepository.class.getMethod("foo", Pageable.class);
|
||||
when(metadata.getDomainType()).thenReturn((Class) String.class);
|
||||
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(String.class));
|
||||
when(metadata.getReturnedDomainClass(method)).thenReturn((Class) String.class);
|
||||
when(metadata.getReturnType(any(Method.class)))
|
||||
.thenAnswer(invocation -> TypeInformation.fromReturnTypeOf(invocation.getArgument(0)));
|
||||
|
||||
@@ -15,23 +15,23 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import static java.util.Collections.*;
|
||||
import static jakarta.persistence.TemporalType.*;
|
||||
import static java.util.Collections.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.any;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import jakarta.persistence.Embeddable;
|
||||
import jakarta.persistence.Parameter;
|
||||
import jakarta.persistence.Query;
|
||||
import jakarta.persistence.TemporalType;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -44,7 +44,9 @@ import org.mockito.quality.Strictness;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.repository.Temporal;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.repository.query.ParametersSource;
|
||||
|
||||
/**
|
||||
* Unit test for {@link ParameterBinder}.
|
||||
@@ -78,7 +80,7 @@ class ParameterBinderUnitTests {
|
||||
|
||||
}
|
||||
|
||||
interface SampleRepository {
|
||||
interface SampleRepository extends Repository<User, Long> {
|
||||
|
||||
User useIndexedParameters(String lastname);
|
||||
|
||||
@@ -149,7 +151,7 @@ class ParameterBinderUnitTests {
|
||||
void bindsEmbeddableCorrectly() throws Exception {
|
||||
|
||||
Method method = getClass().getMethod("findByEmbeddable", SampleEmbeddable.class);
|
||||
JpaParameters parameters = new JpaParameters(method);
|
||||
JpaParameters parameters = createParameters(method);
|
||||
SampleEmbeddable embeddable = new SampleEmbeddable();
|
||||
|
||||
Object[] values = { embeddable };
|
||||
@@ -162,7 +164,7 @@ class ParameterBinderUnitTests {
|
||||
void shouldSetTemporalQueryParameterToDate() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("validWithDefaultTemporalTypeParameter", Date.class);
|
||||
JpaParameters parameters = new JpaParameters(method);
|
||||
JpaParameters parameters = createParameters(method);
|
||||
Date date = new Date();
|
||||
|
||||
Object[] values = { date };
|
||||
@@ -175,7 +177,7 @@ class ParameterBinderUnitTests {
|
||||
void shouldSetTemporalQueryParameterToTimestamp() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("validWithCustomTemporalTypeParameter", Date.class);
|
||||
JpaParameters parameters = new JpaParameters(method);
|
||||
JpaParameters parameters = createParameters(method);
|
||||
Date date = new Date();
|
||||
|
||||
Object[] values = { date };
|
||||
@@ -188,14 +190,14 @@ class ParameterBinderUnitTests {
|
||||
void shouldThrowIllegalArgumentExceptionIfIsAnnotatedWithTemporalParamAndParameterTypeIsNotDate() throws Exception {
|
||||
Method method = SampleRepository.class.getMethod("invalidWithTemporalTypeParameter", String.class);
|
||||
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> new JpaParameters(method));
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> createParameters(method));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-461
|
||||
void shouldAllowBindingOfVarArgsAsIs() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("validWithVarArgs", Integer[].class);
|
||||
JpaParameters parameters = new JpaParameters(method);
|
||||
JpaParameters parameters = createParameters(method);
|
||||
Integer[] ids = new Integer[] { 1, 2, 3 };
|
||||
Object[] values = { ids };
|
||||
bind(method, parameters, values);
|
||||
@@ -207,7 +209,7 @@ class ParameterBinderUnitTests {
|
||||
void unwrapsOptionalParameter() throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("optionalParameter", Optional.class);
|
||||
JpaParameters parameters = new JpaParameters(method);
|
||||
JpaParameters parameters = createParameters(method);
|
||||
|
||||
Object[] values = { Optional.of("Foo") };
|
||||
bind(method, parameters, values);
|
||||
@@ -221,14 +223,14 @@ class ParameterBinderUnitTests {
|
||||
Method method = SampleRepository.class.getMethod("withQuery", String.class, String.class);
|
||||
|
||||
Object[] values = { "foo", "superfluous" };
|
||||
bind(method, new JpaParameters(method), values);
|
||||
bind(method, createParameters(method), values);
|
||||
|
||||
verify(query).setParameter(eq(1), any());
|
||||
verify(query, never()).setParameter(eq(2), any());
|
||||
}
|
||||
|
||||
private void bind(Method method, Object[] values) {
|
||||
bind(method, new JpaParameters(method), values);
|
||||
bind(method, createParameters(method), values);
|
||||
}
|
||||
|
||||
private void bind(Method method, JpaParameters parameters, Object[] values) {
|
||||
@@ -237,7 +239,11 @@ class ParameterBinderUnitTests {
|
||||
}
|
||||
|
||||
private JpaParametersParameterAccessor getAccessor(Method method, Object... values) {
|
||||
return new JpaParametersParameterAccessor(new JpaParameters(method), values);
|
||||
return new JpaParametersParameterAccessor(createParameters(method), values);
|
||||
}
|
||||
|
||||
private static JpaParameters createParameters(Method method) {
|
||||
return new JpaParameters(ParametersSource.of(method));
|
||||
}
|
||||
|
||||
// needs to be public
|
||||
|
||||
@@ -17,12 +17,12 @@ package org.springframework.data.jpa.repository.query;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.query.ParameterMetadataProvider.ParameterMetadata;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.repository.query.Parameters;
|
||||
import org.springframework.data.repository.query.ParametersSource;
|
||||
import org.springframework.data.repository.query.parser.Part;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
@@ -77,7 +78,7 @@ class ParameterMetadataProviderIntegrationTests {
|
||||
|
||||
private ParameterMetadataProvider createProvider(Method method) {
|
||||
|
||||
JpaParameters parameters = new JpaParameters(method);
|
||||
JpaParameters parameters = new JpaParameters(ParametersSource.of(method));
|
||||
simulateDiscoveredParametername(parameters);
|
||||
|
||||
return new ParameterMetadataProvider(em.getCriteriaBuilder(), parameters, EscapeCharacter.DEFAULT);
|
||||
|
||||
@@ -97,7 +97,9 @@ class SimpleJpaQueryUnitTests {
|
||||
when(em.getEntityManagerFactory()).thenReturn(emf);
|
||||
when(em.getDelegate()).thenReturn(em);
|
||||
when(emf.createEntityManager()).thenReturn(em);
|
||||
when(metadata.getRepositoryInterface()).thenReturn((Class) SampleRepository.class);
|
||||
when(metadata.getDomainType()).thenReturn((Class) User.class);
|
||||
when(metadata.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(User.class));
|
||||
when(metadata.getReturnedDomainClass(Mockito.any(Method.class))).thenReturn((Class) User.class);
|
||||
when(metadata.getReturnType(Mockito.any(Method.class)))
|
||||
.thenAnswer(invocation -> TypeInformation.fromReturnTypeOf(invocation.getArgument(0)));
|
||||
|
||||
Reference in New Issue
Block a user