Skip to content

Commit 6c354f9

Browse files
Regex ExampleMatcher Implementation (#195)
* update gitignore * Regex Co-authored-by: Paulo Ferreira <paulo.ferreira25@gmail.com>
1 parent af61c38 commit 6c354f9

File tree

2 files changed

+62
-24
lines changed

2 files changed

+62
-24
lines changed

src/main/java/com/arangodb/springframework/repository/ArangoExampleConverter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ private void addPredicate(
126126
? example.getMatcher().getDefaultStringMatcher()
127127
: specifier.getStringMatcher();
128128
final String string = (String) value;
129-
clause = String.format("LIKE(e.%s, @%s, %b)", fullPath, binding, ignoreCase);
129+
if (stringMatcher == ExampleMatcher.StringMatcher.REGEX) {
130+
clause = String.format("REGEX_TEST(e.%s, @%s, %b)", fullPath, binding, ignoreCase);
131+
} else {
132+
clause = String.format("LIKE(e.%s, @%s, %b)", fullPath, binding, ignoreCase);
133+
}
130134
switch (stringMatcher) {
131135
case STARTING:
132136
value = escape(string) + "%";
@@ -137,6 +141,9 @@ private void addPredicate(
137141
case CONTAINING:
138142
value = "%" + escape(string) + "%";
139143
break;
144+
case REGEX:
145+
value = escape(string);
146+
break;
140147
case DEFAULT:
141148
case EXACT:
142149
default:

src/test/java/com/arangodb/springframework/repository/ArangoRepositoryTest.java

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package com.arangodb.springframework.repository;
22

3-
import com.arangodb.springframework.testdata.Address;
4-
import com.arangodb.springframework.testdata.Customer;
5-
import com.arangodb.springframework.testdata.ShoppingCart;
6-
import org.junit.Test;
7-
import org.springframework.data.domain.*;
8-
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
3+
import static org.hamcrest.Matchers.is;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertThat;
7+
import static org.junit.Assert.assertTrue;
98

109
import java.util.ArrayList;
1110
import java.util.LinkedList;
1211
import java.util.List;
1312
import java.util.Optional;
1413

15-
import static org.hamcrest.Matchers.is;
16-
import static org.junit.Assert.*;
14+
import org.junit.Test;
15+
import org.springframework.data.domain.Example;
16+
import org.springframework.data.domain.ExampleMatcher;
17+
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
18+
import org.springframework.data.domain.Page;
19+
import org.springframework.data.domain.PageRequest;
20+
import org.springframework.data.domain.Sort;
21+
22+
import com.arangodb.springframework.testdata.Address;
23+
import com.arangodb.springframework.testdata.Customer;
24+
import com.arangodb.springframework.testdata.ShoppingCart;
1725

1826
/**
1927
* Created by F625633 on 06/07/2017.
@@ -178,6 +186,29 @@ public void findAllByExampleTest() {
178186
assertTrue(equals(checkList, retrievedList, cmp, eq, false));
179187
}
180188

189+
@Test
190+
public void findAllByExampleRegexTest() {
191+
final List<Customer> toBeRetrieved = new LinkedList<>();
192+
final Customer check1 = new Customer("B", "X", 0);
193+
final Customer check2 = new Customer("B", "Y", 0);
194+
toBeRetrieved.add(new Customer("A", "Z", 0));
195+
toBeRetrieved.add(check1);
196+
toBeRetrieved.add(check2);
197+
toBeRetrieved.add(new Customer("C", "V", 0));
198+
repository.saveAll(toBeRetrieved);
199+
Customer find = new Customer("([B])", null, 0);
200+
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withIgnoreNullValues().withMatcher("name",
201+
match -> match.regex());
202+
final Example<Customer> example = Example.of(find, exampleMatcher);
203+
final Iterable<?> retrieved = repository.findAll(example);
204+
final List<Customer> retrievedList = new LinkedList<>();
205+
retrieved.forEach(e -> retrievedList.add((Customer) e));
206+
final List<Customer> checkList = new LinkedList<>();
207+
checkList.add(check1);
208+
checkList.add(check2);
209+
assertTrue(equals(checkList, retrievedList, cmp, eq, false));
210+
}
211+
181212
@Test
182213
public void findAllSortByExampleTest() {
183214
final List<Customer> toBeRetrieved = new LinkedList<>();
@@ -186,8 +217,8 @@ public void findAllSortByExampleTest() {
186217
toBeRetrieved.add(new Customer("B", "D", 0));
187218
repository.saveAll(toBeRetrieved);
188219
repository.save(new Customer("A", "A", 1));
189-
final Example<Customer> example = Example.of(new Customer("", "", 0),
190-
ExampleMatcher.matchingAny().withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
220+
final Example<Customer> example = Example.of(new Customer("", "", 0), ExampleMatcher.matchingAny()
221+
.withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
191222
final List<Sort.Order> orders = new LinkedList<>();
192223
orders.add(new Sort.Order(Sort.Direction.ASC, "name"));
193224
orders.add(new Sort.Order(Sort.Direction.ASC, "surname"));
@@ -211,8 +242,8 @@ public void findAllPageableByExampleTest() {
211242
toBeRetrieved.add(new Customer("F", "R", 0));
212243
repository.saveAll(toBeRetrieved);
213244
repository.save(new Customer("A", "A", 1));
214-
final Example<Customer> example = Example.of(new Customer("", "", 0),
215-
ExampleMatcher.matchingAny().withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
245+
final Example<Customer> example = Example.of(new Customer("", "", 0), ExampleMatcher.matchingAny()
246+
.withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
216247
final List<Sort.Order> orders = new LinkedList<>();
217248
orders.add(new Sort.Order(Sort.Direction.ASC, "name"));
218249
orders.add(new Sort.Order(Sort.Direction.ASC, "surname"));
@@ -242,8 +273,8 @@ public void countByExampleTest() {
242273
toBeRetrieved.add(new Customer("F", "Q", 0));
243274
toBeRetrieved.add(new Customer("F", "R", 0));
244275
repository.saveAll(toBeRetrieved);
245-
final Example<Customer> example = Example.of(new Customer("", "", 0),
246-
ExampleMatcher.matchingAny().withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
276+
final Example<Customer> example = Example.of(new Customer("", "", 0), ExampleMatcher.matchingAny()
277+
.withIgnoreNullValues().withIgnorePaths(new String[] { "location", "alive" }));
247278
final long size = repository.count(example);
248279
assertTrue(size == toBeRetrieved.size());
249280
}
@@ -296,9 +327,9 @@ public void endingWithByExampleNestedTest() {
296327
nested3.setNestedCustomer(nested2);
297328
exampleCustomer.setNestedCustomer(nested3);
298329
final Example<Customer> example = Example.of(exampleCustomer,
299-
ExampleMatcher.matching().withMatcher("nestedCustomer.name", match -> match.endsWith())
300-
.withIgnoreCase("nestedCustomer.name").withIgnoreNullValues()
301-
.withTransformer("nestedCustomer.age", o -> Optional.of(Integer.valueOf(o.get().toString()) + 1)));
330+
ExampleMatcher.matching().withMatcher("nestedCustomer.name", match -> match.endsWith())
331+
.withIgnoreCase("nestedCustomer.name").withIgnoreNullValues().withTransformer(
332+
"nestedCustomer.age", o -> Optional.of(Integer.valueOf(o.get().toString()) + 1)));
302333
final Customer retrieved = repository.findOne(example).get();
303334
assertEquals(check, retrieved);
304335
}
@@ -321,10 +352,10 @@ public void endingWithByExampleNestedIncludeNullTest() {
321352
nested3.setNestedCustomer(nested2);
322353
exampleCustomer.setNestedCustomer(nested3);
323354
final Example<Customer> example = Example.of(exampleCustomer,
324-
ExampleMatcher.matching().withMatcher("nestedCustomer.name", match -> match.endsWith())
325-
.withIgnorePaths(new String[] { "arangoId", "id", "key", "rev" })
326-
.withIgnoreCase("nestedCustomer.name").withIncludeNullValues()
327-
.withTransformer("nestedCustomer.age", o -> Optional.of(Integer.valueOf(o.get().toString()) + 1)));
355+
ExampleMatcher.matching().withMatcher("nestedCustomer.name", match -> match.endsWith())
356+
.withIgnorePaths(new String[] { "arangoId", "id", "key", "rev" })
357+
.withIgnoreCase("nestedCustomer.name").withIncludeNullValues().withTransformer(
358+
"nestedCustomer.age", o -> Optional.of(Integer.valueOf(o.get().toString()) + 1)));
328359
final Customer retrieved = repository.findOne(example).get();
329360
assertEquals(check, retrieved);
330361
}
@@ -337,8 +368,8 @@ public void containingExampleTest() {
337368
final Customer probe = new Customer();
338369
probe.setName("am");
339370
final Example<Customer> example = Example.of(probe,
340-
ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING).withIgnorePaths("arangoId", "id",
341-
"key", "rev", "surname", "age"));
371+
ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING).withIgnorePaths("arangoId", "id",
372+
"key", "rev", "surname", "age"));
342373
final Optional<Customer> retrieved = repository.findOne(example);
343374
assertThat(retrieved.isPresent(), is(true));
344375
assertThat(retrieved.get().getName(), is("name"));

0 commit comments

Comments
 (0)