|
| 1 | +/* |
| 2 | + * Copyright 2008-2019 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 | + * https://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 org.springframework.data.jpa.repository; |
| 17 | + |
| 18 | +import static org.hamcrest.Matchers.*; |
| 19 | +import static org.junit.Assert.*; |
| 20 | + |
| 21 | +import org.junit.After; |
| 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.data.domain.Page; |
| 27 | +import org.springframework.data.domain.PageRequest; |
| 28 | +import org.springframework.data.jpa.domain.sample.User; |
| 29 | +import org.springframework.data.jpa.repository.custom.UserCustomExtendedRepository; |
| 30 | +import org.springframework.data.repository.query.QueryLookupStrategy; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 33 | +import org.springframework.transaction.annotation.Transactional; |
| 34 | + |
| 35 | +/** |
| 36 | + * Integration test for executing custom finders, thus testing various query lookup strategies. |
| 37 | + * |
| 38 | + * @see QueryLookupStrategy |
| 39 | + * @author Andriy Redko |
| 40 | + */ |
| 41 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 42 | +@ContextConfiguration(locations = "classpath:config/namespace-customfactory-context.xml") |
| 43 | +@Transactional |
| 44 | +public class UserCustomRepositoryFinderTests { |
| 45 | + |
| 46 | + @Autowired UserCustomExtendedRepository userRepository; |
| 47 | + |
| 48 | + User dave, carter, oliver; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() { |
| 52 | + dave = userRepository. save( new User( "Dave", "Matthews", "[email protected]")); |
| 53 | + carter = userRepository. save( new User( "Carter", "Beauford", "[email protected]")); |
| 54 | + oliver = userRepository. save( new User( "Oliver August", "Matthews", "[email protected]")); |
| 55 | + } |
| 56 | + |
| 57 | + @After |
| 58 | + public void clearUp() { |
| 59 | + userRepository.deleteAll(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void executesNamedQueryWithPipes() { |
| 64 | + Page<User> page = userRepository.findByNamedQueryWithLike("eau", PageRequest.of(0, 10)); |
| 65 | + assertThat(page.getNumberOfElements(), is(1)); |
| 66 | + assertThat(page.getTotalElements(), is(1L)); |
| 67 | + assertThat(page.getTotalPages(), is(1)); |
| 68 | + assertThat(page.getContent(), hasItems(carter)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void executesNamedQueryWithConcat() { |
| 73 | + Page<User> page = userRepository.findByNamedQueryWithLikeConcat("eau", PageRequest.of(0, 10)); |
| 74 | + assertThat(page.getNumberOfElements(), is(1)); |
| 75 | + assertThat(page.getTotalElements(), is(1L)); |
| 76 | + assertThat(page.getTotalPages(), is(1)); |
| 77 | + assertThat(page.getContent(), hasItems(carter)); |
| 78 | + } |
| 79 | +} |
0 commit comments