|
| 1 | +/* |
| 2 | + * Copyright 2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package example.springdata.jpa.querybyexample; |
| 17 | + |
| 18 | +import static org.hamcrest.CoreMatchers.*; |
| 19 | +import static org.junit.Assert.*; |
| 20 | +import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*; |
| 21 | + |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 27 | +import org.springframework.data.domain.Example; |
| 28 | +import org.springframework.data.domain.ExampleSpec; |
| 29 | +import org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers; |
| 30 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 31 | +import org.springframework.transaction.annotation.Transactional; |
| 32 | + |
| 33 | +/** |
| 34 | + * Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories. |
| 35 | + * |
| 36 | + * @author Mark Paluch |
| 37 | + */ |
| 38 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 39 | +@Transactional |
| 40 | +@SpringApplicationConfiguration(classes = ApplicationConfiguration.class) |
| 41 | +public class UserRepositoryIntegrationTests { |
| 42 | + |
| 43 | + @Autowired UserRepository repository; |
| 44 | + |
| 45 | + User skyler, walter, flynn, marie, hank; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setUp() { |
| 49 | + |
| 50 | + repository.deleteAll(); |
| 51 | + |
| 52 | + this.skyler = repository.save(new User("Skyler", "White", 45)); |
| 53 | + this.walter = repository.save(new User("Walter", "White", 50)); |
| 54 | + this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17)); |
| 55 | + this.marie = repository.save(new User("Marie", "Schrader", 38)); |
| 56 | + this.hank = repository.save(new User("Hank", "Schrader", 43)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @see DATAJPA-218 |
| 61 | + */ |
| 62 | + @Test |
| 63 | + public void countBySimpleExample() { |
| 64 | + |
| 65 | + Example<User> example = Example.of(new User(null, "White", null)); |
| 66 | + |
| 67 | + assertThat(repository.count(example), is(3L)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @see DATAJPA-218 |
| 72 | + */ |
| 73 | + @Test |
| 74 | + public void ignorePropertiesAndMatchByAge() { |
| 75 | + |
| 76 | + Example<User> example = ExampleSpec.of(User.class). // |
| 77 | + withIgnorePaths("firstname", "lastname").// |
| 78 | + createExample(flynn); |
| 79 | + |
| 80 | + assertThat(repository.findOne(example), is(flynn)); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @see DATAJPA-218 |
| 85 | + */ |
| 86 | + @Test |
| 87 | + public void substringMatching() { |
| 88 | + |
| 89 | + Example<User> example = ExampleSpec.of(User.class).// |
| 90 | + withStringMatcherEnding()// |
| 91 | + .createExample(new User("er", null, null)); |
| 92 | + |
| 93 | + assertThat(repository.findAll(example), hasItems(skyler, walter)); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @see DATAJPA-218 |
| 98 | + */ |
| 99 | + @Test |
| 100 | + public void matchStartingStringsIgnoreCase() { |
| 101 | + |
| 102 | + Example<User> example = ExampleSpec.of(User.class). // |
| 103 | + withIgnorePaths("age").// |
| 104 | + withMatcher("firstname", startsWith()).// |
| 105 | + withMatcher("lastname", ignoreCase()).// |
| 106 | + createExample(new User("Walter", "WHITE", null)); |
| 107 | + |
| 108 | + assertThat(repository.findAll(example), hasItems(flynn, walter)); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @see DATAJPA-218 |
| 113 | + */ |
| 114 | + @Test |
| 115 | + public void configuringMatchersUsingLambdas() { |
| 116 | + |
| 117 | + Example<User> example = ExampleSpec.of(User.class).withIgnorePaths("age"). // |
| 118 | + withMatcher("firstname", matcher -> matcher.startsWith()). // |
| 119 | + withMatcher("lastname", matcher -> matcher.ignoreCase()). // |
| 120 | + createExample(new User("Walter", "WHITE", null)); |
| 121 | + |
| 122 | + assertThat(repository.findAll(example), hasItems(flynn, walter)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @see DATAJPA-218 |
| 127 | + */ |
| 128 | + @Test |
| 129 | + public void valueTransformer() { |
| 130 | + |
| 131 | + Example<User> example = ExampleSpec.of(User.class). // |
| 132 | + withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))).// |
| 133 | + createExample(new User(null, "White", 99)); |
| 134 | + |
| 135 | + assertThat(repository.findAll(example), hasItems(walter)); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments