|
| 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 | + |
| 17 | +package example.springdata.jpa.querybyexample; |
| 18 | + |
| 19 | +import static org.hamcrest.CoreMatchers.*; |
| 20 | +import static org.junit.Assert.*; |
| 21 | +import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*; |
| 22 | +import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith; |
| 23 | + |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 29 | +import org.springframework.data.domain.Example; |
| 30 | +import org.springframework.data.domain.ExampleSpec; |
| 31 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 32 | +import org.springframework.transaction.annotation.Transactional; |
| 33 | + |
| 34 | +/** |
| 35 | + * Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories. |
| 36 | + * |
| 37 | + * @author Mark Paluch |
| 38 | + */ |
| 39 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 40 | +@Transactional |
| 41 | +@SpringApplicationConfiguration(classes = ApplicationConfiguration.class) |
| 42 | +public class UserRepositoryInheritanceIntegrationTests { |
| 43 | + |
| 44 | + @Autowired UserRepository repository; |
| 45 | + |
| 46 | + User skyler, walter, flynn; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() { |
| 50 | + |
| 51 | + repository.deleteAll(); |
| 52 | + |
| 53 | + this.skyler = repository.save(new User("Skyler", "White", 45)); |
| 54 | + this.walter = repository.save(new SpecialUser("Walter", "White", 50)); |
| 55 | + this.flynn = repository.save(new SpecialUser("Walter Jr. (Flynn)", "White", 17)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @see DATAJPA-218 |
| 60 | + */ |
| 61 | + @Test |
| 62 | + public void countBySimpleExample() { |
| 63 | + |
| 64 | + Example<User> example = Example.of(new SpecialUser(null, "White", null)); |
| 65 | + |
| 66 | + assertThat(repository.count(example), is(3L)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @see DATAJPA-218 |
| 71 | + */ |
| 72 | + @Test |
| 73 | + public void countUserByTypedExample() { |
| 74 | + |
| 75 | + Example<User> example = Example.of(new SpecialUser(null, "White", null), // |
| 76 | + ExampleSpec.typed(User.class)); |
| 77 | + |
| 78 | + assertThat(repository.count(example), is(3L)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @see DATAJPA-218 |
| 83 | + */ |
| 84 | + @Test |
| 85 | + public void countSpecialUserByTypedExample() { |
| 86 | + |
| 87 | + Example<SpecialUser> example = Example.of(new SpecialUser(null, "White", null), // |
| 88 | + ExampleSpec.typed(SpecialUser.class)); |
| 89 | + |
| 90 | + assertThat(repository.count(example), is(2L)); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments