/
PartTreeJpaQueryIntegrationTests.java
234 lines (171 loc) · 7.99 KB
/
PartTreeJpaQueryIntegrationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License
import org.springframework.aop.framework.Advised;
");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.repository.query;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;
import org.hibernate.Version;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.provider.HibernateUtils;
import org.springframework.data.jpa.provider.PersistenceProvider;
import org.springframework.data.jpa.repository.Temporal;
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.Param;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration tests for {@link PartTreeJpaQuery}.
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Michael Cramer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:infrastructure.xml")
public class PartTreeJpaQueryIntegrationTests {
private static String PROPERTY = "h.target." + getQueryProperty();
@Rule public ExpectedException thrown = ExpectedException.none();
@PersistenceContext EntityManager entityManager;
PersistenceProvider provider;
@Before
public void setUp() {
this.provider = PersistenceProvider.fromEntityManager(entityManager);
}
@Test // DATADOC-90
public void test() throws Exception {
JpaQueryMethod queryMethod = getQueryMethod("findByFirstname", String.class, Pageable.class);
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
jpaQuery.createQuery(new Object[] { "Matthews", PageRequest.of(0, 1) });
jpaQuery.createQuery(new Object[] { "Matthews", PageRequest.of(0, 1) });
}
@Test
public void cannotIgnoreCaseIfNotString() throws Exception {
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Unable to ignore case of java.lang.Integer types, the property 'id' must reference a String");
testIgnoreCase("findByIdIgnoringCase", 3);
}
@Test
public void cannotIgnoreCaseIfNotStringUnlessIgnoringAll() throws Exception {
testIgnoreCase("findByIdAllIgnoringCase", 3);
}
@Test // DATAJPA-121
public void recreatesQueryIfNullValueIsGiven() throws Exception {
JpaQueryMethod queryMethod = getQueryMethod("findByFirstname", String.class, Pageable.class);
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
Query query = jpaQuery.createQuery(new Object[] { "Matthews", PageRequest.of(0, 1) });
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), endsWith("firstname=:param0"));
query = jpaQuery.createQuery(new Object[] { null, PageRequest.of(0, 1) });
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), endsWith("firstname is null"));
}
@Test // DATAJPA-920
public void shouldLimitExistsProjectionQueries() throws Exception {
JpaQueryMethod queryMethod = getQueryMethod("existsByFirstname", String.class);
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
Query query = jpaQuery.createQuery(new Object[] { "Matthews" });
assertThat(query.getMaxResults(), is(1));
}
@Test // DATAJPA-920
public void shouldSelectAliasedIdForExistsProjectionQueries() throws Exception {
JpaQueryMethod queryMethod = getQueryMethod("existsByFirstname", String.class);
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
Query query = jpaQuery.createQuery(new Object[] { "Matthews" });
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), containsString(".id from User as"));
}
@Test // DATAJPA-1074
public void isEmptyCollection() throws Exception {
JpaQueryMethod queryMethod = getQueryMethod("findByRolesIsEmpty");
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
Query query = jpaQuery.createQuery(new Object[] {});
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), endsWith("roles is empty"));
}
@Test // DATAJPA-1074
public void isNotEmptyCollection() throws Exception {
JpaQueryMethod queryMethod = getQueryMethod("findByRolesIsNotEmpty");
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager, provider);
Query query = jpaQuery.createQuery(new Object[] {});
assertThat(HibernateUtils.getHibernateQuery(getValue(query, PROPERTY)), endsWith("roles is not empty"));
}
@Test(expected = IllegalArgumentException.class) // DATAJPA-1074
public void rejectsIsEmptyOnNonCollectionProperty() throws Exception {
JpaQueryMethod method = getQueryMethod("findByFirstnameIsEmpty");
AbstractJpaQuery jpaQuery = new PartTreeJpaQuery(method, entityManager, provider);
jpaQuery.createQuery(new Object[] { "Oliver" });
}
private void testIgnoreCase(String methodName, Object... values) throws Exception {
Class<?>[] parameterTypes = new Class[values.length];
for (int i = 0; i < values.length; i++) {
parameterTypes[i] = values[i].getClass();
}
JpaQueryMethod queryMethod = getQueryMethod(methodName, parameterTypes);
PartTreeJpaQuery jpaQuery = new PartTreeJpaQuery(queryMethod, entityManager,
PersistenceProvider.fromEntityManager(entityManager));
jpaQuery.createQuery(values);
}
private JpaQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
return new JpaQueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
new SpelAwareProxyProjectionFactory(), PersistenceProvider.fromEntityManager(entityManager));
}
@SuppressWarnings("unchecked")
private static <T> T getValue(Object source, String path) {
Iterator<String> split = Arrays.asList(path.split("\\.")).iterator();
Object result = source;
while (split.hasNext()) {
result = getField(result, split.next());
}
return (T) result;
}
private static String getQueryProperty() {
return isHibernate43() || isHibernate5() ? "jpqlQuery" : "val$jpaqlQuery";
}
private static boolean isHibernate43() {
return Version.getVersionString().startsWith("4.3");
}
private static boolean isHibernate5() {
return Version.getVersionString().startsWith("5.");
}
interface UserRepository extends Repository<User, Long> {
Page<User> findByFirstname(String firstname, Pageable pageable);
User findByIdIgnoringCase(Integer id);
User findByIdAllIgnoringCase(Integer id);
boolean existsByFirstname(String firstname);
List<User> findByCreatedAtAfter(@Temporal(TemporalType.TIMESTAMP) @Param("refDate") Date refDate);
List<User> findByRolesIsEmpty();
List<User> findByRolesIsNotEmpty();
List<User> findByFirstnameIsEmpty();
}
}