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