Source code: org/hibernate/test/pagination/PaginationTest.java
1 //$Id: PaginationTest.java 7867 2005-08-11 23:35:33Z oneovthafew $
2 package org.hibernate.test.pagination;
3
4 import java.math.BigDecimal;
5
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9 import org.hibernate.Session;
10 import org.hibernate.Transaction;
11 import org.hibernate.cfg.Configuration;
12 import org.hibernate.cfg.Environment;
13 import org.hibernate.criterion.Order;
14 import org.hibernate.test.TestCase;
15
16 /**
17 * @author Gavin King
18 */
19 public class PaginationTest extends TestCase {
20
21 public PaginationTest(String str) {
22 super(str);
23 }
24
25 public void testPagination() {
26
27 Session s = openSession();
28 Transaction t = s.beginTransaction();
29 for ( int i=0; i<10; i++ ) {
30 DataPoint dp = new DataPoint();
31 dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) );
32 dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) );
33 s.persist(dp);
34 }
35 t.commit();
36 s.close();
37
38 s = openSession();
39 t = s.beginTransaction();
40 int size = s.createSQLQuery("select id, xval, yval, description from DataPoint order by xval, yval")
41 .addEntity(DataPoint.class)
42 .setMaxResults(5)
43 .list().size();
44 assertEquals(size, 5);
45 size = s.createQuery("from DataPoint order by x, y")
46 .setFirstResult(5)
47 .setMaxResults(2)
48 .list().size();
49 assertEquals(size, 2);
50 size = s.createCriteria(DataPoint.class)
51 .addOrder( Order.asc("x") )
52 .addOrder( Order.asc("y") )
53 .setFirstResult(8)
54 .list().size();
55 assertEquals(size, 2);
56 t.commit();
57 s.close();
58
59 }
60
61 protected void configure(Configuration cfg) {
62 cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "20");
63 }
64
65 protected String[] getMappings() {
66 return new String[] { "pagination/DataPoint.hbm.xml" };
67 }
68
69 public static Test suite() {
70 return new TestSuite(PaginationTest.class);
71 }
72
73 public String getCacheConcurrencyStrategy() {
74 return null;
75 }
76
77 }
78