|
32 | 32 | import java.time.ZoneOffset;
|
33 | 33 | import java.util.ArrayList;
|
34 | 34 | import java.util.Arrays;
|
| 35 | +import java.util.Collections; |
35 | 36 | import java.util.List;
|
36 | 37 | import java.util.Optional;
|
| 38 | +import java.util.stream.Stream; |
37 | 39 |
|
38 | 40 | import org.junit.jupiter.api.BeforeEach;
|
39 | 41 | import org.junit.jupiter.api.Test;
|
40 | 42 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 43 | +import org.junit.jupiter.params.ParameterizedTest; |
| 44 | +import org.junit.jupiter.params.provider.Arguments; |
| 45 | +import org.junit.jupiter.params.provider.MethodSource; |
41 | 46 | import org.springframework.beans.factory.annotation.Autowired;
|
42 | 47 | import org.springframework.beans.factory.config.PropertiesFactoryBean;
|
43 | 48 | import org.springframework.context.ApplicationListener;
|
|
53 | 58 | import org.springframework.data.domain.Pageable;
|
54 | 59 | import org.springframework.data.domain.Slice;
|
55 | 60 | import org.springframework.data.jdbc.core.mapping.AggregateReference;
|
56 |
| -import org.springframework.data.relational.repository.Lock; |
57 | 61 | import org.springframework.data.jdbc.repository.query.Modifying;
|
58 | 62 | import org.springframework.data.jdbc.repository.query.Query;
|
59 | 63 | import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
|
|
65 | 69 | import org.springframework.data.relational.core.mapping.event.AfterConvertEvent;
|
66 | 70 | import org.springframework.data.relational.core.mapping.event.AfterLoadEvent;
|
67 | 71 | import org.springframework.data.relational.core.sql.LockMode;
|
| 72 | +import org.springframework.data.relational.repository.Lock; |
68 | 73 | import org.springframework.data.repository.CrudRepository;
|
69 | 74 | import org.springframework.data.repository.core.NamedQueries;
|
70 | 75 | import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
|
@@ -693,6 +698,138 @@ void findAllByExampleShouldGetNone() {
|
693 | 698 | .isEmpty();
|
694 | 699 | }
|
695 | 700 |
|
| 701 | + @Test |
| 702 | + void findAllByExamplePageableShouldGetOne() { |
| 703 | + |
| 704 | + DummyEntity dummyEntity1 = createDummyEntity(); |
| 705 | + dummyEntity1.setFlag(true); |
| 706 | + |
| 707 | + repository.save(dummyEntity1); |
| 708 | + |
| 709 | + DummyEntity dummyEntity2 = createDummyEntity(); |
| 710 | + dummyEntity2.setName("Diego"); |
| 711 | + |
| 712 | + repository.save(dummyEntity2); |
| 713 | + |
| 714 | + Example<DummyEntity> example = Example.of(new DummyEntity("Diego")); |
| 715 | + Pageable pageRequest = PageRequest.of(0, 10); |
| 716 | + |
| 717 | + Iterable<DummyEntity> allFound = repository.findAll(example, pageRequest); |
| 718 | + |
| 719 | + assertThat(allFound) // |
| 720 | + .isNotNull() // |
| 721 | + .hasSize(1) // |
| 722 | + .extracting(DummyEntity::getName) // |
| 723 | + .containsExactly(example.getProbe().getName()); |
| 724 | + } |
| 725 | + |
| 726 | + @Test |
| 727 | + void findAllByExamplePageableMultipleMatchShouldGetOne() { |
| 728 | + |
| 729 | + DummyEntity dummyEntity1 = createDummyEntity(); |
| 730 | + repository.save(dummyEntity1); |
| 731 | + |
| 732 | + DummyEntity dummyEntity2 = createDummyEntity(); |
| 733 | + repository.save(dummyEntity2); |
| 734 | + |
| 735 | + Example<DummyEntity> example = Example.of(createDummyEntity()); |
| 736 | + Pageable pageRequest = PageRequest.of(0, 10); |
| 737 | + |
| 738 | + Iterable<DummyEntity> allFound = repository.findAll(example, pageRequest); |
| 739 | + |
| 740 | + assertThat(allFound) // |
| 741 | + .isNotNull() // |
| 742 | + .hasSize(2) // |
| 743 | + .extracting(DummyEntity::getName) // |
| 744 | + .containsOnly(example.getProbe().getName()); |
| 745 | + } |
| 746 | + |
| 747 | + @Test |
| 748 | + void findAllByExamplePageableShouldGetNone() { |
| 749 | + |
| 750 | + DummyEntity dummyEntity1 = createDummyEntity(); |
| 751 | + dummyEntity1.setFlag(true); |
| 752 | + |
| 753 | + repository.save(dummyEntity1); |
| 754 | + |
| 755 | + Example<DummyEntity> example = Example.of(new DummyEntity("NotExisting")); |
| 756 | + Pageable pageRequest = PageRequest.of(0, 10); |
| 757 | + |
| 758 | + Iterable<DummyEntity> allFound = repository.findAll(example, pageRequest); |
| 759 | + |
| 760 | + assertThat(allFound) // |
| 761 | + .isNotNull() // |
| 762 | + .isEmpty(); |
| 763 | + } |
| 764 | + |
| 765 | + @Test |
| 766 | + void findAllByExamplePageableOutsidePageShouldGetNone() { |
| 767 | + |
| 768 | + DummyEntity dummyEntity1 = createDummyEntity(); |
| 769 | + repository.save(dummyEntity1); |
| 770 | + |
| 771 | + DummyEntity dummyEntity2 = createDummyEntity(); |
| 772 | + repository.save(dummyEntity2); |
| 773 | + |
| 774 | + Example<DummyEntity> example = Example.of(createDummyEntity()); |
| 775 | + Pageable pageRequest = PageRequest.of(10, 10); |
| 776 | + |
| 777 | + Iterable<DummyEntity> allFound = repository.findAll(example, pageRequest); |
| 778 | + |
| 779 | + assertThat(allFound) // |
| 780 | + .isNotNull() // |
| 781 | + .isEmpty(); |
| 782 | + } |
| 783 | + |
| 784 | + @ParameterizedTest |
| 785 | + @MethodSource("findAllByExamplePageableSource") |
| 786 | + void findAllByExamplePageable(Pageable pageRequest, int size, int totalPages, List<String> notContains) { |
| 787 | + |
| 788 | + for (int i = 0; i < 100; i++) { |
| 789 | + DummyEntity dummyEntity = createDummyEntity(); |
| 790 | + dummyEntity.setFlag(true); |
| 791 | + dummyEntity.setName("" + i); |
| 792 | + |
| 793 | + repository.save(dummyEntity); |
| 794 | + } |
| 795 | + |
| 796 | + DummyEntity dummyEntityExample = createDummyEntity(); |
| 797 | + dummyEntityExample.setName(null); |
| 798 | + dummyEntityExample.setFlag(true); |
| 799 | + |
| 800 | + Example<DummyEntity> example = Example.of(dummyEntityExample); |
| 801 | + |
| 802 | + Page<DummyEntity> allFound = repository.findAll(example, pageRequest); |
| 803 | + |
| 804 | + // page has correct size |
| 805 | + assertThat(allFound) // |
| 806 | + .isNotNull() // |
| 807 | + .hasSize(size); |
| 808 | + |
| 809 | + // correct number of total |
| 810 | + assertThat(allFound.getTotalElements()).isEqualTo(100); |
| 811 | + |
| 812 | + assertThat(allFound.getTotalPages()).isEqualTo(totalPages); |
| 813 | + |
| 814 | + if (!notContains.isEmpty()) { |
| 815 | + assertThat(allFound) // |
| 816 | + .extracting(DummyEntity::getName) // |
| 817 | + .doesNotContain(notContains.toArray(new String[0])); |
| 818 | + } |
| 819 | + } |
| 820 | + |
| 821 | + public static Stream<Arguments> findAllByExamplePageableSource() { |
| 822 | + return Stream.of( // |
| 823 | + Arguments.of(PageRequest.of(0, 3), 3, 34, Arrays.asList("3", "4", "100")), // |
| 824 | + Arguments.of(PageRequest.of(1, 10), 10, 10, Arrays.asList("9", "20", "30")), // |
| 825 | + Arguments.of(PageRequest.of(2, 10), 10, 10, Arrays.asList("1", "2", "3")), // |
| 826 | + Arguments.of(PageRequest.of(33, 3), 1, 34, Collections.emptyList()), // |
| 827 | + Arguments.of(PageRequest.of(36, 3), 0, 34, Collections.emptyList()), // |
| 828 | + Arguments.of(PageRequest.of(0, 10000), 100, 1, Collections.emptyList()), // |
| 829 | + Arguments.of(PageRequest.of(100, 10000), 0, 1, Collections.emptyList()) // |
| 830 | + ); |
| 831 | + } |
| 832 | + |
696 | 833 | @Test
|
697 | 834 | void existsByExampleShouldGetOne() {
|
698 | 835 |
|
|
0 commit comments