|
| 1 | +package org.baeldung.persistence.service; |
| 2 | + |
| 3 | +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 4 | +import static org.hamcrest.Matchers.hasSize; |
| 5 | +import static org.hamcrest.Matchers.lessThan; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import javax.persistence.EntityManager; |
| 11 | +import javax.persistence.PersistenceContext; |
| 12 | +import javax.persistence.Query; |
| 13 | +import javax.persistence.TypedQuery; |
| 14 | +import javax.persistence.criteria.CriteriaBuilder; |
| 15 | +import javax.persistence.criteria.CriteriaQuery; |
| 16 | +import javax.persistence.criteria.Root; |
| 17 | + |
| 18 | +import org.baeldung.config.PersistenceJPAConfig; |
| 19 | +import org.baeldung.persistence.model.Foo; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.test.context.ContextConfiguration; |
| 25 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 26 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 27 | + |
| 28 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 29 | +@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) |
| 30 | +public class FooPaginationPersistenceIntegrationTest { |
| 31 | + |
| 32 | + @PersistenceContext |
| 33 | + private EntityManager entityManager; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private FooService fooService; |
| 37 | + |
| 38 | + @Before |
| 39 | + public final void before() { |
| 40 | + final int minimalNumberOfEntities = 25; |
| 41 | + if (fooService.findAll().size() <= minimalNumberOfEntities) { |
| 42 | + for (int i = 0; i < minimalNumberOfEntities; i++) { |
| 43 | + fooService.create(new Foo(randomAlphabetic(6))); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // tests |
| 49 | + |
| 50 | + @Test |
| 51 | + public final void whenContextIsBootstrapped_thenNoExceptions() { |
| 52 | + // |
| 53 | + } |
| 54 | + |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + @Test |
| 57 | + public final void givenEntitiesExist_whenRetrievingFirstPage_thenCorrect() { |
| 58 | + final int pageSize = 10; |
| 59 | + |
| 60 | + final Query query = entityManager.createQuery("From Foo"); |
| 61 | + configurePagination(query, 1, pageSize); |
| 62 | + |
| 63 | + // When |
| 64 | + final List<Foo> fooList = query.getResultList(); |
| 65 | + |
| 66 | + // Then |
| 67 | + assertThat(fooList, hasSize(pageSize)); |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + @Test |
| 72 | + public final void givenEntitiesExist_whenRetrievingLastPage_thenCorrect() { |
| 73 | + final int pageSize = 10; |
| 74 | + final Query queryTotal = entityManager.createQuery("Select count(f.id) from Foo f"); |
| 75 | + final long countResult = (long) queryTotal.getSingleResult(); |
| 76 | + |
| 77 | + final Query query = entityManager.createQuery("Select f from Foo as f order by f.id"); |
| 78 | + final int lastPage = (int) ((countResult / pageSize) + 1); |
| 79 | + configurePagination(query, lastPage, pageSize); |
| 80 | + final List<Foo> fooList = query.getResultList(); |
| 81 | + |
| 82 | + // Then |
| 83 | + assertThat(fooList, hasSize(lessThan(pageSize + 1))); |
| 84 | + } |
| 85 | + |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + @Test |
| 88 | + public final void givenEntitiesExist_whenRetrievingPage_thenCorrect() { |
| 89 | + final int pageSize = 10; |
| 90 | + |
| 91 | + final Query queryIds = entityManager.createQuery("Select f.id from Foo f order by f.lastName"); |
| 92 | + final List<Integer> fooIds = queryIds.getResultList(); |
| 93 | + |
| 94 | + final Query query = entityManager.createQuery("Select f from Foo e whet f.id in :ids"); |
| 95 | + query.setParameter("ids", fooIds.subList(0, pageSize)); |
| 96 | + final List<Foo> fooList = query.getResultList(); |
| 97 | + |
| 98 | + // Then |
| 99 | + assertThat(fooList, hasSize(pageSize)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public final void givenEntitiesExist_whenRetrievingPageViaCriteria_thenCorrect() { |
| 104 | + final int pageSize = 10; |
| 105 | + final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); |
| 106 | + final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class); |
| 107 | + final Root<Foo> from = criteriaQuery.from(Foo.class); |
| 108 | + final CriteriaQuery<Foo> select = criteriaQuery.select(from); |
| 109 | + final TypedQuery<Foo> typedQuery = entityManager.createQuery(select); |
| 110 | + typedQuery.setFirstResult(0); |
| 111 | + typedQuery.setMaxResults(pageSize); |
| 112 | + final List<Foo> fooList = typedQuery.getResultList(); |
| 113 | + |
| 114 | + // Then |
| 115 | + assertThat(fooList, hasSize(pageSize)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public final void givenEntitiesExist_whenRetrievingPageViaCriteria_thenNoExceptions() { |
| 120 | + int pageNumber = 1; |
| 121 | + final int pageSize = 10; |
| 122 | + final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); |
| 123 | + |
| 124 | + final CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class); |
| 125 | + countQuery.select(criteriaBuilder.count(countQuery.from(Foo.class))); |
| 126 | + final Long count = entityManager.createQuery(countQuery).getSingleResult(); |
| 127 | + |
| 128 | + final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class); |
| 129 | + final Root<Foo> from = criteriaQuery.from(Foo.class); |
| 130 | + final CriteriaQuery<Foo> select = criteriaQuery.select(from); |
| 131 | + |
| 132 | + final TypedQuery<Foo> typedQuery = entityManager.createQuery(select); |
| 133 | + while (pageNumber < count.intValue()) { |
| 134 | + typedQuery.setFirstResult(pageNumber - 1); |
| 135 | + typedQuery.setMaxResults(pageSize); |
| 136 | + System.out.println("Current page: " + typedQuery.getResultList()); |
| 137 | + pageNumber += pageSize; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // UTIL |
| 142 | + |
| 143 | + final int determineLastPage(final int pageSize, final long countResult) { |
| 144 | + return (int) (countResult / pageSize) + 1; |
| 145 | + } |
| 146 | + |
| 147 | + final void configurePagination(final Query query, final int pageNumber, final int pageSize) { |
| 148 | + query.setFirstResult((pageNumber - 1) * pageSize); |
| 149 | + query.setMaxResults(pageSize); |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments