/
PartTreeUnitTests.java
483 lines (404 loc) · 13.8 KB
/
PartTreeUnitTests.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* Copyright 2008-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://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.repository.query.parser;
import static java.util.Arrays.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.repository.query.parser.Part.Type.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.repository.query.parser.Part.IgnoreCaseType;
import org.springframework.data.repository.query.parser.Part.Type;
import org.springframework.data.repository.query.parser.PartTree.OrPart;
/**
* Unit tests for {@link PartTree}.
*
* @author Oliver Gierke
* @author Phillip Webb
* @author Thomas Darimont
*/
public class PartTreeUnitTests {
private String[] PREFIXES = { "find", "read", "get" };
@Test(expected = IllegalArgumentException.class)
public void rejectsNullSource() throws Exception {
new PartTree(null, getClass());
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNullDomainClass() throws Exception {
new PartTree("test", null);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsMultipleOrderBy() throws Exception {
partTree("firstnameOrderByLastnameOrderByFirstname");
}
@Test
public void parsesSimplePropertyCorrectly() throws Exception {
PartTree partTree = partTree("firstname");
assertPart(partTree, parts("firstname"));
}
@Test
public void parsesAndPropertiesCorrectly() throws Exception {
PartTree partTree = partTree("firstnameAndLastname");
assertPart(partTree, parts("firstname", "lastname"));
assertThat(partTree.getSort(), is(nullValue()));
}
@Test
public void parsesOrPropertiesCorrectly() throws Exception {
PartTree partTree = partTree("firstnameOrLastname");
assertPart(partTree, parts("firstname"), parts("lastname"));
assertThat(partTree.getSort(), is(nullValue()));
}
@Test
public void parsesCombinedAndAndOrPropertiesCorrectly() throws Exception {
PartTree tree = partTree("firstnameAndLastnameOrLastname");
assertPart(tree, parts("firstname", "lastname"), parts("lastname"));
}
@Test
public void hasSortIfOrderByIsGiven() throws Exception {
PartTree partTree = partTree("firstnameOrderByLastnameDesc");
assertThat(partTree.getSort(), is(new Sort(Direction.DESC, "lastname")));
}
@Test
public void hasSortIfOrderByIsGivenWithAllIgnoreCase() throws Exception {
hasSortIfOrderByIsGivenWithAllIgnoreCase("firstnameOrderByLastnameDescAllIgnoreCase");
hasSortIfOrderByIsGivenWithAllIgnoreCase("firstnameOrderByLastnameDescAllIgnoringCase");
hasSortIfOrderByIsGivenWithAllIgnoreCase("firstnameAllIgnoreCaseOrderByLastnameDesc");
}
private void hasSortIfOrderByIsGivenWithAllIgnoreCase(String source) throws Exception {
PartTree partTree = partTree(source);
assertThat(partTree.getSort(), is(new Sort(Direction.DESC, "lastname")));
}
@Test
public void detectsDistinctCorrectly() throws Exception {
for (String prefix : PREFIXES) {
detectsDistinctCorrectly(prefix + "DistinctByLastname", true);
detectsDistinctCorrectly(prefix + "UsersDistinctByLastname", true);
detectsDistinctCorrectly(prefix + "DistinctUsersByLastname", true);
detectsDistinctCorrectly(prefix + "UsersByLastname", false);
detectsDistinctCorrectly(prefix + "ByLastname", false);
// Check it's non-greedy (would strip everything until Order*By*
// otherwise)
PartTree tree = detectsDistinctCorrectly(prefix + "ByLastnameOrderByFirstnameDesc", false);
assertThat(tree.getSort(), is(new Sort(Direction.DESC, "firstname")));
}
}
private PartTree detectsDistinctCorrectly(String source, boolean expected) {
PartTree tree = partTree(source);
assertThat("Unexpected distinct value for '" + source + "'", tree.isDistinct(), is(expected));
return tree;
}
@Test
public void parsesWithinCorrectly() {
PartTree tree = partTree("findByLocationWithin");
for (Part part : tree.getParts()) {
assertThat(part.getType(), is(Type.WITHIN));
assertThat(part.getProperty(), is(newProperty("location")));
}
}
@Test
public void parsesNearCorrectly() {
assertType(asList("locationNear"), NEAR, "location");
}
@Test
public void supportToStringWithoutSortOrder() throws Exception {
assertType(asList("firstname"), SIMPLE_PROPERTY, "firstname");
}
@Test
public void supportToStringWithSortOrder() throws Exception {
PartTree tree = partTree("firstnameOrderByLastnameDesc");
assertThat(tree.toString(), is(equalTo("firstname SIMPLE_PROPERTY Order By lastname: DESC")));
}
@Test
public void detectsIgnoreAllCase() throws Exception {
detectsIgnoreAllCase("firstnameOrderByLastnameDescAllIgnoreCase", IgnoreCaseType.WHEN_POSSIBLE);
detectsIgnoreAllCase("firstnameOrderByLastnameDescAllIgnoringCase", IgnoreCaseType.WHEN_POSSIBLE);
detectsIgnoreAllCase("firstnameAllIgnoreCaseOrderByLastnameDesc", IgnoreCaseType.WHEN_POSSIBLE);
detectsIgnoreAllCase("getByFirstnameAllIgnoreCase", IgnoreCaseType.WHEN_POSSIBLE);
detectsIgnoreAllCase("getByFirstname", IgnoreCaseType.NEVER);
detectsIgnoreAllCase("firstnameOrderByLastnameDesc", IgnoreCaseType.NEVER);
}
private void detectsIgnoreAllCase(String source, IgnoreCaseType expected) throws Exception {
PartTree tree = partTree(source);
for (Part part : tree.getParts()) {
assertThat(part.shouldIgnoreCase(), is(expected));
}
}
@Test
public void detectsSpecificIgnoreCase() throws Exception {
PartTree tree = partTree("findByFirstnameIgnoreCaseAndLastname");
assertPart(tree, parts("firstname", "lastname"));
Iterator<Part> parts = tree.getParts().iterator();
assertThat(parts.next().shouldIgnoreCase(), is(IgnoreCaseType.ALWAYS));
assertThat(parts.next().shouldIgnoreCase(), is(IgnoreCaseType.NEVER));
}
@Test
public void detectsSpecificIgnoringCase() throws Exception {
PartTree tree = partTree("findByFirstnameIgnoringCaseAndLastname");
assertPart(tree, parts("firstname", "lastname"));
Iterator<Part> parts = tree.getParts().iterator();
assertThat(parts.next().shouldIgnoreCase(), is(IgnoreCaseType.ALWAYS));
assertThat(parts.next().shouldIgnoreCase(), is(IgnoreCaseType.NEVER));
}
/**
* @see DATACMNS-78
*/
@Test
public void parsesLessThanEqualCorrectly() {
assertType(Arrays.asList("lastnameLessThanEqual", "lastnameIsLessThanEqual"), LESS_THAN_EQUAL, "lastname");
}
/**
* @see DATACMNS-78
*/
@Test
public void parsesGreaterThanEqualCorrectly() {
assertType(Arrays.asList("lastnameGreaterThanEqual", "lastnameIsGreaterThanEqual"), GREATER_THAN_EQUAL, "lastname");
}
@Test
public void returnsAllParts() {
PartTree tree = partTree("findByLastnameAndFirstname");
assertPart(tree, parts("lastname", "firstname"));
}
@Test
public void returnsAllPartsOfType() {
PartTree tree = partTree("findByLastnameAndFirstnameGreaterThan");
Collection<Part> parts = toCollection(tree.getParts(Type.SIMPLE_PROPERTY));
assertThat(parts, hasItem(part("lastname")));
assertThat(parts, is(hasSize(1)));
parts = toCollection(tree.getParts(Type.GREATER_THAN));
assertThat(parts, hasItem(new Part("FirstnameGreaterThan", User.class)));
assertThat(parts, is(hasSize(1)));
}
/**
* @see DATACMNS-94
*/
@Test
public void parsesExistsKeywordCorrectly() {
assertType(asList("lastnameExists"), EXISTS, "lastname", 0, false);
}
/**
* @see DATACMNS-94
*/
@Test
public void parsesRegexKeywordCorrectly() {
assertType(asList("lastnameRegex", "lastnameMatchesRegex", "lastnameMatches"), REGEX, "lastname");
}
/**
* @see DATACMNS-107
*/
@Test
public void parsesTrueKeywordCorrectly() {
assertType(asList("activeTrue", "activeIsTrue"), TRUE, "active", 0, false);
}
/**
* @see DATACMNS-107
*/
@Test
public void parsesFalseKeywordCorrectly() {
assertType(asList("activeFalse", "activeIsFalse"), FALSE, "active", 0, false);
}
/**
* @see DATACMNS-111
*/
@Test
public void parsesStartingWithKeywordCorrectly() {
assertType(asList("firstnameStartsWith", "firstnameStartingWith", "firstnameIsStartingWith"), STARTING_WITH,
"firstname");
}
/**
* @see DATACMNS-111
*/
@Test
public void parsesEndingWithKeywordCorrectly() {
assertType(asList("firstnameEndsWith", "firstnameEndingWith", "firstnameIsEndingWith"), ENDING_WITH, "firstname");
}
/**
* @see DATACMNS-111
*/
@Test
public void parsesContainingKeywordCorrectly() {
assertType(asList("firstnameIsContaining", "firstnameContains", "firstnameContaining"), CONTAINING, "firstname");
}
/**
* @see DATACMNS-141
*/
@Test
public void parsesAfterKeywordCorrectly() {
assertType(asList("birthdayAfter", "birthdayIsAfter"), Type.AFTER, "birthday");
}
/**
* @see DATACMNS-141
*/
@Test
public void parsesBeforeKeywordCorrectly() {
assertType(Arrays.asList("birthdayBefore", "birthdayIsBefore"), Type.BEFORE, "birthday");
}
/**
* @see DATACMNS-182
*/
@Test
public void parsesContainingCorrectly() {
PartTree tree = new PartTree("findAllByLegalNameContainingOrCommonNameContainingAllIgnoringCase",
Organization.class);
assertPart(tree, new Part[] { new Part("legalNameContaining", Organization.class) }, new Part[] { new Part(
"commonNameContaining", Organization.class) });
}
/**
* @see DATACMNS-221
*/
@Test
public void parsesSpecialCharactersCorrectly() {
PartTree tree = new PartTree("findByØreAndÅrOrderByÅrAsc", DomainObjectWithSpecialChars.class);
assertPart(tree, new Part[] { new Part("øre", DomainObjectWithSpecialChars.class),
new Part("år", DomainObjectWithSpecialChars.class) });
assertTrue(tree.getSort().getOrderFor("år").isAscending());
}
/**
* @see DATACMNS-303
*/
@Test
public void identifiesSimpleCountByCorrectly() {
PartTree tree = new PartTree("countByLastname", User.class);
assertThat(tree.isCountProjection(), is(true));
}
/**
* @see DATACMNS-303
*/
@Test
public void identifiesExtendedCountByCorrectly() {
PartTree tree = new PartTree("countUserByLastname", User.class);
assertThat(tree.isCountProjection(), is(true));
}
/**
* @see DATACMNS-303
*/
@Test
public void identifiesCountAndDistinctByCorrectly() {
PartTree tree = new PartTree("countDistinctUserByLastname", User.class);
assertThat(tree.isCountProjection(), is(true));
assertThat(tree.isDistinct(), is(true));
}
/**
* @see DATAJPA-324
*/
@Test
public void resolvesPropertyPathFromGettersOnInterfaces() {
assertThat(new PartTree("findByCategoryId", Product.class), is(notNullValue()));
}
/**
* @see DATACMNS-368
*/
@Test
public void detectPropertyWithOrKeywordPart() {
assertThat(new PartTree("findByOrder", Product.class), is(notNullValue()));
}
/**
* @see DATACMNS-368
*/
@Test
public void detectPropertyWithAndKeywordPart() {
assertThat(new PartTree("findByAnders", Product.class), is(notNullValue()));
}
private static void assertType(Iterable<String> sources, Type type, String property) {
assertType(sources, type, property, 1, true);
}
private static void assertType(Iterable<String> sources, Type type, String property, int numberOfArguments,
boolean parameterRequired) {
for (String source : sources) {
Part part = part(source);
assertThat(part.getType(), is(type));
assertThat(part.getProperty(), is(newProperty(property)));
assertThat(part.getNumberOfArguments(), is(numberOfArguments));
assertThat(part.getParameterRequired(), is(parameterRequired));
}
}
private static PartTree partTree(String source) {
return new PartTree(source, User.class);
}
private static Part part(String part) {
return new Part(part, User.class);
}
private static Part[] parts(String... part) {
Part[] parts = new Part[part.length];
for (int i = 0; i < parts.length; i++) {
parts[i] = part(part[i]);
}
return parts;
}
private static PropertyPath newProperty(String name) {
return PropertyPath.from(name, User.class);
}
private void assertPart(PartTree tree, Part[]... parts) {
Iterator<OrPart> iterator = tree.iterator();
for (Part[] part : parts) {
assertThat(iterator.hasNext(), is(true));
Iterator<Part> partIterator = iterator.next().iterator();
for (int k = 0; k < part.length; k++) {
assertThat(String.format("Expected %d parts but have %d", part.length, k), partIterator.hasNext(), is(true));
Part next = partIterator.next();
assertThat(String.format("Expected %s but got %s!", part[k], next), part[k], is(next));
}
assertThat("Too many parts!", partIterator.hasNext(), is(false));
}
assertThat("Too many or parts!", iterator.hasNext(), is(false));
}
private static <T> Collection<T> toCollection(Iterable<T> iterable) {
List<T> result = new ArrayList<T>();
for (T element : iterable) {
result.add(element);
}
return result;
}
class User {
String firstname;
String lastname;
double[] location;
boolean active;
Date birthday;
}
class Organization {
String commonName;
String legalName;
}
class DomainObjectWithSpecialChars {
String øre;
String år;
public Order getOrder() {
return null;
}
}
interface Product {
Order getOrder(); // contains Or keyword
Anders getAnders(); // constains And keyword
Category getCategory();
}
interface Category {
Long getId();
}
interface Order {
Long getId();
}
interface Anders {
Long getId();
}
}