|
| 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 org.baeldung.persistence.model.Foo; |
| 11 | +import org.baeldung.spring.PersistenceConfig; |
| 12 | +import org.hibernate.Criteria; |
| 13 | +import org.hibernate.Query; |
| 14 | +import org.hibernate.ScrollMode; |
| 15 | +import org.hibernate.ScrollableResults; |
| 16 | +import org.hibernate.Session; |
| 17 | +import org.hibernate.SessionFactory; |
| 18 | +import org.hibernate.criterion.Order; |
| 19 | +import org.hibernate.criterion.Projections; |
| 20 | +import org.hibernate.criterion.Restrictions; |
| 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.test.context.ContextConfiguration; |
| 27 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 28 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 29 | + |
| 30 | +import com.google.common.collect.Lists; |
| 31 | + |
| 32 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 33 | +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) |
| 34 | +public class FooServicePaginationPersistenceIntegrationTest { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private SessionFactory sessionFactory; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private IFooService fooService; |
| 41 | + |
| 42 | + private Session session; |
| 43 | + |
| 44 | + // tests |
| 45 | + |
| 46 | + @Before |
| 47 | + public final void before() { |
| 48 | + final int minimalNumberOfEntities = 25; |
| 49 | + if (fooService.findAll().size() <= minimalNumberOfEntities) { |
| 50 | + for (int i = 0; i < minimalNumberOfEntities; i++) { |
| 51 | + fooService.create(new Foo(randomAlphabetic(6))); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + session = sessionFactory.openSession(); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public final void after() { |
| 60 | + session.close(); |
| 61 | + } |
| 62 | + |
| 63 | + // tests |
| 64 | + |
| 65 | + @Test |
| 66 | + public final void whenContextIsBootstrapped_thenNoExceptions() { |
| 67 | + // |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressWarnings("unchecked") |
| 71 | + @Test |
| 72 | + public final void whenRetrievingPaginatedEntities_thenCorrectSize() { |
| 73 | + final int pageNumber = 1; |
| 74 | + final int pageSize = 10; |
| 75 | + |
| 76 | + final Query query = session.createQuery("From Foo"); |
| 77 | + query.setFirstResult((pageNumber - 1) * pageSize); |
| 78 | + query.setMaxResults(pageSize); |
| 79 | + final List<Foo> fooList = query.list(); |
| 80 | + |
| 81 | + assertThat(fooList, hasSize(pageSize)); |
| 82 | + } |
| 83 | + |
| 84 | + @SuppressWarnings("unchecked") |
| 85 | + @Test |
| 86 | + public final void whenRetrievingAllPages_thenCorrect() { |
| 87 | + int pageNumber = 1; |
| 88 | + final int pageSize = 10; |
| 89 | + |
| 90 | + final String countQ = "Select count (f.id) from Foo f"; |
| 91 | + final Query countQuery = session.createQuery(countQ); |
| 92 | + final Long countResult = (Long) countQuery.uniqueResult(); |
| 93 | + |
| 94 | + final List<Foo> fooList = Lists.newArrayList(); |
| 95 | + int totalEntities = 0; |
| 96 | + final Query query = session.createQuery("From Foo"); |
| 97 | + while (totalEntities < countResult) { |
| 98 | + query.setFirstResult((pageNumber - 1) * pageSize); |
| 99 | + query.setMaxResults(pageSize); |
| 100 | + fooList.addAll(query.list()); |
| 101 | + totalEntities = fooList.size(); |
| 102 | + pageNumber++; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @SuppressWarnings("unchecked") |
| 107 | + @Test |
| 108 | + public final void whenRetrievingLastPage_thenCorrectSize() { |
| 109 | + final int pageSize = 10; |
| 110 | + |
| 111 | + final String countQ = "Select count (f.id) from Foo f"; |
| 112 | + final Query countQuery = session.createQuery(countQ); |
| 113 | + final Long countResults = (Long) countQuery.uniqueResult(); |
| 114 | + final int lastPageNumber = (int) ((countResults / pageSize) + 1); |
| 115 | + |
| 116 | + final Query selectQuery = session.createQuery("From Foo"); |
| 117 | + selectQuery.setFirstResult((lastPageNumber - 1) * pageSize); |
| 118 | + selectQuery.setMaxResults(pageSize); |
| 119 | + final List<Foo> lastPage = selectQuery.list(); |
| 120 | + |
| 121 | + assertThat(lastPage, hasSize(lessThan(pageSize + 1))); |
| 122 | + } |
| 123 | + |
| 124 | + // testing - scrollable |
| 125 | + |
| 126 | + @Test |
| 127 | + public final void givenUsingTheScrollableApi_whenRetrievingPaginatedData_thenCorrect() { |
| 128 | + final int pageSize = 10; |
| 129 | + final String hql = "FROM Foo f order by f.name"; |
| 130 | + final Query query = session.createQuery(hql); |
| 131 | + |
| 132 | + final ScrollableResults resultScroll = query.scroll(ScrollMode.FORWARD_ONLY); |
| 133 | + |
| 134 | + // resultScroll.last(); |
| 135 | + // final int totalResults = resultScroll.getRowNumber() + 1; |
| 136 | + |
| 137 | + resultScroll.first(); |
| 138 | + resultScroll.scroll(0); |
| 139 | + final List<Foo> fooPage = Lists.newArrayList(); |
| 140 | + int i = 0; |
| 141 | + while (pageSize > i++) { |
| 142 | + fooPage.add((Foo) resultScroll.get(0)); |
| 143 | + if (!resultScroll.next()) |
| 144 | + break; |
| 145 | + } |
| 146 | + |
| 147 | + assertThat(fooPage, hasSize(lessThan(10 + 1))); |
| 148 | + } |
| 149 | + |
| 150 | + @SuppressWarnings("unchecked") |
| 151 | + @Test |
| 152 | + public final void givenUsingTheScrollableApi_whenRetrievingPaginatedData_thenCorrect2() { |
| 153 | + final int minId = 0; |
| 154 | + final int maxId = 10; |
| 155 | + |
| 156 | + final Criteria criteriaCount = session.createCriteria(Foo.class, "FOO"); |
| 157 | + criteriaCount.add(Restrictions.between("id", minId, maxId)); |
| 158 | + criteriaCount.addOrder(Order.asc("id")); |
| 159 | + criteriaCount.setProjection(Projections.rowCount()); |
| 160 | + final Long count = (Long) criteriaCount.uniqueResult(); |
| 161 | + |
| 162 | + int pageNumber = 1; |
| 163 | + final int pageSize = 10; |
| 164 | + final List<Foo> fooList = Lists.newArrayList(); |
| 165 | + |
| 166 | + final Criteria criteria = session.createCriteria(Foo.class, "FOO"); |
| 167 | + criteria.add(Restrictions.between("id", minId, maxId)); |
| 168 | + criteria.addOrder(Order.asc("id")); |
| 169 | + int totalEntities = 0; |
| 170 | + while (totalEntities < count.intValue()) { |
| 171 | + criteria.setFirstResult((pageNumber - 1) * pageSize); |
| 172 | + criteria.setMaxResults(pageSize); |
| 173 | + fooList.addAll(criteria.list()); |
| 174 | + totalEntities = fooList.size(); |
| 175 | + pageNumber++; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public final void givenUsingTheScrollableApi_whenRetrievingPaginatedData_thenCorrect3() { |
| 181 | + final int minId = 0; |
| 182 | + final int maxId = 10; |
| 183 | + int pageNumber = 1; |
| 184 | + final int pageSize = 10; |
| 185 | + final List<Foo> fooList = Lists.newArrayList(); |
| 186 | + |
| 187 | + final Criteria criteria = session.createCriteria(Foo.class, "FOO"); |
| 188 | + final Criteria criteriaCount = session.createCriteria(Foo.class, "FOO"); |
| 189 | + criteria.add(Restrictions.between("id", minId, maxId)); |
| 190 | + criteria.addOrder(Order.asc("id")); |
| 191 | + criteriaCount.add(Restrictions.between("id", minId, maxId)); |
| 192 | + criteriaCount.addOrder(Order.asc("id")); |
| 193 | + criteriaCount.setProjection(Projections.rowCount()); |
| 194 | + final Long count = (Long) criteriaCount.uniqueResult(); |
| 195 | + int totalEntities = 0; |
| 196 | + while (totalEntities < count.intValue()) { |
| 197 | + criteria.setFirstResult((pageNumber - 1) * pageSize); |
| 198 | + criteria.setMaxResults(pageSize); |
| 199 | + fooList.addAll(criteria.list()); |
| 200 | + totalEntities = fooList.size(); |
| 201 | + pageNumber++; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments