|
| 1 | +package org.springframework.data.jpa.repository.query; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.ArgumentMatchers.eq; |
| 5 | +import static org.mockito.ArgumentMatchers.isNull; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | + |
| 9 | +import java.lang.reflect.Method; |
| 10 | + |
| 11 | +import javax.persistence.EntityManager; |
| 12 | +import javax.persistence.PersistenceContext; |
| 13 | +import javax.persistence.Query; |
| 14 | + |
| 15 | +import org.hibernate.jpa.TypedParameterValue; |
| 16 | +import org.hibernate.type.StandardBasicTypes; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 20 | +import org.mockito.ArgumentCaptor; |
| 21 | +import org.springframework.data.jpa.domain.sample.User; |
| 22 | +import org.springframework.test.context.ContextConfiguration; |
| 23 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 24 | + |
| 25 | +/** |
| 26 | + * Unit test for {@link JpaParametersParameterAccessor}. |
| 27 | + * |
| 28 | + * @author Wonchul Heo |
| 29 | + */ |
| 30 | +@ExtendWith(SpringExtension.class) |
| 31 | +@ContextConfiguration("classpath:infrastructure.xml") |
| 32 | +class JpaParametersParameterAccessorTests { |
| 33 | + |
| 34 | + @PersistenceContext |
| 35 | + private EntityManager em; |
| 36 | + private Query query; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + void setUp() { |
| 40 | + query = mock(Query.class); |
| 41 | + } |
| 42 | + |
| 43 | + @Test // GH-2370 |
| 44 | + void createsJpaParametersParameterAccessor() throws Exception { |
| 45 | + |
| 46 | + Method withNativeQuery = SampleRepository.class.getMethod("withNativeQuery", Integer.class); |
| 47 | + Object[] values = { null }; |
| 48 | + JpaParameters parameters = new JpaParameters(withNativeQuery); |
| 49 | + JpaParametersParameterAccessor accessor = new JpaParametersParameterAccessor(parameters, values); |
| 50 | + |
| 51 | + bind(parameters, accessor); |
| 52 | + |
| 53 | + verify(query).setParameter(eq(1), isNull()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test // GH-2370 |
| 57 | + void createsHibernateParametersParameterAccessor() throws Exception { |
| 58 | + |
| 59 | + Method withNativeQuery = SampleRepository.class.getMethod("withNativeQuery", Integer.class); |
| 60 | + Object[] values = { null }; |
| 61 | + JpaParameters parameters = new JpaParameters(withNativeQuery); |
| 62 | + JpaParametersParameterAccessor accessor = |
| 63 | + new HibernateJpaParametersParameterAccessor(parameters, values, em); |
| 64 | + |
| 65 | + bind(parameters, accessor); |
| 66 | + |
| 67 | + ArgumentCaptor<TypedParameterValue> captor = ArgumentCaptor.forClass(TypedParameterValue.class); |
| 68 | + verify(query).setParameter(eq(1), captor.capture()); |
| 69 | + TypedParameterValue captorValue = captor.getValue(); |
| 70 | + assertThat(captorValue.getType()).isEqualTo(StandardBasicTypes.INTEGER); |
| 71 | + assertThat(captorValue.getValue()).isNull(); |
| 72 | + } |
| 73 | + |
| 74 | + private void bind(JpaParameters parameters, JpaParametersParameterAccessor accessor) { |
| 75 | + ParameterBinderFactory.createBinder(parameters).bind(QueryParameterSetter.BindableQuery.from(query), |
| 76 | + accessor, |
| 77 | + QueryParameterSetter.ErrorHandling.LENIENT); |
| 78 | + } |
| 79 | + |
| 80 | + interface SampleRepository { |
| 81 | + @org.springframework.data.jpa.repository.Query( |
| 82 | + value = "select 1 from user where age = :age", |
| 83 | + nativeQuery = true) |
| 84 | + User withNativeQuery(Integer age); |
| 85 | + } |
| 86 | +} |
0 commit comments