Skip to content

Commit

Permalink
Adopt JpaParameters to reflect the actual parameter type when using…
Browse files Browse the repository at this point in the history
… generics.

Closes #3254
  • Loading branch information
mp911de committed Dec 8, 2023
1 parent 9048aea commit ae56b1d
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
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;

/**
Expand All @@ -41,16 +44,42 @@ 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) {
super(parameters);
}

@Override
@Deprecated(forRemoval = true)
protected JpaParameter createParameter(MethodParameter parameter) {
return new JpaParameter(parameter);
}
Expand Down Expand Up @@ -82,14 +111,31 @@ public static class JpaParameter extends Parameter {
* 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand Down Expand Up @@ -447,8 +448,8 @@ private <T> T getMergedOrDefaultAnnotationValue(String attribute, Class annotati
}

@Override
protected JpaParameters createParameters(Method method) {
return new JpaParameters(method);
protected Parameters<?, ?> createParameters(ParametersSource parametersSource) {
return new JpaParameters(parametersSource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
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;
Expand Down Expand Up @@ -48,7 +48,7 @@ void withinTransaction() throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
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;

Expand All @@ -42,7 +43,7 @@ void createsJpaParametersParameterAccessor() throws Exception {

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);
Expand All @@ -55,7 +56,7 @@ void createsHibernateParametersParameterAccessor() throws Exception {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@
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 {

Expand All @@ -40,7 +44,7 @@ void findsTemporalParameterConfiguration() throws Exception {

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();
Expand All @@ -51,7 +55,7 @@ void findsTemporalParameterConfiguration() throws Exception {
assertThat(parameter.isTemporalParameter()).isFalse();
}

interface SampleRepository {
interface SampleRepository extends Repository<String, String> {

void foo(@Temporal(TIMESTAMP) Date date, String firstname);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
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;
Expand All @@ -48,6 +47,7 @@
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}.
Expand Down Expand Up @@ -177,7 +177,8 @@ void modifyingExecutionRejectsNonIntegerOrVoidReturnType() {
@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));
Expand All @@ -193,7 +194,8 @@ void pagedExecutionRetrievesObjectsForPageableOutOfRange() throws Exception {
@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));

Expand All @@ -208,7 +210,8 @@ void pagedExecutionShouldNotGenerateCountQueryIfQueryReportedNoResults() throws
@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()));

Expand All @@ -222,7 +225,8 @@ void pagedExecutionShouldUseCountFromResultIfOffsetIsZeroAndResultsWithinPageSiz
@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()));

Expand All @@ -234,10 +238,10 @@ void pagedExecutionShouldUseCountFromResultWithOffsetAndResultsWithinPageSize()
}

@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);
Expand All @@ -251,10 +255,10 @@ void pagedExecutionShouldUseRequestCountFromResultWithOffsetAndResultsHitLowerPa
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* @author Mark Paluch
* @author Erik Pellizzon
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class JpaQueryMethodUnitTests {
Expand Down Expand Up @@ -156,6 +157,8 @@ void returnsQueryIfAvailable() throws Exception {
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));
Expand All @@ -165,6 +168,8 @@ void rejectsInvalidReturntypeOnPagebleFinder() {
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));
Expand Down Expand Up @@ -318,8 +323,10 @@ void detectsLockAndQueryHintsOnIfUsedAsMetaAnnotation() throws Exception {
@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);

Expand All @@ -331,8 +338,10 @@ void shouldStoreJpa21FetchGraphInformationAsHint() {
@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);
Expand All @@ -345,8 +354,10 @@ void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethod() thro
@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);
Expand All @@ -362,8 +373,10 @@ void shouldFindEntityGraphAnnotationOnOverriddenSimpleJpaRepositoryMethodFindOne
@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);
Expand Down Expand Up @@ -473,8 +486,10 @@ void usesAliasedValueForQueryNativeQuery() throws Exception {
@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);
Expand Down
Loading

0 comments on commit ae56b1d

Please sign in to comment.